blob: 0e5b8f6a3753002f99c19efe70790c9a3b211e55 [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 Carlssone1b29ef2008-08-22 16:00:37 +000024#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025using namespace clang;
26using namespace CodeGen;
27
Mike Stump1eb44332009-09-09 15:08:12 +000028void
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000029CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor,
30 llvm::Constant *DeclPtr) {
31 // FIXME: This is ABI dependent and we use the Itanium ABI.
Mike Stump1eb44332009-09-09 15:08:12 +000032
33 const llvm::Type *Int8PtrTy =
Owen Anderson0032b272009-08-13 21:57:51 +000034 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Mike Stump1eb44332009-09-09 15:08:12 +000035
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000036 std::vector<const llvm::Type *> Params;
37 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000038
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000039 // Get the destructor function type
Mike Stump1eb44332009-09-09 15:08:12 +000040 const llvm::Type *DtorFnTy =
Owen Anderson0032b272009-08-13 21:57:51 +000041 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000042 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump1eb44332009-09-09 15:08:12 +000043
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000044 Params.clear();
45 Params.push_back(DtorFnTy);
46 Params.push_back(Int8PtrTy);
47 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000048
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000049 // Get the __cxa_atexit function type
50 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump1eb44332009-09-09 15:08:12 +000051 const llvm::FunctionType *AtExitFnTy =
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000052 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump1eb44332009-09-09 15:08:12 +000053
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000054 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
55 "__cxa_atexit");
Mike Stump1eb44332009-09-09 15:08:12 +000056
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000057 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
58 "__dso_handle");
Mike Stump1eb44332009-09-09 15:08:12 +000059
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000060 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000062 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
63 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
64 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
65 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
66}
67
Mike Stump1eb44332009-09-09 15:08:12 +000068void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000069 llvm::Constant *DeclPtr) {
70 assert(D.hasGlobalStorage() &&
71 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000072
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000073 const Expr *Init = D.getInit();
74 QualType T = D.getType();
Mike Stump1eb44332009-09-09 15:08:12 +000075
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000076 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000077 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000078 } else if (!hasAggregateLLVMType(T)) {
79 llvm::Value *V = EmitScalarExpr(Init);
80 EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T);
81 } else if (T->isAnyComplexType()) {
82 EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified());
83 } else {
84 EmitAggExpr(Init, DeclPtr, T.isVolatileQualified());
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000086 if (const RecordType *RT = T->getAs<RecordType>()) {
87 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
88 if (!RD->hasTrivialDestructor())
89 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
90 }
91 }
92}
93
Anders Carlsson89ed31d2009-08-08 23:24:23 +000094void
95CodeGenModule::EmitCXXGlobalInitFunc() {
96 if (CXXGlobalInits.empty())
97 return;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Owen Anderson0032b272009-08-13 21:57:51 +000099 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000100 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000102 // Create our global initialization function.
103 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000104 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000105 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
106 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000108 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000109 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000110 CXXGlobalInits.size());
111 AddGlobalCtor(Fn);
112}
113
114void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
115 const VarDecl **Decls,
116 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000117 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000118 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000120 for (unsigned i = 0; i != NumDecls; ++i) {
121 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000123 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
124 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
125 }
126 FinishFunction();
127}
128
Mike Stump1eb44332009-09-09 15:08:12 +0000129void
130CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000131 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000132 // FIXME: This should use __cxa_guard_{acquire,release}?
133
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000134 assert(!getContext().getLangOptions().ThreadsafeStatics &&
135 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000136
Anders Carlsson283a0622009-04-13 18:03:33 +0000137 llvm::SmallString<256> GuardVName;
138 llvm::raw_svector_ostream GuardVOut(GuardVName);
139 mangleGuardVariable(&D, getContext(), GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000141 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000142 llvm::GlobalValue *GuardV =
Owen Anderson0032b272009-08-13 21:57:51 +0000143 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000144 GV->getLinkage(),
Owen Anderson0032b272009-08-13 21:57:51 +0000145 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000146 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000148 // Load the first byte of the guard variable.
Owen Anderson0032b272009-08-13 21:57:51 +0000149 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000150 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000151 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000153 // Compare it against 0.
Owen Anderson0032b272009-08-13 21:57:51 +0000154 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000155 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Daniel Dunbar55e87422008-11-11 02:29:29 +0000157 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000158 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000159
160 // If the guard variable is 0, jump to the initializer code.
161 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000163 EmitBlock(InitBlock);
164
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000165 EmitCXXGlobalVarDeclInit(D, GV);
166
Owen Anderson0032b272009-08-13 21:57:51 +0000167 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000168 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000171}
172
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000173RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
174 llvm::Value *Callee,
175 llvm::Value *This,
176 CallExpr::const_arg_iterator ArgBeg,
177 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000178 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000179 "Trying to emit a member call expr on a static method!");
180
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000181 // A call to a trivial destructor requires no code generation.
182 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
183 if (Destructor->isTrivial())
184 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
John McCall183700f2009-09-21 23:43:11 +0000186 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000188 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000190 // Push the this ptr.
191 Args.push_back(std::make_pair(RValue::get(This),
192 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000194 // And the rest of the call args
195 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
John McCall183700f2009-09-21 23:43:11 +0000197 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000198 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
199 Callee, Args, MD);
200}
201
Anders Carlsson774e7c62009-04-03 22:50:24 +0000202RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
203 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
204 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000205
John McCall183700f2009-09-21 23:43:11 +0000206 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000207
Mike Stump1eb44332009-09-09 15:08:12 +0000208 const llvm::Type *Ty =
209 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000210 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000211 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Anders Carlsson774e7c62009-04-03 22:50:24 +0000213 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000214 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000215 else {
216 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000217 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000218 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000219
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000220 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000221 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000222 // virtual call mechanism.
Mike Stumpf0070db2009-08-26 20:46:33 +0000223 llvm::Value *Callee;
Douglas Gregor0979c802009-08-31 21:41:48 +0000224 if (MD->isVirtual() && !ME->hasQualifier())
Mike Stumpf0070db2009-08-26 20:46:33 +0000225 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000226 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000227 = dyn_cast<CXXDestructorDecl>(MD))
228 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000229 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000230 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000231
232 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000233 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000234}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000235
Mike Stump1eb44332009-09-09 15:08:12 +0000236RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000237CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
238 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000239 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000240 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Fariborz Jahanianad258832009-08-13 21:09:41 +0000242 if (MD->isCopyAssignment()) {
243 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
244 if (ClassDecl->hasTrivialCopyAssignment()) {
245 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
246 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
247 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
248 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
249 QualType Ty = E->getType();
250 EmitAggregateCopy(This, Src, Ty);
251 return RValue::get(This);
252 }
253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
John McCall183700f2009-09-21 23:43:11 +0000255 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000256 const llvm::Type *Ty =
257 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000258 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000259 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlsson0f294632009-05-27 04:18:27 +0000261 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anders Carlsson0f294632009-05-27 04:18:27 +0000263 return EmitCXXMemberCall(MD, Callee, This,
264 E->arg_begin() + 1, E->arg_end());
265}
266
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000267llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000268 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000269 "Must be in a C++ member function decl to load 'this'");
270 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
271 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000273 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000274 // ans: See how CodeGenFunction::LoadObjCSelf() uses
275 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000276 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
277}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000278
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000279/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
280/// for-loop to call the default constructor on individual members of the
281/// array. 'Array' is the array type, 'This' is llvm pointer of the start
282/// of the array and 'D' is the default costructor Decl for elements of the
283/// array. It is assumed that all relevant checks have been made by the
284/// caller.
285void
286CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
287 const ArrayType *Array,
288 llvm::Value *This) {
289 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
290 assert(CA && "Do we support VLA for construction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000292 // Create a temporary for the loop index and initialize it with 0.
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000293 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000294 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000295 llvm::Value* zeroConstant =
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000296 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000297 Builder.CreateStore(zeroConstant, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000299 // Start the loop with a block that tests the condition.
300 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
301 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000303 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000305 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000307 // Generate: if (loop-index < number-of-elements fall to the loop body,
308 // otherwise, go to the block after the for-loop.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000309 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000310 llvm::Value * NumElementsPtr =
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000311 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000312 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000313 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000314 "isless");
315 // If the condition is true, execute the body.
316 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000318 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000320 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000321 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000322 Counter = Builder.CreateLoad(IndexPtr);
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000323 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
324 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000326 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000328 // Emit the increment of the loop counter.
329 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
330 Counter = Builder.CreateLoad(IndexPtr);
331 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
332 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000334 // Finally, branch back up to the condition for the next iteration.
335 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000337 // Emit the fall-through block.
338 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000339}
340
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000341/// EmitCXXAggrDestructorCall - calls the default destructor on array
342/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000343void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000344CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
345 const ArrayType *Array,
346 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000347 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
348 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000349 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000350 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000351 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000352 // Create a temporary for the loop index and initialize it with count of
353 // array elements.
354 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
355 "loop.index");
356 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000357 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000358 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
359 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000361 // Start the loop with a block that tests the condition.
362 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
363 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000365 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000367 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000369 // Generate: if (loop-index != 0 fall to the loop body,
370 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000371 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000372 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
373 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
374 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
375 "isne");
376 // If the condition is true, execute the body.
377 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000379 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000381 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
382 // Inside the loop body, emit the constructor call on the array element.
383 Counter = Builder.CreateLoad(IndexPtr);
384 Counter = Builder.CreateSub(Counter, One);
385 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
386 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000388 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000390 // Emit the decrement of the loop counter.
391 Counter = Builder.CreateLoad(IndexPtr);
392 Counter = Builder.CreateSub(Counter, One, "dec");
393 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000395 // Finally, branch back up to the condition for the next iteration.
396 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000398 // Emit the fall-through block.
399 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000400}
401
402void
Mike Stump1eb44332009-09-09 15:08:12 +0000403CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
404 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000405 llvm::Value *This,
406 CallExpr::const_arg_iterator ArgBeg,
407 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000408 if (D->isCopyConstructor(getContext())) {
409 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
410 if (ClassDecl->hasTrivialCopyConstructor()) {
411 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
412 "EmitCXXConstructorCall - user declared copy constructor");
413 const Expr *E = (*ArgBeg);
414 QualType Ty = E->getType();
415 llvm::Value *Src = EmitLValue(E).getAddress();
416 EmitAggregateCopy(This, Src, Ty);
417 return;
418 }
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000421 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
422
423 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000424}
425
Mike Stump1eb44332009-09-09 15:08:12 +0000426void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000427 CXXDtorType Type,
428 llvm::Value *This) {
429 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Anders Carlsson7267c162009-05-29 21:03:38 +0000431 EmitCXXMemberCall(D, Callee, This, 0, 0);
432}
433
Mike Stump1eb44332009-09-09 15:08:12 +0000434void
435CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000436 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000437 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000438
439 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000440 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000441 if (RD->hasTrivialConstructor())
442 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000443
Mike Stump1eb44332009-09-09 15:08:12 +0000444 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000445 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000446 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000447 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000448 EmitAggExpr((*i), Dest, false);
449 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000450 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000451 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000452 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000453 E->arg_begin(), E->arg_end());
454}
455
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000456void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000457 EmitGlobal(GlobalDecl(D, Ctor_Complete));
458 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000459}
Anders Carlsson363c1842009-04-16 23:57:24 +0000460
Mike Stump1eb44332009-09-09 15:08:12 +0000461void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000462 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Anders Carlsson27ae5362009-04-17 01:58:57 +0000464 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000466 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Anders Carlsson27ae5362009-04-17 01:58:57 +0000468 SetFunctionDefinitionAttributes(D, Fn);
469 SetLLVMFunctionAttributesForDefinition(D, Fn);
470}
471
Anders Carlsson363c1842009-04-16 23:57:24 +0000472llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000473CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000474 CXXCtorType Type) {
475 const llvm::FunctionType *FTy =
476 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Anders Carlsson363c1842009-04-16 23:57:24 +0000478 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000479 return cast<llvm::Function>(
480 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000481}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000482
Mike Stump1eb44332009-09-09 15:08:12 +0000483const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000484 CXXCtorType Type) {
485 llvm::SmallString<256> Name;
486 llvm::raw_svector_ostream Out(Name);
487 mangleCXXCtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Anders Carlsson27ae5362009-04-17 01:58:57 +0000489 Name += '\0';
490 return UniqueMangledName(Name.begin(), Name.end());
491}
492
493void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000494 EmitCXXDestructor(D, Dtor_Complete);
495 EmitCXXDestructor(D, Dtor_Base);
496}
497
Mike Stump1eb44332009-09-09 15:08:12 +0000498void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000499 CXXDtorType Type) {
500 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000502 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Anders Carlsson27ae5362009-04-17 01:58:57 +0000504 SetFunctionDefinitionAttributes(D, Fn);
505 SetLLVMFunctionAttributesForDefinition(D, Fn);
506}
507
508llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000509CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000510 CXXDtorType Type) {
511 const llvm::FunctionType *FTy =
512 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Anders Carlsson27ae5362009-04-17 01:58:57 +0000514 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000515 return cast<llvm::Function>(
516 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000517}
518
Mike Stump1eb44332009-09-09 15:08:12 +0000519const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000520 CXXDtorType Type) {
521 llvm::SmallString<256> Name;
522 llvm::raw_svector_ostream Out(Name);
523 mangleCXXDtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Anders Carlsson27ae5362009-04-17 01:58:57 +0000525 Name += '\0';
526 return UniqueMangledName(Name.begin(), Name.end());
527}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000528
Mike Stump32f37012009-08-18 21:49:00 +0000529llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000530 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000531 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000532 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000533
534 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000535 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000536
537 llvm::SmallString<256> OutName;
538 llvm::raw_svector_ostream Out(OutName);
539 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000540 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000541 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000542 llvm::GlobalVariable::LinkageTypes linktype;
543 linktype = llvm::GlobalValue::WeakAnyLinkage;
544 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000545 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000546 // FIXME: descriptor
547 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000548 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000549 // FIXME: TS
550 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
551
552 llvm::Constant *C;
553 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
554 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000555 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000556 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000557 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
558 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000559}
560
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000561class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000562public:
563 /// Index_t - Vtable index type.
564 typedef uint64_t Index_t;
565private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000566 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000567 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000568 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000569 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000570 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000571 /// BLayout - Layout for the most derived class that this vtable is being
572 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000573 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000574 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000575 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000576 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000577 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000578 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000579 /// Index - Maps a method decl into a vtable index. Useful for virtual
580 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000581 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000582 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
583 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump97f4d462009-09-18 19:06:35 +0000584 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump6e319f62009-09-11 23:25:56 +0000585 typedef std::pair<Index_t, Index_t> CallOffset;
586 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
Mike Stump77ca8f62009-09-05 07:20:32 +0000587 Thunks_t Thunks;
Mike Stump6e319f62009-09-11 23:25:56 +0000588 typedef llvm::DenseMap<const CXXMethodDecl *,
589 std::pair<CallOffset, CallOffset> > CovariantThunks_t;
590 CovariantThunks_t CovariantThunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000591 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000592 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000593 // FIXME: Linkage should follow vtable
594 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000595 const uint32_t LLVMPointerWidth;
596 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000597public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000598 VtableBuilder(std::vector<llvm::Constant *> &meth,
599 const CXXRecordDecl *c,
600 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000601 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
602 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000603 CGM(cgm), Extern(true),
604 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000605 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
606 }
Mike Stump32f37012009-08-18 21:49:00 +0000607
Mike Stumpf0070db2009-08-26 20:46:33 +0000608 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stump97f4d462009-09-18 19:06:35 +0000609 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
610 { return VBIndex; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000611
Mike Stump15a24e02009-08-28 23:22:54 +0000612 llvm::Constant *wrap(Index_t i) {
613 llvm::Constant *m;
614 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
615 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000616 }
617
Mike Stump15a24e02009-08-28 23:22:54 +0000618 llvm::Constant *wrap(llvm::Constant *m) {
619 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000620 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000621
Mike Stump7fa0d932009-08-20 02:11:48 +0000622 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000623 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump97f4d462009-09-18 19:06:35 +0000624 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Mike Stump7fa0d932009-08-20 02:11:48 +0000625 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000626 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000627 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
628 if (i->isVirtual() && !SeenVBase.count(Base)) {
629 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000630 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000631 llvm::Constant *m = wrap(BaseOffset);
632 m = wrap((0?700:0) + BaseOffset);
Mike Stump97f4d462009-09-18 19:06:35 +0000633 VBIndex[Base] = -(offsets.size()*LLVMPointerWidth/8)
634 - 3*LLVMPointerWidth/8;
Mike Stump7fa0d932009-08-20 02:11:48 +0000635 offsets.push_back(m);
636 }
Mike Stumpb9837442009-08-20 07:22:17 +0000637 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000638 }
639 }
640
Mike Stumpb9871a22009-08-21 01:45:00 +0000641 void StartNewTable() {
642 SeenVBase.clear();
643 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000644
Mike Stump97f4d462009-09-18 19:06:35 +0000645 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
646
647 /// getVbaseOffset - Returns the index into the vtable for the virtual base
648 /// offset for the given (B) virtual base of the derived class D.
649 Index_t getVbaseOffset(QualType qB, QualType qD) {
650 qD = qD->getAs<PointerType>()->getPointeeType();
651 qB = qB->getAs<PointerType>()->getPointeeType();
652 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
653 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
654 if (D != Class)
655 return VBlookup(D, B);
656 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
657 i = VBIndex.find(B);
658 if (i != VBIndex.end())
659 return i->second;
660 // FIXME: temporal botch, is this data here, by the time we need it?
661
662 // FIXME: Locate the containing virtual base first.
663 return 42;
664 }
665
Mike Stump35191b62009-09-01 22:20:28 +0000666 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000667 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000668 typedef CXXMethodDecl::method_iterator meth_iter;
669
Mike Stumpb9871a22009-08-21 01:45:00 +0000670 // FIXME: Don't like the nested loops. For very large inheritance
671 // heirarchies we could have a table on the side with the final overridder
672 // and just replace each instance of an overridden method once. Would be
673 // nice to measure the cost/benefit on real code.
674
Mike Stumpb9871a22009-08-21 01:45:00 +0000675 for (meth_iter mi = MD->begin_overridden_methods(),
676 e = MD->end_overridden_methods();
677 mi != e; ++mi) {
678 const CXXMethodDecl *OMD = *mi;
679 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000680 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000681 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
682
Mike Stumpdec025b2009-09-07 04:27:52 +0000683 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000684 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000685 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000686 if (submethods[i] != om)
687 continue;
John McCall183700f2009-09-21 23:43:11 +0000688 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000689 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
John McCall183700f2009-09-21 23:43:11 +0000690 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000691 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
692 CallOffset ReturnOffset = std::make_pair(0, 0);
693 if (oret != ret) {
694 // FIXME: calculate offsets for covariance
Mike Stump97f4d462009-09-18 19:06:35 +0000695 ReturnOffset = std::make_pair(42,getVbaseOffset(oret, ret));
Mike Stump6e319f62009-09-11 23:25:56 +0000696 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000697 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000698 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000699
700 Thunks.erase(OMD);
701 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000702 Index_t &idx = VCall[OMD];
703 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000704 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000705 idx = VCalls.size()+1;
706 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000707 } else {
708 VCallOffset[MD] = VCallOffset[OMD];
709 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000710 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000711 VCall[MD] = idx;
Mike Stump6e319f62009-09-11 23:25:56 +0000712 CallOffset ThisOffset;
713 // FIXME: calculate non-virtual offset
714 ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
715 if (ReturnOffset.first || ReturnOffset.second)
716 CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset);
717 else
718 Thunks[MD] = ThisOffset;
Mike Stump35191b62009-09-01 22:20:28 +0000719 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000720 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000721#if 0
722 // FIXME: finish off
723 int64_t O = VCallOffset[OMD] - Offset/8;
724 if (O) {
725 Thunks[MD] = std::make_pair(O, 0);
726 }
727#endif
728 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000729 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000730 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000731
Mike Stump35191b62009-09-01 22:20:28 +0000732 return false;
733 }
734
Mike Stump98cc7102009-09-05 11:28:33 +0000735 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000736 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
737 i != e; ++i) {
738 const CXXMethodDecl *MD = i->first;
739 Index_t idx = Index[MD];
740 Index_t nv_O = i->second.first;
741 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000742 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000743 }
744 Thunks.clear();
Mike Stump6e319f62009-09-11 23:25:56 +0000745 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
746 e = CovariantThunks.end();
747 i != e; ++i) {
748 const CXXMethodDecl *MD = i->first;
749 Index_t idx = Index[MD];
750 Index_t nv_t = i->second.first.first;
751 Index_t v_t = i->second.first.second;
752 Index_t nv_r = i->second.second.first;
753 Index_t v_r = i->second.second.second;
754 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
755 v_r);
756 }
757 CovariantThunks.clear();
Mike Stump77ca8f62009-09-05 07:20:32 +0000758 }
759
Mike Stumpdec025b2009-09-07 04:27:52 +0000760 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
761 int64_t> > *Path, bool MorallyVirtual) {
762 for (std::vector<std::pair<const CXXRecordDecl *,
763 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000764 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000765 const CXXRecordDecl *RD = i->first;
766 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000767 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
768 ++mi)
769 if (mi->isVirtual()) {
770 const CXXMethodDecl *MD = *mi;
Anders Carlssonc7cba152009-09-12 00:00:29 +0000771 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD));
Mike Stumpdec025b2009-09-07 04:27:52 +0000772 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000773 }
774 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000775 }
776
Mike Stump6d10eb82009-09-05 07:49:12 +0000777 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlssonc7cba152009-09-12 00:00:29 +0000778 llvm::Constant *m = 0;
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000779 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonc7cba152009-09-12 00:00:29 +0000780 m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000781 else
Anders Carlssonc7cba152009-09-12 00:00:29 +0000782 m = wrap(CGM.GetAddrOfFunction(MD));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000783
Mike Stump77ca8f62009-09-05 07:20:32 +0000784 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000785 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000786 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Mike Stumpb9871a22009-08-21 01:45:00 +0000788 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000789 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000790 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000791 if (MorallyVirtual) {
792 VCallOffset[MD] = Offset/8;
793 Index_t &idx = VCall[MD];
794 // Allocate the first one, after that, we reuse the previous one.
795 if (idx == 0) {
796 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000797 VCalls.push_back(0);
798 }
799 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000800 }
801
Mike Stump6d10eb82009-09-05 07:49:12 +0000802 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
803 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000804 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
805 ++mi)
806 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +0000807 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +0000808 }
Mike Stump65defe32009-08-18 21:03:28 +0000809
Mike Stump77ca8f62009-09-05 07:20:32 +0000810 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
811 const CXXRecordDecl *PrimaryBase,
812 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
813 int64_t Offset) {
814 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
815 e = RD->bases_end(); i != e; ++i) {
816 if (i->isVirtual())
817 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000818 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +0000819 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
820 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
821 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
822 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +0000823 std::vector<std::pair<const CXXRecordDecl *,
824 int64_t> > S;
825 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +0000826 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +0000827 }
828 }
829 }
830
Mike Stump6d10eb82009-09-05 07:49:12 +0000831 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
832 const ASTRecordLayout &Layout,
833 const CXXRecordDecl *PrimaryBase,
834 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
835 int64_t Offset, bool ForVirtualBase) {
836 StartNewTable();
837 extra = 0;
838 // FIXME: Cleanup.
839 if (!ForVirtualBase) {
840 // then virtual base offsets...
841 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
842 e = offsets.rend(); i != e; ++i)
843 methods.push_back(*i);
844 }
845
846 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +0000847 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
848 e=VCalls.rend();
849 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +0000850 methods.push_back(wrap((0?600:0) + *i));
851 VCalls.clear();
852
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 methods.push_back(wrap(-(Offset/8)));
861 methods.push_back(rtti);
862 Index_t AddressPoint = methods.size();
863
Mike Stump98cc7102009-09-05 11:28:33 +0000864 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +0000865 methods.insert(methods.end(), submethods.begin(), submethods.end());
866 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +0000867
868 // and then the non-virtual bases.
869 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
870 MorallyVirtual, Offset);
871 return AddressPoint;
872 }
873
Mike Stump078d7782009-09-05 08:40:18 +0000874 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +0000875 if (!RD->isDynamicClass())
876 return;
877
878 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000879 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +0000880 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
881
Mike Stump9bbe9622009-09-05 08:37:03 +0000882 // vtables are composed from the chain of primaries.
883 if (PrimaryBase) {
884 if (PrimaryBaseWasVirtual)
885 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +0000886 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +0000887 }
888
889 // And add the virtuals for the class to the primary vtable.
890 AddMethods(RD, MorallyVirtual, Offset);
891 }
892
Mike Stumpe45c90f2009-09-05 09:10:58 +0000893 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +0000894 bool MorallyVirtual = false, int64_t Offset = 0,
895 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +0000896 std::vector<std::pair<const CXXRecordDecl *,
897 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +0000898 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +0000899 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +0000900
901 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000902 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +0000903 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
904
Mike Stump15a24e02009-08-28 23:22:54 +0000905 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +0000906 extra = 0;
907 GenerateVBaseOffsets(offsets, RD, Offset);
908 if (ForVirtualBase)
909 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +0000910
911 // vtables are composed from the chain of primaries.
912 if (PrimaryBase) {
913 if (PrimaryBaseWasVirtual)
914 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +0000915 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +0000916 }
917
Mike Stump15a24e02009-08-28 23:22:54 +0000918 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +0000919 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +0000920
Mike Stump98cc7102009-09-05 11:28:33 +0000921 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +0000922 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +0000923
Mike Stump6d10eb82009-09-05 07:49:12 +0000924 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
925 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +0000926 }
927
Mike Stump98cc7102009-09-05 11:28:33 +0000928 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +0000929 int64_t Offset = 0,
930 std::vector<std::pair<const CXXRecordDecl *,
931 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +0000932 bool alloc = false;
933 if (Path == 0) {
934 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +0000935 Path = new std::vector<std::pair<const CXXRecordDecl *,
936 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +0000937 }
938 // FIXME: We also need to override using all paths to a virtual base,
939 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +0000940 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +0000941 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
942 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000943 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +0000944 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
945 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
946 // Mark it so we don't output it twice.
947 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +0000948 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +0000949 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +0000950 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +0000951 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000952 int64_t BaseOffset = Offset;
953 if (i->isVirtual())
954 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +0000955 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +0000956 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +0000957 }
Mike Stump98cc7102009-09-05 11:28:33 +0000958 Path->pop_back();
959 if (alloc)
960 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +0000961 }
Mike Stump109b13d2009-08-18 21:30:21 +0000962};
Mike Stump8a12b562009-08-06 15:50:11 +0000963
Mike Stumpf0070db2009-08-26 20:46:33 +0000964class VtableInfo {
965public:
966 typedef VtableBuilder::Index_t Index_t;
967private:
968 CodeGenModule &CGM; // Per-module state.
969 /// Index_t - Vtable index type.
970 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
971 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
972 // FIXME: Move to Context.
973 static MapTy IndexFor;
Mike Stump97f4d462009-09-18 19:06:35 +0000974
975 typedef llvm::DenseMap<const CXXRecordDecl *, Index_t> VBElTy;
976 typedef llvm::DenseMap<const CXXRecordDecl *, VBElTy *> VBMapTy;
977 // FIXME: Move to Context.
978 static VBMapTy VBIndexFor;
Mike Stumpf0070db2009-08-26 20:46:33 +0000979public:
980 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
Mike Stump97f4d462009-09-18 19:06:35 +0000981 void RegisterIndex(const CXXRecordDecl *RD, const ElTy &e) {
Mike Stumpf0070db2009-08-26 20:46:33 +0000982 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
983 // We own a copy of this, it will go away shortly.
Mike Stumpf0070db2009-08-26 20:46:33 +0000984 IndexFor[RD] = new ElTy (e);
985 }
Mike Stump97f4d462009-09-18 19:06:35 +0000986 void RegisterVBIndex(const CXXRecordDecl *RD, const VBElTy &e) {
987 assert(VBIndexFor.find(RD) == VBIndexFor.end() && "Don't compute vtbl twice");
988 // We own a copy of this, it will go away shortly.
989 VBIndexFor[RD] = new VBElTy (e);
990 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000991 Index_t lookup(const CXXMethodDecl *MD) {
992 const CXXRecordDecl *RD = MD->getParent();
993 MapTy::iterator I = IndexFor.find(RD);
994 if (I == IndexFor.end()) {
995 std::vector<llvm::Constant *> methods;
Mike Stump97f4d462009-09-18 19:06:35 +0000996 // FIXME: This seems expensive. Can we do a partial job to get
997 // just this data.
Mike Stumpf0070db2009-08-26 20:46:33 +0000998 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +0000999 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001000 b.GenerateVtableForVBases(RD);
Mike Stump97f4d462009-09-18 19:06:35 +00001001 RegisterIndex(RD, b.getIndex());
Mike Stumpf0070db2009-08-26 20:46:33 +00001002 I = IndexFor.find(RD);
1003 }
1004 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1005 return (*I->second)[MD];
1006 }
Mike Stump97f4d462009-09-18 19:06:35 +00001007 Index_t VBlookup(const CXXRecordDecl *RD, const CXXRecordDecl *BD) {
1008 VBMapTy::iterator I = VBIndexFor.find(RD);
1009 if (I == VBIndexFor.end()) {
1010 std::vector<llvm::Constant *> methods;
1011 // FIXME: This seems expensive. Can we do a partial job to get
1012 // just this data.
1013 VtableBuilder b(methods, RD, CGM);
1014 b.GenerateVtableForBase(RD);
1015 b.GenerateVtableForVBases(RD);
1016 RegisterVBIndex(RD, b.getVBIndex());
1017 I = VBIndexFor.find(RD);
1018 }
1019 assert(I->second->find(BD)!=I->second->end() && "Can't find vtable index");
1020 return (*I->second)[BD];
1021 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001022};
1023
Mike Stump97f4d462009-09-18 19:06:35 +00001024// FIXME: move to Context
1025static VtableInfo *vtableinfo;
1026
1027VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
1028 CXXRecordDecl *B) {
1029 if (vtableinfo == 0)
1030 vtableinfo = new VtableInfo(CGM);
1031
1032 return vtableinfo->VBlookup(D, B);
1033}
1034
1035
Mike Stumpf0070db2009-08-26 20:46:33 +00001036// FIXME: Move to Context.
1037VtableInfo::MapTy VtableInfo::IndexFor;
1038
Mike Stump97f4d462009-09-18 19:06:35 +00001039// FIXME: Move to Context.
1040VtableInfo::VBMapTy VtableInfo::VBIndexFor;
1041
Mike Stumpf1216772009-07-31 18:25:34 +00001042llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001043 llvm::SmallString<256> OutName;
1044 llvm::raw_svector_ostream Out(OutName);
1045 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001046 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001047 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001048 llvm::GlobalVariable::LinkageTypes linktype;
1049 linktype = llvm::GlobalValue::WeakAnyLinkage;
1050 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001051 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001052 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001053
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001054 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001055
Mike Stump276b9f12009-08-16 01:46:26 +00001056 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001057 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001058
Mike Stump276b9f12009-08-16 01:46:26 +00001059 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001060 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001061
Mike Stump82b56962009-07-31 21:43:43 +00001062 llvm::Constant *C;
1063 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1064 C = llvm::ConstantArray::get(type, methods);
1065 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001066 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001067 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001068 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001069 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001070 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001071 return vtable;
1072}
1073
Mike Stumped032eb2009-09-04 18:27:16 +00001074llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1075 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001076 bool Extern, int64_t nv,
1077 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +00001078 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +00001079
1080 FunctionArgList Args;
1081 ImplicitParamDecl *ThisDecl =
1082 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1083 MD->getThisType(getContext()));
1084 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1085 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1086 e = MD->param_end();
1087 i != e; ++i) {
1088 ParmVarDecl *D = *i;
1089 Args.push_back(std::make_pair(D, D->getType()));
1090 }
1091 IdentifierInfo *II
1092 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1093 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1094 getContext().getTranslationUnitDecl(),
1095 SourceLocation(), II, R, 0,
1096 Extern
1097 ? FunctionDecl::Extern
1098 : FunctionDecl::Static,
1099 false, true);
1100 StartFunction(FD, R, Fn, Args, SourceLocation());
1101 // FIXME: generate body
1102 FinishFunction();
1103 return Fn;
1104}
1105
Mike Stump6e319f62009-09-11 23:25:56 +00001106llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1107 const CXXMethodDecl *MD,
1108 bool Extern,
1109 int64_t nv_t,
1110 int64_t v_t,
1111 int64_t nv_r,
1112 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +00001113 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +00001114
1115 FunctionArgList Args;
1116 ImplicitParamDecl *ThisDecl =
1117 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1118 MD->getThisType(getContext()));
1119 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1120 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1121 e = MD->param_end();
1122 i != e; ++i) {
1123 ParmVarDecl *D = *i;
1124 Args.push_back(std::make_pair(D, D->getType()));
1125 }
1126 IdentifierInfo *II
1127 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1128 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1129 getContext().getTranslationUnitDecl(),
1130 SourceLocation(), II, R, 0,
1131 Extern
1132 ? FunctionDecl::Extern
1133 : FunctionDecl::Static,
1134 false, true);
1135 StartFunction(FD, R, Fn, Args, SourceLocation());
1136 // FIXME: generate body
1137 FinishFunction();
1138 return Fn;
1139}
1140
Mike Stump77ca8f62009-09-05 07:20:32 +00001141llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1142 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001143 llvm::SmallString<256> OutName;
1144 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001145 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001146 llvm::GlobalVariable::LinkageTypes linktype;
1147 linktype = llvm::GlobalValue::WeakAnyLinkage;
1148 if (!Extern)
1149 linktype = llvm::GlobalValue::InternalLinkage;
1150 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001151 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +00001152 const llvm::FunctionType *FTy =
1153 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1154 FPT->isVariadic());
1155
1156 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1157 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001158 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001159 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1160 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1161 return m;
1162}
1163
Mike Stump6e319f62009-09-11 23:25:56 +00001164llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1165 bool Extern, int64_t nv_t,
1166 int64_t v_t, int64_t nv_r,
1167 int64_t v_r) {
1168 llvm::SmallString<256> OutName;
1169 llvm::raw_svector_ostream Out(OutName);
1170 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1171 llvm::GlobalVariable::LinkageTypes linktype;
1172 linktype = llvm::GlobalValue::WeakAnyLinkage;
1173 if (!Extern)
1174 linktype = llvm::GlobalValue::InternalLinkage;
1175 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001176 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +00001177 const llvm::FunctionType *FTy =
1178 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1179 FPT->isVariadic());
1180
1181 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1182 &getModule());
1183 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1184 v_r);
1185 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1186 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1187 return m;
1188}
1189
Mike Stumpf0070db2009-08-26 20:46:33 +00001190llvm::Value *
1191CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1192 const llvm::Type *Ty) {
1193 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Mike Stumpf0070db2009-08-26 20:46:33 +00001195 // FIXME: move to Context
1196 if (vtableinfo == 0)
1197 vtableinfo = new VtableInfo(CGM);
1198
1199 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1200
1201 Ty = llvm::PointerType::get(Ty, 0);
1202 Ty = llvm::PointerType::get(Ty, 0);
1203 Ty = llvm::PointerType::get(Ty, 0);
1204 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1205 vtbl = Builder.CreateLoad(vtbl);
1206 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1207 Idx, "vfn");
1208 vfn = Builder.CreateLoad(vfn);
1209 return vfn;
1210}
1211
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001212/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1213/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1214/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001215// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001216void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001217 llvm::Value *Src,
1218 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001219 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001220 QualType Ty) {
1221 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1222 assert(CA && "VLA cannot be copied over");
1223 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001225 // Create a temporary for the loop index and initialize it with 0.
1226 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1227 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001228 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001229 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1230 Builder.CreateStore(zeroConstant, IndexPtr, false);
1231 // Start the loop with a block that tests the condition.
1232 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1233 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001235 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001237 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1238 // Generate: if (loop-index < number-of-elements fall to the loop body,
1239 // otherwise, go to the block after the for-loop.
1240 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001241 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001242 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1243 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001244 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001245 "isless");
1246 // If the condition is true, execute the body.
1247 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001249 EmitBlock(ForBody);
1250 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1251 // Inside the loop body, emit the constructor call on the array element.
1252 Counter = Builder.CreateLoad(IndexPtr);
1253 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1254 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1255 if (BitwiseCopy)
1256 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001257 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001258 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001259 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001260 Ctor_Complete);
1261 CallArgList CallArgs;
1262 // Push the this (Dest) ptr.
1263 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1264 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001266 // Push the Src ptr.
1267 CallArgs.push_back(std::make_pair(RValue::get(Src),
1268 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001269 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001270 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001271 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1272 Callee, CallArgs, BaseCopyCtor);
1273 }
1274 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001276 // Emit the increment of the loop counter.
1277 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1278 Counter = Builder.CreateLoad(IndexPtr);
1279 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1280 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001282 // Finally, branch back up to the condition for the next iteration.
1283 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001285 // Emit the fall-through block.
1286 EmitBlock(AfterFor, true);
1287}
1288
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001289/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001290/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001291/// bitwise assignment or via a copy assignment operator function call.
1292/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001293void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001294 llvm::Value *Src,
1295 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001296 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001297 QualType Ty) {
1298 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1299 assert(CA && "VLA cannot be asssigned");
1300 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001302 // Create a temporary for the loop index and initialize it with 0.
1303 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1304 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001305 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001306 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1307 Builder.CreateStore(zeroConstant, IndexPtr, false);
1308 // Start the loop with a block that tests the condition.
1309 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1310 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001312 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001314 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1315 // Generate: if (loop-index < number-of-elements fall to the loop body,
1316 // otherwise, go to the block after the for-loop.
1317 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001318 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001319 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1320 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001321 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001322 "isless");
1323 // If the condition is true, execute the body.
1324 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001326 EmitBlock(ForBody);
1327 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1328 // Inside the loop body, emit the assignment operator call on array element.
1329 Counter = Builder.CreateLoad(IndexPtr);
1330 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1331 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1332 const CXXMethodDecl *MD = 0;
1333 if (BitwiseAssign)
1334 EmitAggregateCopy(Dest, Src, Ty);
1335 else {
1336 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1337 MD);
1338 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1339 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001340 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001341 const llvm::Type *LTy =
1342 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1343 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001344 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001346 CallArgList CallArgs;
1347 // Push the this (Dest) ptr.
1348 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1349 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001351 // Push the Src ptr.
1352 CallArgs.push_back(std::make_pair(RValue::get(Src),
1353 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001354 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001355 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1356 Callee, CallArgs, MD);
1357 }
1358 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001360 // Emit the increment of the loop counter.
1361 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1362 Counter = Builder.CreateLoad(IndexPtr);
1363 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1364 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001366 // Finally, branch back up to the condition for the next iteration.
1367 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001369 // Emit the fall-through block.
1370 EmitBlock(AfterFor, true);
1371}
1372
Fariborz Jahanianca283612009-08-07 23:51:33 +00001373/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1374/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001375/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001376void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001377 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001378 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001379 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1380 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001381 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1382 /*NullCheckValue=*/false);
1383 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1384 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001385 }
1386 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1387 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001388 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
1391 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001392 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001393 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001394 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001395 CallArgList CallArgs;
1396 // Push the this (Dest) ptr.
1397 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1398 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Fariborz Jahanianca283612009-08-07 23:51:33 +00001400 // Push the Src ptr.
1401 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001402 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001403 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001404 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001405 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1406 Callee, CallArgs, BaseCopyCtor);
1407 }
1408}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001409
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001410/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001411/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001412/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001413// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001414void CodeGenFunction::EmitClassCopyAssignment(
1415 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001416 const CXXRecordDecl *ClassDecl,
1417 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001418 QualType Ty) {
1419 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001420 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1421 /*NullCheckValue=*/false);
1422 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1423 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001424 }
1425 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1426 EmitAggregateCopy(Dest, Src, Ty);
1427 return;
1428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001430 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001431 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001432 MD);
1433 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1434 (void)ConstCopyAssignOp;
1435
John McCall183700f2009-09-21 23:43:11 +00001436 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001437 const llvm::Type *LTy =
1438 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001439 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001440 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001442 CallArgList CallArgs;
1443 // Push the this (Dest) ptr.
1444 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1445 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001447 // Push the Src ptr.
1448 CallArgs.push_back(std::make_pair(RValue::get(Src),
1449 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001450 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001451 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001452 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1453 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001454}
1455
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001456/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001457void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001458CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1459 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001460 llvm::Function *Fn,
1461 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001462 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1463 SourceLocation());
1464 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001465 FinishFunction();
1466}
1467
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001468/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001469/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001470/// The implicitly-defined copy constructor for class X performs a memberwise
1471/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001472/// of initialization of bases and members in a user-defined constructor
1473/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001474/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001475/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001476/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001477/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001478/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001479/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001480/// Virtual base class subobjects shall be copied only once by the
1481/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001482
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001483void
1484CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1485 CXXCtorType Type,
1486 llvm::Function *Fn,
1487 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001488 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001489 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001490 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001491 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1492 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001494 FunctionArgList::const_iterator i = Args.begin();
1495 const VarDecl *ThisArg = i->first;
1496 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1497 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1498 const VarDecl *SrcArg = (i+1)->first;
1499 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1500 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001502 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1503 Base != ClassDecl->bases_end(); ++Base) {
1504 // FIXME. copy constrution of virtual base NYI
1505 if (Base->isVirtual())
1506 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001508 CXXRecordDecl *BaseClassDecl
1509 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001510 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1511 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001512 }
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001514 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1515 FieldEnd = ClassDecl->field_end();
1516 Field != FieldEnd; ++Field) {
1517 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001518 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001519 getContext().getAsConstantArrayType(FieldType);
1520 if (Array)
1521 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001523 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1524 CXXRecordDecl *FieldClassDecl
1525 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1526 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1527 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001528 if (Array) {
1529 const llvm::Type *BasePtr = ConvertType(FieldType);
1530 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001531 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001532 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001533 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001534 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1535 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1536 FieldClassDecl, FieldType);
1537 }
Mike Stump1eb44332009-09-09 15:08:12 +00001538 else
1539 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001540 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001541 continue;
1542 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001543 // Do a built-in assignment of scalar data members.
1544 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1545 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1546 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1547 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001548 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001549 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001550}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001551
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001552/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001553/// Before the implicitly-declared copy assignment operator for a class is
1554/// implicitly defined, all implicitly- declared copy assignment operators for
1555/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001556/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001557/// The implicitly-defined copy assignment operator for class X performs
1558/// memberwise assignment of its subob- jects. The direct base classes of X are
1559/// assigned first, in the order of their declaration in
1560/// the base-specifier-list, and then the immediate nonstatic data members of X
1561/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001562/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001563/// if the subobject is of class type, the copy assignment operator for the
1564/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001565/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001566///
Mike Stump1eb44332009-09-09 15:08:12 +00001567/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001568/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001569///
Mike Stump1eb44332009-09-09 15:08:12 +00001570/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001571/// used.
1572void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001573 llvm::Function *Fn,
1574 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001575
1576 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1577 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1578 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001579 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001581 FunctionArgList::const_iterator i = Args.begin();
1582 const VarDecl *ThisArg = i->first;
1583 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1584 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1585 const VarDecl *SrcArg = (i+1)->first;
1586 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1587 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001589 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1590 Base != ClassDecl->bases_end(); ++Base) {
1591 // FIXME. copy assignment of virtual base NYI
1592 if (Base->isVirtual())
1593 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001595 CXXRecordDecl *BaseClassDecl
1596 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1597 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1598 Base->getType());
1599 }
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001601 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1602 FieldEnd = ClassDecl->field_end();
1603 Field != FieldEnd; ++Field) {
1604 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001605 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001606 getContext().getAsConstantArrayType(FieldType);
1607 if (Array)
1608 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001610 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1611 CXXRecordDecl *FieldClassDecl
1612 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1613 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1614 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001615 if (Array) {
1616 const llvm::Type *BasePtr = ConvertType(FieldType);
1617 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1618 llvm::Value *DestBaseAddrPtr =
1619 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1620 llvm::Value *SrcBaseAddrPtr =
1621 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1622 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1623 FieldClassDecl, FieldType);
1624 }
1625 else
Mike Stump1eb44332009-09-09 15:08:12 +00001626 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001627 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001628 continue;
1629 }
1630 // Do a built-in assignment of scalar data members.
1631 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1632 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1633 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1634 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001637 // return *this;
1638 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001640 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001641}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001642
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001643/// EmitCtorPrologue - This routine generates necessary code to initialize
1644/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001645/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001646void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1647 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001648 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001649 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001650 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001652 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001653 E = CD->init_end();
1654 B != E; ++B) {
1655 CXXBaseOrMemberInitializer *Member = (*B);
1656 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001657 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001658 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001659 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001660 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001661 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1662 BaseClassDecl,
1663 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001664 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001665 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001666 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001667 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001668 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001669 // non-static data member initilaizers.
1670 FieldDecl *Field = Member->getMember();
1671 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001672 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001673 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001674 if (Array)
1675 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Mike Stumpf1216772009-07-31 18:25:34 +00001677 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001678 LValue LHS;
1679 if (FieldType->isReferenceType()) {
1680 // FIXME: This is really ugly; should be refactored somehow
1681 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1682 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
1683 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(),
1684 QualType::GCNone, FieldType.getAddressSpace());
1685 } else {
1686 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1687 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001688 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001689 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001690 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001691 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001692 if (Array) {
1693 const llvm::Type *BasePtr = ConvertType(FieldType);
1694 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001695 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001696 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001697 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001698 Array, BaseAddrPtr);
1699 }
1700 else
1701 EmitCXXConstructorCall(Member->getConstructor(),
1702 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001703 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001704 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001705 continue;
1706 }
1707 else {
1708 // Initializing an anonymous union data member.
1709 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001710 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001711 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001712 FieldType = anonMember->getType();
1713 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001714 }
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001716 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001717 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001718 RValue RHS;
1719 if (FieldType->isReferenceType())
1720 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1721 /*IsInitializer=*/true);
1722 else
1723 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1724 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001725 }
1726 }
Mike Stumpf1216772009-07-31 18:25:34 +00001727
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001728 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001729 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001730 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001731 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001732 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001733 ClassDecl->bases_begin();
1734 Base != ClassDecl->bases_end(); ++Base) {
1735 // FIXME. copy assignment of virtual base NYI
1736 if (Base->isVirtual())
1737 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001739 CXXRecordDecl *BaseClassDecl
1740 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1741 if (BaseClassDecl->hasTrivialConstructor())
1742 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001743 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001744 BaseClassDecl->getDefaultConstructor(getContext())) {
1745 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001746 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1747 BaseClassDecl,
1748 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001749 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1750 }
1751 }
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001753 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1754 FieldEnd = ClassDecl->field_end();
1755 Field != FieldEnd; ++Field) {
1756 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001757 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001758 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001759 if (Array)
1760 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001761 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1762 continue;
1763 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001764 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001765 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1766 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1767 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001768 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001769 MemberClassDecl->getDefaultConstructor(getContext())) {
1770 LoadOfThis = LoadCXXThis();
1771 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001772 if (Array) {
1773 const llvm::Type *BasePtr = ConvertType(FieldType);
1774 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001775 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001776 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1777 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1778 }
1779 else
Mike Stump1eb44332009-09-09 15:08:12 +00001780 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001781 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001782 }
1783 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001784 }
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Mike Stumpf1216772009-07-31 18:25:34 +00001786 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001787 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001788 if (!LoadOfThis)
1789 LoadOfThis = LoadCXXThis();
1790 llvm::Value *VtableField;
1791 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001792 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001793 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1794 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1795 llvm::Value *vtable = GenerateVtable(ClassDecl);
1796 Builder.CreateStore(vtable, VtableField);
1797 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001798}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001799
1800/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001801/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001802/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001803/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001804void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1805 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001806 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001807 assert(!ClassDecl->getNumVBases() &&
1808 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001809 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001811 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1812 *E = DD->destr_end(); B != E; ++B) {
1813 uintptr_t BaseOrMember = (*B);
1814 if (DD->isMemberToDestroy(BaseOrMember)) {
1815 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1816 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001817 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001818 getContext().getAsConstantArrayType(FieldType);
1819 if (Array)
1820 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001821 const RecordType *RT = FieldType->getAs<RecordType>();
1822 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1823 if (FieldClassDecl->hasTrivialDestructor())
1824 continue;
1825 llvm::Value *LoadOfThis = LoadCXXThis();
1826 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001827 if (Array) {
1828 const llvm::Type *BasePtr = ConvertType(FieldType);
1829 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001830 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001831 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001832 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001833 Array, BaseAddrPtr);
1834 }
1835 else
1836 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1837 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001838 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001839 const RecordType *RT =
1840 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1841 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1842 if (BaseClassDecl->hasTrivialDestructor())
1843 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001844 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1845 ClassDecl, BaseClassDecl,
1846 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001847 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001848 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001849 }
1850 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001851 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1852 return;
1853 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001854 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001855 // reverse order of their construction.
1856 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001858 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1859 FieldEnd = ClassDecl->field_end();
1860 Field != FieldEnd; ++Field) {
1861 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001862 if (getContext().getAsConstantArrayType(FieldType))
1863 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001864 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1865 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1866 if (FieldClassDecl->hasTrivialDestructor())
1867 continue;
1868 DestructedFields.push_back(*Field);
1869 }
1870 }
1871 if (!DestructedFields.empty())
1872 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1873 FieldDecl *Field = DestructedFields[i];
1874 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001875 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001876 getContext().getAsConstantArrayType(FieldType);
1877 if (Array)
1878 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001879 const RecordType *RT = FieldType->getAs<RecordType>();
1880 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1881 llvm::Value *LoadOfThis = LoadCXXThis();
1882 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001883 if (Array) {
1884 const llvm::Type *BasePtr = ConvertType(FieldType);
1885 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001886 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001887 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001888 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001889 Array, BaseAddrPtr);
1890 }
1891 else
1892 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1893 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001894 }
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001896 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1897 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1898 Base != ClassDecl->bases_end(); ++Base) {
1899 // FIXME. copy assignment of virtual base NYI
1900 if (Base->isVirtual())
1901 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001903 CXXRecordDecl *BaseClassDecl
1904 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1905 if (BaseClassDecl->hasTrivialDestructor())
1906 continue;
1907 DestructedBases.push_back(BaseClassDecl);
1908 }
1909 if (DestructedBases.empty())
1910 return;
1911 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1912 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001913 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1914 ClassDecl,BaseClassDecl,
1915 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001916 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1917 Dtor_Complete, V);
1918 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001919}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001920
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001921void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1922 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001923 llvm::Function *Fn,
1924 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001926 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001927 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1928 "SynthesizeDefaultDestructor - destructor has user declaration");
1929 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001931 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1932 SourceLocation());
1933 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001934 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001935}