blob: 828892a241482463fe1b80cfce9bf4ce52ce8d4f [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) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 StartFunction(0, 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
Mike Stumpf0070db2009-08-26 20:46:33 +0000230 Callee = CGM.GetAddrOfFunction(GlobalDecl(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 Carlsson0f294632009-05-27 04:18:27 +0000259 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(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 Jahanianc238a792009-07-30 00:10:25 +0000279static bool
280GetNestedPaths(llvm::SmallVectorImpl<const CXXRecordDecl *> &NestedBasePaths,
281 const CXXRecordDecl *ClassDecl,
282 const CXXRecordDecl *BaseClassDecl) {
Fariborz Jahanianc238a792009-07-30 00:10:25 +0000283 for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(),
284 e = ClassDecl->bases_end(); i != e; ++i) {
285 if (i->isVirtual())
286 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000287 const CXXRecordDecl *Base =
Mike Stump104ffaa2009-08-04 21:58:42 +0000288 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanianc238a792009-07-30 00:10:25 +0000289 if (Base == BaseClassDecl) {
290 NestedBasePaths.push_back(BaseClassDecl);
291 return true;
292 }
293 }
294 // BaseClassDecl not an immediate base of ClassDecl.
295 for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(),
296 e = ClassDecl->bases_end(); i != e; ++i) {
297 if (i->isVirtual())
298 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000299 const CXXRecordDecl *Base =
Fariborz Jahanianc238a792009-07-30 00:10:25 +0000300 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
301 if (GetNestedPaths(NestedBasePaths, Base, BaseClassDecl)) {
302 NestedBasePaths.push_back(Base);
303 return true;
304 }
305 }
306 return false;
307}
308
Fariborz Jahanian9e809e72009-07-28 17:38:28 +0000309llvm::Value *CodeGenFunction::AddressCXXOfBaseClass(llvm::Value *BaseValue,
Mike Stump1eb44332009-09-09 15:08:12 +0000310 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +0000311 const CXXRecordDecl *BaseClassDecl) {
Fariborz Jahanian9e809e72009-07-28 17:38:28 +0000312 if (ClassDecl == BaseClassDecl)
313 return BaseValue;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Owen Anderson0032b272009-08-13 21:57:51 +0000315 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Fariborz Jahanianc238a792009-07-30 00:10:25 +0000316 llvm::SmallVector<const CXXRecordDecl *, 16> NestedBasePaths;
317 GetNestedPaths(NestedBasePaths, ClassDecl, BaseClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000318 assert(NestedBasePaths.size() > 0 &&
Fariborz Jahanianc238a792009-07-30 00:10:25 +0000319 "AddressCXXOfBaseClass - inheritence path failed");
320 NestedBasePaths.push_back(ClassDecl);
321 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Fariborz Jahanian9e809e72009-07-28 17:38:28 +0000323 // Accessing a member of the base class. Must add delata to
324 // the load of 'this'.
Fariborz Jahanianc238a792009-07-30 00:10:25 +0000325 for (unsigned i = NestedBasePaths.size()-1; i > 0; i--) {
326 const CXXRecordDecl *DerivedClass = NestedBasePaths[i];
327 const CXXRecordDecl *BaseClass = NestedBasePaths[i-1];
Mike Stump1eb44332009-09-09 15:08:12 +0000328 const ASTRecordLayout &Layout =
Fariborz Jahanianc238a792009-07-30 00:10:25 +0000329 getContext().getASTRecordLayout(DerivedClass);
330 Offset += Layout.getBaseClassOffset(BaseClass) / 8;
331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332 llvm::Value *OffsetVal =
Fariborz Jahanian5a8503b2009-07-29 15:54:56 +0000333 llvm::ConstantInt::get(
334 CGM.getTypes().ConvertType(CGM.getContext().LongTy), Offset);
Fariborz Jahanian9e809e72009-07-28 17:38:28 +0000335 BaseValue = Builder.CreateBitCast(BaseValue, I8Ptr);
336 BaseValue = Builder.CreateGEP(BaseValue, OffsetVal, "add.ptr");
Mike Stump1eb44332009-09-09 15:08:12 +0000337 QualType BTy =
Fariborz Jahanian9e809e72009-07-28 17:38:28 +0000338 getContext().getCanonicalType(
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +0000339 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl)));
Fariborz Jahanian9e809e72009-07-28 17:38:28 +0000340 const llvm::Type *BasePtr = ConvertType(BTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000341 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Fariborz Jahanian9e809e72009-07-28 17:38:28 +0000342 BaseValue = Builder.CreateBitCast(BaseValue, BasePtr);
343 return BaseValue;
344}
345
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000346/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
347/// for-loop to call the default constructor on individual members of the
348/// array. 'Array' is the array type, 'This' is llvm pointer of the start
349/// of the array and 'D' is the default costructor Decl for elements of the
350/// array. It is assumed that all relevant checks have been made by the
351/// caller.
352void
353CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
354 const ArrayType *Array,
355 llvm::Value *This) {
356 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
357 assert(CA && "Do we support VLA for construction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000359 // Create a temporary for the loop index and initialize it with 0.
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000360 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000361 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000362 llvm::Value* zeroConstant =
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000363 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000364 Builder.CreateStore(zeroConstant, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000366 // Start the loop with a block that tests the condition.
367 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
368 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000370 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000372 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000374 // Generate: if (loop-index < number-of-elements fall to the loop body,
375 // otherwise, go to the block after the for-loop.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000376 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000377 llvm::Value * NumElementsPtr =
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000378 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000379 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000380 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000381 "isless");
382 // If the condition is true, execute the body.
383 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000385 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000387 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000388 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000389 Counter = Builder.CreateLoad(IndexPtr);
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000390 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
391 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000393 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000395 // Emit the increment of the loop counter.
396 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
397 Counter = Builder.CreateLoad(IndexPtr);
398 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
399 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000401 // Finally, branch back up to the condition for the next iteration.
402 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000404 // Emit the fall-through block.
405 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000406}
407
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000408/// EmitCXXAggrDestructorCall - calls the default destructor on array
409/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000410void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000411CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
412 const ArrayType *Array,
413 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000414 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
415 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000416 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000417 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000418 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000419 // Create a temporary for the loop index and initialize it with count of
420 // array elements.
421 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
422 "loop.index");
423 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000424 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000425 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
426 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000428 // Start the loop with a block that tests the condition.
429 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
430 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000432 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000434 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000436 // Generate: if (loop-index != 0 fall to the loop body,
437 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000438 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000439 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
440 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
441 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
442 "isne");
443 // If the condition is true, execute the body.
444 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000446 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000448 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
449 // Inside the loop body, emit the constructor call on the array element.
450 Counter = Builder.CreateLoad(IndexPtr);
451 Counter = Builder.CreateSub(Counter, One);
452 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
453 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000455 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000457 // Emit the decrement of the loop counter.
458 Counter = Builder.CreateLoad(IndexPtr);
459 Counter = Builder.CreateSub(Counter, One, "dec");
460 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000462 // Finally, branch back up to the condition for the next iteration.
463 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000465 // Emit the fall-through block.
466 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000467}
468
469void
Mike Stump1eb44332009-09-09 15:08:12 +0000470CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
471 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000472 llvm::Value *This,
473 CallExpr::const_arg_iterator ArgBeg,
474 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000475 if (D->isCopyConstructor(getContext())) {
476 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
477 if (ClassDecl->hasTrivialCopyConstructor()) {
478 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
479 "EmitCXXConstructorCall - user declared copy constructor");
480 const Expr *E = (*ArgBeg);
481 QualType Ty = E->getType();
482 llvm::Value *Src = EmitLValue(E).getAddress();
483 EmitAggregateCopy(This, Src, Ty);
484 return;
485 }
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000488 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
489
490 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000491}
492
Mike Stump1eb44332009-09-09 15:08:12 +0000493void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000494 CXXDtorType Type,
495 llvm::Value *This) {
496 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Anders Carlsson7267c162009-05-29 21:03:38 +0000498 EmitCXXMemberCall(D, Callee, This, 0, 0);
499}
500
Mike Stump1eb44332009-09-09 15:08:12 +0000501void
502CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000503 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000504 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000505
506 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000507 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000508 if (RD->hasTrivialConstructor())
509 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000510
Mike Stump1eb44332009-09-09 15:08:12 +0000511 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000512 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000513 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000514 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000515 EmitAggExpr((*i), Dest, false);
516 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000517 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000518 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000519 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000520 E->arg_begin(), E->arg_end());
521}
522
Anders Carlssona00703d2009-05-31 01:40:14 +0000523llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
Anders Carlssoned4e3672009-05-31 20:21:44 +0000524 if (E->isArray()) {
525 ErrorUnsupported(E, "new[] expression");
Owen Anderson03e20502009-07-30 23:11:26 +0000526 return llvm::UndefValue::get(ConvertType(E->getType()));
Anders Carlssoned4e3672009-05-31 20:21:44 +0000527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Anders Carlssoned4e3672009-05-31 20:21:44 +0000529 QualType AllocType = E->getAllocatedType();
530 FunctionDecl *NewFD = E->getOperatorNew();
531 const FunctionProtoType *NewFTy = NewFD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Anders Carlssoned4e3672009-05-31 20:21:44 +0000533 CallArgList NewArgs;
534
535 // The allocation size is the first argument.
536 QualType SizeTy = getContext().getSizeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000537 llvm::Value *AllocSize =
538 llvm::ConstantInt::get(ConvertType(SizeTy),
Anders Carlssoned4e3672009-05-31 20:21:44 +0000539 getContext().getTypeSize(AllocType) / 8);
540
541 NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Anders Carlssoned4e3672009-05-31 20:21:44 +0000543 // Emit the rest of the arguments.
544 // FIXME: Ideally, this should just use EmitCallArgs.
545 CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin();
546
547 // First, use the types from the function type.
548 // We start at 1 here because the first argument (the allocation size)
549 // has already been emitted.
550 for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) {
551 QualType ArgType = NewFTy->getArgType(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Anders Carlssoned4e3672009-05-31 20:21:44 +0000553 assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
Mike Stump1eb44332009-09-09 15:08:12 +0000554 getTypePtr() ==
555 getContext().getCanonicalType(NewArg->getType()).getTypePtr() &&
Anders Carlssoned4e3672009-05-31 20:21:44 +0000556 "type mismatch in call argument!");
Mike Stump1eb44332009-09-09 15:08:12 +0000557
558 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
Anders Carlssoned4e3672009-05-31 20:21:44 +0000559 ArgType));
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Anders Carlssoned4e3672009-05-31 20:21:44 +0000561 }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
563 // Either we've emitted all the call args, or we have a call to a
Anders Carlssoned4e3672009-05-31 20:21:44 +0000564 // variadic function.
Mike Stump1eb44332009-09-09 15:08:12 +0000565 assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) &&
Anders Carlssoned4e3672009-05-31 20:21:44 +0000566 "Extra arguments in non-variadic function!");
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Anders Carlssoned4e3672009-05-31 20:21:44 +0000568 // If we still have any arguments, emit them using the type of the argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000569 for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end();
Anders Carlssoned4e3672009-05-31 20:21:44 +0000570 NewArg != NewArgEnd; ++NewArg) {
571 QualType ArgType = NewArg->getType();
572 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType),
573 ArgType));
574 }
575
576 // Emit the call to new.
Mike Stump1eb44332009-09-09 15:08:12 +0000577 RValue RV =
Anders Carlssoned4e3672009-05-31 20:21:44 +0000578 EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs),
579 CGM.GetAddrOfFunction(GlobalDecl(NewFD)),
580 NewArgs, NewFD);
581
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000582 // If an allocation function is declared with an empty exception specification
583 // it returns null to indicate failure to allocate storage. [expr.new]p13.
584 // (We don't need to check for null when there's no new initializer and
585 // we're allocating a POD type).
586 bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() &&
587 !(AllocType->isPODType() && !E->hasInitializer());
Anders Carlssoned4e3672009-05-31 20:21:44 +0000588
Anders Carlssonf1108532009-06-01 00:05:16 +0000589 llvm::BasicBlock *NewNull = 0;
590 llvm::BasicBlock *NewNotNull = 0;
591 llvm::BasicBlock *NewEnd = 0;
592
593 llvm::Value *NewPtr = RV.getScalarVal();
594
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000595 if (NullCheckResult) {
Anders Carlssonf1108532009-06-01 00:05:16 +0000596 NewNull = createBasicBlock("new.null");
597 NewNotNull = createBasicBlock("new.notnull");
598 NewEnd = createBasicBlock("new.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000599
600 llvm::Value *IsNull =
601 Builder.CreateICmpEQ(NewPtr,
Owen Andersonc9c88b42009-07-31 20:28:54 +0000602 llvm::Constant::getNullValue(NewPtr->getType()),
Anders Carlssonf1108532009-06-01 00:05:16 +0000603 "isnull");
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Anders Carlssonf1108532009-06-01 00:05:16 +0000605 Builder.CreateCondBr(IsNull, NewNull, NewNotNull);
606 EmitBlock(NewNotNull);
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Anders Carlssonf1108532009-06-01 00:05:16 +0000609 NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000611 if (AllocType->isPODType()) {
Anders Carlsson215bd202009-06-01 00:26:14 +0000612 if (E->getNumConstructorArgs() > 0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000613 assert(E->getNumConstructorArgs() == 1 &&
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000614 "Can only have one argument to initializer of POD type.");
615
616 const Expr *Init = E->getConstructorArg(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000617
618 if (!hasAggregateLLVMType(AllocType))
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000619 Builder.CreateStore(EmitScalarExpr(Init), NewPtr);
Anders Carlsson3923e952009-05-31 21:07:58 +0000620 else if (AllocType->isAnyComplexType())
621 EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified());
Anders Carlsson627a3e52009-05-31 21:12:26 +0000622 else
623 EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified());
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000624 }
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000625 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000626 // Call the constructor.
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000627 CXXConstructorDecl *Ctor = E->getConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000628
629 EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr,
630 E->constructor_arg_begin(),
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000631 E->constructor_arg_end());
Anders Carlssoned4e3672009-05-31 20:21:44 +0000632 }
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000633
Anders Carlssonf1108532009-06-01 00:05:16 +0000634 if (NullCheckResult) {
635 Builder.CreateBr(NewEnd);
636 EmitBlock(NewNull);
637 Builder.CreateBr(NewEnd);
638 EmitBlock(NewEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Anders Carlssonf1108532009-06-01 00:05:16 +0000640 llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType());
641 PHI->reserveOperandSpace(2);
642 PHI->addIncoming(NewPtr, NewNotNull);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000643 PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Anders Carlssonf1108532009-06-01 00:05:16 +0000645 NewPtr = PHI;
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000648 return NewPtr;
Anders Carlssona00703d2009-05-31 01:40:14 +0000649}
650
Anders Carlsson60e282c2009-08-16 21:13:42 +0000651void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
652 if (E->isArrayForm()) {
653 ErrorUnsupported(E, "delete[] expression");
654 return;
655 };
656
Mike Stump1eb44332009-09-09 15:08:12 +0000657 QualType DeleteTy =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000658 E->getArgument()->getType()->getAs<PointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Anders Carlsson60e282c2009-08-16 21:13:42 +0000660 llvm::Value *Ptr = EmitScalarExpr(E->getArgument());
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anders Carlsson60e282c2009-08-16 21:13:42 +0000662 // Null check the pointer.
663 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
664 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
665
Mike Stump1eb44332009-09-09 15:08:12 +0000666 llvm::Value *IsNull =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000667 Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
668 "isnull");
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Anders Carlsson60e282c2009-08-16 21:13:42 +0000670 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
671 EmitBlock(DeleteNotNull);
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Anders Carlsson60e282c2009-08-16 21:13:42 +0000673 // Call the destructor if necessary.
674 if (const RecordType *RT = DeleteTy->getAs<RecordType>()) {
675 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
676 if (!RD->hasTrivialDestructor()) {
677 const CXXDestructorDecl *Dtor = RD->getDestructor(getContext());
678 if (Dtor->isVirtual()) {
679 ErrorUnsupported(E, "delete expression with virtual destructor");
680 return;
681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Anders Carlsson60e282c2009-08-16 21:13:42 +0000683 EmitCXXDestructorCall(Dtor, Dtor_Complete, Ptr);
684 }
685 }
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Anders Carlsson60e282c2009-08-16 21:13:42 +0000688 // Call delete.
689 FunctionDecl *DeleteFD = E->getOperatorDelete();
Mike Stump1eb44332009-09-09 15:08:12 +0000690 const FunctionProtoType *DeleteFTy =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000691 DeleteFD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Anders Carlsson60e282c2009-08-16 21:13:42 +0000693 CallArgList DeleteArgs;
694
695 QualType ArgTy = DeleteFTy->getArgType(0);
696 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
697 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Anders Carlsson60e282c2009-08-16 21:13:42 +0000699 // Emit the call to delete.
Mike Stump1eb44332009-09-09 15:08:12 +0000700 EmitCall(CGM.getTypes().getFunctionInfo(DeleteFTy->getResultType(),
Anders Carlsson60e282c2009-08-16 21:13:42 +0000701 DeleteArgs),
702 CGM.GetAddrOfFunction(GlobalDecl(DeleteFD)),
703 DeleteArgs, DeleteFD);
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Anders Carlsson60e282c2009-08-16 21:13:42 +0000705 EmitBlock(DeleteEnd);
706}
707
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000708void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000709 EmitGlobal(GlobalDecl(D, Ctor_Complete));
710 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000711}
Anders Carlsson363c1842009-04-16 23:57:24 +0000712
Mike Stump1eb44332009-09-09 15:08:12 +0000713void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000714 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Anders Carlsson27ae5362009-04-17 01:58:57 +0000716 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Anders Carlsson27ae5362009-04-17 01:58:57 +0000718 CodeGenFunction(*this).GenerateCode(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Anders Carlsson27ae5362009-04-17 01:58:57 +0000720 SetFunctionDefinitionAttributes(D, Fn);
721 SetLLVMFunctionAttributesForDefinition(D, Fn);
722}
723
Anders Carlsson363c1842009-04-16 23:57:24 +0000724llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000725CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000726 CXXCtorType Type) {
727 const llvm::FunctionType *FTy =
728 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Anders Carlsson363c1842009-04-16 23:57:24 +0000730 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000731 return cast<llvm::Function>(
732 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000733}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000734
Mike Stump1eb44332009-09-09 15:08:12 +0000735const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000736 CXXCtorType Type) {
737 llvm::SmallString<256> Name;
738 llvm::raw_svector_ostream Out(Name);
739 mangleCXXCtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Anders Carlsson27ae5362009-04-17 01:58:57 +0000741 Name += '\0';
742 return UniqueMangledName(Name.begin(), Name.end());
743}
744
745void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000746 EmitCXXDestructor(D, Dtor_Complete);
747 EmitCXXDestructor(D, Dtor_Base);
748}
749
Mike Stump1eb44332009-09-09 15:08:12 +0000750void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000751 CXXDtorType Type) {
752 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Anders Carlsson27ae5362009-04-17 01:58:57 +0000754 CodeGenFunction(*this).GenerateCode(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Anders Carlsson27ae5362009-04-17 01:58:57 +0000756 SetFunctionDefinitionAttributes(D, Fn);
757 SetLLVMFunctionAttributesForDefinition(D, Fn);
758}
759
760llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000761CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000762 CXXDtorType Type) {
763 const llvm::FunctionType *FTy =
764 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Anders Carlsson27ae5362009-04-17 01:58:57 +0000766 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000767 return cast<llvm::Function>(
768 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000769}
770
Mike Stump1eb44332009-09-09 15:08:12 +0000771const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000772 CXXDtorType Type) {
773 llvm::SmallString<256> Name;
774 llvm::raw_svector_ostream Out(Name);
775 mangleCXXDtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Anders Carlsson27ae5362009-04-17 01:58:57 +0000777 Name += '\0';
778 return UniqueMangledName(Name.begin(), Name.end());
779}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000780
Mike Stump32f37012009-08-18 21:49:00 +0000781llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000782 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000783 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000784 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000785
786 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000787 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000788
789 llvm::SmallString<256> OutName;
790 llvm::raw_svector_ostream Out(OutName);
791 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000792 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000793 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000794 llvm::GlobalVariable::LinkageTypes linktype;
795 linktype = llvm::GlobalValue::WeakAnyLinkage;
796 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000797 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000798 // FIXME: descriptor
799 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000800 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000801 // FIXME: TS
802 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
803
804 llvm::Constant *C;
805 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
806 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000807 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000808 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000809 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
810 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000811}
812
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000813class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000814public:
815 /// Index_t - Vtable index type.
816 typedef uint64_t Index_t;
817private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000818 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000819 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000820 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000821 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000822 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000823 /// BLayout - Layout for the most derived class that this vtable is being
824 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000825 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000826 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000827 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000828 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000829 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000830 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000831 /// Index - Maps a method decl into a vtable index. Useful for virtual
832 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000833 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000834 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
835 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump77ca8f62009-09-05 07:20:32 +0000836 typedef llvm::DenseMap<const CXXMethodDecl *,
837 std::pair<Index_t, Index_t> > Thunks_t;
838 Thunks_t Thunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000839 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000840 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000841 // FIXME: Linkage should follow vtable
842 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000843 const uint32_t LLVMPointerWidth;
844 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000845public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000846 VtableBuilder(std::vector<llvm::Constant *> &meth,
847 const CXXRecordDecl *c,
848 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000849 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
850 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000851 CGM(cgm), Extern(true),
852 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000853 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
854 }
Mike Stump32f37012009-08-18 21:49:00 +0000855
Mike Stumpf0070db2009-08-26 20:46:33 +0000856 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000857
Mike Stump15a24e02009-08-28 23:22:54 +0000858 llvm::Constant *wrap(Index_t i) {
859 llvm::Constant *m;
860 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
861 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000862 }
863
Mike Stump15a24e02009-08-28 23:22:54 +0000864 llvm::Constant *wrap(llvm::Constant *m) {
865 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000866 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000867
Mike Stump7fa0d932009-08-20 02:11:48 +0000868 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000869 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump7fa0d932009-08-20 02:11:48 +0000870 for (CXXRecordDecl::base_class_const_iterator i =RD->bases_begin(),
871 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000872 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000873 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
874 if (i->isVirtual() && !SeenVBase.count(Base)) {
875 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000876 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000877 llvm::Constant *m = wrap(BaseOffset);
878 m = wrap((0?700:0) + BaseOffset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000879 offsets.push_back(m);
880 }
Mike Stumpb9837442009-08-20 07:22:17 +0000881 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000882 }
883 }
884
Mike Stumpb9871a22009-08-21 01:45:00 +0000885 void StartNewTable() {
886 SeenVBase.clear();
887 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000888
Mike Stump35191b62009-09-01 22:20:28 +0000889 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000890 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000891 typedef CXXMethodDecl::method_iterator meth_iter;
892
Mike Stumpb9871a22009-08-21 01:45:00 +0000893 // FIXME: Don't like the nested loops. For very large inheritance
894 // heirarchies we could have a table on the side with the final overridder
895 // and just replace each instance of an overridden method once. Would be
896 // nice to measure the cost/benefit on real code.
897
Mike Stumpb9871a22009-08-21 01:45:00 +0000898 for (meth_iter mi = MD->begin_overridden_methods(),
899 e = MD->end_overridden_methods();
900 mi != e; ++mi) {
901 const CXXMethodDecl *OMD = *mi;
902 llvm::Constant *om;
903 om = CGM.GetAddrOfFunction(GlobalDecl(OMD), Ptr8Ty);
904 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
905
Mike Stumpdec025b2009-09-07 04:27:52 +0000906 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000907 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000908 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000909 if (submethods[i] != om)
910 continue;
Mike Stumpdec025b2009-09-07 04:27:52 +0000911 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000912 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000913
914 Thunks.erase(OMD);
915 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000916 Index_t &idx = VCall[OMD];
917 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000918 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000919 idx = VCalls.size()+1;
920 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000921 } else {
922 VCallOffset[MD] = VCallOffset[OMD];
923 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000924 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000925 VCall[MD] = idx;
926 // FIXME: 0?
927 Thunks[MD] = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
Mike Stump35191b62009-09-01 22:20:28 +0000928 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000929 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000930#if 0
931 // FIXME: finish off
932 int64_t O = VCallOffset[OMD] - Offset/8;
933 if (O) {
934 Thunks[MD] = std::make_pair(O, 0);
935 }
936#endif
937 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000938 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000939 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000940
Mike Stump35191b62009-09-01 22:20:28 +0000941 return false;
942 }
943
Mike Stump98cc7102009-09-05 11:28:33 +0000944 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000945 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
946 i != e; ++i) {
947 const CXXMethodDecl *MD = i->first;
948 Index_t idx = Index[MD];
949 Index_t nv_O = i->second.first;
950 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000951 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000952 }
953 Thunks.clear();
954 }
955
Mike Stumpdec025b2009-09-07 04:27:52 +0000956 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
957 int64_t> > *Path, bool MorallyVirtual) {
958 for (std::vector<std::pair<const CXXRecordDecl *,
959 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000960 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000961 const CXXRecordDecl *RD = i->first;
962 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000963 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
964 ++mi)
965 if (mi->isVirtual()) {
966 const CXXMethodDecl *MD = *mi;
967 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(GlobalDecl(MD),
968 Ptr8Ty));
Mike Stumpdec025b2009-09-07 04:27:52 +0000969 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000970 }
971 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000972 }
973
Mike Stump6d10eb82009-09-05 07:49:12 +0000974 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000975 GlobalDecl GD;
976 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
977 GD = GlobalDecl(Dtor, Dtor_Complete);
978 else
979 GD = GlobalDecl(MD);
980
981 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(GD, Ptr8Ty));
Mike Stump77ca8f62009-09-05 07:20:32 +0000982 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000983 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000984 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Mike Stumpb9871a22009-08-21 01:45:00 +0000986 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000987 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000988 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000989 if (MorallyVirtual) {
990 VCallOffset[MD] = Offset/8;
991 Index_t &idx = VCall[MD];
992 // Allocate the first one, after that, we reuse the previous one.
993 if (idx == 0) {
994 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000995 VCalls.push_back(0);
996 }
997 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000998 }
999
Mike Stump6d10eb82009-09-05 07:49:12 +00001000 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
1001 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +00001002 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
1003 ++mi)
1004 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +00001005 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +00001006 }
Mike Stump65defe32009-08-18 21:03:28 +00001007
Mike Stump77ca8f62009-09-05 07:20:32 +00001008 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
1009 const CXXRecordDecl *PrimaryBase,
1010 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1011 int64_t Offset) {
1012 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1013 e = RD->bases_end(); i != e; ++i) {
1014 if (i->isVirtual())
1015 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001016 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +00001017 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1018 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
1019 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
1020 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +00001021 std::vector<std::pair<const CXXRecordDecl *,
1022 int64_t> > S;
1023 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +00001024 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +00001025 }
1026 }
1027 }
1028
Mike Stump6d10eb82009-09-05 07:49:12 +00001029 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
1030 const ASTRecordLayout &Layout,
1031 const CXXRecordDecl *PrimaryBase,
1032 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1033 int64_t Offset, bool ForVirtualBase) {
1034 StartNewTable();
1035 extra = 0;
1036 // FIXME: Cleanup.
1037 if (!ForVirtualBase) {
1038 // then virtual base offsets...
1039 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1040 e = offsets.rend(); i != e; ++i)
1041 methods.push_back(*i);
1042 }
1043
1044 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +00001045 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
1046 e=VCalls.rend();
1047 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +00001048 methods.push_back(wrap((0?600:0) + *i));
1049 VCalls.clear();
1050
1051 if (ForVirtualBase) {
1052 // then virtual base offsets...
1053 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1054 e = offsets.rend(); i != e; ++i)
1055 methods.push_back(*i);
1056 }
1057
1058 methods.push_back(wrap(-(Offset/8)));
1059 methods.push_back(rtti);
1060 Index_t AddressPoint = methods.size();
1061
Mike Stump98cc7102009-09-05 11:28:33 +00001062 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +00001063 methods.insert(methods.end(), submethods.begin(), submethods.end());
1064 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +00001065
1066 // and then the non-virtual bases.
1067 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1068 MorallyVirtual, Offset);
1069 return AddressPoint;
1070 }
1071
Mike Stump078d7782009-09-05 08:40:18 +00001072 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +00001073 if (!RD->isDynamicClass())
1074 return;
1075
1076 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001077 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +00001078 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1079
Mike Stump9bbe9622009-09-05 08:37:03 +00001080 // vtables are composed from the chain of primaries.
1081 if (PrimaryBase) {
1082 if (PrimaryBaseWasVirtual)
1083 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001084 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +00001085 }
1086
1087 // And add the virtuals for the class to the primary vtable.
1088 AddMethods(RD, MorallyVirtual, Offset);
1089 }
1090
Mike Stumpe45c90f2009-09-05 09:10:58 +00001091 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +00001092 bool MorallyVirtual = false, int64_t Offset = 0,
1093 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +00001094 std::vector<std::pair<const CXXRecordDecl *,
1095 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +00001096 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +00001097 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +00001098
1099 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001100 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +00001101 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1102
Mike Stump15a24e02009-08-28 23:22:54 +00001103 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +00001104 extra = 0;
1105 GenerateVBaseOffsets(offsets, RD, Offset);
1106 if (ForVirtualBase)
1107 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +00001108
1109 // vtables are composed from the chain of primaries.
1110 if (PrimaryBase) {
1111 if (PrimaryBaseWasVirtual)
1112 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001113 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +00001114 }
1115
Mike Stump15a24e02009-08-28 23:22:54 +00001116 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +00001117 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +00001118
Mike Stump98cc7102009-09-05 11:28:33 +00001119 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +00001120 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +00001121
Mike Stump6d10eb82009-09-05 07:49:12 +00001122 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1123 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +00001124 }
1125
Mike Stump98cc7102009-09-05 11:28:33 +00001126 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +00001127 int64_t Offset = 0,
1128 std::vector<std::pair<const CXXRecordDecl *,
1129 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +00001130 bool alloc = false;
1131 if (Path == 0) {
1132 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +00001133 Path = new std::vector<std::pair<const CXXRecordDecl *,
1134 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +00001135 }
1136 // FIXME: We also need to override using all paths to a virtual base,
1137 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +00001138 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +00001139 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1140 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00001141 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +00001142 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1143 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1144 // Mark it so we don't output it twice.
1145 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +00001146 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +00001147 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +00001148 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +00001149 }
Mike Stumpdec025b2009-09-07 04:27:52 +00001150 int64_t BaseOffset = Offset;
1151 if (i->isVirtual())
1152 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +00001153 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +00001154 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +00001155 }
Mike Stump98cc7102009-09-05 11:28:33 +00001156 Path->pop_back();
1157 if (alloc)
1158 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +00001159 }
Mike Stump109b13d2009-08-18 21:30:21 +00001160};
Mike Stump8a12b562009-08-06 15:50:11 +00001161
Mike Stumpf0070db2009-08-26 20:46:33 +00001162class VtableInfo {
1163public:
1164 typedef VtableBuilder::Index_t Index_t;
1165private:
1166 CodeGenModule &CGM; // Per-module state.
1167 /// Index_t - Vtable index type.
1168 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
1169 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
1170 // FIXME: Move to Context.
1171 static MapTy IndexFor;
1172public:
1173 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
1174 void register_index(const CXXRecordDecl *RD, const ElTy &e) {
1175 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
1176 // We own a copy of this, it will go away shortly.
1177 new ElTy (e);
1178 IndexFor[RD] = new ElTy (e);
1179 }
1180 Index_t lookup(const CXXMethodDecl *MD) {
1181 const CXXRecordDecl *RD = MD->getParent();
1182 MapTy::iterator I = IndexFor.find(RD);
1183 if (I == IndexFor.end()) {
1184 std::vector<llvm::Constant *> methods;
1185 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001186 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001187 b.GenerateVtableForVBases(RD);
Mike Stumpf0070db2009-08-26 20:46:33 +00001188 register_index(RD, b.getIndex());
1189 I = IndexFor.find(RD);
1190 }
1191 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1192 return (*I->second)[MD];
1193 }
1194};
1195
1196// FIXME: Move to Context.
1197VtableInfo::MapTy VtableInfo::IndexFor;
1198
Mike Stumpf1216772009-07-31 18:25:34 +00001199llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001200 llvm::SmallString<256> OutName;
1201 llvm::raw_svector_ostream Out(OutName);
1202 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001203 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001204 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001205 llvm::GlobalVariable::LinkageTypes linktype;
1206 linktype = llvm::GlobalValue::WeakAnyLinkage;
1207 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001208 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001209 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001210
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001211 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001212
Mike Stump276b9f12009-08-16 01:46:26 +00001213 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001214 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001215
Mike Stump276b9f12009-08-16 01:46:26 +00001216 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001217 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001218
Mike Stump82b56962009-07-31 21:43:43 +00001219 llvm::Constant *C;
1220 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1221 C = llvm::ConstantArray::get(type, methods);
1222 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001223 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001224 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001225 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001226 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001227 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001228 return vtable;
1229}
1230
Mike Stumpf0070db2009-08-26 20:46:33 +00001231// FIXME: move to Context
1232static VtableInfo *vtableinfo;
1233
Mike Stumped032eb2009-09-04 18:27:16 +00001234llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1235 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001236 bool Extern, int64_t nv,
1237 int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001238 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1239
1240 FunctionArgList Args;
1241 ImplicitParamDecl *ThisDecl =
1242 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1243 MD->getThisType(getContext()));
1244 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1245 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1246 e = MD->param_end();
1247 i != e; ++i) {
1248 ParmVarDecl *D = *i;
1249 Args.push_back(std::make_pair(D, D->getType()));
1250 }
1251 IdentifierInfo *II
1252 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1253 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1254 getContext().getTranslationUnitDecl(),
1255 SourceLocation(), II, R, 0,
1256 Extern
1257 ? FunctionDecl::Extern
1258 : FunctionDecl::Static,
1259 false, true);
1260 StartFunction(FD, R, Fn, Args, SourceLocation());
1261 // FIXME: generate body
1262 FinishFunction();
1263 return Fn;
1264}
1265
Mike Stump77ca8f62009-09-05 07:20:32 +00001266llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1267 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001268 llvm::SmallString<256> OutName;
1269 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001270 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001271 llvm::GlobalVariable::LinkageTypes linktype;
1272 linktype = llvm::GlobalValue::WeakAnyLinkage;
1273 if (!Extern)
1274 linktype = llvm::GlobalValue::InternalLinkage;
1275 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1276 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1277 const llvm::FunctionType *FTy =
1278 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1279 FPT->isVariadic());
1280
1281 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1282 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001283 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001284 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1285 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1286 return m;
1287}
1288
Mike Stumpf0070db2009-08-26 20:46:33 +00001289llvm::Value *
1290CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1291 const llvm::Type *Ty) {
1292 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Mike Stumpf0070db2009-08-26 20:46:33 +00001294 // FIXME: move to Context
1295 if (vtableinfo == 0)
1296 vtableinfo = new VtableInfo(CGM);
1297
1298 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1299
1300 Ty = llvm::PointerType::get(Ty, 0);
1301 Ty = llvm::PointerType::get(Ty, 0);
1302 Ty = llvm::PointerType::get(Ty, 0);
1303 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1304 vtbl = Builder.CreateLoad(vtbl);
1305 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1306 Idx, "vfn");
1307 vfn = Builder.CreateLoad(vfn);
1308 return vfn;
1309}
1310
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001311/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1312/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1313/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001314// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001315void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001316 llvm::Value *Src,
1317 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001318 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001319 QualType Ty) {
1320 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1321 assert(CA && "VLA cannot be copied over");
1322 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001324 // Create a temporary for the loop index and initialize it with 0.
1325 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1326 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001327 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001328 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1329 Builder.CreateStore(zeroConstant, IndexPtr, false);
1330 // Start the loop with a block that tests the condition.
1331 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1332 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001334 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001336 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1337 // Generate: if (loop-index < number-of-elements fall to the loop body,
1338 // otherwise, go to the block after the for-loop.
1339 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001340 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001341 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1342 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001343 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001344 "isless");
1345 // If the condition is true, execute the body.
1346 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001348 EmitBlock(ForBody);
1349 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1350 // Inside the loop body, emit the constructor call on the array element.
1351 Counter = Builder.CreateLoad(IndexPtr);
1352 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1353 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1354 if (BitwiseCopy)
1355 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001356 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001357 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001358 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001359 Ctor_Complete);
1360 CallArgList CallArgs;
1361 // Push the this (Dest) ptr.
1362 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1363 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001365 // Push the Src ptr.
1366 CallArgs.push_back(std::make_pair(RValue::get(Src),
1367 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001368 QualType ResultType =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001369 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1370 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1371 Callee, CallArgs, BaseCopyCtor);
1372 }
1373 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001375 // Emit the increment of the loop counter.
1376 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1377 Counter = Builder.CreateLoad(IndexPtr);
1378 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1379 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001381 // Finally, branch back up to the condition for the next iteration.
1382 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001384 // Emit the fall-through block.
1385 EmitBlock(AfterFor, true);
1386}
1387
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001388/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001389/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001390/// bitwise assignment or via a copy assignment operator function call.
1391/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001392void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001393 llvm::Value *Src,
1394 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001395 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001396 QualType Ty) {
1397 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1398 assert(CA && "VLA cannot be asssigned");
1399 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001401 // Create a temporary for the loop index and initialize it with 0.
1402 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1403 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001404 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001405 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1406 Builder.CreateStore(zeroConstant, IndexPtr, false);
1407 // Start the loop with a block that tests the condition.
1408 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1409 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001411 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001413 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1414 // Generate: if (loop-index < number-of-elements fall to the loop body,
1415 // otherwise, go to the block after the for-loop.
1416 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001417 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001418 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1419 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001420 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001421 "isless");
1422 // If the condition is true, execute the body.
1423 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001425 EmitBlock(ForBody);
1426 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1427 // Inside the loop body, emit the assignment operator call on array element.
1428 Counter = Builder.CreateLoad(IndexPtr);
1429 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1430 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1431 const CXXMethodDecl *MD = 0;
1432 if (BitwiseAssign)
1433 EmitAggregateCopy(Dest, Src, Ty);
1434 else {
1435 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1436 MD);
1437 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1438 (void)hasCopyAssign;
1439 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1440 const llvm::Type *LTy =
1441 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1442 FPT->isVariadic());
1443 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001445 CallArgList CallArgs;
1446 // Push the this (Dest) ptr.
1447 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1448 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001450 // Push the Src ptr.
1451 CallArgs.push_back(std::make_pair(RValue::get(Src),
1452 MD->getParamDecl(0)->getType()));
Mike Stumped032eb2009-09-04 18:27:16 +00001453 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001454 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1455 Callee, CallArgs, MD);
1456 }
1457 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001459 // Emit the increment of the loop counter.
1460 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1461 Counter = Builder.CreateLoad(IndexPtr);
1462 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1463 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001465 // Finally, branch back up to the condition for the next iteration.
1466 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001468 // Emit the fall-through block.
1469 EmitBlock(AfterFor, true);
1470}
1471
Fariborz Jahanianca283612009-08-07 23:51:33 +00001472/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1473/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001474/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001475void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001476 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001477 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001478 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1479 if (ClassDecl) {
1480 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl);
1481 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ;
1482 }
1483 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1484 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001485 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001486 }
Mike Stump1eb44332009-09-09 15:08:12 +00001487
1488 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001489 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001490 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001491 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001492 CallArgList CallArgs;
1493 // Push the this (Dest) ptr.
1494 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1495 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Fariborz Jahanianca283612009-08-07 23:51:33 +00001497 // Push the Src ptr.
1498 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001499 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001500 QualType ResultType =
Fariborz Jahanianca283612009-08-07 23:51:33 +00001501 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1502 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1503 Callee, CallArgs, BaseCopyCtor);
1504 }
1505}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001506
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001507/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001508/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001509/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001510// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001511void CodeGenFunction::EmitClassCopyAssignment(
1512 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001513 const CXXRecordDecl *ClassDecl,
1514 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001515 QualType Ty) {
1516 if (ClassDecl) {
1517 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl);
1518 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ;
1519 }
1520 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1521 EmitAggregateCopy(Dest, Src, Ty);
1522 return;
1523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001525 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001526 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001527 MD);
1528 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1529 (void)ConstCopyAssignOp;
1530
1531 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +00001532 const llvm::Type *LTy =
1533 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001534 FPT->isVariadic());
1535 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001537 CallArgList CallArgs;
1538 // Push the this (Dest) ptr.
1539 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1540 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001542 // Push the Src ptr.
1543 CallArgs.push_back(std::make_pair(RValue::get(Src),
1544 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001545 QualType ResultType =
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001546 MD->getType()->getAsFunctionType()->getResultType();
1547 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1548 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001549}
1550
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001551/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001552void
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001553CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *CD,
1554 const FunctionDecl *FD,
1555 llvm::Function *Fn,
1556 const FunctionArgList &Args) {
1557 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
1558 EmitCtorPrologue(CD);
1559 FinishFunction();
1560}
1561
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001562/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001563/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001564/// The implicitly-defined copy constructor for class X performs a memberwise
1565/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001566/// of initialization of bases and members in a user-defined constructor
1567/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001568/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001569/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001570/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001571/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001572/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001573/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001574/// Virtual base class subobjects shall be copied only once by the
1575/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001576
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001577void CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *CD,
1578 const FunctionDecl *FD,
1579 llvm::Function *Fn,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001580 const FunctionArgList &Args) {
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001581 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1582 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001583 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
1584 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001586 FunctionArgList::const_iterator i = Args.begin();
1587 const VarDecl *ThisArg = i->first;
1588 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1589 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1590 const VarDecl *SrcArg = (i+1)->first;
1591 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1592 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001594 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1595 Base != ClassDecl->bases_end(); ++Base) {
1596 // FIXME. copy constrution of virtual base NYI
1597 if (Base->isVirtual())
1598 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001600 CXXRecordDecl *BaseClassDecl
1601 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001602 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1603 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001606 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1607 FieldEnd = ClassDecl->field_end();
1608 Field != FieldEnd; ++Field) {
1609 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001610 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001611 getContext().getAsConstantArrayType(FieldType);
1612 if (Array)
1613 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001615 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1616 CXXRecordDecl *FieldClassDecl
1617 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1618 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1619 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001620 if (Array) {
1621 const llvm::Type *BasePtr = ConvertType(FieldType);
1622 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001623 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001624 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001625 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001626 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1627 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1628 FieldClassDecl, FieldType);
1629 }
Mike Stump1eb44332009-09-09 15:08:12 +00001630 else
1631 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001632 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001633 continue;
1634 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001635 // Do a built-in assignment of scalar data members.
1636 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1637 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1638 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1639 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001640 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001641 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001642}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001643
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001644/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001645/// Before the implicitly-declared copy assignment operator for a class is
1646/// implicitly defined, all implicitly- declared copy assignment operators for
1647/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001648/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001649/// The implicitly-defined copy assignment operator for class X performs
1650/// memberwise assignment of its subob- jects. The direct base classes of X are
1651/// assigned first, in the order of their declaration in
1652/// the base-specifier-list, and then the immediate nonstatic data members of X
1653/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001654/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001655/// if the subobject is of class type, the copy assignment operator for the
1656/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001657/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001658///
Mike Stump1eb44332009-09-09 15:08:12 +00001659/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001660/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001661///
Mike Stump1eb44332009-09-09 15:08:12 +00001662/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001663/// used.
1664void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
1665 const FunctionDecl *FD,
1666 llvm::Function *Fn,
1667 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001668
1669 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1670 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1671 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001672 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001674 FunctionArgList::const_iterator i = Args.begin();
1675 const VarDecl *ThisArg = i->first;
1676 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1677 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1678 const VarDecl *SrcArg = (i+1)->first;
1679 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1680 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001682 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1683 Base != ClassDecl->bases_end(); ++Base) {
1684 // FIXME. copy assignment of virtual base NYI
1685 if (Base->isVirtual())
1686 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001688 CXXRecordDecl *BaseClassDecl
1689 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1690 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1691 Base->getType());
1692 }
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001694 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1695 FieldEnd = ClassDecl->field_end();
1696 Field != FieldEnd; ++Field) {
1697 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001698 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001699 getContext().getAsConstantArrayType(FieldType);
1700 if (Array)
1701 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001703 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1704 CXXRecordDecl *FieldClassDecl
1705 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1706 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1707 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001708 if (Array) {
1709 const llvm::Type *BasePtr = ConvertType(FieldType);
1710 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1711 llvm::Value *DestBaseAddrPtr =
1712 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1713 llvm::Value *SrcBaseAddrPtr =
1714 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1715 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1716 FieldClassDecl, FieldType);
1717 }
1718 else
Mike Stump1eb44332009-09-09 15:08:12 +00001719 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001720 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001721 continue;
1722 }
1723 // Do a built-in assignment of scalar data members.
1724 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1725 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1726 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1727 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001728 }
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001730 // return *this;
1731 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001733 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001734}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001735
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001736/// EmitCtorPrologue - This routine generates necessary code to initialize
1737/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001738/// FIXME: This needs to take a CXXCtorType.
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001739void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001740 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001741 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001742 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001744 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001745 E = CD->init_end();
1746 B != E; ++B) {
1747 CXXBaseOrMemberInitializer *Member = (*B);
1748 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001749 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001750 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001751 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001752 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001753 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001754 BaseClassDecl);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001755 EmitCXXConstructorCall(Member->getConstructor(),
1756 Ctor_Complete, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001757 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001758 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001759 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001760 // non-static data member initilaizers.
1761 FieldDecl *Field = Member->getMember();
1762 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001763 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001764 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001765 if (Array)
1766 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Mike Stumpf1216772009-07-31 18:25:34 +00001768 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001769 LValue LHS;
1770 if (FieldType->isReferenceType()) {
1771 // FIXME: This is really ugly; should be refactored somehow
1772 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1773 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
1774 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(),
1775 QualType::GCNone, FieldType.getAddressSpace());
1776 } else {
1777 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1778 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001779 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001780 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001781 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001782 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001783 if (Array) {
1784 const llvm::Type *BasePtr = ConvertType(FieldType);
1785 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001786 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001787 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001788 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001789 Array, BaseAddrPtr);
1790 }
1791 else
1792 EmitCXXConstructorCall(Member->getConstructor(),
1793 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001794 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001795 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001796 continue;
1797 }
1798 else {
1799 // Initializing an anonymous union data member.
1800 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001801 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001802 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001803 FieldType = anonMember->getType();
1804 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001805 }
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001807 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001808 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001809 RValue RHS;
1810 if (FieldType->isReferenceType())
1811 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1812 /*IsInitializer=*/true);
1813 else
1814 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1815 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001816 }
1817 }
Mike Stumpf1216772009-07-31 18:25:34 +00001818
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001819 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001820 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001821 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001822 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001823 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001824 ClassDecl->bases_begin();
1825 Base != ClassDecl->bases_end(); ++Base) {
1826 // FIXME. copy assignment of virtual base NYI
1827 if (Base->isVirtual())
1828 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001830 CXXRecordDecl *BaseClassDecl
1831 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1832 if (BaseClassDecl->hasTrivialConstructor())
1833 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001834 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001835 BaseClassDecl->getDefaultConstructor(getContext())) {
1836 LoadOfThis = LoadCXXThis();
1837 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1838 BaseClassDecl);
1839 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1840 }
1841 }
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001843 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1844 FieldEnd = ClassDecl->field_end();
1845 Field != FieldEnd; ++Field) {
1846 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001847 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001848 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001849 if (Array)
1850 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001851 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1852 continue;
1853 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001854 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001855 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1856 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1857 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001858 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001859 MemberClassDecl->getDefaultConstructor(getContext())) {
1860 LoadOfThis = LoadCXXThis();
1861 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001862 if (Array) {
1863 const llvm::Type *BasePtr = ConvertType(FieldType);
1864 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001865 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001866 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1867 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1868 }
1869 else
Mike Stump1eb44332009-09-09 15:08:12 +00001870 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001871 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001872 }
1873 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Mike Stumpf1216772009-07-31 18:25:34 +00001876 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001877 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001878 if (!LoadOfThis)
1879 LoadOfThis = LoadCXXThis();
1880 llvm::Value *VtableField;
1881 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001882 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001883 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1884 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1885 llvm::Value *vtable = GenerateVtable(ClassDecl);
1886 Builder.CreateStore(vtable, VtableField);
1887 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001888}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001889
1890/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001891/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001892/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001893/// FIXME: This needs to take a CXXDtorType.
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001894void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) {
1895 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001896 assert(!ClassDecl->getNumVBases() &&
1897 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001898 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001900 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1901 *E = DD->destr_end(); B != E; ++B) {
1902 uintptr_t BaseOrMember = (*B);
1903 if (DD->isMemberToDestroy(BaseOrMember)) {
1904 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1905 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001906 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001907 getContext().getAsConstantArrayType(FieldType);
1908 if (Array)
1909 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001910 const RecordType *RT = FieldType->getAs<RecordType>();
1911 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1912 if (FieldClassDecl->hasTrivialDestructor())
1913 continue;
1914 llvm::Value *LoadOfThis = LoadCXXThis();
1915 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001916 if (Array) {
1917 const llvm::Type *BasePtr = ConvertType(FieldType);
1918 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001919 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001920 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001921 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001922 Array, BaseAddrPtr);
1923 }
1924 else
1925 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1926 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001927 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001928 const RecordType *RT =
1929 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1930 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1931 if (BaseClassDecl->hasTrivialDestructor())
1932 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001933 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001934 ClassDecl,BaseClassDecl);
1935 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1936 Dtor_Complete, V);
1937 }
1938 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001939 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1940 return;
1941 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001942 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001943 // reverse order of their construction.
1944 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001946 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1947 FieldEnd = ClassDecl->field_end();
1948 Field != FieldEnd; ++Field) {
1949 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001950 if (getContext().getAsConstantArrayType(FieldType))
1951 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001952 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1953 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1954 if (FieldClassDecl->hasTrivialDestructor())
1955 continue;
1956 DestructedFields.push_back(*Field);
1957 }
1958 }
1959 if (!DestructedFields.empty())
1960 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1961 FieldDecl *Field = DestructedFields[i];
1962 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001963 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001964 getContext().getAsConstantArrayType(FieldType);
1965 if (Array)
1966 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001967 const RecordType *RT = FieldType->getAs<RecordType>();
1968 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1969 llvm::Value *LoadOfThis = LoadCXXThis();
1970 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001971 if (Array) {
1972 const llvm::Type *BasePtr = ConvertType(FieldType);
1973 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001974 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001975 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001976 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001977 Array, BaseAddrPtr);
1978 }
1979 else
1980 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1981 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001982 }
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001984 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1985 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1986 Base != ClassDecl->bases_end(); ++Base) {
1987 // FIXME. copy assignment of virtual base NYI
1988 if (Base->isVirtual())
1989 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001991 CXXRecordDecl *BaseClassDecl
1992 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1993 if (BaseClassDecl->hasTrivialDestructor())
1994 continue;
1995 DestructedBases.push_back(BaseClassDecl);
1996 }
1997 if (DestructedBases.empty())
1998 return;
1999 for (int i = DestructedBases.size() -1; i >= 0; --i) {
2000 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Mike Stump1eb44332009-09-09 15:08:12 +00002001 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002002 ClassDecl,BaseClassDecl);
2003 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2004 Dtor_Complete, V);
2005 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002006}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002007
2008void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *CD,
2009 const FunctionDecl *FD,
2010 llvm::Function *Fn,
2011 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002013 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
2014 assert(!ClassDecl->hasUserDeclaredDestructor() &&
2015 "SynthesizeDefaultDestructor - destructor has user declaration");
2016 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002018 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
2019 EmitDtorEpilogue(CD);
2020 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00002021}