blob: 4932692ffddb02371656c0840267661a3f0a1282 [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()) {
611 ErrorUnsupported(E, "delete expression with virtual destructor");
612 return;
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anders Carlsson60e282c2009-08-16 21:13:42 +0000615 EmitCXXDestructorCall(Dtor, Dtor_Complete, Ptr);
616 }
617 }
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Anders Carlsson60e282c2009-08-16 21:13:42 +0000620 // Call delete.
621 FunctionDecl *DeleteFD = E->getOperatorDelete();
Mike Stump1eb44332009-09-09 15:08:12 +0000622 const FunctionProtoType *DeleteFTy =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000623 DeleteFD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Anders Carlsson60e282c2009-08-16 21:13:42 +0000625 CallArgList DeleteArgs;
626
627 QualType ArgTy = DeleteFTy->getArgType(0);
628 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
629 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Anders Carlsson60e282c2009-08-16 21:13:42 +0000631 // Emit the call to delete.
Mike Stump1eb44332009-09-09 15:08:12 +0000632 EmitCall(CGM.getTypes().getFunctionInfo(DeleteFTy->getResultType(),
Anders Carlsson60e282c2009-08-16 21:13:42 +0000633 DeleteArgs),
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000634 CGM.GetAddrOfFunction(DeleteFD),
Anders Carlsson60e282c2009-08-16 21:13:42 +0000635 DeleteArgs, DeleteFD);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlsson60e282c2009-08-16 21:13:42 +0000637 EmitBlock(DeleteEnd);
638}
639
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000640void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000641 EmitGlobal(GlobalDecl(D, Ctor_Complete));
642 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000643}
Anders Carlsson363c1842009-04-16 23:57:24 +0000644
Mike Stump1eb44332009-09-09 15:08:12 +0000645void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000646 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Anders Carlsson27ae5362009-04-17 01:58:57 +0000648 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000650 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Anders Carlsson27ae5362009-04-17 01:58:57 +0000652 SetFunctionDefinitionAttributes(D, Fn);
653 SetLLVMFunctionAttributesForDefinition(D, Fn);
654}
655
Anders Carlsson363c1842009-04-16 23:57:24 +0000656llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000657CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000658 CXXCtorType Type) {
659 const llvm::FunctionType *FTy =
660 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anders Carlsson363c1842009-04-16 23:57:24 +0000662 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000663 return cast<llvm::Function>(
664 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000665}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000666
Mike Stump1eb44332009-09-09 15:08:12 +0000667const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000668 CXXCtorType Type) {
669 llvm::SmallString<256> Name;
670 llvm::raw_svector_ostream Out(Name);
671 mangleCXXCtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Anders Carlsson27ae5362009-04-17 01:58:57 +0000673 Name += '\0';
674 return UniqueMangledName(Name.begin(), Name.end());
675}
676
677void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000678 EmitCXXDestructor(D, Dtor_Complete);
679 EmitCXXDestructor(D, Dtor_Base);
680}
681
Mike Stump1eb44332009-09-09 15:08:12 +0000682void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000683 CXXDtorType Type) {
684 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000686 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Anders Carlsson27ae5362009-04-17 01:58:57 +0000688 SetFunctionDefinitionAttributes(D, Fn);
689 SetLLVMFunctionAttributesForDefinition(D, Fn);
690}
691
692llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000693CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000694 CXXDtorType Type) {
695 const llvm::FunctionType *FTy =
696 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Anders Carlsson27ae5362009-04-17 01:58:57 +0000698 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000699 return cast<llvm::Function>(
700 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000701}
702
Mike Stump1eb44332009-09-09 15:08:12 +0000703const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000704 CXXDtorType Type) {
705 llvm::SmallString<256> Name;
706 llvm::raw_svector_ostream Out(Name);
707 mangleCXXDtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Anders Carlsson27ae5362009-04-17 01:58:57 +0000709 Name += '\0';
710 return UniqueMangledName(Name.begin(), Name.end());
711}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000712
Mike Stump32f37012009-08-18 21:49:00 +0000713llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000714 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000715 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000716 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000717
718 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000719 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000720
721 llvm::SmallString<256> OutName;
722 llvm::raw_svector_ostream Out(OutName);
723 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000724 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000725 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000726 llvm::GlobalVariable::LinkageTypes linktype;
727 linktype = llvm::GlobalValue::WeakAnyLinkage;
728 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000729 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000730 // FIXME: descriptor
731 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000732 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000733 // FIXME: TS
734 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
735
736 llvm::Constant *C;
737 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
738 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000739 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000740 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000741 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
742 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000743}
744
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000745class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000746public:
747 /// Index_t - Vtable index type.
748 typedef uint64_t Index_t;
749private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000750 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000751 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000752 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000753 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000754 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000755 /// BLayout - Layout for the most derived class that this vtable is being
756 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000757 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000758 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000759 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000760 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000761 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000762 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000763 /// Index - Maps a method decl into a vtable index. Useful for virtual
764 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000765 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000766 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
767 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump6e319f62009-09-11 23:25:56 +0000768 typedef std::pair<Index_t, Index_t> CallOffset;
769 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
Mike Stump77ca8f62009-09-05 07:20:32 +0000770 Thunks_t Thunks;
Mike Stump6e319f62009-09-11 23:25:56 +0000771 typedef llvm::DenseMap<const CXXMethodDecl *,
772 std::pair<CallOffset, CallOffset> > CovariantThunks_t;
773 CovariantThunks_t CovariantThunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000774 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000775 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000776 // FIXME: Linkage should follow vtable
777 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000778 const uint32_t LLVMPointerWidth;
779 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000780public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000781 VtableBuilder(std::vector<llvm::Constant *> &meth,
782 const CXXRecordDecl *c,
783 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000784 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
785 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000786 CGM(cgm), Extern(true),
787 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000788 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
789 }
Mike Stump32f37012009-08-18 21:49:00 +0000790
Mike Stumpf0070db2009-08-26 20:46:33 +0000791 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000792
Mike Stump15a24e02009-08-28 23:22:54 +0000793 llvm::Constant *wrap(Index_t i) {
794 llvm::Constant *m;
795 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
796 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000797 }
798
Mike Stump15a24e02009-08-28 23:22:54 +0000799 llvm::Constant *wrap(llvm::Constant *m) {
800 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000801 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000802
Mike Stump7fa0d932009-08-20 02:11:48 +0000803 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000804 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump7fa0d932009-08-20 02:11:48 +0000805 for (CXXRecordDecl::base_class_const_iterator i =RD->bases_begin(),
806 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000807 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000808 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
809 if (i->isVirtual() && !SeenVBase.count(Base)) {
810 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000811 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000812 llvm::Constant *m = wrap(BaseOffset);
813 m = wrap((0?700:0) + BaseOffset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000814 offsets.push_back(m);
815 }
Mike Stumpb9837442009-08-20 07:22:17 +0000816 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000817 }
818 }
819
Mike Stumpb9871a22009-08-21 01:45:00 +0000820 void StartNewTable() {
821 SeenVBase.clear();
822 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000823
Mike Stump35191b62009-09-01 22:20:28 +0000824 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000825 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000826 typedef CXXMethodDecl::method_iterator meth_iter;
827
Mike Stumpb9871a22009-08-21 01:45:00 +0000828 // FIXME: Don't like the nested loops. For very large inheritance
829 // heirarchies we could have a table on the side with the final overridder
830 // and just replace each instance of an overridden method once. Would be
831 // nice to measure the cost/benefit on real code.
832
Mike Stumpb9871a22009-08-21 01:45:00 +0000833 for (meth_iter mi = MD->begin_overridden_methods(),
834 e = MD->end_overridden_methods();
835 mi != e; ++mi) {
836 const CXXMethodDecl *OMD = *mi;
837 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000838 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000839 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
840
Mike Stumpdec025b2009-09-07 04:27:52 +0000841 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000842 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000843 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000844 if (submethods[i] != om)
845 continue;
Mike Stump6e319f62009-09-11 23:25:56 +0000846 QualType nc_oret = OMD->getType()->getAsFunctionType()->getResultType();
847 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
848 QualType nc_ret = MD->getType()->getAsFunctionType()->getResultType();
849 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
850 CallOffset ReturnOffset = std::make_pair(0, 0);
851 if (oret != ret) {
852 // FIXME: calculate offsets for covariance
853 ReturnOffset = std::make_pair(42,42);
854 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000855 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000856 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000857
858 Thunks.erase(OMD);
859 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000860 Index_t &idx = VCall[OMD];
861 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000862 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000863 idx = VCalls.size()+1;
864 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000865 } else {
866 VCallOffset[MD] = VCallOffset[OMD];
867 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000868 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000869 VCall[MD] = idx;
Mike Stump6e319f62009-09-11 23:25:56 +0000870 CallOffset ThisOffset;
871 // FIXME: calculate non-virtual offset
872 ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
873 if (ReturnOffset.first || ReturnOffset.second)
874 CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset);
875 else
876 Thunks[MD] = ThisOffset;
Mike Stump35191b62009-09-01 22:20:28 +0000877 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000878 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000879#if 0
880 // FIXME: finish off
881 int64_t O = VCallOffset[OMD] - Offset/8;
882 if (O) {
883 Thunks[MD] = std::make_pair(O, 0);
884 }
885#endif
886 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000887 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000888 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000889
Mike Stump35191b62009-09-01 22:20:28 +0000890 return false;
891 }
892
Mike Stump98cc7102009-09-05 11:28:33 +0000893 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000894 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
895 i != e; ++i) {
896 const CXXMethodDecl *MD = i->first;
897 Index_t idx = Index[MD];
898 Index_t nv_O = i->second.first;
899 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000900 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000901 }
902 Thunks.clear();
Mike Stump6e319f62009-09-11 23:25:56 +0000903 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
904 e = CovariantThunks.end();
905 i != e; ++i) {
906 const CXXMethodDecl *MD = i->first;
907 Index_t idx = Index[MD];
908 Index_t nv_t = i->second.first.first;
909 Index_t v_t = i->second.first.second;
910 Index_t nv_r = i->second.second.first;
911 Index_t v_r = i->second.second.second;
912 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
913 v_r);
914 }
915 CovariantThunks.clear();
Mike Stump77ca8f62009-09-05 07:20:32 +0000916 }
917
Mike Stumpdec025b2009-09-07 04:27:52 +0000918 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
919 int64_t> > *Path, bool MorallyVirtual) {
920 for (std::vector<std::pair<const CXXRecordDecl *,
921 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000922 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000923 const CXXRecordDecl *RD = i->first;
924 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000925 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
926 ++mi)
927 if (mi->isVirtual()) {
928 const CXXMethodDecl *MD = *mi;
Anders Carlssonc7cba152009-09-12 00:00:29 +0000929 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD));
Mike Stumpdec025b2009-09-07 04:27:52 +0000930 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000931 }
932 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000933 }
934
Mike Stump6d10eb82009-09-05 07:49:12 +0000935 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlssonc7cba152009-09-12 00:00:29 +0000936 llvm::Constant *m = 0;
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000937 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonc7cba152009-09-12 00:00:29 +0000938 m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000939 else
Anders Carlssonc7cba152009-09-12 00:00:29 +0000940 m = wrap(CGM.GetAddrOfFunction(MD));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000941
Mike Stump77ca8f62009-09-05 07:20:32 +0000942 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000943 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000944 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Mike Stumpb9871a22009-08-21 01:45:00 +0000946 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000947 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000948 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000949 if (MorallyVirtual) {
950 VCallOffset[MD] = Offset/8;
951 Index_t &idx = VCall[MD];
952 // Allocate the first one, after that, we reuse the previous one.
953 if (idx == 0) {
954 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000955 VCalls.push_back(0);
956 }
957 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000958 }
959
Mike Stump6d10eb82009-09-05 07:49:12 +0000960 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
961 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000962 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
963 ++mi)
964 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +0000965 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +0000966 }
Mike Stump65defe32009-08-18 21:03:28 +0000967
Mike Stump77ca8f62009-09-05 07:20:32 +0000968 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
969 const CXXRecordDecl *PrimaryBase,
970 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
971 int64_t Offset) {
972 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
973 e = RD->bases_end(); i != e; ++i) {
974 if (i->isVirtual())
975 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000976 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +0000977 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
978 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
979 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
980 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +0000981 std::vector<std::pair<const CXXRecordDecl *,
982 int64_t> > S;
983 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +0000984 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +0000985 }
986 }
987 }
988
Mike Stump6d10eb82009-09-05 07:49:12 +0000989 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
990 const ASTRecordLayout &Layout,
991 const CXXRecordDecl *PrimaryBase,
992 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
993 int64_t Offset, bool ForVirtualBase) {
994 StartNewTable();
995 extra = 0;
996 // FIXME: Cleanup.
997 if (!ForVirtualBase) {
998 // then virtual base offsets...
999 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1000 e = offsets.rend(); i != e; ++i)
1001 methods.push_back(*i);
1002 }
1003
1004 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +00001005 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
1006 e=VCalls.rend();
1007 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +00001008 methods.push_back(wrap((0?600:0) + *i));
1009 VCalls.clear();
1010
1011 if (ForVirtualBase) {
1012 // then virtual base offsets...
1013 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1014 e = offsets.rend(); i != e; ++i)
1015 methods.push_back(*i);
1016 }
1017
1018 methods.push_back(wrap(-(Offset/8)));
1019 methods.push_back(rtti);
1020 Index_t AddressPoint = methods.size();
1021
Mike Stump98cc7102009-09-05 11:28:33 +00001022 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +00001023 methods.insert(methods.end(), submethods.begin(), submethods.end());
1024 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +00001025
1026 // and then the non-virtual bases.
1027 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1028 MorallyVirtual, Offset);
1029 return AddressPoint;
1030 }
1031
Mike Stump078d7782009-09-05 08:40:18 +00001032 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +00001033 if (!RD->isDynamicClass())
1034 return;
1035
1036 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001037 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +00001038 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1039
Mike Stump9bbe9622009-09-05 08:37:03 +00001040 // vtables are composed from the chain of primaries.
1041 if (PrimaryBase) {
1042 if (PrimaryBaseWasVirtual)
1043 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001044 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +00001045 }
1046
1047 // And add the virtuals for the class to the primary vtable.
1048 AddMethods(RD, MorallyVirtual, Offset);
1049 }
1050
Mike Stumpe45c90f2009-09-05 09:10:58 +00001051 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +00001052 bool MorallyVirtual = false, int64_t Offset = 0,
1053 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +00001054 std::vector<std::pair<const CXXRecordDecl *,
1055 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +00001056 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +00001057 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +00001058
1059 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001060 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +00001061 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1062
Mike Stump15a24e02009-08-28 23:22:54 +00001063 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +00001064 extra = 0;
1065 GenerateVBaseOffsets(offsets, RD, Offset);
1066 if (ForVirtualBase)
1067 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +00001068
1069 // 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 Stump109b13d2009-08-18 21:30:21 +00001074 }
1075
Mike Stump15a24e02009-08-28 23:22:54 +00001076 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +00001077 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +00001078
Mike Stump98cc7102009-09-05 11:28:33 +00001079 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +00001080 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +00001081
Mike Stump6d10eb82009-09-05 07:49:12 +00001082 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1083 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +00001084 }
1085
Mike Stump98cc7102009-09-05 11:28:33 +00001086 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +00001087 int64_t Offset = 0,
1088 std::vector<std::pair<const CXXRecordDecl *,
1089 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +00001090 bool alloc = false;
1091 if (Path == 0) {
1092 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +00001093 Path = new std::vector<std::pair<const CXXRecordDecl *,
1094 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +00001095 }
1096 // FIXME: We also need to override using all paths to a virtual base,
1097 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +00001098 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +00001099 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1100 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00001101 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +00001102 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1103 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1104 // Mark it so we don't output it twice.
1105 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +00001106 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +00001107 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +00001108 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +00001109 }
Mike Stumpdec025b2009-09-07 04:27:52 +00001110 int64_t BaseOffset = Offset;
1111 if (i->isVirtual())
1112 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +00001113 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +00001114 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +00001115 }
Mike Stump98cc7102009-09-05 11:28:33 +00001116 Path->pop_back();
1117 if (alloc)
1118 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +00001119 }
Mike Stump109b13d2009-08-18 21:30:21 +00001120};
Mike Stump8a12b562009-08-06 15:50:11 +00001121
Mike Stumpf0070db2009-08-26 20:46:33 +00001122class VtableInfo {
1123public:
1124 typedef VtableBuilder::Index_t Index_t;
1125private:
1126 CodeGenModule &CGM; // Per-module state.
1127 /// Index_t - Vtable index type.
1128 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
1129 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
1130 // FIXME: Move to Context.
1131 static MapTy IndexFor;
1132public:
1133 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
1134 void register_index(const CXXRecordDecl *RD, const ElTy &e) {
1135 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
1136 // We own a copy of this, it will go away shortly.
1137 new ElTy (e);
1138 IndexFor[RD] = new ElTy (e);
1139 }
1140 Index_t lookup(const CXXMethodDecl *MD) {
1141 const CXXRecordDecl *RD = MD->getParent();
1142 MapTy::iterator I = IndexFor.find(RD);
1143 if (I == IndexFor.end()) {
1144 std::vector<llvm::Constant *> methods;
1145 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001146 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001147 b.GenerateVtableForVBases(RD);
Mike Stumpf0070db2009-08-26 20:46:33 +00001148 register_index(RD, b.getIndex());
1149 I = IndexFor.find(RD);
1150 }
1151 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1152 return (*I->second)[MD];
1153 }
1154};
1155
1156// FIXME: Move to Context.
1157VtableInfo::MapTy VtableInfo::IndexFor;
1158
Mike Stumpf1216772009-07-31 18:25:34 +00001159llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001160 llvm::SmallString<256> OutName;
1161 llvm::raw_svector_ostream Out(OutName);
1162 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001163 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001164 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001165 llvm::GlobalVariable::LinkageTypes linktype;
1166 linktype = llvm::GlobalValue::WeakAnyLinkage;
1167 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001168 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001169 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001170
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001171 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001172
Mike Stump276b9f12009-08-16 01:46:26 +00001173 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001174 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001175
Mike Stump276b9f12009-08-16 01:46:26 +00001176 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001177 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001178
Mike Stump82b56962009-07-31 21:43:43 +00001179 llvm::Constant *C;
1180 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1181 C = llvm::ConstantArray::get(type, methods);
1182 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001183 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001184 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001185 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001186 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001187 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001188 return vtable;
1189}
1190
Mike Stumpf0070db2009-08-26 20:46:33 +00001191// FIXME: move to Context
1192static VtableInfo *vtableinfo;
1193
Mike Stumped032eb2009-09-04 18:27:16 +00001194llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1195 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001196 bool Extern, int64_t nv,
1197 int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001198 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1199
1200 FunctionArgList Args;
1201 ImplicitParamDecl *ThisDecl =
1202 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1203 MD->getThisType(getContext()));
1204 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1205 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1206 e = MD->param_end();
1207 i != e; ++i) {
1208 ParmVarDecl *D = *i;
1209 Args.push_back(std::make_pair(D, D->getType()));
1210 }
1211 IdentifierInfo *II
1212 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1213 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1214 getContext().getTranslationUnitDecl(),
1215 SourceLocation(), II, R, 0,
1216 Extern
1217 ? FunctionDecl::Extern
1218 : FunctionDecl::Static,
1219 false, true);
1220 StartFunction(FD, R, Fn, Args, SourceLocation());
1221 // FIXME: generate body
1222 FinishFunction();
1223 return Fn;
1224}
1225
Mike Stump6e319f62009-09-11 23:25:56 +00001226llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1227 const CXXMethodDecl *MD,
1228 bool Extern,
1229 int64_t nv_t,
1230 int64_t v_t,
1231 int64_t nv_r,
1232 int64_t v_r) {
1233 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1234
1235 FunctionArgList Args;
1236 ImplicitParamDecl *ThisDecl =
1237 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1238 MD->getThisType(getContext()));
1239 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1240 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1241 e = MD->param_end();
1242 i != e; ++i) {
1243 ParmVarDecl *D = *i;
1244 Args.push_back(std::make_pair(D, D->getType()));
1245 }
1246 IdentifierInfo *II
1247 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1248 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1249 getContext().getTranslationUnitDecl(),
1250 SourceLocation(), II, R, 0,
1251 Extern
1252 ? FunctionDecl::Extern
1253 : FunctionDecl::Static,
1254 false, true);
1255 StartFunction(FD, R, Fn, Args, SourceLocation());
1256 // FIXME: generate body
1257 FinishFunction();
1258 return Fn;
1259}
1260
Mike Stump77ca8f62009-09-05 07:20:32 +00001261llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1262 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001263 llvm::SmallString<256> OutName;
1264 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001265 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001266 llvm::GlobalVariable::LinkageTypes linktype;
1267 linktype = llvm::GlobalValue::WeakAnyLinkage;
1268 if (!Extern)
1269 linktype = llvm::GlobalValue::InternalLinkage;
1270 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1271 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1272 const llvm::FunctionType *FTy =
1273 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1274 FPT->isVariadic());
1275
1276 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1277 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001278 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001279 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1280 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1281 return m;
1282}
1283
Mike Stump6e319f62009-09-11 23:25:56 +00001284llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1285 bool Extern, int64_t nv_t,
1286 int64_t v_t, int64_t nv_r,
1287 int64_t v_r) {
1288 llvm::SmallString<256> OutName;
1289 llvm::raw_svector_ostream Out(OutName);
1290 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1291 llvm::GlobalVariable::LinkageTypes linktype;
1292 linktype = llvm::GlobalValue::WeakAnyLinkage;
1293 if (!Extern)
1294 linktype = llvm::GlobalValue::InternalLinkage;
1295 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1296 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1297 const llvm::FunctionType *FTy =
1298 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1299 FPT->isVariadic());
1300
1301 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1302 &getModule());
1303 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1304 v_r);
1305 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1306 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1307 return m;
1308}
1309
Mike Stumpf0070db2009-08-26 20:46:33 +00001310llvm::Value *
1311CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1312 const llvm::Type *Ty) {
1313 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Mike Stumpf0070db2009-08-26 20:46:33 +00001315 // FIXME: move to Context
1316 if (vtableinfo == 0)
1317 vtableinfo = new VtableInfo(CGM);
1318
1319 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1320
1321 Ty = llvm::PointerType::get(Ty, 0);
1322 Ty = llvm::PointerType::get(Ty, 0);
1323 Ty = llvm::PointerType::get(Ty, 0);
1324 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1325 vtbl = Builder.CreateLoad(vtbl);
1326 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1327 Idx, "vfn");
1328 vfn = Builder.CreateLoad(vfn);
1329 return vfn;
1330}
1331
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001332/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1333/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1334/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001335// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001336void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001337 llvm::Value *Src,
1338 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001339 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001340 QualType Ty) {
1341 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1342 assert(CA && "VLA cannot be copied over");
1343 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001345 // Create a temporary for the loop index and initialize it with 0.
1346 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1347 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001348 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001349 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1350 Builder.CreateStore(zeroConstant, IndexPtr, false);
1351 // Start the loop with a block that tests the condition.
1352 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1353 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001355 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001357 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1358 // Generate: if (loop-index < number-of-elements fall to the loop body,
1359 // otherwise, go to the block after the for-loop.
1360 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001361 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001362 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1363 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001364 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001365 "isless");
1366 // If the condition is true, execute the body.
1367 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001369 EmitBlock(ForBody);
1370 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1371 // Inside the loop body, emit the constructor call on the array element.
1372 Counter = Builder.CreateLoad(IndexPtr);
1373 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1374 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1375 if (BitwiseCopy)
1376 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001377 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001378 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001379 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001380 Ctor_Complete);
1381 CallArgList CallArgs;
1382 // Push the this (Dest) ptr.
1383 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1384 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001386 // Push the Src ptr.
1387 CallArgs.push_back(std::make_pair(RValue::get(Src),
1388 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001389 QualType ResultType =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001390 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1391 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1392 Callee, CallArgs, BaseCopyCtor);
1393 }
1394 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001396 // Emit the increment of the loop counter.
1397 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1398 Counter = Builder.CreateLoad(IndexPtr);
1399 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1400 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001402 // Finally, branch back up to the condition for the next iteration.
1403 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001405 // Emit the fall-through block.
1406 EmitBlock(AfterFor, true);
1407}
1408
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001409/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001410/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001411/// bitwise assignment or via a copy assignment operator function call.
1412/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001413void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001414 llvm::Value *Src,
1415 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001416 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001417 QualType Ty) {
1418 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1419 assert(CA && "VLA cannot be asssigned");
1420 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001422 // Create a temporary for the loop index and initialize it with 0.
1423 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1424 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001425 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001426 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1427 Builder.CreateStore(zeroConstant, IndexPtr, false);
1428 // Start the loop with a block that tests the condition.
1429 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1430 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001432 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001434 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1435 // Generate: if (loop-index < number-of-elements fall to the loop body,
1436 // otherwise, go to the block after the for-loop.
1437 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001438 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001439 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1440 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001441 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001442 "isless");
1443 // If the condition is true, execute the body.
1444 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001446 EmitBlock(ForBody);
1447 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1448 // Inside the loop body, emit the assignment operator call on array element.
1449 Counter = Builder.CreateLoad(IndexPtr);
1450 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1451 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1452 const CXXMethodDecl *MD = 0;
1453 if (BitwiseAssign)
1454 EmitAggregateCopy(Dest, Src, Ty);
1455 else {
1456 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1457 MD);
1458 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1459 (void)hasCopyAssign;
1460 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1461 const llvm::Type *LTy =
1462 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1463 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001464 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001466 CallArgList CallArgs;
1467 // Push the this (Dest) ptr.
1468 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1469 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001471 // Push the Src ptr.
1472 CallArgs.push_back(std::make_pair(RValue::get(Src),
1473 MD->getParamDecl(0)->getType()));
Mike Stumped032eb2009-09-04 18:27:16 +00001474 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001475 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1476 Callee, CallArgs, MD);
1477 }
1478 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001480 // Emit the increment of the loop counter.
1481 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1482 Counter = Builder.CreateLoad(IndexPtr);
1483 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1484 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001486 // Finally, branch back up to the condition for the next iteration.
1487 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001489 // Emit the fall-through block.
1490 EmitBlock(AfterFor, true);
1491}
1492
Fariborz Jahanianca283612009-08-07 23:51:33 +00001493/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1494/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001495/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001496void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001497 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001498 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001499 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1500 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001501 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1502 /*NullCheckValue=*/false);
1503 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1504 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001505 }
1506 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1507 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001508 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
1511 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001512 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001513 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001514 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001515 CallArgList CallArgs;
1516 // Push the this (Dest) ptr.
1517 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1518 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Fariborz Jahanianca283612009-08-07 23:51:33 +00001520 // Push the Src ptr.
1521 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001522 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001523 QualType ResultType =
Fariborz Jahanianca283612009-08-07 23:51:33 +00001524 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1525 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1526 Callee, CallArgs, BaseCopyCtor);
1527 }
1528}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001529
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001530/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001531/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001532/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001533// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001534void CodeGenFunction::EmitClassCopyAssignment(
1535 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001536 const CXXRecordDecl *ClassDecl,
1537 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001538 QualType Ty) {
1539 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001540 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1541 /*NullCheckValue=*/false);
1542 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1543 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001544 }
1545 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1546 EmitAggregateCopy(Dest, Src, Ty);
1547 return;
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001550 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001551 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001552 MD);
1553 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1554 (void)ConstCopyAssignOp;
1555
1556 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +00001557 const llvm::Type *LTy =
1558 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001559 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001560 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001562 CallArgList CallArgs;
1563 // Push the this (Dest) ptr.
1564 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1565 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001567 // Push the Src ptr.
1568 CallArgs.push_back(std::make_pair(RValue::get(Src),
1569 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001570 QualType ResultType =
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001571 MD->getType()->getAsFunctionType()->getResultType();
1572 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1573 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001574}
1575
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001576/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001577void
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001578CodeGenFunction::SynthesizeDefaultConstructor(GlobalDecl GD,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001579 const FunctionDecl *FD,
1580 llvm::Function *Fn,
1581 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001582 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(GD.getDecl());
1583
1584 StartFunction(GD, FD->getResultType(), Fn, Args, SourceLocation());
1585 EmitCtorPrologue(Ctor);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001586 FinishFunction();
1587}
1588
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001589/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001590/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001591/// The implicitly-defined copy constructor for class X performs a memberwise
1592/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001593/// of initialization of bases and members in a user-defined constructor
1594/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001595/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001596/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001597/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001598/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001599/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001600/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001601/// Virtual base class subobjects shall be copied only once by the
1602/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001603
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001604void CodeGenFunction::SynthesizeCXXCopyConstructor(GlobalDecl GD,
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001605 const FunctionDecl *FD,
1606 llvm::Function *Fn,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001607 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001608 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(GD.getDecl());
1609 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001610 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001611 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001612 StartFunction(GD, Ctor->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001614 FunctionArgList::const_iterator i = Args.begin();
1615 const VarDecl *ThisArg = i->first;
1616 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1617 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1618 const VarDecl *SrcArg = (i+1)->first;
1619 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1620 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001622 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1623 Base != ClassDecl->bases_end(); ++Base) {
1624 // FIXME. copy constrution of virtual base NYI
1625 if (Base->isVirtual())
1626 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001628 CXXRecordDecl *BaseClassDecl
1629 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001630 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1631 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001632 }
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001634 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1635 FieldEnd = ClassDecl->field_end();
1636 Field != FieldEnd; ++Field) {
1637 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001638 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001639 getContext().getAsConstantArrayType(FieldType);
1640 if (Array)
1641 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001643 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1644 CXXRecordDecl *FieldClassDecl
1645 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1646 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1647 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001648 if (Array) {
1649 const llvm::Type *BasePtr = ConvertType(FieldType);
1650 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001651 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001652 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001653 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001654 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1655 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1656 FieldClassDecl, FieldType);
1657 }
Mike Stump1eb44332009-09-09 15:08:12 +00001658 else
1659 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001660 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001661 continue;
1662 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001663 // Do a built-in assignment of scalar data members.
1664 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1665 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1666 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1667 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001668 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001669 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001670}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001671
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001672/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001673/// Before the implicitly-declared copy assignment operator for a class is
1674/// implicitly defined, all implicitly- declared copy assignment operators for
1675/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001676/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001677/// The implicitly-defined copy assignment operator for class X performs
1678/// memberwise assignment of its subob- jects. The direct base classes of X are
1679/// assigned first, in the order of their declaration in
1680/// the base-specifier-list, and then the immediate nonstatic data members of X
1681/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001682/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001683/// if the subobject is of class type, the copy assignment operator for the
1684/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001685/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001686///
Mike Stump1eb44332009-09-09 15:08:12 +00001687/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001688/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001689///
Mike Stump1eb44332009-09-09 15:08:12 +00001690/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001691/// used.
1692void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
1693 const FunctionDecl *FD,
1694 llvm::Function *Fn,
1695 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001696
1697 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1698 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1699 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001700 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001702 FunctionArgList::const_iterator i = Args.begin();
1703 const VarDecl *ThisArg = i->first;
1704 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1705 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1706 const VarDecl *SrcArg = (i+1)->first;
1707 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1708 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001710 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1711 Base != ClassDecl->bases_end(); ++Base) {
1712 // FIXME. copy assignment of virtual base NYI
1713 if (Base->isVirtual())
1714 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001716 CXXRecordDecl *BaseClassDecl
1717 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1718 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1719 Base->getType());
1720 }
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001722 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1723 FieldEnd = ClassDecl->field_end();
1724 Field != FieldEnd; ++Field) {
1725 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001726 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001727 getContext().getAsConstantArrayType(FieldType);
1728 if (Array)
1729 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001731 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1732 CXXRecordDecl *FieldClassDecl
1733 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1734 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1735 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001736 if (Array) {
1737 const llvm::Type *BasePtr = ConvertType(FieldType);
1738 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1739 llvm::Value *DestBaseAddrPtr =
1740 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1741 llvm::Value *SrcBaseAddrPtr =
1742 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1743 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1744 FieldClassDecl, FieldType);
1745 }
1746 else
Mike Stump1eb44332009-09-09 15:08:12 +00001747 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001748 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001749 continue;
1750 }
1751 // Do a built-in assignment of scalar data members.
1752 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1753 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1754 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1755 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001756 }
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001758 // return *this;
1759 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001761 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001762}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001763
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001764/// EmitCtorPrologue - This routine generates necessary code to initialize
1765/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001766/// FIXME: This needs to take a CXXCtorType.
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001767void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001768 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001769 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001770 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001772 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001773 E = CD->init_end();
1774 B != E; ++B) {
1775 CXXBaseOrMemberInitializer *Member = (*B);
1776 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001777 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001778 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001779 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001780 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001781 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1782 BaseClassDecl,
1783 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001784 EmitCXXConstructorCall(Member->getConstructor(),
1785 Ctor_Complete, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001786 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001787 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001788 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001789 // non-static data member initilaizers.
1790 FieldDecl *Field = Member->getMember();
1791 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001792 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001793 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001794 if (Array)
1795 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Mike Stumpf1216772009-07-31 18:25:34 +00001797 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001798 LValue LHS;
1799 if (FieldType->isReferenceType()) {
1800 // FIXME: This is really ugly; should be refactored somehow
1801 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1802 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
1803 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(),
1804 QualType::GCNone, FieldType.getAddressSpace());
1805 } else {
1806 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1807 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001808 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001809 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001810 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001811 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001812 if (Array) {
1813 const llvm::Type *BasePtr = ConvertType(FieldType);
1814 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001815 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001816 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001817 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001818 Array, BaseAddrPtr);
1819 }
1820 else
1821 EmitCXXConstructorCall(Member->getConstructor(),
1822 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001823 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001824 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001825 continue;
1826 }
1827 else {
1828 // Initializing an anonymous union data member.
1829 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001830 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001831 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001832 FieldType = anonMember->getType();
1833 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001836 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001837 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001838 RValue RHS;
1839 if (FieldType->isReferenceType())
1840 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1841 /*IsInitializer=*/true);
1842 else
1843 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1844 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001845 }
1846 }
Mike Stumpf1216772009-07-31 18:25:34 +00001847
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001848 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001849 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001850 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001851 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001852 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001853 ClassDecl->bases_begin();
1854 Base != ClassDecl->bases_end(); ++Base) {
1855 // FIXME. copy assignment of virtual base NYI
1856 if (Base->isVirtual())
1857 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001859 CXXRecordDecl *BaseClassDecl
1860 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1861 if (BaseClassDecl->hasTrivialConstructor())
1862 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001863 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001864 BaseClassDecl->getDefaultConstructor(getContext())) {
1865 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001866 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1867 BaseClassDecl,
1868 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001869 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1870 }
1871 }
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001873 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1874 FieldEnd = ClassDecl->field_end();
1875 Field != FieldEnd; ++Field) {
1876 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001877 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001878 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001879 if (Array)
1880 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001881 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1882 continue;
1883 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001884 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001885 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1886 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1887 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001888 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001889 MemberClassDecl->getDefaultConstructor(getContext())) {
1890 LoadOfThis = LoadCXXThis();
1891 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001892 if (Array) {
1893 const llvm::Type *BasePtr = ConvertType(FieldType);
1894 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001895 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001896 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1897 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1898 }
1899 else
Mike Stump1eb44332009-09-09 15:08:12 +00001900 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001901 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001902 }
1903 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001904 }
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Mike Stumpf1216772009-07-31 18:25:34 +00001906 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001907 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001908 if (!LoadOfThis)
1909 LoadOfThis = LoadCXXThis();
1910 llvm::Value *VtableField;
1911 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001912 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001913 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1914 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1915 llvm::Value *vtable = GenerateVtable(ClassDecl);
1916 Builder.CreateStore(vtable, VtableField);
1917 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001918}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001919
1920/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001921/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001922/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001923/// FIXME: This needs to take a CXXDtorType.
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001924void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) {
1925 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001926 assert(!ClassDecl->getNumVBases() &&
1927 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001928 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001930 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1931 *E = DD->destr_end(); B != E; ++B) {
1932 uintptr_t BaseOrMember = (*B);
1933 if (DD->isMemberToDestroy(BaseOrMember)) {
1934 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1935 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001936 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001937 getContext().getAsConstantArrayType(FieldType);
1938 if (Array)
1939 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001940 const RecordType *RT = FieldType->getAs<RecordType>();
1941 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1942 if (FieldClassDecl->hasTrivialDestructor())
1943 continue;
1944 llvm::Value *LoadOfThis = LoadCXXThis();
1945 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001946 if (Array) {
1947 const llvm::Type *BasePtr = ConvertType(FieldType);
1948 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001949 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001950 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001951 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001952 Array, BaseAddrPtr);
1953 }
1954 else
1955 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1956 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001957 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001958 const RecordType *RT =
1959 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1960 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1961 if (BaseClassDecl->hasTrivialDestructor())
1962 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001963 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1964 ClassDecl, BaseClassDecl,
1965 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001966 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1967 Dtor_Complete, V);
1968 }
1969 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001970 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1971 return;
1972 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001973 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001974 // reverse order of their construction.
1975 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001977 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1978 FieldEnd = ClassDecl->field_end();
1979 Field != FieldEnd; ++Field) {
1980 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001981 if (getContext().getAsConstantArrayType(FieldType))
1982 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001983 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1984 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1985 if (FieldClassDecl->hasTrivialDestructor())
1986 continue;
1987 DestructedFields.push_back(*Field);
1988 }
1989 }
1990 if (!DestructedFields.empty())
1991 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1992 FieldDecl *Field = DestructedFields[i];
1993 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001994 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001995 getContext().getAsConstantArrayType(FieldType);
1996 if (Array)
1997 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001998 const RecordType *RT = FieldType->getAs<RecordType>();
1999 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2000 llvm::Value *LoadOfThis = LoadCXXThis();
2001 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002002 if (Array) {
2003 const llvm::Type *BasePtr = ConvertType(FieldType);
2004 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002005 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002006 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002007 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002008 Array, BaseAddrPtr);
2009 }
2010 else
2011 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
2012 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002013 }
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002015 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
2016 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
2017 Base != ClassDecl->bases_end(); ++Base) {
2018 // FIXME. copy assignment of virtual base NYI
2019 if (Base->isVirtual())
2020 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002022 CXXRecordDecl *BaseClassDecl
2023 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2024 if (BaseClassDecl->hasTrivialDestructor())
2025 continue;
2026 DestructedBases.push_back(BaseClassDecl);
2027 }
2028 if (DestructedBases.empty())
2029 return;
2030 for (int i = DestructedBases.size() -1; i >= 0; --i) {
2031 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00002032 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
2033 ClassDecl,BaseClassDecl,
2034 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002035 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2036 Dtor_Complete, V);
2037 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002038}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002039
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002040void CodeGenFunction::SynthesizeDefaultDestructor(GlobalDecl GD,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002041 const FunctionDecl *FD,
2042 llvm::Function *Fn,
2043 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002045 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(GD.getDecl());
2046
2047 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002048 assert(!ClassDecl->hasUserDeclaredDestructor() &&
2049 "SynthesizeDefaultDestructor - destructor has user declaration");
2050 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002051
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002052 StartFunction(GD, Dtor->getResultType(), Fn, Args, SourceLocation());
2053 EmitDtorEpilogue(Dtor);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002054 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00002055}