blob: 742077f3ff4088f1c9d1d654ddb8df5cec55eadb [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 Stump6e319f62009-09-11 23:25:56 +0000835 typedef std::pair<Index_t, Index_t> CallOffset;
836 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
Mike Stump77ca8f62009-09-05 07:20:32 +0000837 Thunks_t Thunks;
Mike Stump6e319f62009-09-11 23:25:56 +0000838 typedef llvm::DenseMap<const CXXMethodDecl *,
839 std::pair<CallOffset, CallOffset> > CovariantThunks_t;
840 CovariantThunks_t CovariantThunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000841 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000842 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000843 // FIXME: Linkage should follow vtable
844 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000845 const uint32_t LLVMPointerWidth;
846 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000847public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000848 VtableBuilder(std::vector<llvm::Constant *> &meth,
849 const CXXRecordDecl *c,
850 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000851 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
852 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000853 CGM(cgm), Extern(true),
854 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000855 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
856 }
Mike Stump32f37012009-08-18 21:49:00 +0000857
Mike Stumpf0070db2009-08-26 20:46:33 +0000858 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000859
Mike Stump15a24e02009-08-28 23:22:54 +0000860 llvm::Constant *wrap(Index_t i) {
861 llvm::Constant *m;
862 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
863 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000864 }
865
Mike Stump15a24e02009-08-28 23:22:54 +0000866 llvm::Constant *wrap(llvm::Constant *m) {
867 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000868 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000869
Mike Stump7fa0d932009-08-20 02:11:48 +0000870 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000871 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump7fa0d932009-08-20 02:11:48 +0000872 for (CXXRecordDecl::base_class_const_iterator i =RD->bases_begin(),
873 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000874 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000875 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
876 if (i->isVirtual() && !SeenVBase.count(Base)) {
877 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000878 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000879 llvm::Constant *m = wrap(BaseOffset);
880 m = wrap((0?700:0) + BaseOffset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000881 offsets.push_back(m);
882 }
Mike Stumpb9837442009-08-20 07:22:17 +0000883 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000884 }
885 }
886
Mike Stumpb9871a22009-08-21 01:45:00 +0000887 void StartNewTable() {
888 SeenVBase.clear();
889 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000890
Mike Stump35191b62009-09-01 22:20:28 +0000891 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000892 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000893 typedef CXXMethodDecl::method_iterator meth_iter;
894
Mike Stumpb9871a22009-08-21 01:45:00 +0000895 // FIXME: Don't like the nested loops. For very large inheritance
896 // heirarchies we could have a table on the side with the final overridder
897 // and just replace each instance of an overridden method once. Would be
898 // nice to measure the cost/benefit on real code.
899
Mike Stumpb9871a22009-08-21 01:45:00 +0000900 for (meth_iter mi = MD->begin_overridden_methods(),
901 e = MD->end_overridden_methods();
902 mi != e; ++mi) {
903 const CXXMethodDecl *OMD = *mi;
904 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000905 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000906 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
907
Mike Stumpdec025b2009-09-07 04:27:52 +0000908 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000909 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000910 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000911 if (submethods[i] != om)
912 continue;
Mike Stump6e319f62009-09-11 23:25:56 +0000913 QualType nc_oret = OMD->getType()->getAsFunctionType()->getResultType();
914 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
915 QualType nc_ret = MD->getType()->getAsFunctionType()->getResultType();
916 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
917 CallOffset ReturnOffset = std::make_pair(0, 0);
918 if (oret != ret) {
919 // FIXME: calculate offsets for covariance
920 ReturnOffset = std::make_pair(42,42);
921 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000922 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000923 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000924
925 Thunks.erase(OMD);
926 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000927 Index_t &idx = VCall[OMD];
928 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000929 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000930 idx = VCalls.size()+1;
931 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000932 } else {
933 VCallOffset[MD] = VCallOffset[OMD];
934 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000935 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000936 VCall[MD] = idx;
Mike Stump6e319f62009-09-11 23:25:56 +0000937 CallOffset ThisOffset;
938 // FIXME: calculate non-virtual offset
939 ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
940 if (ReturnOffset.first || ReturnOffset.second)
941 CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset);
942 else
943 Thunks[MD] = ThisOffset;
Mike Stump35191b62009-09-01 22:20:28 +0000944 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000945 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000946#if 0
947 // FIXME: finish off
948 int64_t O = VCallOffset[OMD] - Offset/8;
949 if (O) {
950 Thunks[MD] = std::make_pair(O, 0);
951 }
952#endif
953 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000954 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000955 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000956
Mike Stump35191b62009-09-01 22:20:28 +0000957 return false;
958 }
959
Mike Stump98cc7102009-09-05 11:28:33 +0000960 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000961 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
962 i != e; ++i) {
963 const CXXMethodDecl *MD = i->first;
964 Index_t idx = Index[MD];
965 Index_t nv_O = i->second.first;
966 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000967 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000968 }
969 Thunks.clear();
Mike Stump6e319f62009-09-11 23:25:56 +0000970 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
971 e = CovariantThunks.end();
972 i != e; ++i) {
973 const CXXMethodDecl *MD = i->first;
974 Index_t idx = Index[MD];
975 Index_t nv_t = i->second.first.first;
976 Index_t v_t = i->second.first.second;
977 Index_t nv_r = i->second.second.first;
978 Index_t v_r = i->second.second.second;
979 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
980 v_r);
981 }
982 CovariantThunks.clear();
Mike Stump77ca8f62009-09-05 07:20:32 +0000983 }
984
Mike Stumpdec025b2009-09-07 04:27:52 +0000985 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
986 int64_t> > *Path, bool MorallyVirtual) {
987 for (std::vector<std::pair<const CXXRecordDecl *,
988 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000989 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000990 const CXXRecordDecl *RD = i->first;
991 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000992 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
993 ++mi)
994 if (mi->isVirtual()) {
995 const CXXMethodDecl *MD = *mi;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000996 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD, Ptr8Ty));
Mike Stumpdec025b2009-09-07 04:27:52 +0000997 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000998 }
999 }
Mike Stumpf9a883c2009-09-01 23:22:44 +00001000 }
1001
Mike Stump6d10eb82009-09-05 07:49:12 +00001002 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlsson3fec4c62009-09-09 23:17:18 +00001003 GlobalDecl GD;
1004 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
1005 GD = GlobalDecl(Dtor, Dtor_Complete);
1006 else
1007 GD = GlobalDecl(MD);
1008
1009 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(GD, Ptr8Ty));
Mike Stump77ca8f62009-09-05 07:20:32 +00001010 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +00001011 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +00001012 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Mike Stumpb9871a22009-08-21 01:45:00 +00001014 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +00001015 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +00001016 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +00001017 if (MorallyVirtual) {
1018 VCallOffset[MD] = Offset/8;
1019 Index_t &idx = VCall[MD];
1020 // Allocate the first one, after that, we reuse the previous one.
1021 if (idx == 0) {
1022 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +00001023 VCalls.push_back(0);
1024 }
1025 }
Mike Stumpb9871a22009-08-21 01:45:00 +00001026 }
1027
Mike Stump6d10eb82009-09-05 07:49:12 +00001028 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
1029 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +00001030 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
1031 ++mi)
1032 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +00001033 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +00001034 }
Mike Stump65defe32009-08-18 21:03:28 +00001035
Mike Stump77ca8f62009-09-05 07:20:32 +00001036 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
1037 const CXXRecordDecl *PrimaryBase,
1038 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1039 int64_t Offset) {
1040 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1041 e = RD->bases_end(); i != e; ++i) {
1042 if (i->isVirtual())
1043 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001044 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +00001045 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1046 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
1047 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
1048 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +00001049 std::vector<std::pair<const CXXRecordDecl *,
1050 int64_t> > S;
1051 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +00001052 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +00001053 }
1054 }
1055 }
1056
Mike Stump6d10eb82009-09-05 07:49:12 +00001057 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
1058 const ASTRecordLayout &Layout,
1059 const CXXRecordDecl *PrimaryBase,
1060 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1061 int64_t Offset, bool ForVirtualBase) {
1062 StartNewTable();
1063 extra = 0;
1064 // FIXME: Cleanup.
1065 if (!ForVirtualBase) {
1066 // then virtual base offsets...
1067 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1068 e = offsets.rend(); i != e; ++i)
1069 methods.push_back(*i);
1070 }
1071
1072 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +00001073 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
1074 e=VCalls.rend();
1075 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +00001076 methods.push_back(wrap((0?600:0) + *i));
1077 VCalls.clear();
1078
1079 if (ForVirtualBase) {
1080 // then virtual base offsets...
1081 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1082 e = offsets.rend(); i != e; ++i)
1083 methods.push_back(*i);
1084 }
1085
1086 methods.push_back(wrap(-(Offset/8)));
1087 methods.push_back(rtti);
1088 Index_t AddressPoint = methods.size();
1089
Mike Stump98cc7102009-09-05 11:28:33 +00001090 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +00001091 methods.insert(methods.end(), submethods.begin(), submethods.end());
1092 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +00001093
1094 // and then the non-virtual bases.
1095 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1096 MorallyVirtual, Offset);
1097 return AddressPoint;
1098 }
1099
Mike Stump078d7782009-09-05 08:40:18 +00001100 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +00001101 if (!RD->isDynamicClass())
1102 return;
1103
1104 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001105 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +00001106 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1107
Mike Stump9bbe9622009-09-05 08:37:03 +00001108 // vtables are composed from the chain of primaries.
1109 if (PrimaryBase) {
1110 if (PrimaryBaseWasVirtual)
1111 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001112 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +00001113 }
1114
1115 // And add the virtuals for the class to the primary vtable.
1116 AddMethods(RD, MorallyVirtual, Offset);
1117 }
1118
Mike Stumpe45c90f2009-09-05 09:10:58 +00001119 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +00001120 bool MorallyVirtual = false, int64_t Offset = 0,
1121 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +00001122 std::vector<std::pair<const CXXRecordDecl *,
1123 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +00001124 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +00001125 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +00001126
1127 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001128 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +00001129 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1130
Mike Stump15a24e02009-08-28 23:22:54 +00001131 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +00001132 extra = 0;
1133 GenerateVBaseOffsets(offsets, RD, Offset);
1134 if (ForVirtualBase)
1135 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +00001136
1137 // vtables are composed from the chain of primaries.
1138 if (PrimaryBase) {
1139 if (PrimaryBaseWasVirtual)
1140 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001141 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +00001142 }
1143
Mike Stump15a24e02009-08-28 23:22:54 +00001144 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +00001145 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +00001146
Mike Stump98cc7102009-09-05 11:28:33 +00001147 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +00001148 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +00001149
Mike Stump6d10eb82009-09-05 07:49:12 +00001150 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1151 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +00001152 }
1153
Mike Stump98cc7102009-09-05 11:28:33 +00001154 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +00001155 int64_t Offset = 0,
1156 std::vector<std::pair<const CXXRecordDecl *,
1157 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +00001158 bool alloc = false;
1159 if (Path == 0) {
1160 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +00001161 Path = new std::vector<std::pair<const CXXRecordDecl *,
1162 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +00001163 }
1164 // FIXME: We also need to override using all paths to a virtual base,
1165 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +00001166 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +00001167 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1168 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00001169 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +00001170 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1171 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1172 // Mark it so we don't output it twice.
1173 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +00001174 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +00001175 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +00001176 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +00001177 }
Mike Stumpdec025b2009-09-07 04:27:52 +00001178 int64_t BaseOffset = Offset;
1179 if (i->isVirtual())
1180 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +00001181 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +00001182 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +00001183 }
Mike Stump98cc7102009-09-05 11:28:33 +00001184 Path->pop_back();
1185 if (alloc)
1186 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +00001187 }
Mike Stump109b13d2009-08-18 21:30:21 +00001188};
Mike Stump8a12b562009-08-06 15:50:11 +00001189
Mike Stumpf0070db2009-08-26 20:46:33 +00001190class VtableInfo {
1191public:
1192 typedef VtableBuilder::Index_t Index_t;
1193private:
1194 CodeGenModule &CGM; // Per-module state.
1195 /// Index_t - Vtable index type.
1196 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
1197 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
1198 // FIXME: Move to Context.
1199 static MapTy IndexFor;
1200public:
1201 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
1202 void register_index(const CXXRecordDecl *RD, const ElTy &e) {
1203 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
1204 // We own a copy of this, it will go away shortly.
1205 new ElTy (e);
1206 IndexFor[RD] = new ElTy (e);
1207 }
1208 Index_t lookup(const CXXMethodDecl *MD) {
1209 const CXXRecordDecl *RD = MD->getParent();
1210 MapTy::iterator I = IndexFor.find(RD);
1211 if (I == IndexFor.end()) {
1212 std::vector<llvm::Constant *> methods;
1213 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001214 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001215 b.GenerateVtableForVBases(RD);
Mike Stumpf0070db2009-08-26 20:46:33 +00001216 register_index(RD, b.getIndex());
1217 I = IndexFor.find(RD);
1218 }
1219 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1220 return (*I->second)[MD];
1221 }
1222};
1223
1224// FIXME: Move to Context.
1225VtableInfo::MapTy VtableInfo::IndexFor;
1226
Mike Stumpf1216772009-07-31 18:25:34 +00001227llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001228 llvm::SmallString<256> OutName;
1229 llvm::raw_svector_ostream Out(OutName);
1230 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001231 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001232 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001233 llvm::GlobalVariable::LinkageTypes linktype;
1234 linktype = llvm::GlobalValue::WeakAnyLinkage;
1235 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001236 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001237 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001238
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001239 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001240
Mike Stump276b9f12009-08-16 01:46:26 +00001241 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001242 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001243
Mike Stump276b9f12009-08-16 01:46:26 +00001244 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001245 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001246
Mike Stump82b56962009-07-31 21:43:43 +00001247 llvm::Constant *C;
1248 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1249 C = llvm::ConstantArray::get(type, methods);
1250 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001251 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001252 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001253 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001254 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001255 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001256 return vtable;
1257}
1258
Mike Stumpf0070db2009-08-26 20:46:33 +00001259// FIXME: move to Context
1260static VtableInfo *vtableinfo;
1261
Mike Stumped032eb2009-09-04 18:27:16 +00001262llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1263 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001264 bool Extern, int64_t nv,
1265 int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001266 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1267
1268 FunctionArgList Args;
1269 ImplicitParamDecl *ThisDecl =
1270 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1271 MD->getThisType(getContext()));
1272 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1273 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1274 e = MD->param_end();
1275 i != e; ++i) {
1276 ParmVarDecl *D = *i;
1277 Args.push_back(std::make_pair(D, D->getType()));
1278 }
1279 IdentifierInfo *II
1280 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1281 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1282 getContext().getTranslationUnitDecl(),
1283 SourceLocation(), II, R, 0,
1284 Extern
1285 ? FunctionDecl::Extern
1286 : FunctionDecl::Static,
1287 false, true);
1288 StartFunction(FD, R, Fn, Args, SourceLocation());
1289 // FIXME: generate body
1290 FinishFunction();
1291 return Fn;
1292}
1293
Mike Stump6e319f62009-09-11 23:25:56 +00001294llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1295 const CXXMethodDecl *MD,
1296 bool Extern,
1297 int64_t nv_t,
1298 int64_t v_t,
1299 int64_t nv_r,
1300 int64_t v_r) {
1301 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1302
1303 FunctionArgList Args;
1304 ImplicitParamDecl *ThisDecl =
1305 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1306 MD->getThisType(getContext()));
1307 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1308 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1309 e = MD->param_end();
1310 i != e; ++i) {
1311 ParmVarDecl *D = *i;
1312 Args.push_back(std::make_pair(D, D->getType()));
1313 }
1314 IdentifierInfo *II
1315 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1316 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1317 getContext().getTranslationUnitDecl(),
1318 SourceLocation(), II, R, 0,
1319 Extern
1320 ? FunctionDecl::Extern
1321 : FunctionDecl::Static,
1322 false, true);
1323 StartFunction(FD, R, Fn, Args, SourceLocation());
1324 // FIXME: generate body
1325 FinishFunction();
1326 return Fn;
1327}
1328
Mike Stump77ca8f62009-09-05 07:20:32 +00001329llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1330 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001331 llvm::SmallString<256> OutName;
1332 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001333 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001334 llvm::GlobalVariable::LinkageTypes linktype;
1335 linktype = llvm::GlobalValue::WeakAnyLinkage;
1336 if (!Extern)
1337 linktype = llvm::GlobalValue::InternalLinkage;
1338 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1339 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1340 const llvm::FunctionType *FTy =
1341 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1342 FPT->isVariadic());
1343
1344 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1345 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001346 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001347 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1348 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1349 return m;
1350}
1351
Mike Stump6e319f62009-09-11 23:25:56 +00001352llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1353 bool Extern, int64_t nv_t,
1354 int64_t v_t, int64_t nv_r,
1355 int64_t v_r) {
1356 llvm::SmallString<256> OutName;
1357 llvm::raw_svector_ostream Out(OutName);
1358 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1359 llvm::GlobalVariable::LinkageTypes linktype;
1360 linktype = llvm::GlobalValue::WeakAnyLinkage;
1361 if (!Extern)
1362 linktype = llvm::GlobalValue::InternalLinkage;
1363 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1364 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1365 const llvm::FunctionType *FTy =
1366 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1367 FPT->isVariadic());
1368
1369 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1370 &getModule());
1371 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1372 v_r);
1373 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1374 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1375 return m;
1376}
1377
Mike Stumpf0070db2009-08-26 20:46:33 +00001378llvm::Value *
1379CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1380 const llvm::Type *Ty) {
1381 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Mike Stumpf0070db2009-08-26 20:46:33 +00001383 // FIXME: move to Context
1384 if (vtableinfo == 0)
1385 vtableinfo = new VtableInfo(CGM);
1386
1387 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1388
1389 Ty = llvm::PointerType::get(Ty, 0);
1390 Ty = llvm::PointerType::get(Ty, 0);
1391 Ty = llvm::PointerType::get(Ty, 0);
1392 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1393 vtbl = Builder.CreateLoad(vtbl);
1394 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1395 Idx, "vfn");
1396 vfn = Builder.CreateLoad(vfn);
1397 return vfn;
1398}
1399
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001400/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1401/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1402/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001403// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001404void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001405 llvm::Value *Src,
1406 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001407 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001408 QualType Ty) {
1409 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1410 assert(CA && "VLA cannot be copied over");
1411 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001413 // Create a temporary for the loop index and initialize it with 0.
1414 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1415 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001416 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001417 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1418 Builder.CreateStore(zeroConstant, IndexPtr, false);
1419 // Start the loop with a block that tests the condition.
1420 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1421 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001423 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001425 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1426 // Generate: if (loop-index < number-of-elements fall to the loop body,
1427 // otherwise, go to the block after the for-loop.
1428 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001429 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001430 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1431 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001432 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001433 "isless");
1434 // If the condition is true, execute the body.
1435 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001437 EmitBlock(ForBody);
1438 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1439 // Inside the loop body, emit the constructor call on the array element.
1440 Counter = Builder.CreateLoad(IndexPtr);
1441 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1442 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1443 if (BitwiseCopy)
1444 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001445 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001446 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001447 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001448 Ctor_Complete);
1449 CallArgList CallArgs;
1450 // Push the this (Dest) ptr.
1451 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1452 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001454 // Push the Src ptr.
1455 CallArgs.push_back(std::make_pair(RValue::get(Src),
1456 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001457 QualType ResultType =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001458 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1459 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1460 Callee, CallArgs, BaseCopyCtor);
1461 }
1462 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001464 // Emit the increment of the loop counter.
1465 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1466 Counter = Builder.CreateLoad(IndexPtr);
1467 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1468 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001470 // Finally, branch back up to the condition for the next iteration.
1471 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001473 // Emit the fall-through block.
1474 EmitBlock(AfterFor, true);
1475}
1476
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001477/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001478/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001479/// bitwise assignment or via a copy assignment operator function call.
1480/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001481void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001482 llvm::Value *Src,
1483 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001484 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001485 QualType Ty) {
1486 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1487 assert(CA && "VLA cannot be asssigned");
1488 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001490 // Create a temporary for the loop index and initialize it with 0.
1491 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1492 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001493 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001494 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1495 Builder.CreateStore(zeroConstant, IndexPtr, false);
1496 // Start the loop with a block that tests the condition.
1497 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1498 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001500 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001502 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1503 // Generate: if (loop-index < number-of-elements fall to the loop body,
1504 // otherwise, go to the block after the for-loop.
1505 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001506 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001507 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1508 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001509 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001510 "isless");
1511 // If the condition is true, execute the body.
1512 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001514 EmitBlock(ForBody);
1515 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1516 // Inside the loop body, emit the assignment operator call on array element.
1517 Counter = Builder.CreateLoad(IndexPtr);
1518 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1519 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1520 const CXXMethodDecl *MD = 0;
1521 if (BitwiseAssign)
1522 EmitAggregateCopy(Dest, Src, Ty);
1523 else {
1524 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1525 MD);
1526 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1527 (void)hasCopyAssign;
1528 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1529 const llvm::Type *LTy =
1530 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1531 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001532 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001534 CallArgList CallArgs;
1535 // Push the this (Dest) ptr.
1536 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1537 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001539 // Push the Src ptr.
1540 CallArgs.push_back(std::make_pair(RValue::get(Src),
1541 MD->getParamDecl(0)->getType()));
Mike Stumped032eb2009-09-04 18:27:16 +00001542 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001543 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1544 Callee, CallArgs, MD);
1545 }
1546 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001548 // Emit the increment of the loop counter.
1549 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1550 Counter = Builder.CreateLoad(IndexPtr);
1551 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1552 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001554 // Finally, branch back up to the condition for the next iteration.
1555 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001557 // Emit the fall-through block.
1558 EmitBlock(AfterFor, true);
1559}
1560
Fariborz Jahanianca283612009-08-07 23:51:33 +00001561/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1562/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001563/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001564void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001565 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001566 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001567 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1568 if (ClassDecl) {
1569 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl);
1570 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ;
1571 }
1572 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1573 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001574 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001575 }
Mike Stump1eb44332009-09-09 15:08:12 +00001576
1577 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001578 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001579 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001580 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001581 CallArgList CallArgs;
1582 // Push the this (Dest) ptr.
1583 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1584 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Fariborz Jahanianca283612009-08-07 23:51:33 +00001586 // Push the Src ptr.
1587 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001588 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001589 QualType ResultType =
Fariborz Jahanianca283612009-08-07 23:51:33 +00001590 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1591 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1592 Callee, CallArgs, BaseCopyCtor);
1593 }
1594}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001595
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001596/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001597/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001598/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001599// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001600void CodeGenFunction::EmitClassCopyAssignment(
1601 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001602 const CXXRecordDecl *ClassDecl,
1603 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001604 QualType Ty) {
1605 if (ClassDecl) {
1606 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl);
1607 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ;
1608 }
1609 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1610 EmitAggregateCopy(Dest, Src, Ty);
1611 return;
1612 }
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001614 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001615 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001616 MD);
1617 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1618 (void)ConstCopyAssignOp;
1619
1620 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +00001621 const llvm::Type *LTy =
1622 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001623 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001624 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001626 CallArgList CallArgs;
1627 // Push the this (Dest) ptr.
1628 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1629 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001631 // Push the Src ptr.
1632 CallArgs.push_back(std::make_pair(RValue::get(Src),
1633 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001634 QualType ResultType =
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001635 MD->getType()->getAsFunctionType()->getResultType();
1636 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1637 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001638}
1639
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001640/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001641void
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001642CodeGenFunction::SynthesizeDefaultConstructor(GlobalDecl GD,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001643 const FunctionDecl *FD,
1644 llvm::Function *Fn,
1645 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001646 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(GD.getDecl());
1647
1648 StartFunction(GD, FD->getResultType(), Fn, Args, SourceLocation());
1649 EmitCtorPrologue(Ctor);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001650 FinishFunction();
1651}
1652
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001653/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001654/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001655/// The implicitly-defined copy constructor for class X performs a memberwise
1656/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001657/// of initialization of bases and members in a user-defined constructor
1658/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001659/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001660/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001661/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001662/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001663/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001664/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001665/// Virtual base class subobjects shall be copied only once by the
1666/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001667
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001668void CodeGenFunction::SynthesizeCXXCopyConstructor(GlobalDecl GD,
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001669 const FunctionDecl *FD,
1670 llvm::Function *Fn,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001671 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001672 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(GD.getDecl());
1673 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001674 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001675 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001676 StartFunction(GD, Ctor->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001678 FunctionArgList::const_iterator i = Args.begin();
1679 const VarDecl *ThisArg = i->first;
1680 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1681 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1682 const VarDecl *SrcArg = (i+1)->first;
1683 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1684 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001686 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1687 Base != ClassDecl->bases_end(); ++Base) {
1688 // FIXME. copy constrution of virtual base NYI
1689 if (Base->isVirtual())
1690 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001692 CXXRecordDecl *BaseClassDecl
1693 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001694 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1695 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001696 }
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001698 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1699 FieldEnd = ClassDecl->field_end();
1700 Field != FieldEnd; ++Field) {
1701 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001702 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001703 getContext().getAsConstantArrayType(FieldType);
1704 if (Array)
1705 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001707 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1708 CXXRecordDecl *FieldClassDecl
1709 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1710 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1711 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001712 if (Array) {
1713 const llvm::Type *BasePtr = ConvertType(FieldType);
1714 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001715 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001716 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001717 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001718 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1719 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1720 FieldClassDecl, FieldType);
1721 }
Mike Stump1eb44332009-09-09 15:08:12 +00001722 else
1723 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001724 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001725 continue;
1726 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001727 // Do a built-in assignment of scalar data members.
1728 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1729 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1730 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1731 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001732 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001733 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001734}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001735
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001736/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001737/// Before the implicitly-declared copy assignment operator for a class is
1738/// implicitly defined, all implicitly- declared copy assignment operators for
1739/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001740/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001741/// The implicitly-defined copy assignment operator for class X performs
1742/// memberwise assignment of its subob- jects. The direct base classes of X are
1743/// assigned first, in the order of their declaration in
1744/// the base-specifier-list, and then the immediate nonstatic data members of X
1745/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001746/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001747/// if the subobject is of class type, the copy assignment operator for the
1748/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001749/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001750///
Mike Stump1eb44332009-09-09 15:08:12 +00001751/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001752/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001753///
Mike Stump1eb44332009-09-09 15:08:12 +00001754/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001755/// used.
1756void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
1757 const FunctionDecl *FD,
1758 llvm::Function *Fn,
1759 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001760
1761 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1762 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1763 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001764 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001766 FunctionArgList::const_iterator i = Args.begin();
1767 const VarDecl *ThisArg = i->first;
1768 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1769 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1770 const VarDecl *SrcArg = (i+1)->first;
1771 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1772 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001774 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1775 Base != ClassDecl->bases_end(); ++Base) {
1776 // FIXME. copy assignment of virtual base NYI
1777 if (Base->isVirtual())
1778 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001780 CXXRecordDecl *BaseClassDecl
1781 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1782 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1783 Base->getType());
1784 }
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001786 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1787 FieldEnd = ClassDecl->field_end();
1788 Field != FieldEnd; ++Field) {
1789 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001790 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001791 getContext().getAsConstantArrayType(FieldType);
1792 if (Array)
1793 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001795 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1796 CXXRecordDecl *FieldClassDecl
1797 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1798 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1799 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001800 if (Array) {
1801 const llvm::Type *BasePtr = ConvertType(FieldType);
1802 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1803 llvm::Value *DestBaseAddrPtr =
1804 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1805 llvm::Value *SrcBaseAddrPtr =
1806 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1807 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1808 FieldClassDecl, FieldType);
1809 }
1810 else
Mike Stump1eb44332009-09-09 15:08:12 +00001811 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001812 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001813 continue;
1814 }
1815 // Do a built-in assignment of scalar data members.
1816 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1817 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1818 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1819 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001820 }
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001822 // return *this;
1823 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001825 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001826}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001827
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001828/// EmitCtorPrologue - This routine generates necessary code to initialize
1829/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001830/// FIXME: This needs to take a CXXCtorType.
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001831void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001832 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001833 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001834 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001836 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001837 E = CD->init_end();
1838 B != E; ++B) {
1839 CXXBaseOrMemberInitializer *Member = (*B);
1840 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001841 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001842 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001843 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001844 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001845 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001846 BaseClassDecl);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001847 EmitCXXConstructorCall(Member->getConstructor(),
1848 Ctor_Complete, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001849 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001850 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001851 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001852 // non-static data member initilaizers.
1853 FieldDecl *Field = Member->getMember();
1854 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001855 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001856 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001857 if (Array)
1858 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Mike Stumpf1216772009-07-31 18:25:34 +00001860 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001861 LValue LHS;
1862 if (FieldType->isReferenceType()) {
1863 // FIXME: This is really ugly; should be refactored somehow
1864 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1865 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
1866 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(),
1867 QualType::GCNone, FieldType.getAddressSpace());
1868 } else {
1869 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1870 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001871 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001872 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001873 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001874 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001875 if (Array) {
1876 const llvm::Type *BasePtr = ConvertType(FieldType);
1877 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001878 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001879 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001880 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001881 Array, BaseAddrPtr);
1882 }
1883 else
1884 EmitCXXConstructorCall(Member->getConstructor(),
1885 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001886 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001887 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001888 continue;
1889 }
1890 else {
1891 // Initializing an anonymous union data member.
1892 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001893 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001894 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001895 FieldType = anonMember->getType();
1896 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001897 }
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001899 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001900 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001901 RValue RHS;
1902 if (FieldType->isReferenceType())
1903 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1904 /*IsInitializer=*/true);
1905 else
1906 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1907 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001908 }
1909 }
Mike Stumpf1216772009-07-31 18:25:34 +00001910
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001911 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001912 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001913 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001914 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001915 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001916 ClassDecl->bases_begin();
1917 Base != ClassDecl->bases_end(); ++Base) {
1918 // FIXME. copy assignment of virtual base NYI
1919 if (Base->isVirtual())
1920 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001922 CXXRecordDecl *BaseClassDecl
1923 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1924 if (BaseClassDecl->hasTrivialConstructor())
1925 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001926 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001927 BaseClassDecl->getDefaultConstructor(getContext())) {
1928 LoadOfThis = LoadCXXThis();
1929 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1930 BaseClassDecl);
1931 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1932 }
1933 }
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001935 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1936 FieldEnd = ClassDecl->field_end();
1937 Field != FieldEnd; ++Field) {
1938 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001939 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001940 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001941 if (Array)
1942 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001943 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1944 continue;
1945 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001946 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001947 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1948 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1949 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001950 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001951 MemberClassDecl->getDefaultConstructor(getContext())) {
1952 LoadOfThis = LoadCXXThis();
1953 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001954 if (Array) {
1955 const llvm::Type *BasePtr = ConvertType(FieldType);
1956 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001957 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001958 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1959 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1960 }
1961 else
Mike Stump1eb44332009-09-09 15:08:12 +00001962 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001963 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001964 }
1965 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001966 }
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Mike Stumpf1216772009-07-31 18:25:34 +00001968 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001969 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001970 if (!LoadOfThis)
1971 LoadOfThis = LoadCXXThis();
1972 llvm::Value *VtableField;
1973 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001974 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001975 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1976 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1977 llvm::Value *vtable = GenerateVtable(ClassDecl);
1978 Builder.CreateStore(vtable, VtableField);
1979 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001980}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001981
1982/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001983/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001984/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001985/// FIXME: This needs to take a CXXDtorType.
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001986void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) {
1987 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001988 assert(!ClassDecl->getNumVBases() &&
1989 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001990 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001992 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1993 *E = DD->destr_end(); B != E; ++B) {
1994 uintptr_t BaseOrMember = (*B);
1995 if (DD->isMemberToDestroy(BaseOrMember)) {
1996 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1997 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001998 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001999 getContext().getAsConstantArrayType(FieldType);
2000 if (Array)
2001 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002002 const RecordType *RT = FieldType->getAs<RecordType>();
2003 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2004 if (FieldClassDecl->hasTrivialDestructor())
2005 continue;
2006 llvm::Value *LoadOfThis = LoadCXXThis();
2007 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002008 if (Array) {
2009 const llvm::Type *BasePtr = ConvertType(FieldType);
2010 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002011 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002012 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002013 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002014 Array, BaseAddrPtr);
2015 }
2016 else
2017 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
2018 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00002019 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002020 const RecordType *RT =
2021 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
2022 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2023 if (BaseClassDecl->hasTrivialDestructor())
2024 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002025 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002026 ClassDecl,BaseClassDecl);
2027 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2028 Dtor_Complete, V);
2029 }
2030 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002031 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
2032 return;
2033 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00002034 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002035 // reverse order of their construction.
2036 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002038 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
2039 FieldEnd = ClassDecl->field_end();
2040 Field != FieldEnd; ++Field) {
2041 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002042 if (getContext().getAsConstantArrayType(FieldType))
2043 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002044 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
2045 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2046 if (FieldClassDecl->hasTrivialDestructor())
2047 continue;
2048 DestructedFields.push_back(*Field);
2049 }
2050 }
2051 if (!DestructedFields.empty())
2052 for (int i = DestructedFields.size() -1; i >= 0; --i) {
2053 FieldDecl *Field = DestructedFields[i];
2054 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002055 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002056 getContext().getAsConstantArrayType(FieldType);
2057 if (Array)
2058 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002059 const RecordType *RT = FieldType->getAs<RecordType>();
2060 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2061 llvm::Value *LoadOfThis = LoadCXXThis();
2062 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002063 if (Array) {
2064 const llvm::Type *BasePtr = ConvertType(FieldType);
2065 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002066 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002067 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002068 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002069 Array, BaseAddrPtr);
2070 }
2071 else
2072 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
2073 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002074 }
Mike Stump1eb44332009-09-09 15:08:12 +00002075
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002076 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
2077 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
2078 Base != ClassDecl->bases_end(); ++Base) {
2079 // FIXME. copy assignment of virtual base NYI
2080 if (Base->isVirtual())
2081 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002083 CXXRecordDecl *BaseClassDecl
2084 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2085 if (BaseClassDecl->hasTrivialDestructor())
2086 continue;
2087 DestructedBases.push_back(BaseClassDecl);
2088 }
2089 if (DestructedBases.empty())
2090 return;
2091 for (int i = DestructedBases.size() -1; i >= 0; --i) {
2092 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Mike Stump1eb44332009-09-09 15:08:12 +00002093 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002094 ClassDecl,BaseClassDecl);
2095 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2096 Dtor_Complete, V);
2097 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002098}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002099
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002100void CodeGenFunction::SynthesizeDefaultDestructor(GlobalDecl GD,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002101 const FunctionDecl *FD,
2102 llvm::Function *Fn,
2103 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002105 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(GD.getDecl());
2106
2107 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002108 assert(!ClassDecl->hasUserDeclaredDestructor() &&
2109 "SynthesizeDefaultDestructor - destructor has user declaration");
2110 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002112 StartFunction(GD, Dtor->getResultType(), Fn, Args, SourceLocation());
2113 EmitDtorEpilogue(Dtor);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002114 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00002115}