blob: e739a9ce6c9a28cf3b1d79b17228797260496698 [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump1eb44332009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlssone1b29ef2008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000024#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025using namespace clang;
26using namespace CodeGen;
27
Mike Stump1eb44332009-09-09 15:08:12 +000028void
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000029CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor,
30 llvm::Constant *DeclPtr) {
31 // FIXME: This is ABI dependent and we use the Itanium ABI.
Mike Stump1eb44332009-09-09 15:08:12 +000032
33 const llvm::Type *Int8PtrTy =
Owen Anderson0032b272009-08-13 21:57:51 +000034 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Mike Stump1eb44332009-09-09 15:08:12 +000035
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000036 std::vector<const llvm::Type *> Params;
37 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000038
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000039 // Get the destructor function type
Mike Stump1eb44332009-09-09 15:08:12 +000040 const llvm::Type *DtorFnTy =
Owen Anderson0032b272009-08-13 21:57:51 +000041 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000042 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump1eb44332009-09-09 15:08:12 +000043
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000044 Params.clear();
45 Params.push_back(DtorFnTy);
46 Params.push_back(Int8PtrTy);
47 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000048
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000049 // Get the __cxa_atexit function type
50 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump1eb44332009-09-09 15:08:12 +000051 const llvm::FunctionType *AtExitFnTy =
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000052 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump1eb44332009-09-09 15:08:12 +000053
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000054 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
55 "__cxa_atexit");
Mike Stump1eb44332009-09-09 15:08:12 +000056
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000057 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
58 "__dso_handle");
Mike Stump1eb44332009-09-09 15:08:12 +000059
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000060 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000062 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
63 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
64 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
65 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
66}
67
Mike Stump1eb44332009-09-09 15:08:12 +000068void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000069 llvm::Constant *DeclPtr) {
70 assert(D.hasGlobalStorage() &&
71 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000072
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000073 const Expr *Init = D.getInit();
74 QualType T = D.getType();
Mike Stump1eb44332009-09-09 15:08:12 +000075
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000076 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000077 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000078 } else if (!hasAggregateLLVMType(T)) {
79 llvm::Value *V = EmitScalarExpr(Init);
80 EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T);
81 } else if (T->isAnyComplexType()) {
82 EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified());
83 } else {
84 EmitAggExpr(Init, DeclPtr, T.isVolatileQualified());
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000086 if (const RecordType *RT = T->getAs<RecordType>()) {
87 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
88 if (!RD->hasTrivialDestructor())
89 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
90 }
91 }
92}
93
Anders Carlsson89ed31d2009-08-08 23:24:23 +000094void
95CodeGenModule::EmitCXXGlobalInitFunc() {
96 if (CXXGlobalInits.empty())
97 return;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Owen Anderson0032b272009-08-13 21:57:51 +000099 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000100 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000102 // Create our global initialization function.
103 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000104 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000105 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
106 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000108 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000109 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000110 CXXGlobalInits.size());
111 AddGlobalCtor(Fn);
112}
113
114void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
115 const VarDecl **Decls,
116 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000117 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000118 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000120 for (unsigned i = 0; i != NumDecls; ++i) {
121 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000123 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
124 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
125 }
126 FinishFunction();
127}
128
Mike Stump1eb44332009-09-09 15:08:12 +0000129void
130CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000131 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000132 // FIXME: This should use __cxa_guard_{acquire,release}?
133
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000134 assert(!getContext().getLangOptions().ThreadsafeStatics &&
135 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000136
Anders Carlsson283a0622009-04-13 18:03:33 +0000137 llvm::SmallString<256> GuardVName;
138 llvm::raw_svector_ostream GuardVOut(GuardVName);
139 mangleGuardVariable(&D, getContext(), GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000141 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000142 llvm::GlobalValue *GuardV =
Owen Anderson0032b272009-08-13 21:57:51 +0000143 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000144 GV->getLinkage(),
Owen Anderson0032b272009-08-13 21:57:51 +0000145 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000146 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000148 // Load the first byte of the guard variable.
Owen Anderson0032b272009-08-13 21:57:51 +0000149 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000150 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000151 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000153 // Compare it against 0.
Owen Anderson0032b272009-08-13 21:57:51 +0000154 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000155 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Daniel Dunbar55e87422008-11-11 02:29:29 +0000157 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000158 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000159
160 // If the guard variable is 0, jump to the initializer code.
161 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000163 EmitBlock(InitBlock);
164
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000165 EmitCXXGlobalVarDeclInit(D, GV);
166
Owen Anderson0032b272009-08-13 21:57:51 +0000167 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000168 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000171}
172
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000173RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
174 llvm::Value *Callee,
175 llvm::Value *This,
176 CallExpr::const_arg_iterator ArgBeg,
177 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000178 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000179 "Trying to emit a member call expr on a static method!");
180
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000181 // A call to a trivial destructor requires no code generation.
182 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
183 if (Destructor->isTrivial())
184 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000186 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000188 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000190 // Push the this ptr.
191 Args.push_back(std::make_pair(RValue::get(This),
192 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000194 // And the rest of the call args
195 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000197 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
198 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
199 Callee, Args, MD);
200}
201
Anders Carlsson774e7c62009-04-03 22:50:24 +0000202RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
203 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
204 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000205
Anders Carlssone9918d22009-04-08 20:31:57 +0000206 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump7116da12009-07-30 21:47:44 +0000207
Mike Stump1eb44332009-09-09 15:08:12 +0000208 const llvm::Type *Ty =
209 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000210 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000211 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Anders Carlsson774e7c62009-04-03 22:50:24 +0000213 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000214 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000215 else {
216 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000217 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000218 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000219
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000220 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000221 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000222 // virtual call mechanism.
Mike Stumpf0070db2009-08-26 20:46:33 +0000223 llvm::Value *Callee;
Douglas Gregor0979c802009-08-31 21:41:48 +0000224 if (MD->isVirtual() && !ME->hasQualifier())
Mike Stumpf0070db2009-08-26 20:46:33 +0000225 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000226 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000227 = dyn_cast<CXXDestructorDecl>(MD))
228 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000229 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000230 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000231
232 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000233 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000234}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000235
Mike Stump1eb44332009-09-09 15:08:12 +0000236RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000237CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
238 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000239 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000240 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Fariborz Jahanianad258832009-08-13 21:09:41 +0000242 if (MD->isCopyAssignment()) {
243 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
244 if (ClassDecl->hasTrivialCopyAssignment()) {
245 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
246 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
247 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
248 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
249 QualType Ty = E->getType();
250 EmitAggregateCopy(This, Src, Ty);
251 return RValue::get(This);
252 }
253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Anders Carlsson0f294632009-05-27 04:18:27 +0000255 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000256 const llvm::Type *Ty =
257 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000258 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000259 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Anders Carlsson0f294632009-05-27 04:18:27 +0000261 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anders Carlsson0f294632009-05-27 04:18:27 +0000263 return EmitCXXMemberCall(MD, Callee, This,
264 E->arg_begin() + 1, E->arg_end());
265}
266
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000267llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000268 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000269 "Must be in a C++ member function decl to load 'this'");
270 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
271 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000273 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000274 // ans: See how CodeGenFunction::LoadObjCSelf() uses
275 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000276 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
277}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000278
Fariborz 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),
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000579 CGM.GetAddrOfFunction(NewFD), NewArgs, NewFD);
Anders Carlssoned4e3672009-05-31 20:21:44 +0000580
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000581 // If an allocation function is declared with an empty exception specification
582 // it returns null to indicate failure to allocate storage. [expr.new]p13.
583 // (We don't need to check for null when there's no new initializer and
584 // we're allocating a POD type).
585 bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() &&
586 !(AllocType->isPODType() && !E->hasInitializer());
Anders Carlssoned4e3672009-05-31 20:21:44 +0000587
Anders Carlssonf1108532009-06-01 00:05:16 +0000588 llvm::BasicBlock *NewNull = 0;
589 llvm::BasicBlock *NewNotNull = 0;
590 llvm::BasicBlock *NewEnd = 0;
591
592 llvm::Value *NewPtr = RV.getScalarVal();
593
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000594 if (NullCheckResult) {
Anders Carlssonf1108532009-06-01 00:05:16 +0000595 NewNull = createBasicBlock("new.null");
596 NewNotNull = createBasicBlock("new.notnull");
597 NewEnd = createBasicBlock("new.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000598
599 llvm::Value *IsNull =
600 Builder.CreateICmpEQ(NewPtr,
Owen Andersonc9c88b42009-07-31 20:28:54 +0000601 llvm::Constant::getNullValue(NewPtr->getType()),
Anders Carlssonf1108532009-06-01 00:05:16 +0000602 "isnull");
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Anders Carlssonf1108532009-06-01 00:05:16 +0000604 Builder.CreateCondBr(IsNull, NewNull, NewNotNull);
605 EmitBlock(NewNotNull);
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Anders Carlssonf1108532009-06-01 00:05:16 +0000608 NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000610 if (AllocType->isPODType()) {
Anders Carlsson215bd202009-06-01 00:26:14 +0000611 if (E->getNumConstructorArgs() > 0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000612 assert(E->getNumConstructorArgs() == 1 &&
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000613 "Can only have one argument to initializer of POD type.");
614
615 const Expr *Init = E->getConstructorArg(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000616
617 if (!hasAggregateLLVMType(AllocType))
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000618 Builder.CreateStore(EmitScalarExpr(Init), NewPtr);
Anders Carlsson3923e952009-05-31 21:07:58 +0000619 else if (AllocType->isAnyComplexType())
620 EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified());
Anders Carlsson627a3e52009-05-31 21:12:26 +0000621 else
622 EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified());
Anders Carlsson6d0ffad2009-05-31 20:56:36 +0000623 }
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000624 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000625 // Call the constructor.
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000626 CXXConstructorDecl *Ctor = E->getConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000627
628 EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr,
629 E->constructor_arg_begin(),
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000630 E->constructor_arg_end());
Anders Carlssoned4e3672009-05-31 20:21:44 +0000631 }
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000632
Anders Carlssonf1108532009-06-01 00:05:16 +0000633 if (NullCheckResult) {
634 Builder.CreateBr(NewEnd);
635 EmitBlock(NewNull);
636 Builder.CreateBr(NewEnd);
637 EmitBlock(NewEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Anders Carlssonf1108532009-06-01 00:05:16 +0000639 llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType());
640 PHI->reserveOperandSpace(2);
641 PHI->addIncoming(NewPtr, NewNotNull);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000642 PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Anders Carlssonf1108532009-06-01 00:05:16 +0000644 NewPtr = PHI;
645 }
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Anders Carlssond3fd6ba2009-05-31 21:53:59 +0000647 return NewPtr;
Anders Carlssona00703d2009-05-31 01:40:14 +0000648}
649
Anders Carlsson60e282c2009-08-16 21:13:42 +0000650void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
651 if (E->isArrayForm()) {
652 ErrorUnsupported(E, "delete[] expression");
653 return;
654 };
655
Mike Stump1eb44332009-09-09 15:08:12 +0000656 QualType DeleteTy =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000657 E->getArgument()->getType()->getAs<PointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Anders Carlsson60e282c2009-08-16 21:13:42 +0000659 llvm::Value *Ptr = EmitScalarExpr(E->getArgument());
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Anders Carlsson60e282c2009-08-16 21:13:42 +0000661 // Null check the pointer.
662 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
663 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
664
Mike Stump1eb44332009-09-09 15:08:12 +0000665 llvm::Value *IsNull =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000666 Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()),
667 "isnull");
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Anders Carlsson60e282c2009-08-16 21:13:42 +0000669 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
670 EmitBlock(DeleteNotNull);
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Anders Carlsson60e282c2009-08-16 21:13:42 +0000672 // Call the destructor if necessary.
673 if (const RecordType *RT = DeleteTy->getAs<RecordType>()) {
674 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
675 if (!RD->hasTrivialDestructor()) {
676 const CXXDestructorDecl *Dtor = RD->getDestructor(getContext());
677 if (Dtor->isVirtual()) {
678 ErrorUnsupported(E, "delete expression with virtual destructor");
679 return;
680 }
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Anders Carlsson60e282c2009-08-16 21:13:42 +0000682 EmitCXXDestructorCall(Dtor, Dtor_Complete, Ptr);
683 }
684 }
685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Anders Carlsson60e282c2009-08-16 21:13:42 +0000687 // Call delete.
688 FunctionDecl *DeleteFD = E->getOperatorDelete();
Mike Stump1eb44332009-09-09 15:08:12 +0000689 const FunctionProtoType *DeleteFTy =
Anders Carlsson60e282c2009-08-16 21:13:42 +0000690 DeleteFD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Anders Carlsson60e282c2009-08-16 21:13:42 +0000692 CallArgList DeleteArgs;
693
694 QualType ArgTy = DeleteFTy->getArgType(0);
695 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
696 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Anders Carlsson60e282c2009-08-16 21:13:42 +0000698 // Emit the call to delete.
Mike Stump1eb44332009-09-09 15:08:12 +0000699 EmitCall(CGM.getTypes().getFunctionInfo(DeleteFTy->getResultType(),
Anders Carlsson60e282c2009-08-16 21:13:42 +0000700 DeleteArgs),
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000701 CGM.GetAddrOfFunction(DeleteFD),
Anders Carlsson60e282c2009-08-16 21:13:42 +0000702 DeleteArgs, DeleteFD);
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Anders Carlsson60e282c2009-08-16 21:13:42 +0000704 EmitBlock(DeleteEnd);
705}
706
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000707void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000708 EmitGlobal(GlobalDecl(D, Ctor_Complete));
709 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000710}
Anders Carlsson363c1842009-04-16 23:57:24 +0000711
Mike Stump1eb44332009-09-09 15:08:12 +0000712void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000713 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Anders Carlsson27ae5362009-04-17 01:58:57 +0000715 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000717 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Anders Carlsson27ae5362009-04-17 01:58:57 +0000719 SetFunctionDefinitionAttributes(D, Fn);
720 SetLLVMFunctionAttributesForDefinition(D, Fn);
721}
722
Anders Carlsson363c1842009-04-16 23:57:24 +0000723llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000724CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000725 CXXCtorType Type) {
726 const llvm::FunctionType *FTy =
727 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Anders Carlsson363c1842009-04-16 23:57:24 +0000729 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000730 return cast<llvm::Function>(
731 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000732}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000733
Mike Stump1eb44332009-09-09 15:08:12 +0000734const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000735 CXXCtorType Type) {
736 llvm::SmallString<256> Name;
737 llvm::raw_svector_ostream Out(Name);
738 mangleCXXCtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Anders Carlsson27ae5362009-04-17 01:58:57 +0000740 Name += '\0';
741 return UniqueMangledName(Name.begin(), Name.end());
742}
743
744void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000745 EmitCXXDestructor(D, Dtor_Complete);
746 EmitCXXDestructor(D, Dtor_Base);
747}
748
Mike Stump1eb44332009-09-09 15:08:12 +0000749void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000750 CXXDtorType Type) {
751 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000753 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Anders Carlsson27ae5362009-04-17 01:58:57 +0000755 SetFunctionDefinitionAttributes(D, Fn);
756 SetLLVMFunctionAttributesForDefinition(D, Fn);
757}
758
759llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000760CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000761 CXXDtorType Type) {
762 const llvm::FunctionType *FTy =
763 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Anders Carlsson27ae5362009-04-17 01:58:57 +0000765 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000766 return cast<llvm::Function>(
767 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000768}
769
Mike Stump1eb44332009-09-09 15:08:12 +0000770const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000771 CXXDtorType Type) {
772 llvm::SmallString<256> Name;
773 llvm::raw_svector_ostream Out(Name);
774 mangleCXXDtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Anders Carlsson27ae5362009-04-17 01:58:57 +0000776 Name += '\0';
777 return UniqueMangledName(Name.begin(), Name.end());
778}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000779
Mike Stump32f37012009-08-18 21:49:00 +0000780llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000781 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000782 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000783 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000784
785 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000786 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000787
788 llvm::SmallString<256> OutName;
789 llvm::raw_svector_ostream Out(OutName);
790 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000791 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000792 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000793 llvm::GlobalVariable::LinkageTypes linktype;
794 linktype = llvm::GlobalValue::WeakAnyLinkage;
795 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000796 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000797 // FIXME: descriptor
798 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000799 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000800 // FIXME: TS
801 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
802
803 llvm::Constant *C;
804 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
805 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000806 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000807 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000808 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
809 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000810}
811
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000812class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000813public:
814 /// Index_t - Vtable index type.
815 typedef uint64_t Index_t;
816private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000817 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000818 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000819 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000820 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000821 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000822 /// BLayout - Layout for the most derived class that this vtable is being
823 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000824 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000825 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000826 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000827 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000828 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000829 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000830 /// Index - Maps a method decl into a vtable index. Useful for virtual
831 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000832 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000833 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
834 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump77ca8f62009-09-05 07:20:32 +0000835 typedef llvm::DenseMap<const CXXMethodDecl *,
836 std::pair<Index_t, Index_t> > Thunks_t;
837 Thunks_t Thunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000838 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000839 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000840 // FIXME: Linkage should follow vtable
841 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000842 const uint32_t LLVMPointerWidth;
843 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000844public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000845 VtableBuilder(std::vector<llvm::Constant *> &meth,
846 const CXXRecordDecl *c,
847 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000848 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
849 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000850 CGM(cgm), Extern(true),
851 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000852 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
853 }
Mike Stump32f37012009-08-18 21:49:00 +0000854
Mike Stumpf0070db2009-08-26 20:46:33 +0000855 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000856
Mike Stump15a24e02009-08-28 23:22:54 +0000857 llvm::Constant *wrap(Index_t i) {
858 llvm::Constant *m;
859 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
860 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000861 }
862
Mike Stump15a24e02009-08-28 23:22:54 +0000863 llvm::Constant *wrap(llvm::Constant *m) {
864 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000865 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000866
Mike Stump7fa0d932009-08-20 02:11:48 +0000867 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000868 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump7fa0d932009-08-20 02:11:48 +0000869 for (CXXRecordDecl::base_class_const_iterator i =RD->bases_begin(),
870 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000871 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000872 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
873 if (i->isVirtual() && !SeenVBase.count(Base)) {
874 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000875 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000876 llvm::Constant *m = wrap(BaseOffset);
877 m = wrap((0?700:0) + BaseOffset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000878 offsets.push_back(m);
879 }
Mike Stumpb9837442009-08-20 07:22:17 +0000880 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000881 }
882 }
883
Mike Stumpb9871a22009-08-21 01:45:00 +0000884 void StartNewTable() {
885 SeenVBase.clear();
886 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000887
Mike Stump35191b62009-09-01 22:20:28 +0000888 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000889 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000890 typedef CXXMethodDecl::method_iterator meth_iter;
891
Mike Stumpb9871a22009-08-21 01:45:00 +0000892 // FIXME: Don't like the nested loops. For very large inheritance
893 // heirarchies we could have a table on the side with the final overridder
894 // and just replace each instance of an overridden method once. Would be
895 // nice to measure the cost/benefit on real code.
896
Mike Stumpb9871a22009-08-21 01:45:00 +0000897 for (meth_iter mi = MD->begin_overridden_methods(),
898 e = MD->end_overridden_methods();
899 mi != e; ++mi) {
900 const CXXMethodDecl *OMD = *mi;
901 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000902 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000903 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
904
Mike Stumpdec025b2009-09-07 04:27:52 +0000905 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000906 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000907 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000908 if (submethods[i] != om)
909 continue;
Mike Stumpdec025b2009-09-07 04:27:52 +0000910 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000911 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000912
913 Thunks.erase(OMD);
914 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000915 Index_t &idx = VCall[OMD];
916 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000917 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000918 idx = VCalls.size()+1;
919 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000920 } else {
921 VCallOffset[MD] = VCallOffset[OMD];
922 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000923 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000924 VCall[MD] = idx;
925 // FIXME: 0?
926 Thunks[MD] = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
Mike Stump35191b62009-09-01 22:20:28 +0000927 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000928 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000929#if 0
930 // FIXME: finish off
931 int64_t O = VCallOffset[OMD] - Offset/8;
932 if (O) {
933 Thunks[MD] = std::make_pair(O, 0);
934 }
935#endif
936 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000937 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000938 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000939
Mike Stump35191b62009-09-01 22:20:28 +0000940 return false;
941 }
942
Mike Stump98cc7102009-09-05 11:28:33 +0000943 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000944 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
945 i != e; ++i) {
946 const CXXMethodDecl *MD = i->first;
947 Index_t idx = Index[MD];
948 Index_t nv_O = i->second.first;
949 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000950 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000951 }
952 Thunks.clear();
953 }
954
Mike Stumpdec025b2009-09-07 04:27:52 +0000955 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
956 int64_t> > *Path, bool MorallyVirtual) {
957 for (std::vector<std::pair<const CXXRecordDecl *,
958 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000959 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000960 const CXXRecordDecl *RD = i->first;
961 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000962 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
963 ++mi)
964 if (mi->isVirtual()) {
965 const CXXMethodDecl *MD = *mi;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000966 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD, Ptr8Ty));
Mike Stumpdec025b2009-09-07 04:27:52 +0000967 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000968 }
969 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000970 }
971
Mike Stump6d10eb82009-09-05 07:49:12 +0000972 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000973 GlobalDecl GD;
974 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
975 GD = GlobalDecl(Dtor, Dtor_Complete);
976 else
977 GD = GlobalDecl(MD);
978
979 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(GD, Ptr8Ty));
Mike Stump77ca8f62009-09-05 07:20:32 +0000980 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000981 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000982 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Mike Stumpb9871a22009-08-21 01:45:00 +0000984 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000985 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000986 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000987 if (MorallyVirtual) {
988 VCallOffset[MD] = Offset/8;
989 Index_t &idx = VCall[MD];
990 // Allocate the first one, after that, we reuse the previous one.
991 if (idx == 0) {
992 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000993 VCalls.push_back(0);
994 }
995 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000996 }
997
Mike Stump6d10eb82009-09-05 07:49:12 +0000998 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
999 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +00001000 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
1001 ++mi)
1002 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +00001003 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +00001004 }
Mike Stump65defe32009-08-18 21:03:28 +00001005
Mike Stump77ca8f62009-09-05 07:20:32 +00001006 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
1007 const CXXRecordDecl *PrimaryBase,
1008 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1009 int64_t Offset) {
1010 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1011 e = RD->bases_end(); i != e; ++i) {
1012 if (i->isVirtual())
1013 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001014 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +00001015 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1016 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
1017 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
1018 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +00001019 std::vector<std::pair<const CXXRecordDecl *,
1020 int64_t> > S;
1021 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +00001022 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +00001023 }
1024 }
1025 }
1026
Mike Stump6d10eb82009-09-05 07:49:12 +00001027 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
1028 const ASTRecordLayout &Layout,
1029 const CXXRecordDecl *PrimaryBase,
1030 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1031 int64_t Offset, bool ForVirtualBase) {
1032 StartNewTable();
1033 extra = 0;
1034 // FIXME: Cleanup.
1035 if (!ForVirtualBase) {
1036 // then virtual base offsets...
1037 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1038 e = offsets.rend(); i != e; ++i)
1039 methods.push_back(*i);
1040 }
1041
1042 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +00001043 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
1044 e=VCalls.rend();
1045 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +00001046 methods.push_back(wrap((0?600:0) + *i));
1047 VCalls.clear();
1048
1049 if (ForVirtualBase) {
1050 // then virtual base offsets...
1051 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1052 e = offsets.rend(); i != e; ++i)
1053 methods.push_back(*i);
1054 }
1055
1056 methods.push_back(wrap(-(Offset/8)));
1057 methods.push_back(rtti);
1058 Index_t AddressPoint = methods.size();
1059
Mike Stump98cc7102009-09-05 11:28:33 +00001060 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +00001061 methods.insert(methods.end(), submethods.begin(), submethods.end());
1062 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +00001063
1064 // and then the non-virtual bases.
1065 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1066 MorallyVirtual, Offset);
1067 return AddressPoint;
1068 }
1069
Mike Stump078d7782009-09-05 08:40:18 +00001070 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +00001071 if (!RD->isDynamicClass())
1072 return;
1073
1074 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001075 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +00001076 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1077
Mike Stump9bbe9622009-09-05 08:37:03 +00001078 // vtables are composed from the chain of primaries.
1079 if (PrimaryBase) {
1080 if (PrimaryBaseWasVirtual)
1081 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001082 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +00001083 }
1084
1085 // And add the virtuals for the class to the primary vtable.
1086 AddMethods(RD, MorallyVirtual, Offset);
1087 }
1088
Mike Stumpe45c90f2009-09-05 09:10:58 +00001089 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +00001090 bool MorallyVirtual = false, int64_t Offset = 0,
1091 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +00001092 std::vector<std::pair<const CXXRecordDecl *,
1093 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +00001094 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +00001095 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +00001096
1097 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001098 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +00001099 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1100
Mike Stump15a24e02009-08-28 23:22:54 +00001101 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +00001102 extra = 0;
1103 GenerateVBaseOffsets(offsets, RD, Offset);
1104 if (ForVirtualBase)
1105 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +00001106
1107 // vtables are composed from the chain of primaries.
1108 if (PrimaryBase) {
1109 if (PrimaryBaseWasVirtual)
1110 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001111 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +00001112 }
1113
Mike Stump15a24e02009-08-28 23:22:54 +00001114 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +00001115 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +00001116
Mike Stump98cc7102009-09-05 11:28:33 +00001117 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +00001118 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +00001119
Mike Stump6d10eb82009-09-05 07:49:12 +00001120 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1121 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +00001122 }
1123
Mike Stump98cc7102009-09-05 11:28:33 +00001124 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +00001125 int64_t Offset = 0,
1126 std::vector<std::pair<const CXXRecordDecl *,
1127 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +00001128 bool alloc = false;
1129 if (Path == 0) {
1130 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +00001131 Path = new std::vector<std::pair<const CXXRecordDecl *,
1132 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +00001133 }
1134 // FIXME: We also need to override using all paths to a virtual base,
1135 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +00001136 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +00001137 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1138 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00001139 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +00001140 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1141 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1142 // Mark it so we don't output it twice.
1143 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +00001144 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +00001145 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +00001146 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +00001147 }
Mike Stumpdec025b2009-09-07 04:27:52 +00001148 int64_t BaseOffset = Offset;
1149 if (i->isVirtual())
1150 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +00001151 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +00001152 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +00001153 }
Mike Stump98cc7102009-09-05 11:28:33 +00001154 Path->pop_back();
1155 if (alloc)
1156 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +00001157 }
Mike Stump109b13d2009-08-18 21:30:21 +00001158};
Mike Stump8a12b562009-08-06 15:50:11 +00001159
Mike Stumpf0070db2009-08-26 20:46:33 +00001160class VtableInfo {
1161public:
1162 typedef VtableBuilder::Index_t Index_t;
1163private:
1164 CodeGenModule &CGM; // Per-module state.
1165 /// Index_t - Vtable index type.
1166 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
1167 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
1168 // FIXME: Move to Context.
1169 static MapTy IndexFor;
1170public:
1171 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
1172 void register_index(const CXXRecordDecl *RD, const ElTy &e) {
1173 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
1174 // We own a copy of this, it will go away shortly.
1175 new ElTy (e);
1176 IndexFor[RD] = new ElTy (e);
1177 }
1178 Index_t lookup(const CXXMethodDecl *MD) {
1179 const CXXRecordDecl *RD = MD->getParent();
1180 MapTy::iterator I = IndexFor.find(RD);
1181 if (I == IndexFor.end()) {
1182 std::vector<llvm::Constant *> methods;
1183 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001184 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001185 b.GenerateVtableForVBases(RD);
Mike Stumpf0070db2009-08-26 20:46:33 +00001186 register_index(RD, b.getIndex());
1187 I = IndexFor.find(RD);
1188 }
1189 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1190 return (*I->second)[MD];
1191 }
1192};
1193
1194// FIXME: Move to Context.
1195VtableInfo::MapTy VtableInfo::IndexFor;
1196
Mike Stumpf1216772009-07-31 18:25:34 +00001197llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001198 llvm::SmallString<256> OutName;
1199 llvm::raw_svector_ostream Out(OutName);
1200 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001201 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001202 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001203 llvm::GlobalVariable::LinkageTypes linktype;
1204 linktype = llvm::GlobalValue::WeakAnyLinkage;
1205 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001206 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001207 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001208
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001209 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001210
Mike Stump276b9f12009-08-16 01:46:26 +00001211 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001212 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001213
Mike Stump276b9f12009-08-16 01:46:26 +00001214 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001215 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001216
Mike Stump82b56962009-07-31 21:43:43 +00001217 llvm::Constant *C;
1218 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1219 C = llvm::ConstantArray::get(type, methods);
1220 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001221 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001222 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001223 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001224 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001225 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001226 return vtable;
1227}
1228
Mike Stumpf0070db2009-08-26 20:46:33 +00001229// FIXME: move to Context
1230static VtableInfo *vtableinfo;
1231
Mike Stumped032eb2009-09-04 18:27:16 +00001232llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1233 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001234 bool Extern, int64_t nv,
1235 int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001236 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1237
1238 FunctionArgList Args;
1239 ImplicitParamDecl *ThisDecl =
1240 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1241 MD->getThisType(getContext()));
1242 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1243 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1244 e = MD->param_end();
1245 i != e; ++i) {
1246 ParmVarDecl *D = *i;
1247 Args.push_back(std::make_pair(D, D->getType()));
1248 }
1249 IdentifierInfo *II
1250 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1251 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1252 getContext().getTranslationUnitDecl(),
1253 SourceLocation(), II, R, 0,
1254 Extern
1255 ? FunctionDecl::Extern
1256 : FunctionDecl::Static,
1257 false, true);
1258 StartFunction(FD, R, Fn, Args, SourceLocation());
1259 // FIXME: generate body
1260 FinishFunction();
1261 return Fn;
1262}
1263
Mike Stump77ca8f62009-09-05 07:20:32 +00001264llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1265 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001266 llvm::SmallString<256> OutName;
1267 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001268 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001269 llvm::GlobalVariable::LinkageTypes linktype;
1270 linktype = llvm::GlobalValue::WeakAnyLinkage;
1271 if (!Extern)
1272 linktype = llvm::GlobalValue::InternalLinkage;
1273 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1274 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1275 const llvm::FunctionType *FTy =
1276 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1277 FPT->isVariadic());
1278
1279 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1280 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001281 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001282 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1283 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1284 return m;
1285}
1286
Mike Stumpf0070db2009-08-26 20:46:33 +00001287llvm::Value *
1288CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1289 const llvm::Type *Ty) {
1290 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Mike Stumpf0070db2009-08-26 20:46:33 +00001292 // FIXME: move to Context
1293 if (vtableinfo == 0)
1294 vtableinfo = new VtableInfo(CGM);
1295
1296 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1297
1298 Ty = llvm::PointerType::get(Ty, 0);
1299 Ty = llvm::PointerType::get(Ty, 0);
1300 Ty = llvm::PointerType::get(Ty, 0);
1301 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1302 vtbl = Builder.CreateLoad(vtbl);
1303 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1304 Idx, "vfn");
1305 vfn = Builder.CreateLoad(vfn);
1306 return vfn;
1307}
1308
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001309/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1310/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1311/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001312// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001313void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001314 llvm::Value *Src,
1315 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001316 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001317 QualType Ty) {
1318 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1319 assert(CA && "VLA cannot be copied over");
1320 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001322 // Create a temporary for the loop index and initialize it with 0.
1323 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1324 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001325 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001326 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1327 Builder.CreateStore(zeroConstant, IndexPtr, false);
1328 // Start the loop with a block that tests the condition.
1329 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1330 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001332 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001334 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1335 // Generate: if (loop-index < number-of-elements fall to the loop body,
1336 // otherwise, go to the block after the for-loop.
1337 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001338 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001339 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1340 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001341 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001342 "isless");
1343 // If the condition is true, execute the body.
1344 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001346 EmitBlock(ForBody);
1347 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1348 // Inside the loop body, emit the constructor call on the array element.
1349 Counter = Builder.CreateLoad(IndexPtr);
1350 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1351 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1352 if (BitwiseCopy)
1353 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001354 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001355 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001356 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001357 Ctor_Complete);
1358 CallArgList CallArgs;
1359 // Push the this (Dest) ptr.
1360 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1361 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001363 // Push the Src ptr.
1364 CallArgs.push_back(std::make_pair(RValue::get(Src),
1365 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001366 QualType ResultType =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001367 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1368 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1369 Callee, CallArgs, BaseCopyCtor);
1370 }
1371 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001373 // Emit the increment of the loop counter.
1374 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1375 Counter = Builder.CreateLoad(IndexPtr);
1376 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1377 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001379 // Finally, branch back up to the condition for the next iteration.
1380 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001382 // Emit the fall-through block.
1383 EmitBlock(AfterFor, true);
1384}
1385
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001386/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001387/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001388/// bitwise assignment or via a copy assignment operator function call.
1389/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001390void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001391 llvm::Value *Src,
1392 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001393 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001394 QualType Ty) {
1395 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1396 assert(CA && "VLA cannot be asssigned");
1397 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001399 // Create a temporary for the loop index and initialize it with 0.
1400 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1401 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001402 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001403 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1404 Builder.CreateStore(zeroConstant, IndexPtr, false);
1405 // Start the loop with a block that tests the condition.
1406 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1407 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001409 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001411 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1412 // Generate: if (loop-index < number-of-elements fall to the loop body,
1413 // otherwise, go to the block after the for-loop.
1414 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001415 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001416 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1417 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001418 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001419 "isless");
1420 // If the condition is true, execute the body.
1421 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001423 EmitBlock(ForBody);
1424 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1425 // Inside the loop body, emit the assignment operator call on array element.
1426 Counter = Builder.CreateLoad(IndexPtr);
1427 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1428 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1429 const CXXMethodDecl *MD = 0;
1430 if (BitwiseAssign)
1431 EmitAggregateCopy(Dest, Src, Ty);
1432 else {
1433 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1434 MD);
1435 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1436 (void)hasCopyAssign;
1437 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1438 const llvm::Type *LTy =
1439 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1440 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001441 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001443 CallArgList CallArgs;
1444 // Push the this (Dest) ptr.
1445 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1446 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001448 // Push the Src ptr.
1449 CallArgs.push_back(std::make_pair(RValue::get(Src),
1450 MD->getParamDecl(0)->getType()));
Mike Stumped032eb2009-09-04 18:27:16 +00001451 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001452 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1453 Callee, CallArgs, MD);
1454 }
1455 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001457 // Emit the increment of the loop counter.
1458 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1459 Counter = Builder.CreateLoad(IndexPtr);
1460 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1461 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001463 // Finally, branch back up to the condition for the next iteration.
1464 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001466 // Emit the fall-through block.
1467 EmitBlock(AfterFor, true);
1468}
1469
Fariborz Jahanianca283612009-08-07 23:51:33 +00001470/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1471/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001472/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001473void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001474 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001475 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001476 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1477 if (ClassDecl) {
1478 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl);
1479 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ;
1480 }
1481 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1482 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001483 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001484 }
Mike Stump1eb44332009-09-09 15:08:12 +00001485
1486 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001487 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001488 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001489 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001490 CallArgList CallArgs;
1491 // Push the this (Dest) ptr.
1492 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1493 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Fariborz Jahanianca283612009-08-07 23:51:33 +00001495 // Push the Src ptr.
1496 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001497 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001498 QualType ResultType =
Fariborz Jahanianca283612009-08-07 23:51:33 +00001499 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1500 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1501 Callee, CallArgs, BaseCopyCtor);
1502 }
1503}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001504
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001505/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001506/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001507/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001508// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001509void CodeGenFunction::EmitClassCopyAssignment(
1510 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001511 const CXXRecordDecl *ClassDecl,
1512 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001513 QualType Ty) {
1514 if (ClassDecl) {
1515 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl);
1516 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ;
1517 }
1518 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1519 EmitAggregateCopy(Dest, Src, Ty);
1520 return;
1521 }
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001523 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001524 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001525 MD);
1526 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1527 (void)ConstCopyAssignOp;
1528
1529 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +00001530 const llvm::Type *LTy =
1531 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001532 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001533 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001535 CallArgList CallArgs;
1536 // Push the this (Dest) ptr.
1537 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1538 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001540 // Push the Src ptr.
1541 CallArgs.push_back(std::make_pair(RValue::get(Src),
1542 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001543 QualType ResultType =
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001544 MD->getType()->getAsFunctionType()->getResultType();
1545 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1546 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001547}
1548
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001549/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001550void
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001551CodeGenFunction::SynthesizeDefaultConstructor(GlobalDecl GD,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001552 const FunctionDecl *FD,
1553 llvm::Function *Fn,
1554 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001555 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(GD.getDecl());
1556
1557 StartFunction(GD, FD->getResultType(), Fn, Args, SourceLocation());
1558 EmitCtorPrologue(Ctor);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001559 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
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001577void CodeGenFunction::SynthesizeCXXCopyConstructor(GlobalDecl GD,
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001578 const FunctionDecl *FD,
1579 llvm::Function *Fn,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001580 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001581 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(GD.getDecl());
1582 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001583 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001584 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001585 StartFunction(GD, Ctor->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001587 FunctionArgList::const_iterator i = Args.begin();
1588 const VarDecl *ThisArg = i->first;
1589 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1590 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1591 const VarDecl *SrcArg = (i+1)->first;
1592 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1593 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001595 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1596 Base != ClassDecl->bases_end(); ++Base) {
1597 // FIXME. copy constrution of virtual base NYI
1598 if (Base->isVirtual())
1599 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001601 CXXRecordDecl *BaseClassDecl
1602 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001603 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1604 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001605 }
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001607 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1608 FieldEnd = ClassDecl->field_end();
1609 Field != FieldEnd; ++Field) {
1610 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001611 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001612 getContext().getAsConstantArrayType(FieldType);
1613 if (Array)
1614 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001616 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1617 CXXRecordDecl *FieldClassDecl
1618 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1619 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1620 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001621 if (Array) {
1622 const llvm::Type *BasePtr = ConvertType(FieldType);
1623 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001624 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001625 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001626 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001627 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1628 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1629 FieldClassDecl, FieldType);
1630 }
Mike Stump1eb44332009-09-09 15:08:12 +00001631 else
1632 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001633 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001634 continue;
1635 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001636 // Do a built-in assignment of scalar data members.
1637 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1638 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1639 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1640 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001641 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001642 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001643}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001644
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001645/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001646/// Before the implicitly-declared copy assignment operator for a class is
1647/// implicitly defined, all implicitly- declared copy assignment operators for
1648/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001649/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001650/// The implicitly-defined copy assignment operator for class X performs
1651/// memberwise assignment of its subob- jects. The direct base classes of X are
1652/// assigned first, in the order of their declaration in
1653/// the base-specifier-list, and then the immediate nonstatic data members of X
1654/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001655/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001656/// if the subobject is of class type, the copy assignment operator for the
1657/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001658/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001659///
Mike Stump1eb44332009-09-09 15:08:12 +00001660/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001661/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001662///
Mike Stump1eb44332009-09-09 15:08:12 +00001663/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001664/// used.
1665void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
1666 const FunctionDecl *FD,
1667 llvm::Function *Fn,
1668 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001669
1670 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1671 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1672 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001673 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001675 FunctionArgList::const_iterator i = Args.begin();
1676 const VarDecl *ThisArg = i->first;
1677 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1678 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1679 const VarDecl *SrcArg = (i+1)->first;
1680 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1681 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001683 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1684 Base != ClassDecl->bases_end(); ++Base) {
1685 // FIXME. copy assignment of virtual base NYI
1686 if (Base->isVirtual())
1687 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001689 CXXRecordDecl *BaseClassDecl
1690 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1691 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1692 Base->getType());
1693 }
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001695 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1696 FieldEnd = ClassDecl->field_end();
1697 Field != FieldEnd; ++Field) {
1698 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001699 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001700 getContext().getAsConstantArrayType(FieldType);
1701 if (Array)
1702 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001704 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1705 CXXRecordDecl *FieldClassDecl
1706 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1707 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1708 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001709 if (Array) {
1710 const llvm::Type *BasePtr = ConvertType(FieldType);
1711 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1712 llvm::Value *DestBaseAddrPtr =
1713 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1714 llvm::Value *SrcBaseAddrPtr =
1715 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1716 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1717 FieldClassDecl, FieldType);
1718 }
1719 else
Mike Stump1eb44332009-09-09 15:08:12 +00001720 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001721 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001722 continue;
1723 }
1724 // Do a built-in assignment of scalar data members.
1725 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1726 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1727 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1728 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001729 }
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001731 // return *this;
1732 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001734 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001735}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001736
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001737/// EmitCtorPrologue - This routine generates necessary code to initialize
1738/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001739/// FIXME: This needs to take a CXXCtorType.
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001740void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001741 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001742 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001743 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001745 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001746 E = CD->init_end();
1747 B != E; ++B) {
1748 CXXBaseOrMemberInitializer *Member = (*B);
1749 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001750 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001751 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001752 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001753 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001754 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001755 BaseClassDecl);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001756 EmitCXXConstructorCall(Member->getConstructor(),
1757 Ctor_Complete, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001758 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001759 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001760 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001761 // non-static data member initilaizers.
1762 FieldDecl *Field = Member->getMember();
1763 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001764 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001765 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001766 if (Array)
1767 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Mike Stumpf1216772009-07-31 18:25:34 +00001769 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001770 LValue LHS;
1771 if (FieldType->isReferenceType()) {
1772 // FIXME: This is really ugly; should be refactored somehow
1773 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1774 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
1775 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(),
1776 QualType::GCNone, FieldType.getAddressSpace());
1777 } else {
1778 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1779 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001780 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001781 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001782 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001783 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001784 if (Array) {
1785 const llvm::Type *BasePtr = ConvertType(FieldType);
1786 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001787 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001788 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001789 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001790 Array, BaseAddrPtr);
1791 }
1792 else
1793 EmitCXXConstructorCall(Member->getConstructor(),
1794 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001795 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001796 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001797 continue;
1798 }
1799 else {
1800 // Initializing an anonymous union data member.
1801 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001802 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001803 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001804 FieldType = anonMember->getType();
1805 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001806 }
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001808 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001809 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001810 RValue RHS;
1811 if (FieldType->isReferenceType())
1812 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1813 /*IsInitializer=*/true);
1814 else
1815 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1816 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001817 }
1818 }
Mike Stumpf1216772009-07-31 18:25:34 +00001819
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001820 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001821 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001822 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001823 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001824 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001825 ClassDecl->bases_begin();
1826 Base != ClassDecl->bases_end(); ++Base) {
1827 // FIXME. copy assignment of virtual base NYI
1828 if (Base->isVirtual())
1829 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001831 CXXRecordDecl *BaseClassDecl
1832 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1833 if (BaseClassDecl->hasTrivialConstructor())
1834 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001835 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001836 BaseClassDecl->getDefaultConstructor(getContext())) {
1837 LoadOfThis = LoadCXXThis();
1838 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1839 BaseClassDecl);
1840 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1841 }
1842 }
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001844 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1845 FieldEnd = ClassDecl->field_end();
1846 Field != FieldEnd; ++Field) {
1847 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001848 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001849 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001850 if (Array)
1851 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001852 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1853 continue;
1854 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001855 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001856 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1857 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1858 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001859 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001860 MemberClassDecl->getDefaultConstructor(getContext())) {
1861 LoadOfThis = LoadCXXThis();
1862 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001863 if (Array) {
1864 const llvm::Type *BasePtr = ConvertType(FieldType);
1865 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001866 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001867 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1868 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1869 }
1870 else
Mike Stump1eb44332009-09-09 15:08:12 +00001871 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001872 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001873 }
1874 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001875 }
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Mike Stumpf1216772009-07-31 18:25:34 +00001877 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001878 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001879 if (!LoadOfThis)
1880 LoadOfThis = LoadCXXThis();
1881 llvm::Value *VtableField;
1882 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001883 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001884 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1885 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1886 llvm::Value *vtable = GenerateVtable(ClassDecl);
1887 Builder.CreateStore(vtable, VtableField);
1888 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001889}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001890
1891/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001892/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001893/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001894/// FIXME: This needs to take a CXXDtorType.
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001895void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) {
1896 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001897 assert(!ClassDecl->getNumVBases() &&
1898 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001899 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001901 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1902 *E = DD->destr_end(); B != E; ++B) {
1903 uintptr_t BaseOrMember = (*B);
1904 if (DD->isMemberToDestroy(BaseOrMember)) {
1905 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1906 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001907 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001908 getContext().getAsConstantArrayType(FieldType);
1909 if (Array)
1910 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001911 const RecordType *RT = FieldType->getAs<RecordType>();
1912 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1913 if (FieldClassDecl->hasTrivialDestructor())
1914 continue;
1915 llvm::Value *LoadOfThis = LoadCXXThis();
1916 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001917 if (Array) {
1918 const llvm::Type *BasePtr = ConvertType(FieldType);
1919 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001920 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001921 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001922 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001923 Array, BaseAddrPtr);
1924 }
1925 else
1926 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1927 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001928 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001929 const RecordType *RT =
1930 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1931 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1932 if (BaseClassDecl->hasTrivialDestructor())
1933 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001934 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001935 ClassDecl,BaseClassDecl);
1936 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1937 Dtor_Complete, V);
1938 }
1939 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001940 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1941 return;
1942 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001943 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001944 // reverse order of their construction.
1945 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001947 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1948 FieldEnd = ClassDecl->field_end();
1949 Field != FieldEnd; ++Field) {
1950 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001951 if (getContext().getAsConstantArrayType(FieldType))
1952 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001953 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1954 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1955 if (FieldClassDecl->hasTrivialDestructor())
1956 continue;
1957 DestructedFields.push_back(*Field);
1958 }
1959 }
1960 if (!DestructedFields.empty())
1961 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1962 FieldDecl *Field = DestructedFields[i];
1963 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001964 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001965 getContext().getAsConstantArrayType(FieldType);
1966 if (Array)
1967 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001968 const RecordType *RT = FieldType->getAs<RecordType>();
1969 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1970 llvm::Value *LoadOfThis = LoadCXXThis();
1971 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001972 if (Array) {
1973 const llvm::Type *BasePtr = ConvertType(FieldType);
1974 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001975 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001976 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001977 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001978 Array, BaseAddrPtr);
1979 }
1980 else
1981 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1982 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001983 }
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001985 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1986 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1987 Base != ClassDecl->bases_end(); ++Base) {
1988 // FIXME. copy assignment of virtual base NYI
1989 if (Base->isVirtual())
1990 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001992 CXXRecordDecl *BaseClassDecl
1993 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1994 if (BaseClassDecl->hasTrivialDestructor())
1995 continue;
1996 DestructedBases.push_back(BaseClassDecl);
1997 }
1998 if (DestructedBases.empty())
1999 return;
2000 for (int i = DestructedBases.size() -1; i >= 0; --i) {
2001 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Mike Stump1eb44332009-09-09 15:08:12 +00002002 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002003 ClassDecl,BaseClassDecl);
2004 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2005 Dtor_Complete, V);
2006 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002007}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002008
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002009void CodeGenFunction::SynthesizeDefaultDestructor(GlobalDecl GD,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002010 const FunctionDecl *FD,
2011 llvm::Function *Fn,
2012 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002014 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(GD.getDecl());
2015
2016 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002017 assert(!ClassDecl->hasUserDeclaredDestructor() &&
2018 "SynthesizeDefaultDestructor - destructor has user declaration");
2019 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002021 StartFunction(GD, Dtor->getResultType(), Fn, Args, SourceLocation());
2022 EmitDtorEpilogue(Dtor);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002023 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00002024}