blob: 715f1b0d0d1929bce2ce399d50c14b663d9a9f35 [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
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000186 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
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
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000197 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
198 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
Anders Carlssone9918d22009-04-08 20:31:57 +0000206 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
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
Anders Carlsson0f294632009-05-27 04:18:27 +0000255 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000256 const llvm::Type *Ty =
257 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000258 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000259 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlsson0f294632009-05-27 04:18:27 +0000261 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anders Carlsson0f294632009-05-27 04:18:27 +0000263 return EmitCXXMemberCall(MD, Callee, This,
264 E->arg_begin() + 1, E->arg_end());
265}
266
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000267llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000268 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000269 "Must be in a C++ member function decl to load 'this'");
270 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
271 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000273 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000274 // ans: See how CodeGenFunction::LoadObjCSelf() uses
275 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000276 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
277}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000278
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000279/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
280/// for-loop to call the default constructor on individual members of the
281/// array. 'Array' is the array type, 'This' is llvm pointer of the start
282/// of the array and 'D' is the default costructor Decl for elements of the
283/// array. It is assumed that all relevant checks have been made by the
284/// caller.
285void
286CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
287 const ArrayType *Array,
288 llvm::Value *This) {
289 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
290 assert(CA && "Do we support VLA for construction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000292 // Create a temporary for the loop index and initialize it with 0.
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000293 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000294 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000295 llvm::Value* zeroConstant =
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000296 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000297 Builder.CreateStore(zeroConstant, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000299 // Start the loop with a block that tests the condition.
300 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
301 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000303 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000305 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000307 // Generate: if (loop-index < number-of-elements fall to the loop body,
308 // otherwise, go to the block after the for-loop.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000309 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000310 llvm::Value * NumElementsPtr =
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000311 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000312 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000313 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000314 "isless");
315 // If the condition is true, execute the body.
316 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000318 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000320 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000321 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000322 Counter = Builder.CreateLoad(IndexPtr);
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000323 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
324 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000326 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000328 // Emit the increment of the loop counter.
329 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
330 Counter = Builder.CreateLoad(IndexPtr);
331 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
332 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000334 // Finally, branch back up to the condition for the next iteration.
335 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000337 // Emit the fall-through block.
338 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000339}
340
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000341/// EmitCXXAggrDestructorCall - calls the default destructor on array
342/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000343void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000344CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
345 const ArrayType *Array,
346 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000347 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
348 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000349 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000350 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000351 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000352 // Create a temporary for the loop index and initialize it with count of
353 // array elements.
354 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
355 "loop.index");
356 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000357 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000358 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
359 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000361 // Start the loop with a block that tests the condition.
362 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
363 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000365 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000367 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000369 // Generate: if (loop-index != 0 fall to the loop body,
370 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000371 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000372 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
373 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
374 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
375 "isne");
376 // If the condition is true, execute the body.
377 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000379 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000381 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
382 // Inside the loop body, emit the constructor call on the array element.
383 Counter = Builder.CreateLoad(IndexPtr);
384 Counter = Builder.CreateSub(Counter, One);
385 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
386 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000388 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000390 // Emit the decrement of the loop counter.
391 Counter = Builder.CreateLoad(IndexPtr);
392 Counter = Builder.CreateSub(Counter, One, "dec");
393 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000395 // Finally, branch back up to the condition for the next iteration.
396 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000398 // Emit the fall-through block.
399 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000400}
401
402void
Mike Stump1eb44332009-09-09 15:08:12 +0000403CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
404 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000405 llvm::Value *This,
406 CallExpr::const_arg_iterator ArgBeg,
407 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000408 if (D->isCopyConstructor(getContext())) {
409 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
410 if (ClassDecl->hasTrivialCopyConstructor()) {
411 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
412 "EmitCXXConstructorCall - user declared copy constructor");
413 const Expr *E = (*ArgBeg);
414 QualType Ty = E->getType();
415 llvm::Value *Src = EmitLValue(E).getAddress();
416 EmitAggregateCopy(This, Src, Ty);
417 return;
418 }
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000421 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
422
423 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000424}
425
Mike Stump1eb44332009-09-09 15:08:12 +0000426void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000427 CXXDtorType Type,
428 llvm::Value *This) {
429 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Anders Carlsson7267c162009-05-29 21:03:38 +0000431 EmitCXXMemberCall(D, Callee, This, 0, 0);
432}
433
Mike Stump1eb44332009-09-09 15:08:12 +0000434void
435CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000436 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000437 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000438
439 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000440 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000441 if (RD->hasTrivialConstructor())
442 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000443
Mike Stump1eb44332009-09-09 15:08:12 +0000444 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000445 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000446 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000447 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000448 EmitAggExpr((*i), Dest, false);
449 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000450 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000451 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000452 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000453 E->arg_begin(), E->arg_end());
454}
455
Anders Carlssona00703d2009-05-31 01:40:14 +0000456llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
Anders Carlssoned4e3672009-05-31 20:21:44 +0000457 if (E->isArray()) {
458 ErrorUnsupported(E, "new[] expression");
Owen Anderson03e20502009-07-30 23:11:26 +0000459 return llvm::UndefValue::get(ConvertType(E->getType()));
Anders Carlssoned4e3672009-05-31 20:21:44 +0000460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Anders Carlssoned4e3672009-05-31 20:21:44 +0000462 QualType AllocType = E->getAllocatedType();
463 FunctionDecl *NewFD = E->getOperatorNew();
464 const FunctionProtoType *NewFTy = NewFD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Anders Carlssoned4e3672009-05-31 20:21:44 +0000466 CallArgList NewArgs;
467
468 // The allocation size is the first argument.
469 QualType SizeTy = getContext().getSizeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000470 llvm::Value *AllocSize =
471 llvm::ConstantInt::get(ConvertType(SizeTy),
Anders Carlssoned4e3672009-05-31 20:21:44 +0000472 getContext().getTypeSize(AllocType) / 8);
473
474 NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Anders Carlssoned4e3672009-05-31 20:21:44 +0000476 // Emit the rest of the arguments.
477 // FIXME: Ideally, this should just use EmitCallArgs.
478 CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin();
479
480 // First, use the types from the function type.
481 // We start at 1 here because the first argument (the allocation size)
482 // has already been emitted.
483 for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) {
484 QualType ArgType = NewFTy->getArgType(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Anders Carlssoned4e3672009-05-31 20:21:44 +0000486 assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
Mike Stump1eb44332009-09-09 15:08:12 +0000487 getTypePtr() ==
488 getContext().getCanonicalType(NewArg->getType()).getTypePtr() &&
Anders Carlssoned4e3672009-05-31 20:21:44 +0000489 "type mismatch in call argument!");
Mike Stump1eb44332009-09-09 15:08:12 +0000490
491 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
Anders Carlssoned4e3672009-05-31 20:21:44 +0000492 ArgType));
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anders Carlssoned4e3672009-05-31 20:21:44 +0000494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
496 // Either we've emitted all the call args, or we have a call to a
Anders Carlssoned4e3672009-05-31 20:21:44 +0000497 // variadic function.
Mike Stump1eb44332009-09-09 15:08:12 +0000498 assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) &&
Anders Carlssoned4e3672009-05-31 20:21:44 +0000499 "Extra arguments in non-variadic function!");
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Anders Carlssoned4e3672009-05-31 20:21:44 +0000501 // If we still have any arguments, emit them using the type of the argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000502 for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end();
Anders Carlssoned4e3672009-05-31 20:21:44 +0000503 NewArg != NewArgEnd; ++NewArg) {
504 QualType ArgType = NewArg->getType();
505 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
506 ArgType));
507 }
508
509 // Emit the call to new.
Mike Stump1eb44332009-09-09 15:08:12 +0000510 RValue RV =
Anders Carlssoned4e3672009-05-31 20:21:44 +0000511 EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs),
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000512 CGM.GetAddrOfFunction(NewFD), NewArgs, NewFD);
Anders Carlssoned4e3672009-05-31 20:21:44 +0000513
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000514 // If an allocation function is declared with an empty exception specification
515 // it returns null to indicate failure to allocate storage. [expr.new]p13.
516 // (We don't need to check for null when there's no new initializer and
517 // we're allocating a POD type).
518 bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() &&
519 !(AllocType->isPODType() && !E->hasInitializer());
Anders Carlssoned4e3672009-05-31 20:21:44 +0000520
Anders Carlssonf1108532009-06-01 00:05:16 +0000521 llvm::BasicBlock *NewNull = 0;
522 llvm::BasicBlock *NewNotNull = 0;
523 llvm::BasicBlock *NewEnd = 0;
524
525 llvm::Value *NewPtr = RV.getScalarVal();
526
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000527 if (NullCheckResult) {
Anders Carlssonf1108532009-06-01 00:05:16 +0000528 NewNull = createBasicBlock("new.null");
529 NewNotNull = createBasicBlock("new.notnull");
530 NewEnd = createBasicBlock("new.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000531
532 llvm::Value *IsNull =
533 Builder.CreateICmpEQ(NewPtr,
Owen Andersonc9c88b42009-07-31 20:28:54 +0000534 llvm::Constant::getNullValue(NewPtr->getType()),
Anders Carlssonf1108532009-06-01 00:05:16 +0000535 "isnull");
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Anders Carlssonf1108532009-06-01 00:05:16 +0000537 Builder.CreateCondBr(IsNull, NewNull, NewNotNull);
538 EmitBlock(NewNotNull);
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Anders Carlssonf1108532009-06-01 00:05:16 +0000541 NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000543 if (AllocType->isPODType()) {
Anders Carlsson215bd202009-06-01 00:26:14 +0000544 if (E->getNumConstructorArgs() > 0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 assert(E->getNumConstructorArgs() == 1 &&
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000546 "Can only have one argument to initializer of POD type.");
547
548 const Expr *Init = E->getConstructorArg(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
550 if (!hasAggregateLLVMType(AllocType))
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000551 Builder.CreateStore(EmitScalarExpr(Init), NewPtr);
Anders Carlsson3923e952009-05-31 21:07:58 +0000552 else if (AllocType->isAnyComplexType())
553 EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified());
Anders Carlsson627a3e52009-05-31 21:12:26 +0000554 else
555 EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified());
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000556 }
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000557 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000558 // Call the constructor.
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000559 CXXConstructorDecl *Ctor = E->getConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000560
561 EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr,
562 E->constructor_arg_begin(),
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000563 E->constructor_arg_end());
Anders Carlssoned4e3672009-05-31 20:21:44 +0000564 }
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000565
Anders Carlssonf1108532009-06-01 00:05:16 +0000566 if (NullCheckResult) {
567 Builder.CreateBr(NewEnd);
568 EmitBlock(NewNull);
569 Builder.CreateBr(NewEnd);
570 EmitBlock(NewEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Anders Carlssonf1108532009-06-01 00:05:16 +0000572 llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType());
573 PHI->reserveOperandSpace(2);
574 PHI->addIncoming(NewPtr, NewNotNull);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000575 PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Anders Carlssonf1108532009-06-01 00:05:16 +0000577 NewPtr = PHI;
578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000580 return NewPtr;
Anders Carlssona00703d2009-05-31 01:40:14 +0000581}
582
Anders Carlsson60e282c2009-08-16 21:13:42 +0000583void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
584 if (E->isArrayForm()) {
585 ErrorUnsupported(E, "delete[] expression");
586 return;
587 };
588
Mike Stump1eb44332009-09-09 15:08:12 +0000589 QualType DeleteTy =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000590 E->getArgument()->getType()->getAs<PointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Anders Carlsson60e282c2009-08-16 21:13:42 +0000592 llvm::Value *Ptr = EmitScalarExpr(E->getArgument());
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Anders Carlsson60e282c2009-08-16 21:13:42 +0000594 // Null check the pointer.
595 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
596 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
597
Mike Stump1eb44332009-09-09 15:08:12 +0000598 llvm::Value *IsNull =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000599 Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
600 "isnull");
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Anders Carlsson60e282c2009-08-16 21:13:42 +0000602 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
603 EmitBlock(DeleteNotNull);
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Anders Carlsson60e282c2009-08-16 21:13:42 +0000605 // Call the destructor if necessary.
606 if (const RecordType *RT = DeleteTy->getAs<RecordType>()) {
607 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
608 if (!RD->hasTrivialDestructor()) {
609 const CXXDestructorDecl *Dtor = RD->getDestructor(getContext());
610 if (Dtor->isVirtual()) {
Anders Carlssondb291042009-09-14 00:16:25 +0000611 const llvm::Type *Ty =
612 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(Dtor),
613 /*isVariadic=*/false);
614
615 llvm::Value *Callee = BuildVirtualCall(Dtor, Ptr, Ty);
616 EmitCXXMemberCall(Dtor, Callee, Ptr, 0, 0);
617 } else
618 EmitCXXDestructorCall(Dtor, Dtor_Complete, Ptr);
Anders Carlsson60e282c2009-08-16 21:13:42 +0000619 }
620 }
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Anders Carlsson60e282c2009-08-16 21:13:42 +0000623 // Call delete.
624 FunctionDecl *DeleteFD = E->getOperatorDelete();
Mike Stump1eb44332009-09-09 15:08:12 +0000625 const FunctionProtoType *DeleteFTy =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000626 DeleteFD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Anders Carlsson60e282c2009-08-16 21:13:42 +0000628 CallArgList DeleteArgs;
629
630 QualType ArgTy = DeleteFTy->getArgType(0);
631 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
632 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Anders Carlsson60e282c2009-08-16 21:13:42 +0000634 // Emit the call to delete.
Mike Stump1eb44332009-09-09 15:08:12 +0000635 EmitCall(CGM.getTypes().getFunctionInfo(DeleteFTy->getResultType(),
Anders Carlsson60e282c2009-08-16 21:13:42 +0000636 DeleteArgs),
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000637 CGM.GetAddrOfFunction(DeleteFD),
Anders Carlsson60e282c2009-08-16 21:13:42 +0000638 DeleteArgs, DeleteFD);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Anders Carlsson60e282c2009-08-16 21:13:42 +0000640 EmitBlock(DeleteEnd);
641}
642
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000643void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000644 EmitGlobal(GlobalDecl(D, Ctor_Complete));
645 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000646}
Anders Carlsson363c1842009-04-16 23:57:24 +0000647
Mike Stump1eb44332009-09-09 15:08:12 +0000648void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000649 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Anders Carlsson27ae5362009-04-17 01:58:57 +0000651 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000653 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Anders Carlsson27ae5362009-04-17 01:58:57 +0000655 SetFunctionDefinitionAttributes(D, Fn);
656 SetLLVMFunctionAttributesForDefinition(D, Fn);
657}
658
Anders Carlsson363c1842009-04-16 23:57:24 +0000659llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000660CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000661 CXXCtorType Type) {
662 const llvm::FunctionType *FTy =
663 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Anders Carlsson363c1842009-04-16 23:57:24 +0000665 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000666 return cast<llvm::Function>(
667 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000668}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000669
Mike Stump1eb44332009-09-09 15:08:12 +0000670const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000671 CXXCtorType Type) {
672 llvm::SmallString<256> Name;
673 llvm::raw_svector_ostream Out(Name);
674 mangleCXXCtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Anders Carlsson27ae5362009-04-17 01:58:57 +0000676 Name += '\0';
677 return UniqueMangledName(Name.begin(), Name.end());
678}
679
680void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000681 EmitCXXDestructor(D, Dtor_Complete);
682 EmitCXXDestructor(D, Dtor_Base);
683}
684
Mike Stump1eb44332009-09-09 15:08:12 +0000685void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000686 CXXDtorType Type) {
687 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000689 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Anders Carlsson27ae5362009-04-17 01:58:57 +0000691 SetFunctionDefinitionAttributes(D, Fn);
692 SetLLVMFunctionAttributesForDefinition(D, Fn);
693}
694
695llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000696CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000697 CXXDtorType Type) {
698 const llvm::FunctionType *FTy =
699 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Anders Carlsson27ae5362009-04-17 01:58:57 +0000701 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000702 return cast<llvm::Function>(
703 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000704}
705
Mike Stump1eb44332009-09-09 15:08:12 +0000706const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000707 CXXDtorType Type) {
708 llvm::SmallString<256> Name;
709 llvm::raw_svector_ostream Out(Name);
710 mangleCXXDtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Anders Carlsson27ae5362009-04-17 01:58:57 +0000712 Name += '\0';
713 return UniqueMangledName(Name.begin(), Name.end());
714}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000715
Mike Stump32f37012009-08-18 21:49:00 +0000716llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000717 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000718 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000719 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000720
721 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000722 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000723
724 llvm::SmallString<256> OutName;
725 llvm::raw_svector_ostream Out(OutName);
726 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000727 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000728 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000729 llvm::GlobalVariable::LinkageTypes linktype;
730 linktype = llvm::GlobalValue::WeakAnyLinkage;
731 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000732 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000733 // FIXME: descriptor
734 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000735 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000736 // FIXME: TS
737 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
738
739 llvm::Constant *C;
740 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
741 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000742 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000743 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000744 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
745 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000746}
747
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000748class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000749public:
750 /// Index_t - Vtable index type.
751 typedef uint64_t Index_t;
752private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000753 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000754 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000755 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000756 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000757 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000758 /// BLayout - Layout for the most derived class that this vtable is being
759 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000760 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000761 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000762 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000763 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000764 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000765 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000766 /// Index - Maps a method decl into a vtable index. Useful for virtual
767 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000768 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000769 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
770 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump97f4d462009-09-18 19:06:35 +0000771 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump6e319f62009-09-11 23:25:56 +0000772 typedef std::pair<Index_t, Index_t> CallOffset;
773 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
Mike Stump77ca8f62009-09-05 07:20:32 +0000774 Thunks_t Thunks;
Mike Stump6e319f62009-09-11 23:25:56 +0000775 typedef llvm::DenseMap<const CXXMethodDecl *,
776 std::pair<CallOffset, CallOffset> > CovariantThunks_t;
777 CovariantThunks_t CovariantThunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000778 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000779 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000780 // FIXME: Linkage should follow vtable
781 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000782 const uint32_t LLVMPointerWidth;
783 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000784public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000785 VtableBuilder(std::vector<llvm::Constant *> &meth,
786 const CXXRecordDecl *c,
787 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000788 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
789 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000790 CGM(cgm), Extern(true),
791 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000792 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
793 }
Mike Stump32f37012009-08-18 21:49:00 +0000794
Mike Stumpf0070db2009-08-26 20:46:33 +0000795 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stump97f4d462009-09-18 19:06:35 +0000796 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
797 { return VBIndex; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000798
Mike Stump15a24e02009-08-28 23:22:54 +0000799 llvm::Constant *wrap(Index_t i) {
800 llvm::Constant *m;
801 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
802 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000803 }
804
Mike Stump15a24e02009-08-28 23:22:54 +0000805 llvm::Constant *wrap(llvm::Constant *m) {
806 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000807 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000808
Mike Stump7fa0d932009-08-20 02:11:48 +0000809 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000810 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump97f4d462009-09-18 19:06:35 +0000811 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Mike Stump7fa0d932009-08-20 02:11:48 +0000812 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000813 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000814 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
815 if (i->isVirtual() && !SeenVBase.count(Base)) {
816 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000817 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000818 llvm::Constant *m = wrap(BaseOffset);
819 m = wrap((0?700:0) + BaseOffset);
Mike Stump97f4d462009-09-18 19:06:35 +0000820 VBIndex[Base] = -(offsets.size()*LLVMPointerWidth/8)
821 - 3*LLVMPointerWidth/8;
Mike Stump7fa0d932009-08-20 02:11:48 +0000822 offsets.push_back(m);
823 }
Mike Stumpb9837442009-08-20 07:22:17 +0000824 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000825 }
826 }
827
Mike Stumpb9871a22009-08-21 01:45:00 +0000828 void StartNewTable() {
829 SeenVBase.clear();
830 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000831
Mike Stump97f4d462009-09-18 19:06:35 +0000832 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
833
834 /// getVbaseOffset - Returns the index into the vtable for the virtual base
835 /// offset for the given (B) virtual base of the derived class D.
836 Index_t getVbaseOffset(QualType qB, QualType qD) {
837 qD = qD->getAs<PointerType>()->getPointeeType();
838 qB = qB->getAs<PointerType>()->getPointeeType();
839 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
840 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
841 if (D != Class)
842 return VBlookup(D, B);
843 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
844 i = VBIndex.find(B);
845 if (i != VBIndex.end())
846 return i->second;
847 // FIXME: temporal botch, is this data here, by the time we need it?
848
849 // FIXME: Locate the containing virtual base first.
850 return 42;
851 }
852
Mike Stump35191b62009-09-01 22:20:28 +0000853 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000854 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000855 typedef CXXMethodDecl::method_iterator meth_iter;
856
Mike Stumpb9871a22009-08-21 01:45:00 +0000857 // FIXME: Don't like the nested loops. For very large inheritance
858 // heirarchies we could have a table on the side with the final overridder
859 // and just replace each instance of an overridden method once. Would be
860 // nice to measure the cost/benefit on real code.
861
Mike Stumpb9871a22009-08-21 01:45:00 +0000862 for (meth_iter mi = MD->begin_overridden_methods(),
863 e = MD->end_overridden_methods();
864 mi != e; ++mi) {
865 const CXXMethodDecl *OMD = *mi;
866 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000867 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000868 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
869
Mike Stumpdec025b2009-09-07 04:27:52 +0000870 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000871 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000872 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000873 if (submethods[i] != om)
874 continue;
Mike Stump6e319f62009-09-11 23:25:56 +0000875 QualType nc_oret = OMD->getType()->getAsFunctionType()->getResultType();
876 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
877 QualType nc_ret = MD->getType()->getAsFunctionType()->getResultType();
878 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
879 CallOffset ReturnOffset = std::make_pair(0, 0);
880 if (oret != ret) {
881 // FIXME: calculate offsets for covariance
Mike Stump97f4d462009-09-18 19:06:35 +0000882 ReturnOffset = std::make_pair(42,getVbaseOffset(oret, ret));
Mike Stump6e319f62009-09-11 23:25:56 +0000883 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000884 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000885 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000886
887 Thunks.erase(OMD);
888 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000889 Index_t &idx = VCall[OMD];
890 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000891 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000892 idx = VCalls.size()+1;
893 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000894 } else {
895 VCallOffset[MD] = VCallOffset[OMD];
896 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000897 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000898 VCall[MD] = idx;
Mike Stump6e319f62009-09-11 23:25:56 +0000899 CallOffset ThisOffset;
900 // FIXME: calculate non-virtual offset
901 ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
902 if (ReturnOffset.first || ReturnOffset.second)
903 CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset);
904 else
905 Thunks[MD] = ThisOffset;
Mike Stump35191b62009-09-01 22:20:28 +0000906 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000907 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000908#if 0
909 // FIXME: finish off
910 int64_t O = VCallOffset[OMD] - Offset/8;
911 if (O) {
912 Thunks[MD] = std::make_pair(O, 0);
913 }
914#endif
915 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000916 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000917 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000918
Mike Stump35191b62009-09-01 22:20:28 +0000919 return false;
920 }
921
Mike Stump98cc7102009-09-05 11:28:33 +0000922 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000923 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
924 i != e; ++i) {
925 const CXXMethodDecl *MD = i->first;
926 Index_t idx = Index[MD];
927 Index_t nv_O = i->second.first;
928 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000929 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000930 }
931 Thunks.clear();
Mike Stump6e319f62009-09-11 23:25:56 +0000932 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
933 e = CovariantThunks.end();
934 i != e; ++i) {
935 const CXXMethodDecl *MD = i->first;
936 Index_t idx = Index[MD];
937 Index_t nv_t = i->second.first.first;
938 Index_t v_t = i->second.first.second;
939 Index_t nv_r = i->second.second.first;
940 Index_t v_r = i->second.second.second;
941 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
942 v_r);
943 }
944 CovariantThunks.clear();
Mike Stump77ca8f62009-09-05 07:20:32 +0000945 }
946
Mike Stumpdec025b2009-09-07 04:27:52 +0000947 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
948 int64_t> > *Path, bool MorallyVirtual) {
949 for (std::vector<std::pair<const CXXRecordDecl *,
950 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000951 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000952 const CXXRecordDecl *RD = i->first;
953 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000954 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
955 ++mi)
956 if (mi->isVirtual()) {
957 const CXXMethodDecl *MD = *mi;
Anders Carlssonc7cba152009-09-12 00:00:29 +0000958 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD));
Mike Stumpdec025b2009-09-07 04:27:52 +0000959 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000960 }
961 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000962 }
963
Mike Stump6d10eb82009-09-05 07:49:12 +0000964 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlssonc7cba152009-09-12 00:00:29 +0000965 llvm::Constant *m = 0;
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000966 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonc7cba152009-09-12 00:00:29 +0000967 m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000968 else
Anders Carlssonc7cba152009-09-12 00:00:29 +0000969 m = wrap(CGM.GetAddrOfFunction(MD));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000970
Mike Stump77ca8f62009-09-05 07:20:32 +0000971 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000972 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000973 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Mike Stumpb9871a22009-08-21 01:45:00 +0000975 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000976 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000977 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000978 if (MorallyVirtual) {
979 VCallOffset[MD] = Offset/8;
980 Index_t &idx = VCall[MD];
981 // Allocate the first one, after that, we reuse the previous one.
982 if (idx == 0) {
983 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000984 VCalls.push_back(0);
985 }
986 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000987 }
988
Mike Stump6d10eb82009-09-05 07:49:12 +0000989 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
990 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000991 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
992 ++mi)
993 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +0000994 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +0000995 }
Mike Stump65defe32009-08-18 21:03:28 +0000996
Mike Stump77ca8f62009-09-05 07:20:32 +0000997 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
998 const CXXRecordDecl *PrimaryBase,
999 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1000 int64_t Offset) {
1001 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1002 e = RD->bases_end(); i != e; ++i) {
1003 if (i->isVirtual())
1004 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001005 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +00001006 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1007 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
1008 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
1009 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +00001010 std::vector<std::pair<const CXXRecordDecl *,
1011 int64_t> > S;
1012 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +00001013 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +00001014 }
1015 }
1016 }
1017
Mike Stump6d10eb82009-09-05 07:49:12 +00001018 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
1019 const ASTRecordLayout &Layout,
1020 const CXXRecordDecl *PrimaryBase,
1021 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1022 int64_t Offset, bool ForVirtualBase) {
1023 StartNewTable();
1024 extra = 0;
1025 // FIXME: Cleanup.
1026 if (!ForVirtualBase) {
1027 // then virtual base offsets...
1028 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1029 e = offsets.rend(); i != e; ++i)
1030 methods.push_back(*i);
1031 }
1032
1033 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +00001034 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
1035 e=VCalls.rend();
1036 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +00001037 methods.push_back(wrap((0?600:0) + *i));
1038 VCalls.clear();
1039
1040 if (ForVirtualBase) {
1041 // then virtual base offsets...
1042 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1043 e = offsets.rend(); i != e; ++i)
1044 methods.push_back(*i);
1045 }
1046
1047 methods.push_back(wrap(-(Offset/8)));
1048 methods.push_back(rtti);
1049 Index_t AddressPoint = methods.size();
1050
Mike Stump98cc7102009-09-05 11:28:33 +00001051 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +00001052 methods.insert(methods.end(), submethods.begin(), submethods.end());
1053 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +00001054
1055 // and then the non-virtual bases.
1056 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1057 MorallyVirtual, Offset);
1058 return AddressPoint;
1059 }
1060
Mike Stump078d7782009-09-05 08:40:18 +00001061 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +00001062 if (!RD->isDynamicClass())
1063 return;
1064
1065 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001066 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +00001067 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1068
Mike Stump9bbe9622009-09-05 08:37:03 +00001069 // vtables are composed from the chain of primaries.
1070 if (PrimaryBase) {
1071 if (PrimaryBaseWasVirtual)
1072 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001073 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +00001074 }
1075
1076 // And add the virtuals for the class to the primary vtable.
1077 AddMethods(RD, MorallyVirtual, Offset);
1078 }
1079
Mike Stumpe45c90f2009-09-05 09:10:58 +00001080 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +00001081 bool MorallyVirtual = false, int64_t Offset = 0,
1082 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +00001083 std::vector<std::pair<const CXXRecordDecl *,
1084 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +00001085 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +00001086 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +00001087
1088 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001089 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +00001090 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1091
Mike Stump15a24e02009-08-28 23:22:54 +00001092 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +00001093 extra = 0;
1094 GenerateVBaseOffsets(offsets, RD, Offset);
1095 if (ForVirtualBase)
1096 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +00001097
1098 // vtables are composed from the chain of primaries.
1099 if (PrimaryBase) {
1100 if (PrimaryBaseWasVirtual)
1101 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001102 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +00001103 }
1104
Mike Stump15a24e02009-08-28 23:22:54 +00001105 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +00001106 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +00001107
Mike Stump98cc7102009-09-05 11:28:33 +00001108 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +00001109 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +00001110
Mike Stump6d10eb82009-09-05 07:49:12 +00001111 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1112 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +00001113 }
1114
Mike Stump98cc7102009-09-05 11:28:33 +00001115 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +00001116 int64_t Offset = 0,
1117 std::vector<std::pair<const CXXRecordDecl *,
1118 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +00001119 bool alloc = false;
1120 if (Path == 0) {
1121 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +00001122 Path = new std::vector<std::pair<const CXXRecordDecl *,
1123 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +00001124 }
1125 // FIXME: We also need to override using all paths to a virtual base,
1126 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +00001127 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +00001128 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1129 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00001130 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +00001131 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1132 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1133 // Mark it so we don't output it twice.
1134 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +00001135 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +00001136 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +00001137 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +00001138 }
Mike Stumpdec025b2009-09-07 04:27:52 +00001139 int64_t BaseOffset = Offset;
1140 if (i->isVirtual())
1141 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +00001142 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +00001143 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +00001144 }
Mike Stump98cc7102009-09-05 11:28:33 +00001145 Path->pop_back();
1146 if (alloc)
1147 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +00001148 }
Mike Stump109b13d2009-08-18 21:30:21 +00001149};
Mike Stump8a12b562009-08-06 15:50:11 +00001150
Mike Stumpf0070db2009-08-26 20:46:33 +00001151class VtableInfo {
1152public:
1153 typedef VtableBuilder::Index_t Index_t;
1154private:
1155 CodeGenModule &CGM; // Per-module state.
1156 /// Index_t - Vtable index type.
1157 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
1158 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
1159 // FIXME: Move to Context.
1160 static MapTy IndexFor;
Mike Stump97f4d462009-09-18 19:06:35 +00001161
1162 typedef llvm::DenseMap<const CXXRecordDecl *, Index_t> VBElTy;
1163 typedef llvm::DenseMap<const CXXRecordDecl *, VBElTy *> VBMapTy;
1164 // FIXME: Move to Context.
1165 static VBMapTy VBIndexFor;
Mike Stumpf0070db2009-08-26 20:46:33 +00001166public:
1167 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
Mike Stump97f4d462009-09-18 19:06:35 +00001168 void RegisterIndex(const CXXRecordDecl *RD, const ElTy &e) {
Mike Stumpf0070db2009-08-26 20:46:33 +00001169 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
1170 // We own a copy of this, it will go away shortly.
Mike Stumpf0070db2009-08-26 20:46:33 +00001171 IndexFor[RD] = new ElTy (e);
1172 }
Mike Stump97f4d462009-09-18 19:06:35 +00001173 void RegisterVBIndex(const CXXRecordDecl *RD, const VBElTy &e) {
1174 assert(VBIndexFor.find(RD) == VBIndexFor.end() && "Don't compute vtbl twice");
1175 // We own a copy of this, it will go away shortly.
1176 VBIndexFor[RD] = new VBElTy (e);
1177 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001178 Index_t lookup(const CXXMethodDecl *MD) {
1179 const CXXRecordDecl *RD = MD->getParent();
1180 MapTy::iterator I = IndexFor.find(RD);
1181 if (I == IndexFor.end()) {
1182 std::vector<llvm::Constant *> methods;
Mike Stump97f4d462009-09-18 19:06:35 +00001183 // FIXME: This seems expensive. Can we do a partial job to get
1184 // just this data.
Mike Stumpf0070db2009-08-26 20:46:33 +00001185 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001186 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001187 b.GenerateVtableForVBases(RD);
Mike Stump97f4d462009-09-18 19:06:35 +00001188 RegisterIndex(RD, b.getIndex());
Mike Stumpf0070db2009-08-26 20:46:33 +00001189 I = IndexFor.find(RD);
1190 }
1191 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1192 return (*I->second)[MD];
1193 }
Mike Stump97f4d462009-09-18 19:06:35 +00001194 Index_t VBlookup(const CXXRecordDecl *RD, const CXXRecordDecl *BD) {
1195 VBMapTy::iterator I = VBIndexFor.find(RD);
1196 if (I == VBIndexFor.end()) {
1197 std::vector<llvm::Constant *> methods;
1198 // FIXME: This seems expensive. Can we do a partial job to get
1199 // just this data.
1200 VtableBuilder b(methods, RD, CGM);
1201 b.GenerateVtableForBase(RD);
1202 b.GenerateVtableForVBases(RD);
1203 RegisterVBIndex(RD, b.getVBIndex());
1204 I = VBIndexFor.find(RD);
1205 }
1206 assert(I->second->find(BD)!=I->second->end() && "Can't find vtable index");
1207 return (*I->second)[BD];
1208 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001209};
1210
Mike Stump97f4d462009-09-18 19:06:35 +00001211// FIXME: move to Context
1212static VtableInfo *vtableinfo;
1213
1214VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
1215 CXXRecordDecl *B) {
1216 if (vtableinfo == 0)
1217 vtableinfo = new VtableInfo(CGM);
1218
1219 return vtableinfo->VBlookup(D, B);
1220}
1221
1222
Mike Stumpf0070db2009-08-26 20:46:33 +00001223// FIXME: Move to Context.
1224VtableInfo::MapTy VtableInfo::IndexFor;
1225
Mike Stump97f4d462009-09-18 19:06:35 +00001226// FIXME: Move to Context.
1227VtableInfo::VBMapTy VtableInfo::VBIndexFor;
1228
Mike Stumpf1216772009-07-31 18:25:34 +00001229llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001230 llvm::SmallString<256> OutName;
1231 llvm::raw_svector_ostream Out(OutName);
1232 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001233 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001234 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001235 llvm::GlobalVariable::LinkageTypes linktype;
1236 linktype = llvm::GlobalValue::WeakAnyLinkage;
1237 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001238 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001239 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001240
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001241 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001242
Mike Stump276b9f12009-08-16 01:46:26 +00001243 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001244 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001245
Mike Stump276b9f12009-08-16 01:46:26 +00001246 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001247 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001248
Mike Stump82b56962009-07-31 21:43:43 +00001249 llvm::Constant *C;
1250 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1251 C = llvm::ConstantArray::get(type, methods);
1252 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001253 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001254 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001255 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001256 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001257 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001258 return vtable;
1259}
1260
Mike Stumped032eb2009-09-04 18:27:16 +00001261llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1262 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001263 bool Extern, int64_t nv,
1264 int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001265 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1266
1267 FunctionArgList Args;
1268 ImplicitParamDecl *ThisDecl =
1269 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1270 MD->getThisType(getContext()));
1271 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1272 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1273 e = MD->param_end();
1274 i != e; ++i) {
1275 ParmVarDecl *D = *i;
1276 Args.push_back(std::make_pair(D, D->getType()));
1277 }
1278 IdentifierInfo *II
1279 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1280 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1281 getContext().getTranslationUnitDecl(),
1282 SourceLocation(), II, R, 0,
1283 Extern
1284 ? FunctionDecl::Extern
1285 : FunctionDecl::Static,
1286 false, true);
1287 StartFunction(FD, R, Fn, Args, SourceLocation());
1288 // FIXME: generate body
1289 FinishFunction();
1290 return Fn;
1291}
1292
Mike Stump6e319f62009-09-11 23:25:56 +00001293llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1294 const CXXMethodDecl *MD,
1295 bool Extern,
1296 int64_t nv_t,
1297 int64_t v_t,
1298 int64_t nv_r,
1299 int64_t v_r) {
1300 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1301
1302 FunctionArgList Args;
1303 ImplicitParamDecl *ThisDecl =
1304 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1305 MD->getThisType(getContext()));
1306 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1307 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1308 e = MD->param_end();
1309 i != e; ++i) {
1310 ParmVarDecl *D = *i;
1311 Args.push_back(std::make_pair(D, D->getType()));
1312 }
1313 IdentifierInfo *II
1314 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1315 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1316 getContext().getTranslationUnitDecl(),
1317 SourceLocation(), II, R, 0,
1318 Extern
1319 ? FunctionDecl::Extern
1320 : FunctionDecl::Static,
1321 false, true);
1322 StartFunction(FD, R, Fn, Args, SourceLocation());
1323 // FIXME: generate body
1324 FinishFunction();
1325 return Fn;
1326}
1327
Mike Stump77ca8f62009-09-05 07:20:32 +00001328llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1329 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001330 llvm::SmallString<256> OutName;
1331 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001332 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001333 llvm::GlobalVariable::LinkageTypes linktype;
1334 linktype = llvm::GlobalValue::WeakAnyLinkage;
1335 if (!Extern)
1336 linktype = llvm::GlobalValue::InternalLinkage;
1337 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1338 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1339 const llvm::FunctionType *FTy =
1340 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1341 FPT->isVariadic());
1342
1343 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1344 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001345 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001346 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1347 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1348 return m;
1349}
1350
Mike Stump6e319f62009-09-11 23:25:56 +00001351llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1352 bool Extern, int64_t nv_t,
1353 int64_t v_t, int64_t nv_r,
1354 int64_t v_r) {
1355 llvm::SmallString<256> OutName;
1356 llvm::raw_svector_ostream Out(OutName);
1357 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1358 llvm::GlobalVariable::LinkageTypes linktype;
1359 linktype = llvm::GlobalValue::WeakAnyLinkage;
1360 if (!Extern)
1361 linktype = llvm::GlobalValue::InternalLinkage;
1362 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1363 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1364 const llvm::FunctionType *FTy =
1365 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1366 FPT->isVariadic());
1367
1368 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1369 &getModule());
1370 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1371 v_r);
1372 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1373 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1374 return m;
1375}
1376
Mike Stumpf0070db2009-08-26 20:46:33 +00001377llvm::Value *
1378CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1379 const llvm::Type *Ty) {
1380 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Mike Stumpf0070db2009-08-26 20:46:33 +00001382 // FIXME: move to Context
1383 if (vtableinfo == 0)
1384 vtableinfo = new VtableInfo(CGM);
1385
1386 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1387
1388 Ty = llvm::PointerType::get(Ty, 0);
1389 Ty = llvm::PointerType::get(Ty, 0);
1390 Ty = llvm::PointerType::get(Ty, 0);
1391 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1392 vtbl = Builder.CreateLoad(vtbl);
1393 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1394 Idx, "vfn");
1395 vfn = Builder.CreateLoad(vfn);
1396 return vfn;
1397}
1398
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001399/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1400/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1401/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001402// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001403void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001404 llvm::Value *Src,
1405 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001406 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001407 QualType Ty) {
1408 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1409 assert(CA && "VLA cannot be copied over");
1410 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001412 // Create a temporary for the loop index and initialize it with 0.
1413 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1414 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001415 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001416 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1417 Builder.CreateStore(zeroConstant, IndexPtr, false);
1418 // Start the loop with a block that tests the condition.
1419 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1420 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001422 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001424 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1425 // Generate: if (loop-index < number-of-elements fall to the loop body,
1426 // otherwise, go to the block after the for-loop.
1427 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001428 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001429 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1430 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001431 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001432 "isless");
1433 // If the condition is true, execute the body.
1434 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001436 EmitBlock(ForBody);
1437 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1438 // Inside the loop body, emit the constructor call on the array element.
1439 Counter = Builder.CreateLoad(IndexPtr);
1440 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1441 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1442 if (BitwiseCopy)
1443 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001444 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001445 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001446 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001447 Ctor_Complete);
1448 CallArgList CallArgs;
1449 // Push the this (Dest) ptr.
1450 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1451 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001453 // Push the Src ptr.
1454 CallArgs.push_back(std::make_pair(RValue::get(Src),
1455 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001456 QualType ResultType =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001457 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1458 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1459 Callee, CallArgs, BaseCopyCtor);
1460 }
1461 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001463 // Emit the increment of the loop counter.
1464 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1465 Counter = Builder.CreateLoad(IndexPtr);
1466 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1467 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001469 // Finally, branch back up to the condition for the next iteration.
1470 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001472 // Emit the fall-through block.
1473 EmitBlock(AfterFor, true);
1474}
1475
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001476/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001477/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001478/// bitwise assignment or via a copy assignment operator function call.
1479/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001480void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001481 llvm::Value *Src,
1482 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001483 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001484 QualType Ty) {
1485 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1486 assert(CA && "VLA cannot be asssigned");
1487 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001489 // Create a temporary for the loop index and initialize it with 0.
1490 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1491 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001492 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001493 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1494 Builder.CreateStore(zeroConstant, IndexPtr, false);
1495 // Start the loop with a block that tests the condition.
1496 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1497 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001499 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001501 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1502 // Generate: if (loop-index < number-of-elements fall to the loop body,
1503 // otherwise, go to the block after the for-loop.
1504 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001505 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001506 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1507 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001508 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001509 "isless");
1510 // If the condition is true, execute the body.
1511 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001513 EmitBlock(ForBody);
1514 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1515 // Inside the loop body, emit the assignment operator call on array element.
1516 Counter = Builder.CreateLoad(IndexPtr);
1517 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1518 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1519 const CXXMethodDecl *MD = 0;
1520 if (BitwiseAssign)
1521 EmitAggregateCopy(Dest, Src, Ty);
1522 else {
1523 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1524 MD);
1525 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1526 (void)hasCopyAssign;
1527 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1528 const llvm::Type *LTy =
1529 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1530 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001531 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001533 CallArgList CallArgs;
1534 // Push the this (Dest) ptr.
1535 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1536 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001538 // Push the Src ptr.
1539 CallArgs.push_back(std::make_pair(RValue::get(Src),
1540 MD->getParamDecl(0)->getType()));
Mike Stumped032eb2009-09-04 18:27:16 +00001541 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001542 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1543 Callee, CallArgs, MD);
1544 }
1545 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001547 // Emit the increment of the loop counter.
1548 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1549 Counter = Builder.CreateLoad(IndexPtr);
1550 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1551 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001553 // Finally, branch back up to the condition for the next iteration.
1554 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001556 // Emit the fall-through block.
1557 EmitBlock(AfterFor, true);
1558}
1559
Fariborz Jahanianca283612009-08-07 23:51:33 +00001560/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1561/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001562/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001563void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001564 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001565 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001566 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1567 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001568 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1569 /*NullCheckValue=*/false);
1570 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1571 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001572 }
1573 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1574 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001575 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
1578 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001579 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001580 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001581 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001582 CallArgList CallArgs;
1583 // Push the this (Dest) ptr.
1584 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1585 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Fariborz Jahanianca283612009-08-07 23:51:33 +00001587 // Push the Src ptr.
1588 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001589 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001590 QualType ResultType =
Fariborz Jahanianca283612009-08-07 23:51:33 +00001591 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1592 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1593 Callee, CallArgs, BaseCopyCtor);
1594 }
1595}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001596
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001597/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001598/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001599/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001600// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001601void CodeGenFunction::EmitClassCopyAssignment(
1602 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001603 const CXXRecordDecl *ClassDecl,
1604 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001605 QualType Ty) {
1606 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001607 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1608 /*NullCheckValue=*/false);
1609 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1610 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001611 }
1612 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1613 EmitAggregateCopy(Dest, Src, Ty);
1614 return;
1615 }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001617 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001618 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001619 MD);
1620 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1621 (void)ConstCopyAssignOp;
1622
1623 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +00001624 const llvm::Type *LTy =
1625 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001626 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001627 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001629 CallArgList CallArgs;
1630 // Push the this (Dest) ptr.
1631 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1632 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001634 // Push the Src ptr.
1635 CallArgs.push_back(std::make_pair(RValue::get(Src),
1636 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001637 QualType ResultType =
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001638 MD->getType()->getAsFunctionType()->getResultType();
1639 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1640 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001641}
1642
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001643/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001644void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001645CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1646 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001647 llvm::Function *Fn,
1648 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001649 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1650 SourceLocation());
1651 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001652 FinishFunction();
1653}
1654
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001655/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001656/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001657/// The implicitly-defined copy constructor for class X performs a memberwise
1658/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001659/// of initialization of bases and members in a user-defined constructor
1660/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001661/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001662/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001663/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001664/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001665/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001666/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001667/// Virtual base class subobjects shall be copied only once by the
1668/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001669
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001670void
1671CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1672 CXXCtorType Type,
1673 llvm::Function *Fn,
1674 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001675 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001676 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001677 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001678 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1679 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001681 FunctionArgList::const_iterator i = Args.begin();
1682 const VarDecl *ThisArg = i->first;
1683 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1684 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1685 const VarDecl *SrcArg = (i+1)->first;
1686 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1687 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001689 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1690 Base != ClassDecl->bases_end(); ++Base) {
1691 // FIXME. copy constrution of virtual base NYI
1692 if (Base->isVirtual())
1693 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001695 CXXRecordDecl *BaseClassDecl
1696 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001697 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1698 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001699 }
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001701 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1702 FieldEnd = ClassDecl->field_end();
1703 Field != FieldEnd; ++Field) {
1704 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001705 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001706 getContext().getAsConstantArrayType(FieldType);
1707 if (Array)
1708 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001710 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1711 CXXRecordDecl *FieldClassDecl
1712 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1713 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1714 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001715 if (Array) {
1716 const llvm::Type *BasePtr = ConvertType(FieldType);
1717 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001718 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001719 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001720 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001721 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1722 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1723 FieldClassDecl, FieldType);
1724 }
Mike Stump1eb44332009-09-09 15:08:12 +00001725 else
1726 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001727 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001728 continue;
1729 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001730 // Do a built-in assignment of scalar data members.
1731 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1732 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1733 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1734 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001735 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001736 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001737}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001738
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001739/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001740/// Before the implicitly-declared copy assignment operator for a class is
1741/// implicitly defined, all implicitly- declared copy assignment operators for
1742/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001743/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001744/// The implicitly-defined copy assignment operator for class X performs
1745/// memberwise assignment of its subob- jects. The direct base classes of X are
1746/// assigned first, in the order of their declaration in
1747/// the base-specifier-list, and then the immediate nonstatic data members of X
1748/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001749/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001750/// if the subobject is of class type, the copy assignment operator for the
1751/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001752/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001753///
Mike Stump1eb44332009-09-09 15:08:12 +00001754/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001755/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001756///
Mike Stump1eb44332009-09-09 15:08:12 +00001757/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001758/// used.
1759void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001760 llvm::Function *Fn,
1761 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001762
1763 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1764 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1765 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001766 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001768 FunctionArgList::const_iterator i = Args.begin();
1769 const VarDecl *ThisArg = i->first;
1770 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1771 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1772 const VarDecl *SrcArg = (i+1)->first;
1773 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1774 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001776 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1777 Base != ClassDecl->bases_end(); ++Base) {
1778 // FIXME. copy assignment of virtual base NYI
1779 if (Base->isVirtual())
1780 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001782 CXXRecordDecl *BaseClassDecl
1783 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1784 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1785 Base->getType());
1786 }
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001788 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1789 FieldEnd = ClassDecl->field_end();
1790 Field != FieldEnd; ++Field) {
1791 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001792 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001793 getContext().getAsConstantArrayType(FieldType);
1794 if (Array)
1795 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001797 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1798 CXXRecordDecl *FieldClassDecl
1799 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1800 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1801 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001802 if (Array) {
1803 const llvm::Type *BasePtr = ConvertType(FieldType);
1804 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1805 llvm::Value *DestBaseAddrPtr =
1806 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1807 llvm::Value *SrcBaseAddrPtr =
1808 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1809 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1810 FieldClassDecl, FieldType);
1811 }
1812 else
Mike Stump1eb44332009-09-09 15:08:12 +00001813 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001814 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001815 continue;
1816 }
1817 // Do a built-in assignment of scalar data members.
1818 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1819 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1820 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1821 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001822 }
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001824 // return *this;
1825 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001827 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001828}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001829
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001830/// EmitCtorPrologue - This routine generates necessary code to initialize
1831/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001832/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001833void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1834 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001835 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001836 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001837 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001839 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001840 E = CD->init_end();
1841 B != E; ++B) {
1842 CXXBaseOrMemberInitializer *Member = (*B);
1843 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001844 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001845 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001846 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001847 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001848 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1849 BaseClassDecl,
1850 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001851 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001852 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001853 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001854 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001855 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001856 // non-static data member initilaizers.
1857 FieldDecl *Field = Member->getMember();
1858 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001859 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001860 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001861 if (Array)
1862 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Mike Stumpf1216772009-07-31 18:25:34 +00001864 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001865 LValue LHS;
1866 if (FieldType->isReferenceType()) {
1867 // FIXME: This is really ugly; should be refactored somehow
1868 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1869 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
1870 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(),
1871 QualType::GCNone, FieldType.getAddressSpace());
1872 } else {
1873 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1874 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001875 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001876 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001877 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001878 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001879 if (Array) {
1880 const llvm::Type *BasePtr = ConvertType(FieldType);
1881 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001882 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001883 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001884 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001885 Array, BaseAddrPtr);
1886 }
1887 else
1888 EmitCXXConstructorCall(Member->getConstructor(),
1889 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001890 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001891 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001892 continue;
1893 }
1894 else {
1895 // Initializing an anonymous union data member.
1896 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001897 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001898 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001899 FieldType = anonMember->getType();
1900 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001901 }
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001903 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001904 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001905 RValue RHS;
1906 if (FieldType->isReferenceType())
1907 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1908 /*IsInitializer=*/true);
1909 else
1910 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1911 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001912 }
1913 }
Mike Stumpf1216772009-07-31 18:25:34 +00001914
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001915 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001916 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001917 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001918 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001919 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001920 ClassDecl->bases_begin();
1921 Base != ClassDecl->bases_end(); ++Base) {
1922 // FIXME. copy assignment of virtual base NYI
1923 if (Base->isVirtual())
1924 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001926 CXXRecordDecl *BaseClassDecl
1927 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1928 if (BaseClassDecl->hasTrivialConstructor())
1929 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001930 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001931 BaseClassDecl->getDefaultConstructor(getContext())) {
1932 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001933 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1934 BaseClassDecl,
1935 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001936 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1937 }
1938 }
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001940 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1941 FieldEnd = ClassDecl->field_end();
1942 Field != FieldEnd; ++Field) {
1943 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001944 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001945 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001946 if (Array)
1947 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001948 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1949 continue;
1950 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001951 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001952 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1953 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1954 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001955 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001956 MemberClassDecl->getDefaultConstructor(getContext())) {
1957 LoadOfThis = LoadCXXThis();
1958 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001959 if (Array) {
1960 const llvm::Type *BasePtr = ConvertType(FieldType);
1961 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001962 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001963 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1964 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1965 }
1966 else
Mike Stump1eb44332009-09-09 15:08:12 +00001967 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001968 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001969 }
1970 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001971 }
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Mike Stumpf1216772009-07-31 18:25:34 +00001973 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001974 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001975 if (!LoadOfThis)
1976 LoadOfThis = LoadCXXThis();
1977 llvm::Value *VtableField;
1978 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001979 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001980 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1981 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1982 llvm::Value *vtable = GenerateVtable(ClassDecl);
1983 Builder.CreateStore(vtable, VtableField);
1984 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001985}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001986
1987/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001988/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001989/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001990/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001991void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1992 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001993 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001994 assert(!ClassDecl->getNumVBases() &&
1995 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001996 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001998 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1999 *E = DD->destr_end(); B != E; ++B) {
2000 uintptr_t BaseOrMember = (*B);
2001 if (DD->isMemberToDestroy(BaseOrMember)) {
2002 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
2003 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002004 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002005 getContext().getAsConstantArrayType(FieldType);
2006 if (Array)
2007 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002008 const RecordType *RT = FieldType->getAs<RecordType>();
2009 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2010 if (FieldClassDecl->hasTrivialDestructor())
2011 continue;
2012 llvm::Value *LoadOfThis = LoadCXXThis();
2013 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002014 if (Array) {
2015 const llvm::Type *BasePtr = ConvertType(FieldType);
2016 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002017 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002018 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002019 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002020 Array, BaseAddrPtr);
2021 }
2022 else
2023 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
2024 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00002025 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002026 const RecordType *RT =
2027 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
2028 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2029 if (BaseClassDecl->hasTrivialDestructor())
2030 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00002031 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
2032 ClassDecl, BaseClassDecl,
2033 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002034 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00002035 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002036 }
2037 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002038 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
2039 return;
2040 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00002041 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002042 // reverse order of their construction.
2043 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002045 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
2046 FieldEnd = ClassDecl->field_end();
2047 Field != FieldEnd; ++Field) {
2048 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002049 if (getContext().getAsConstantArrayType(FieldType))
2050 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002051 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
2052 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2053 if (FieldClassDecl->hasTrivialDestructor())
2054 continue;
2055 DestructedFields.push_back(*Field);
2056 }
2057 }
2058 if (!DestructedFields.empty())
2059 for (int i = DestructedFields.size() -1; i >= 0; --i) {
2060 FieldDecl *Field = DestructedFields[i];
2061 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002062 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002063 getContext().getAsConstantArrayType(FieldType);
2064 if (Array)
2065 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002066 const RecordType *RT = FieldType->getAs<RecordType>();
2067 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2068 llvm::Value *LoadOfThis = LoadCXXThis();
2069 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002070 if (Array) {
2071 const llvm::Type *BasePtr = ConvertType(FieldType);
2072 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002073 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002074 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002075 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002076 Array, BaseAddrPtr);
2077 }
2078 else
2079 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
2080 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002081 }
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002083 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
2084 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
2085 Base != ClassDecl->bases_end(); ++Base) {
2086 // FIXME. copy assignment of virtual base NYI
2087 if (Base->isVirtual())
2088 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002090 CXXRecordDecl *BaseClassDecl
2091 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2092 if (BaseClassDecl->hasTrivialDestructor())
2093 continue;
2094 DestructedBases.push_back(BaseClassDecl);
2095 }
2096 if (DestructedBases.empty())
2097 return;
2098 for (int i = DestructedBases.size() -1; i >= 0; --i) {
2099 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00002100 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
2101 ClassDecl,BaseClassDecl,
2102 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002103 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2104 Dtor_Complete, V);
2105 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002106}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002107
Anders Carlssonde1d26b2009-09-14 05:32:02 +00002108void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
2109 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002110 llvm::Function *Fn,
2111 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00002112
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002113 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002114 assert(!ClassDecl->hasUserDeclaredDestructor() &&
2115 "SynthesizeDefaultDestructor - destructor has user declaration");
2116 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Anders Carlssonde1d26b2009-09-14 05:32:02 +00002118 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
2119 SourceLocation());
2120 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002121 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00002122}