blob: 0a0d29169f12071d1e6aca7c1cba108fae2f53bb [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
Anders Carlsson569c1f42009-09-23 02:45:36 +0000281/// array.
282/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
283/// array type and 'ArrayPtr' points to the beginning fo the array.
284/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000285void
286CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000287 const ConstantArrayType *ArrayTy,
288 llvm::Value *ArrayPtr) {
289 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
290 llvm::Value * NumElements =
291 llvm::ConstantInt::get(SizeTy,
292 getContext().getConstantArrayElementCount(ArrayTy));
293
294 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
295}
296
297void
298CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
299 llvm::Value *NumElements,
300 llvm::Value *ArrayPtr) {
301 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000303 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000304 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
305 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
306 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000308 // Start the loop with a block that tests the condition.
309 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
310 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000312 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000314 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000316 // Generate: if (loop-index < number-of-elements fall to the loop body,
317 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000318 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000319 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000320 // If the condition is true, execute the body.
321 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000323 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000325 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000326 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000327 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000328 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
329 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000330 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000332 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000334 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000335 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000336 Counter = Builder.CreateLoad(IndexPtr);
337 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
338 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000340 // Finally, branch back up to the condition for the next iteration.
341 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000343 // Emit the fall-through block.
344 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000345}
346
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000347/// EmitCXXAggrDestructorCall - calls the default destructor on array
348/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000349void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000350CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
351 const ArrayType *Array,
352 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000353 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
354 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000355 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000356 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000357 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000358 // Create a temporary for the loop index and initialize it with count of
359 // array elements.
360 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
361 "loop.index");
362 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000363 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000364 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
365 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000367 // Start the loop with a block that tests the condition.
368 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
369 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000371 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000373 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000375 // Generate: if (loop-index != 0 fall to the loop body,
376 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000377 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000378 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
379 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
380 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
381 "isne");
382 // If the condition is true, execute the body.
383 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000385 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000387 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
388 // Inside the loop body, emit the constructor call on the array element.
389 Counter = Builder.CreateLoad(IndexPtr);
390 Counter = Builder.CreateSub(Counter, One);
391 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
392 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000394 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000396 // Emit the decrement of the loop counter.
397 Counter = Builder.CreateLoad(IndexPtr);
398 Counter = Builder.CreateSub(Counter, One, "dec");
399 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000401 // Finally, branch back up to the condition for the next iteration.
402 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000404 // Emit the fall-through block.
405 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000406}
407
408void
Mike Stump1eb44332009-09-09 15:08:12 +0000409CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
410 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000411 llvm::Value *This,
412 CallExpr::const_arg_iterator ArgBeg,
413 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000414 if (D->isCopyConstructor(getContext())) {
415 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
416 if (ClassDecl->hasTrivialCopyConstructor()) {
417 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
418 "EmitCXXConstructorCall - user declared copy constructor");
419 const Expr *E = (*ArgBeg);
420 QualType Ty = E->getType();
421 llvm::Value *Src = EmitLValue(E).getAddress();
422 EmitAggregateCopy(This, Src, Ty);
423 return;
424 }
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000427 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
428
429 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000430}
431
Mike Stump1eb44332009-09-09 15:08:12 +0000432void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000433 CXXDtorType Type,
434 llvm::Value *This) {
435 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Anders Carlsson7267c162009-05-29 21:03:38 +0000437 EmitCXXMemberCall(D, Callee, This, 0, 0);
438}
439
Mike Stump1eb44332009-09-09 15:08:12 +0000440void
441CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000442 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000443 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000444
445 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000446 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000447 if (RD->hasTrivialConstructor())
448 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000449
Mike Stump1eb44332009-09-09 15:08:12 +0000450 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000451 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000452 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000453 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000454 EmitAggExpr((*i), Dest, false);
455 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000456 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000457 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000458 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000459 E->arg_begin(), E->arg_end());
460}
461
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000462void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000463 EmitGlobal(GlobalDecl(D, Ctor_Complete));
464 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000465}
Anders Carlsson363c1842009-04-16 23:57:24 +0000466
Mike Stump1eb44332009-09-09 15:08:12 +0000467void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000468 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Anders Carlsson27ae5362009-04-17 01:58:57 +0000470 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000472 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Anders Carlsson27ae5362009-04-17 01:58:57 +0000474 SetFunctionDefinitionAttributes(D, Fn);
475 SetLLVMFunctionAttributesForDefinition(D, Fn);
476}
477
Anders Carlsson363c1842009-04-16 23:57:24 +0000478llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000479CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000480 CXXCtorType Type) {
481 const llvm::FunctionType *FTy =
482 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Anders Carlsson363c1842009-04-16 23:57:24 +0000484 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000485 return cast<llvm::Function>(
486 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000487}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000488
Mike Stump1eb44332009-09-09 15:08:12 +0000489const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000490 CXXCtorType Type) {
491 llvm::SmallString<256> Name;
492 llvm::raw_svector_ostream Out(Name);
493 mangleCXXCtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Anders Carlsson27ae5362009-04-17 01:58:57 +0000495 Name += '\0';
496 return UniqueMangledName(Name.begin(), Name.end());
497}
498
499void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000500 EmitCXXDestructor(D, Dtor_Complete);
501 EmitCXXDestructor(D, Dtor_Base);
502}
503
Mike Stump1eb44332009-09-09 15:08:12 +0000504void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000505 CXXDtorType Type) {
506 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000508 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Anders Carlsson27ae5362009-04-17 01:58:57 +0000510 SetFunctionDefinitionAttributes(D, Fn);
511 SetLLVMFunctionAttributesForDefinition(D, Fn);
512}
513
514llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000515CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000516 CXXDtorType Type) {
517 const llvm::FunctionType *FTy =
518 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Anders Carlsson27ae5362009-04-17 01:58:57 +0000520 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000521 return cast<llvm::Function>(
522 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000523}
524
Mike Stump1eb44332009-09-09 15:08:12 +0000525const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000526 CXXDtorType Type) {
527 llvm::SmallString<256> Name;
528 llvm::raw_svector_ostream Out(Name);
529 mangleCXXDtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlsson27ae5362009-04-17 01:58:57 +0000531 Name += '\0';
532 return UniqueMangledName(Name.begin(), Name.end());
533}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000534
Mike Stump32f37012009-08-18 21:49:00 +0000535llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000536 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000537 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000538 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000539
540 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000541 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000542
543 llvm::SmallString<256> OutName;
544 llvm::raw_svector_ostream Out(OutName);
545 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000546 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000547 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000548 llvm::GlobalVariable::LinkageTypes linktype;
549 linktype = llvm::GlobalValue::WeakAnyLinkage;
550 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000551 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000552 // FIXME: descriptor
553 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000554 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000555 // FIXME: TS
556 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
557
558 llvm::Constant *C;
559 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
560 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000561 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000562 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000563 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
564 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000565}
566
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000567class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000568public:
569 /// Index_t - Vtable index type.
570 typedef uint64_t Index_t;
571private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000572 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000573 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000574 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000575 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000576 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000577 /// BLayout - Layout for the most derived class that this vtable is being
578 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000579 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000580 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000581 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000582 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000583 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000584 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000585 /// Index - Maps a method decl into a vtable index. Useful for virtual
586 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000587 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000588 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
589 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump97f4d462009-09-18 19:06:35 +0000590 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump6e319f62009-09-11 23:25:56 +0000591 typedef std::pair<Index_t, Index_t> CallOffset;
592 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
Mike Stump77ca8f62009-09-05 07:20:32 +0000593 Thunks_t Thunks;
Mike Stump6e319f62009-09-11 23:25:56 +0000594 typedef llvm::DenseMap<const CXXMethodDecl *,
595 std::pair<CallOffset, CallOffset> > CovariantThunks_t;
596 CovariantThunks_t CovariantThunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000597 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000598 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000599 // FIXME: Linkage should follow vtable
600 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000601 const uint32_t LLVMPointerWidth;
602 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000603public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000604 VtableBuilder(std::vector<llvm::Constant *> &meth,
605 const CXXRecordDecl *c,
606 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000607 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
608 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000609 CGM(cgm), Extern(true),
610 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000611 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
612 }
Mike Stump32f37012009-08-18 21:49:00 +0000613
Mike Stumpf0070db2009-08-26 20:46:33 +0000614 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stump97f4d462009-09-18 19:06:35 +0000615 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
616 { return VBIndex; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000617
Mike Stump15a24e02009-08-28 23:22:54 +0000618 llvm::Constant *wrap(Index_t i) {
619 llvm::Constant *m;
620 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
621 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000622 }
623
Mike Stump15a24e02009-08-28 23:22:54 +0000624 llvm::Constant *wrap(llvm::Constant *m) {
625 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000626 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000627
Mike Stump7fa0d932009-08-20 02:11:48 +0000628 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000629 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump97f4d462009-09-18 19:06:35 +0000630 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Mike Stump7fa0d932009-08-20 02:11:48 +0000631 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000632 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000633 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
634 if (i->isVirtual() && !SeenVBase.count(Base)) {
635 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000636 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000637 llvm::Constant *m = wrap(BaseOffset);
638 m = wrap((0?700:0) + BaseOffset);
Mike Stump97f4d462009-09-18 19:06:35 +0000639 VBIndex[Base] = -(offsets.size()*LLVMPointerWidth/8)
640 - 3*LLVMPointerWidth/8;
Mike Stump7fa0d932009-08-20 02:11:48 +0000641 offsets.push_back(m);
642 }
Mike Stumpb9837442009-08-20 07:22:17 +0000643 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000644 }
645 }
646
Mike Stumpb9871a22009-08-21 01:45:00 +0000647 void StartNewTable() {
648 SeenVBase.clear();
649 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000650
Mike Stump97f4d462009-09-18 19:06:35 +0000651 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
652
653 /// getVbaseOffset - Returns the index into the vtable for the virtual base
654 /// offset for the given (B) virtual base of the derived class D.
655 Index_t getVbaseOffset(QualType qB, QualType qD) {
656 qD = qD->getAs<PointerType>()->getPointeeType();
657 qB = qB->getAs<PointerType>()->getPointeeType();
658 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
659 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
660 if (D != Class)
661 return VBlookup(D, B);
662 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
663 i = VBIndex.find(B);
664 if (i != VBIndex.end())
665 return i->second;
666 // FIXME: temporal botch, is this data here, by the time we need it?
667
668 // FIXME: Locate the containing virtual base first.
669 return 42;
670 }
671
Mike Stump35191b62009-09-01 22:20:28 +0000672 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000673 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000674 typedef CXXMethodDecl::method_iterator meth_iter;
675
Mike Stumpb9871a22009-08-21 01:45:00 +0000676 // FIXME: Don't like the nested loops. For very large inheritance
677 // heirarchies we could have a table on the side with the final overridder
678 // and just replace each instance of an overridden method once. Would be
679 // nice to measure the cost/benefit on real code.
680
Mike Stumpb9871a22009-08-21 01:45:00 +0000681 for (meth_iter mi = MD->begin_overridden_methods(),
682 e = MD->end_overridden_methods();
683 mi != e; ++mi) {
684 const CXXMethodDecl *OMD = *mi;
685 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000686 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000687 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
688
Mike Stumpdec025b2009-09-07 04:27:52 +0000689 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000690 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000691 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000692 if (submethods[i] != om)
693 continue;
John McCall183700f2009-09-21 23:43:11 +0000694 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000695 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
John McCall183700f2009-09-21 23:43:11 +0000696 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000697 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
698 CallOffset ReturnOffset = std::make_pair(0, 0);
699 if (oret != ret) {
700 // FIXME: calculate offsets for covariance
Mike Stump97f4d462009-09-18 19:06:35 +0000701 ReturnOffset = std::make_pair(42,getVbaseOffset(oret, ret));
Mike Stump6e319f62009-09-11 23:25:56 +0000702 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000703 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000704 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000705
706 Thunks.erase(OMD);
707 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000708 Index_t &idx = VCall[OMD];
709 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000710 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000711 idx = VCalls.size()+1;
712 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000713 } else {
714 VCallOffset[MD] = VCallOffset[OMD];
715 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000716 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000717 VCall[MD] = idx;
Mike Stump6e319f62009-09-11 23:25:56 +0000718 CallOffset ThisOffset;
719 // FIXME: calculate non-virtual offset
720 ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
721 if (ReturnOffset.first || ReturnOffset.second)
722 CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset);
723 else
724 Thunks[MD] = ThisOffset;
Mike Stump35191b62009-09-01 22:20:28 +0000725 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000726 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000727#if 0
728 // FIXME: finish off
729 int64_t O = VCallOffset[OMD] - Offset/8;
730 if (O) {
731 Thunks[MD] = std::make_pair(O, 0);
732 }
733#endif
734 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000735 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000736 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000737
Mike Stump35191b62009-09-01 22:20:28 +0000738 return false;
739 }
740
Mike Stump98cc7102009-09-05 11:28:33 +0000741 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000742 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
743 i != e; ++i) {
744 const CXXMethodDecl *MD = i->first;
745 Index_t idx = Index[MD];
746 Index_t nv_O = i->second.first;
747 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000748 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000749 }
750 Thunks.clear();
Mike Stump6e319f62009-09-11 23:25:56 +0000751 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
752 e = CovariantThunks.end();
753 i != e; ++i) {
754 const CXXMethodDecl *MD = i->first;
755 Index_t idx = Index[MD];
756 Index_t nv_t = i->second.first.first;
757 Index_t v_t = i->second.first.second;
758 Index_t nv_r = i->second.second.first;
759 Index_t v_r = i->second.second.second;
760 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
761 v_r);
762 }
763 CovariantThunks.clear();
Mike Stump77ca8f62009-09-05 07:20:32 +0000764 }
765
Mike Stumpdec025b2009-09-07 04:27:52 +0000766 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
767 int64_t> > *Path, bool MorallyVirtual) {
768 for (std::vector<std::pair<const CXXRecordDecl *,
769 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000770 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000771 const CXXRecordDecl *RD = i->first;
772 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000773 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
774 ++mi)
775 if (mi->isVirtual()) {
776 const CXXMethodDecl *MD = *mi;
Anders Carlssonc7cba152009-09-12 00:00:29 +0000777 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD));
Mike Stumpdec025b2009-09-07 04:27:52 +0000778 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000779 }
780 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000781 }
782
Mike Stump6d10eb82009-09-05 07:49:12 +0000783 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlssonc7cba152009-09-12 00:00:29 +0000784 llvm::Constant *m = 0;
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000785 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonc7cba152009-09-12 00:00:29 +0000786 m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000787 else
Anders Carlssonc7cba152009-09-12 00:00:29 +0000788 m = wrap(CGM.GetAddrOfFunction(MD));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000789
Mike Stump77ca8f62009-09-05 07:20:32 +0000790 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000791 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000792 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Mike Stumpb9871a22009-08-21 01:45:00 +0000794 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000795 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000796 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000797 if (MorallyVirtual) {
798 VCallOffset[MD] = Offset/8;
799 Index_t &idx = VCall[MD];
800 // Allocate the first one, after that, we reuse the previous one.
801 if (idx == 0) {
802 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000803 VCalls.push_back(0);
804 }
805 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000806 }
807
Mike Stump6d10eb82009-09-05 07:49:12 +0000808 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
809 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000810 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
811 ++mi)
812 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +0000813 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +0000814 }
Mike Stump65defe32009-08-18 21:03:28 +0000815
Mike Stump77ca8f62009-09-05 07:20:32 +0000816 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
817 const CXXRecordDecl *PrimaryBase,
818 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
819 int64_t Offset) {
820 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
821 e = RD->bases_end(); i != e; ++i) {
822 if (i->isVirtual())
823 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000824 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +0000825 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
826 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
827 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
828 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +0000829 std::vector<std::pair<const CXXRecordDecl *,
830 int64_t> > S;
831 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +0000832 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +0000833 }
834 }
835 }
836
Mike Stump6d10eb82009-09-05 07:49:12 +0000837 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
838 const ASTRecordLayout &Layout,
839 const CXXRecordDecl *PrimaryBase,
840 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
841 int64_t Offset, bool ForVirtualBase) {
842 StartNewTable();
843 extra = 0;
844 // FIXME: Cleanup.
845 if (!ForVirtualBase) {
846 // then virtual base offsets...
847 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
848 e = offsets.rend(); i != e; ++i)
849 methods.push_back(*i);
850 }
851
852 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +0000853 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
854 e=VCalls.rend();
855 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +0000856 methods.push_back(wrap((0?600:0) + *i));
857 VCalls.clear();
858
859 if (ForVirtualBase) {
860 // then virtual base offsets...
861 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
862 e = offsets.rend(); i != e; ++i)
863 methods.push_back(*i);
864 }
865
866 methods.push_back(wrap(-(Offset/8)));
867 methods.push_back(rtti);
868 Index_t AddressPoint = methods.size();
869
Mike Stump98cc7102009-09-05 11:28:33 +0000870 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +0000871 methods.insert(methods.end(), submethods.begin(), submethods.end());
872 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +0000873
874 // and then the non-virtual bases.
875 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
876 MorallyVirtual, Offset);
877 return AddressPoint;
878 }
879
Mike Stump078d7782009-09-05 08:40:18 +0000880 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +0000881 if (!RD->isDynamicClass())
882 return;
883
884 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000885 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +0000886 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
887
Mike Stump9bbe9622009-09-05 08:37:03 +0000888 // vtables are composed from the chain of primaries.
889 if (PrimaryBase) {
890 if (PrimaryBaseWasVirtual)
891 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +0000892 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +0000893 }
894
895 // And add the virtuals for the class to the primary vtable.
896 AddMethods(RD, MorallyVirtual, Offset);
897 }
898
Mike Stumpe45c90f2009-09-05 09:10:58 +0000899 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +0000900 bool MorallyVirtual = false, int64_t Offset = 0,
901 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +0000902 std::vector<std::pair<const CXXRecordDecl *,
903 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +0000904 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +0000905 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +0000906
907 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000908 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +0000909 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
910
Mike Stump15a24e02009-08-28 23:22:54 +0000911 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +0000912 extra = 0;
913 GenerateVBaseOffsets(offsets, RD, Offset);
914 if (ForVirtualBase)
915 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +0000916
917 // vtables are composed from the chain of primaries.
918 if (PrimaryBase) {
919 if (PrimaryBaseWasVirtual)
920 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +0000921 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +0000922 }
923
Mike Stump15a24e02009-08-28 23:22:54 +0000924 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +0000925 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +0000926
Mike Stump98cc7102009-09-05 11:28:33 +0000927 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +0000928 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +0000929
Mike Stump6d10eb82009-09-05 07:49:12 +0000930 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
931 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +0000932 }
933
Mike Stump98cc7102009-09-05 11:28:33 +0000934 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +0000935 int64_t Offset = 0,
936 std::vector<std::pair<const CXXRecordDecl *,
937 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +0000938 bool alloc = false;
939 if (Path == 0) {
940 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +0000941 Path = new std::vector<std::pair<const CXXRecordDecl *,
942 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +0000943 }
944 // FIXME: We also need to override using all paths to a virtual base,
945 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +0000946 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +0000947 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
948 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000949 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +0000950 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
951 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
952 // Mark it so we don't output it twice.
953 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +0000954 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +0000955 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +0000956 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +0000957 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000958 int64_t BaseOffset = Offset;
959 if (i->isVirtual())
960 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +0000961 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +0000962 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +0000963 }
Mike Stump98cc7102009-09-05 11:28:33 +0000964 Path->pop_back();
965 if (alloc)
966 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +0000967 }
Mike Stump109b13d2009-08-18 21:30:21 +0000968};
Mike Stump8a12b562009-08-06 15:50:11 +0000969
Mike Stumpf0070db2009-08-26 20:46:33 +0000970class VtableInfo {
971public:
972 typedef VtableBuilder::Index_t Index_t;
973private:
974 CodeGenModule &CGM; // Per-module state.
975 /// Index_t - Vtable index type.
976 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
977 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
978 // FIXME: Move to Context.
979 static MapTy IndexFor;
Mike Stump97f4d462009-09-18 19:06:35 +0000980
981 typedef llvm::DenseMap<const CXXRecordDecl *, Index_t> VBElTy;
982 typedef llvm::DenseMap<const CXXRecordDecl *, VBElTy *> VBMapTy;
983 // FIXME: Move to Context.
984 static VBMapTy VBIndexFor;
Mike Stumpf0070db2009-08-26 20:46:33 +0000985public:
986 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
Mike Stump97f4d462009-09-18 19:06:35 +0000987 void RegisterIndex(const CXXRecordDecl *RD, const ElTy &e) {
Mike Stumpf0070db2009-08-26 20:46:33 +0000988 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
989 // We own a copy of this, it will go away shortly.
Mike Stumpf0070db2009-08-26 20:46:33 +0000990 IndexFor[RD] = new ElTy (e);
991 }
Mike Stump97f4d462009-09-18 19:06:35 +0000992 void RegisterVBIndex(const CXXRecordDecl *RD, const VBElTy &e) {
993 assert(VBIndexFor.find(RD) == VBIndexFor.end() && "Don't compute vtbl twice");
994 // We own a copy of this, it will go away shortly.
995 VBIndexFor[RD] = new VBElTy (e);
996 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000997 Index_t lookup(const CXXMethodDecl *MD) {
998 const CXXRecordDecl *RD = MD->getParent();
999 MapTy::iterator I = IndexFor.find(RD);
1000 if (I == IndexFor.end()) {
1001 std::vector<llvm::Constant *> methods;
Mike Stump97f4d462009-09-18 19:06:35 +00001002 // FIXME: This seems expensive. Can we do a partial job to get
1003 // just this data.
Mike Stumpf0070db2009-08-26 20:46:33 +00001004 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001005 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001006 b.GenerateVtableForVBases(RD);
Mike Stump97f4d462009-09-18 19:06:35 +00001007 RegisterIndex(RD, b.getIndex());
Mike Stumpf0070db2009-08-26 20:46:33 +00001008 I = IndexFor.find(RD);
1009 }
1010 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1011 return (*I->second)[MD];
1012 }
Mike Stump97f4d462009-09-18 19:06:35 +00001013 Index_t VBlookup(const CXXRecordDecl *RD, const CXXRecordDecl *BD) {
1014 VBMapTy::iterator I = VBIndexFor.find(RD);
1015 if (I == VBIndexFor.end()) {
1016 std::vector<llvm::Constant *> methods;
1017 // FIXME: This seems expensive. Can we do a partial job to get
1018 // just this data.
1019 VtableBuilder b(methods, RD, CGM);
1020 b.GenerateVtableForBase(RD);
1021 b.GenerateVtableForVBases(RD);
1022 RegisterVBIndex(RD, b.getVBIndex());
1023 I = VBIndexFor.find(RD);
1024 }
1025 assert(I->second->find(BD)!=I->second->end() && "Can't find vtable index");
1026 return (*I->second)[BD];
1027 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001028};
1029
Mike Stump97f4d462009-09-18 19:06:35 +00001030// FIXME: move to Context
1031static VtableInfo *vtableinfo;
1032
1033VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
1034 CXXRecordDecl *B) {
1035 if (vtableinfo == 0)
1036 vtableinfo = new VtableInfo(CGM);
1037
1038 return vtableinfo->VBlookup(D, B);
1039}
1040
1041
Mike Stumpf0070db2009-08-26 20:46:33 +00001042// FIXME: Move to Context.
1043VtableInfo::MapTy VtableInfo::IndexFor;
1044
Mike Stump97f4d462009-09-18 19:06:35 +00001045// FIXME: Move to Context.
1046VtableInfo::VBMapTy VtableInfo::VBIndexFor;
1047
Mike Stumpf1216772009-07-31 18:25:34 +00001048llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001049 llvm::SmallString<256> OutName;
1050 llvm::raw_svector_ostream Out(OutName);
1051 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001052 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001053 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001054 llvm::GlobalVariable::LinkageTypes linktype;
1055 linktype = llvm::GlobalValue::WeakAnyLinkage;
1056 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001057 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001058 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001059
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001060 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001061
Mike Stump276b9f12009-08-16 01:46:26 +00001062 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001063 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001064
Mike Stump276b9f12009-08-16 01:46:26 +00001065 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001066 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001067
Mike Stump82b56962009-07-31 21:43:43 +00001068 llvm::Constant *C;
1069 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1070 C = llvm::ConstantArray::get(type, methods);
1071 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001072 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001073 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001074 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001075 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001076 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001077 return vtable;
1078}
1079
Mike Stumped032eb2009-09-04 18:27:16 +00001080llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1081 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001082 bool Extern, int64_t nv,
1083 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +00001084 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +00001085
1086 FunctionArgList Args;
1087 ImplicitParamDecl *ThisDecl =
1088 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1089 MD->getThisType(getContext()));
1090 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1091 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1092 e = MD->param_end();
1093 i != e; ++i) {
1094 ParmVarDecl *D = *i;
1095 Args.push_back(std::make_pair(D, D->getType()));
1096 }
1097 IdentifierInfo *II
1098 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1099 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1100 getContext().getTranslationUnitDecl(),
1101 SourceLocation(), II, R, 0,
1102 Extern
1103 ? FunctionDecl::Extern
1104 : FunctionDecl::Static,
1105 false, true);
1106 StartFunction(FD, R, Fn, Args, SourceLocation());
1107 // FIXME: generate body
1108 FinishFunction();
1109 return Fn;
1110}
1111
Mike Stump6e319f62009-09-11 23:25:56 +00001112llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1113 const CXXMethodDecl *MD,
1114 bool Extern,
1115 int64_t nv_t,
1116 int64_t v_t,
1117 int64_t nv_r,
1118 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +00001119 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +00001120
1121 FunctionArgList Args;
1122 ImplicitParamDecl *ThisDecl =
1123 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1124 MD->getThisType(getContext()));
1125 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1126 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1127 e = MD->param_end();
1128 i != e; ++i) {
1129 ParmVarDecl *D = *i;
1130 Args.push_back(std::make_pair(D, D->getType()));
1131 }
1132 IdentifierInfo *II
1133 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1134 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1135 getContext().getTranslationUnitDecl(),
1136 SourceLocation(), II, R, 0,
1137 Extern
1138 ? FunctionDecl::Extern
1139 : FunctionDecl::Static,
1140 false, true);
1141 StartFunction(FD, R, Fn, Args, SourceLocation());
1142 // FIXME: generate body
1143 FinishFunction();
1144 return Fn;
1145}
1146
Mike Stump77ca8f62009-09-05 07:20:32 +00001147llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1148 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001149 llvm::SmallString<256> OutName;
1150 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001151 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001152 llvm::GlobalVariable::LinkageTypes linktype;
1153 linktype = llvm::GlobalValue::WeakAnyLinkage;
1154 if (!Extern)
1155 linktype = llvm::GlobalValue::InternalLinkage;
1156 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001157 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +00001158 const llvm::FunctionType *FTy =
1159 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1160 FPT->isVariadic());
1161
1162 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1163 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001164 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001165 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1166 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1167 return m;
1168}
1169
Mike Stump6e319f62009-09-11 23:25:56 +00001170llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1171 bool Extern, int64_t nv_t,
1172 int64_t v_t, int64_t nv_r,
1173 int64_t v_r) {
1174 llvm::SmallString<256> OutName;
1175 llvm::raw_svector_ostream Out(OutName);
1176 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1177 llvm::GlobalVariable::LinkageTypes linktype;
1178 linktype = llvm::GlobalValue::WeakAnyLinkage;
1179 if (!Extern)
1180 linktype = llvm::GlobalValue::InternalLinkage;
1181 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001182 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +00001183 const llvm::FunctionType *FTy =
1184 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1185 FPT->isVariadic());
1186
1187 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1188 &getModule());
1189 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1190 v_r);
1191 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1192 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1193 return m;
1194}
1195
Mike Stumpf0070db2009-08-26 20:46:33 +00001196llvm::Value *
1197CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1198 const llvm::Type *Ty) {
1199 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Mike Stumpf0070db2009-08-26 20:46:33 +00001201 // FIXME: move to Context
1202 if (vtableinfo == 0)
1203 vtableinfo = new VtableInfo(CGM);
1204
1205 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1206
1207 Ty = llvm::PointerType::get(Ty, 0);
1208 Ty = llvm::PointerType::get(Ty, 0);
1209 Ty = llvm::PointerType::get(Ty, 0);
1210 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1211 vtbl = Builder.CreateLoad(vtbl);
1212 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1213 Idx, "vfn");
1214 vfn = Builder.CreateLoad(vfn);
1215 return vfn;
1216}
1217
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001218/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1219/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1220/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001221// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001222void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001223 llvm::Value *Src,
1224 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001225 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001226 QualType Ty) {
1227 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1228 assert(CA && "VLA cannot be copied over");
1229 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001231 // Create a temporary for the loop index and initialize it with 0.
1232 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1233 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001234 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001235 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1236 Builder.CreateStore(zeroConstant, IndexPtr, false);
1237 // Start the loop with a block that tests the condition.
1238 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1239 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001241 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001243 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1244 // Generate: if (loop-index < number-of-elements fall to the loop body,
1245 // otherwise, go to the block after the for-loop.
1246 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001247 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001248 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1249 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001250 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001251 "isless");
1252 // If the condition is true, execute the body.
1253 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001255 EmitBlock(ForBody);
1256 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1257 // Inside the loop body, emit the constructor call on the array element.
1258 Counter = Builder.CreateLoad(IndexPtr);
1259 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1260 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1261 if (BitwiseCopy)
1262 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001263 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001264 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001265 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001266 Ctor_Complete);
1267 CallArgList CallArgs;
1268 // Push the this (Dest) ptr.
1269 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1270 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001272 // Push the Src ptr.
1273 CallArgs.push_back(std::make_pair(RValue::get(Src),
1274 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001275 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001276 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001277 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1278 Callee, CallArgs, BaseCopyCtor);
1279 }
1280 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001282 // Emit the increment of the loop counter.
1283 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1284 Counter = Builder.CreateLoad(IndexPtr);
1285 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1286 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001288 // Finally, branch back up to the condition for the next iteration.
1289 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001291 // Emit the fall-through block.
1292 EmitBlock(AfterFor, true);
1293}
1294
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001295/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001296/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001297/// bitwise assignment or via a copy assignment operator function call.
1298/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001299void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001300 llvm::Value *Src,
1301 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001302 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001303 QualType Ty) {
1304 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1305 assert(CA && "VLA cannot be asssigned");
1306 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001308 // Create a temporary for the loop index and initialize it with 0.
1309 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1310 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001311 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001312 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1313 Builder.CreateStore(zeroConstant, IndexPtr, false);
1314 // Start the loop with a block that tests the condition.
1315 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1316 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001318 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001320 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1321 // Generate: if (loop-index < number-of-elements fall to the loop body,
1322 // otherwise, go to the block after the for-loop.
1323 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001324 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001325 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1326 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001327 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001328 "isless");
1329 // If the condition is true, execute the body.
1330 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001332 EmitBlock(ForBody);
1333 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1334 // Inside the loop body, emit the assignment operator call on array element.
1335 Counter = Builder.CreateLoad(IndexPtr);
1336 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1337 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1338 const CXXMethodDecl *MD = 0;
1339 if (BitwiseAssign)
1340 EmitAggregateCopy(Dest, Src, Ty);
1341 else {
1342 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1343 MD);
1344 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1345 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001346 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001347 const llvm::Type *LTy =
1348 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1349 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001350 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001352 CallArgList CallArgs;
1353 // Push the this (Dest) ptr.
1354 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1355 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001357 // Push the Src ptr.
1358 CallArgs.push_back(std::make_pair(RValue::get(Src),
1359 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001360 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001361 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1362 Callee, CallArgs, MD);
1363 }
1364 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001366 // Emit the increment of the loop counter.
1367 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1368 Counter = Builder.CreateLoad(IndexPtr);
1369 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1370 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001372 // Finally, branch back up to the condition for the next iteration.
1373 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001375 // Emit the fall-through block.
1376 EmitBlock(AfterFor, true);
1377}
1378
Fariborz Jahanianca283612009-08-07 23:51:33 +00001379/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1380/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001381/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001382void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001383 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001384 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001385 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1386 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001387 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1388 /*NullCheckValue=*/false);
1389 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1390 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001391 }
1392 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1393 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001394 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001395 }
Mike Stump1eb44332009-09-09 15:08:12 +00001396
1397 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001398 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001399 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001400 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001401 CallArgList CallArgs;
1402 // Push the this (Dest) ptr.
1403 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1404 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Fariborz Jahanianca283612009-08-07 23:51:33 +00001406 // Push the Src ptr.
1407 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001408 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001409 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001410 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001411 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1412 Callee, CallArgs, BaseCopyCtor);
1413 }
1414}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001415
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001416/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001417/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001418/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001419// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001420void CodeGenFunction::EmitClassCopyAssignment(
1421 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001422 const CXXRecordDecl *ClassDecl,
1423 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001424 QualType Ty) {
1425 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001426 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1427 /*NullCheckValue=*/false);
1428 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1429 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001430 }
1431 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1432 EmitAggregateCopy(Dest, Src, Ty);
1433 return;
1434 }
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001436 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001437 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001438 MD);
1439 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1440 (void)ConstCopyAssignOp;
1441
John McCall183700f2009-09-21 23:43:11 +00001442 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001443 const llvm::Type *LTy =
1444 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001445 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001446 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001448 CallArgList CallArgs;
1449 // Push the this (Dest) ptr.
1450 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1451 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001453 // Push the Src ptr.
1454 CallArgs.push_back(std::make_pair(RValue::get(Src),
1455 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001456 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001457 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001458 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1459 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001460}
1461
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001462/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001463void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001464CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1465 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001466 llvm::Function *Fn,
1467 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001468 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1469 SourceLocation());
1470 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001471 FinishFunction();
1472}
1473
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001474/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001475/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001476/// The implicitly-defined copy constructor for class X performs a memberwise
1477/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001478/// of initialization of bases and members in a user-defined constructor
1479/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001480/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001481/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001482/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001483/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001484/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001485/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001486/// Virtual base class subobjects shall be copied only once by the
1487/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001488
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001489void
1490CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1491 CXXCtorType Type,
1492 llvm::Function *Fn,
1493 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001494 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001495 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001496 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001497 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1498 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001500 FunctionArgList::const_iterator i = Args.begin();
1501 const VarDecl *ThisArg = i->first;
1502 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1503 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1504 const VarDecl *SrcArg = (i+1)->first;
1505 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1506 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001508 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1509 Base != ClassDecl->bases_end(); ++Base) {
1510 // FIXME. copy constrution of virtual base NYI
1511 if (Base->isVirtual())
1512 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001514 CXXRecordDecl *BaseClassDecl
1515 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001516 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1517 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001520 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1521 FieldEnd = ClassDecl->field_end();
1522 Field != FieldEnd; ++Field) {
1523 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001524 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001525 getContext().getAsConstantArrayType(FieldType);
1526 if (Array)
1527 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001529 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1530 CXXRecordDecl *FieldClassDecl
1531 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1532 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1533 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001534 if (Array) {
1535 const llvm::Type *BasePtr = ConvertType(FieldType);
1536 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001537 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001538 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001539 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001540 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1541 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1542 FieldClassDecl, FieldType);
1543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544 else
1545 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001546 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001547 continue;
1548 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001549 // Do a built-in assignment of scalar data members.
1550 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1551 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1552 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1553 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001554 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001555 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001556}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001557
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001558/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001559/// Before the implicitly-declared copy assignment operator for a class is
1560/// implicitly defined, all implicitly- declared copy assignment operators for
1561/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001562/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001563/// The implicitly-defined copy assignment operator for class X performs
1564/// memberwise assignment of its subob- jects. The direct base classes of X are
1565/// assigned first, in the order of their declaration in
1566/// the base-specifier-list, and then the immediate nonstatic data members of X
1567/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001568/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001569/// if the subobject is of class type, the copy assignment operator for the
1570/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001571/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001572///
Mike Stump1eb44332009-09-09 15:08:12 +00001573/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001574/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001575///
Mike Stump1eb44332009-09-09 15:08:12 +00001576/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001577/// used.
1578void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001579 llvm::Function *Fn,
1580 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001581
1582 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1583 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1584 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001585 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001587 FunctionArgList::const_iterator i = Args.begin();
1588 const VarDecl *ThisArg = i->first;
1589 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1590 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1591 const VarDecl *SrcArg = (i+1)->first;
1592 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1593 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001595 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1596 Base != ClassDecl->bases_end(); ++Base) {
1597 // FIXME. copy assignment of virtual base NYI
1598 if (Base->isVirtual())
1599 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001601 CXXRecordDecl *BaseClassDecl
1602 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1603 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1604 Base->getType());
1605 }
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001607 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1608 FieldEnd = ClassDecl->field_end();
1609 Field != FieldEnd; ++Field) {
1610 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001611 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001612 getContext().getAsConstantArrayType(FieldType);
1613 if (Array)
1614 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001616 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1617 CXXRecordDecl *FieldClassDecl
1618 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1619 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1620 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001621 if (Array) {
1622 const llvm::Type *BasePtr = ConvertType(FieldType);
1623 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1624 llvm::Value *DestBaseAddrPtr =
1625 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1626 llvm::Value *SrcBaseAddrPtr =
1627 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1628 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1629 FieldClassDecl, FieldType);
1630 }
1631 else
Mike Stump1eb44332009-09-09 15:08:12 +00001632 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001633 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001634 continue;
1635 }
1636 // Do a built-in assignment of scalar data members.
1637 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1638 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1639 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1640 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001641 }
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001643 // return *this;
1644 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001646 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001647}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001648
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001649/// EmitCtorPrologue - This routine generates necessary code to initialize
1650/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001651/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001652void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1653 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001654 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001655 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001656 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001658 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001659 E = CD->init_end();
1660 B != E; ++B) {
1661 CXXBaseOrMemberInitializer *Member = (*B);
1662 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001663 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001664 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001665 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001666 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001667 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1668 BaseClassDecl,
1669 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001670 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001671 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001672 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001673 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001674 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001675 // non-static data member initilaizers.
1676 FieldDecl *Field = Member->getMember();
1677 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001678 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001679 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001680 if (Array)
1681 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Mike Stumpf1216772009-07-31 18:25:34 +00001683 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001684 LValue LHS;
1685 if (FieldType->isReferenceType()) {
1686 // FIXME: This is really ugly; should be refactored somehow
1687 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1688 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
1689 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(),
1690 QualType::GCNone, FieldType.getAddressSpace());
1691 } else {
1692 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1693 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001694 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001695 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001696 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001697 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001698 if (Array) {
1699 const llvm::Type *BasePtr = ConvertType(FieldType);
1700 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001701 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001702 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001703 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001704 Array, BaseAddrPtr);
1705 }
1706 else
1707 EmitCXXConstructorCall(Member->getConstructor(),
1708 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001709 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001710 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001711 continue;
1712 }
1713 else {
1714 // Initializing an anonymous union data member.
1715 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001716 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001717 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001718 FieldType = anonMember->getType();
1719 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001720 }
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001722 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001723 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001724 RValue RHS;
1725 if (FieldType->isReferenceType())
1726 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1727 /*IsInitializer=*/true);
1728 else
1729 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1730 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001731 }
1732 }
Mike Stumpf1216772009-07-31 18:25:34 +00001733
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001734 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001735 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001736 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001737 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001738 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001739 ClassDecl->bases_begin();
1740 Base != ClassDecl->bases_end(); ++Base) {
1741 // FIXME. copy assignment of virtual base NYI
1742 if (Base->isVirtual())
1743 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001745 CXXRecordDecl *BaseClassDecl
1746 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1747 if (BaseClassDecl->hasTrivialConstructor())
1748 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001749 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001750 BaseClassDecl->getDefaultConstructor(getContext())) {
1751 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001752 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1753 BaseClassDecl,
1754 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001755 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1756 }
1757 }
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001759 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1760 FieldEnd = ClassDecl->field_end();
1761 Field != FieldEnd; ++Field) {
1762 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001763 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001764 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001765 if (Array)
1766 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001767 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1768 continue;
1769 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001770 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001771 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1772 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1773 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001774 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001775 MemberClassDecl->getDefaultConstructor(getContext())) {
1776 LoadOfThis = LoadCXXThis();
1777 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001778 if (Array) {
1779 const llvm::Type *BasePtr = ConvertType(FieldType);
1780 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001781 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001782 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1783 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1784 }
1785 else
Mike Stump1eb44332009-09-09 15:08:12 +00001786 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001787 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001788 }
1789 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001790 }
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Mike Stumpf1216772009-07-31 18:25:34 +00001792 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001793 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001794 if (!LoadOfThis)
1795 LoadOfThis = LoadCXXThis();
1796 llvm::Value *VtableField;
1797 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001798 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001799 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1800 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1801 llvm::Value *vtable = GenerateVtable(ClassDecl);
1802 Builder.CreateStore(vtable, VtableField);
1803 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001804}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001805
1806/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001807/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001808/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001809/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001810void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1811 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001812 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001813 assert(!ClassDecl->getNumVBases() &&
1814 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001815 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001817 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1818 *E = DD->destr_end(); B != E; ++B) {
1819 uintptr_t BaseOrMember = (*B);
1820 if (DD->isMemberToDestroy(BaseOrMember)) {
1821 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1822 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001823 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001824 getContext().getAsConstantArrayType(FieldType);
1825 if (Array)
1826 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001827 const RecordType *RT = FieldType->getAs<RecordType>();
1828 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1829 if (FieldClassDecl->hasTrivialDestructor())
1830 continue;
1831 llvm::Value *LoadOfThis = LoadCXXThis();
1832 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001833 if (Array) {
1834 const llvm::Type *BasePtr = ConvertType(FieldType);
1835 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001836 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001837 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001838 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001839 Array, BaseAddrPtr);
1840 }
1841 else
1842 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1843 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001844 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001845 const RecordType *RT =
1846 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1847 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1848 if (BaseClassDecl->hasTrivialDestructor())
1849 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001850 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1851 ClassDecl, BaseClassDecl,
1852 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001853 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001854 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001855 }
1856 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001857 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1858 return;
1859 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001860 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001861 // reverse order of their construction.
1862 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001864 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1865 FieldEnd = ClassDecl->field_end();
1866 Field != FieldEnd; ++Field) {
1867 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001868 if (getContext().getAsConstantArrayType(FieldType))
1869 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001870 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1871 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1872 if (FieldClassDecl->hasTrivialDestructor())
1873 continue;
1874 DestructedFields.push_back(*Field);
1875 }
1876 }
1877 if (!DestructedFields.empty())
1878 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1879 FieldDecl *Field = DestructedFields[i];
1880 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001881 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001882 getContext().getAsConstantArrayType(FieldType);
1883 if (Array)
1884 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001885 const RecordType *RT = FieldType->getAs<RecordType>();
1886 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1887 llvm::Value *LoadOfThis = LoadCXXThis();
1888 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001889 if (Array) {
1890 const llvm::Type *BasePtr = ConvertType(FieldType);
1891 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001892 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001893 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001894 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001895 Array, BaseAddrPtr);
1896 }
1897 else
1898 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1899 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001900 }
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001902 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1903 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1904 Base != ClassDecl->bases_end(); ++Base) {
1905 // FIXME. copy assignment of virtual base NYI
1906 if (Base->isVirtual())
1907 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001909 CXXRecordDecl *BaseClassDecl
1910 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1911 if (BaseClassDecl->hasTrivialDestructor())
1912 continue;
1913 DestructedBases.push_back(BaseClassDecl);
1914 }
1915 if (DestructedBases.empty())
1916 return;
1917 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1918 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001919 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1920 ClassDecl,BaseClassDecl,
1921 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001922 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1923 Dtor_Complete, V);
1924 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001925}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001926
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001927void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1928 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001929 llvm::Function *Fn,
1930 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001932 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001933 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1934 "SynthesizeDefaultDestructor - destructor has user declaration");
1935 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001937 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1938 SourceLocation());
1939 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001940 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001941}