blob: 7def3c78347906c04187c637a1bf60b840c6d111 [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 Carlssonc7cba152009-09-12 00:00:29 +0000996 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD));
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 Carlssonc7cba152009-09-12 00:00:29 +00001003 llvm::Constant *m = 0;
Anders Carlsson3fec4c62009-09-09 23:17:18 +00001004 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonc7cba152009-09-12 00:00:29 +00001005 m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
Anders Carlsson3fec4c62009-09-09 23:17:18 +00001006 else
Anders Carlssonc7cba152009-09-12 00:00:29 +00001007 m = wrap(CGM.GetAddrOfFunction(MD));
Anders Carlsson3fec4c62009-09-09 23:17:18 +00001008
Mike Stump77ca8f62009-09-05 07:20:32 +00001009 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +00001010 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +00001011 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Mike Stumpb9871a22009-08-21 01:45:00 +00001013 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +00001014 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +00001015 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +00001016 if (MorallyVirtual) {
1017 VCallOffset[MD] = Offset/8;
1018 Index_t &idx = VCall[MD];
1019 // Allocate the first one, after that, we reuse the previous one.
1020 if (idx == 0) {
1021 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +00001022 VCalls.push_back(0);
1023 }
1024 }
Mike Stumpb9871a22009-08-21 01:45:00 +00001025 }
1026
Mike Stump6d10eb82009-09-05 07:49:12 +00001027 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
1028 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +00001029 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
1030 ++mi)
1031 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +00001032 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +00001033 }
Mike Stump65defe32009-08-18 21:03:28 +00001034
Mike Stump77ca8f62009-09-05 07:20:32 +00001035 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
1036 const CXXRecordDecl *PrimaryBase,
1037 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1038 int64_t Offset) {
1039 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1040 e = RD->bases_end(); i != e; ++i) {
1041 if (i->isVirtual())
1042 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001043 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +00001044 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1045 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
1046 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
1047 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +00001048 std::vector<std::pair<const CXXRecordDecl *,
1049 int64_t> > S;
1050 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +00001051 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +00001052 }
1053 }
1054 }
1055
Mike Stump6d10eb82009-09-05 07:49:12 +00001056 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
1057 const ASTRecordLayout &Layout,
1058 const CXXRecordDecl *PrimaryBase,
1059 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
1060 int64_t Offset, bool ForVirtualBase) {
1061 StartNewTable();
1062 extra = 0;
1063 // FIXME: Cleanup.
1064 if (!ForVirtualBase) {
1065 // then virtual base offsets...
1066 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1067 e = offsets.rend(); i != e; ++i)
1068 methods.push_back(*i);
1069 }
1070
1071 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +00001072 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
1073 e=VCalls.rend();
1074 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +00001075 methods.push_back(wrap((0?600:0) + *i));
1076 VCalls.clear();
1077
1078 if (ForVirtualBase) {
1079 // then virtual base offsets...
1080 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
1081 e = offsets.rend(); i != e; ++i)
1082 methods.push_back(*i);
1083 }
1084
1085 methods.push_back(wrap(-(Offset/8)));
1086 methods.push_back(rtti);
1087 Index_t AddressPoint = methods.size();
1088
Mike Stump98cc7102009-09-05 11:28:33 +00001089 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +00001090 methods.insert(methods.end(), submethods.begin(), submethods.end());
1091 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +00001092
1093 // and then the non-virtual bases.
1094 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1095 MorallyVirtual, Offset);
1096 return AddressPoint;
1097 }
1098
Mike Stump078d7782009-09-05 08:40:18 +00001099 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +00001100 if (!RD->isDynamicClass())
1101 return;
1102
1103 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001104 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +00001105 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1106
Mike Stump9bbe9622009-09-05 08:37:03 +00001107 // vtables are composed from the chain of primaries.
1108 if (PrimaryBase) {
1109 if (PrimaryBaseWasVirtual)
1110 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001111 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +00001112 }
1113
1114 // And add the virtuals for the class to the primary vtable.
1115 AddMethods(RD, MorallyVirtual, Offset);
1116 }
1117
Mike Stumpe45c90f2009-09-05 09:10:58 +00001118 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +00001119 bool MorallyVirtual = false, int64_t Offset = 0,
1120 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +00001121 std::vector<std::pair<const CXXRecordDecl *,
1122 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +00001123 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +00001124 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +00001125
1126 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001127 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +00001128 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1129
Mike Stump15a24e02009-08-28 23:22:54 +00001130 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +00001131 extra = 0;
1132 GenerateVBaseOffsets(offsets, RD, Offset);
1133 if (ForVirtualBase)
1134 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +00001135
1136 // vtables are composed from the chain of primaries.
1137 if (PrimaryBase) {
1138 if (PrimaryBaseWasVirtual)
1139 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001140 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +00001141 }
1142
Mike Stump15a24e02009-08-28 23:22:54 +00001143 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +00001144 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +00001145
Mike Stump98cc7102009-09-05 11:28:33 +00001146 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +00001147 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +00001148
Mike Stump6d10eb82009-09-05 07:49:12 +00001149 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1150 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +00001151 }
1152
Mike Stump98cc7102009-09-05 11:28:33 +00001153 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +00001154 int64_t Offset = 0,
1155 std::vector<std::pair<const CXXRecordDecl *,
1156 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +00001157 bool alloc = false;
1158 if (Path == 0) {
1159 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +00001160 Path = new std::vector<std::pair<const CXXRecordDecl *,
1161 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +00001162 }
1163 // FIXME: We also need to override using all paths to a virtual base,
1164 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +00001165 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +00001166 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1167 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00001168 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +00001169 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1170 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1171 // Mark it so we don't output it twice.
1172 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +00001173 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +00001174 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +00001175 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +00001176 }
Mike Stumpdec025b2009-09-07 04:27:52 +00001177 int64_t BaseOffset = Offset;
1178 if (i->isVirtual())
1179 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +00001180 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +00001181 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +00001182 }
Mike Stump98cc7102009-09-05 11:28:33 +00001183 Path->pop_back();
1184 if (alloc)
1185 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +00001186 }
Mike Stump109b13d2009-08-18 21:30:21 +00001187};
Mike Stump8a12b562009-08-06 15:50:11 +00001188
Mike Stumpf0070db2009-08-26 20:46:33 +00001189class VtableInfo {
1190public:
1191 typedef VtableBuilder::Index_t Index_t;
1192private:
1193 CodeGenModule &CGM; // Per-module state.
1194 /// Index_t - Vtable index type.
1195 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
1196 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
1197 // FIXME: Move to Context.
1198 static MapTy IndexFor;
1199public:
1200 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
1201 void register_index(const CXXRecordDecl *RD, const ElTy &e) {
1202 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
1203 // We own a copy of this, it will go away shortly.
1204 new ElTy (e);
1205 IndexFor[RD] = new ElTy (e);
1206 }
1207 Index_t lookup(const CXXMethodDecl *MD) {
1208 const CXXRecordDecl *RD = MD->getParent();
1209 MapTy::iterator I = IndexFor.find(RD);
1210 if (I == IndexFor.end()) {
1211 std::vector<llvm::Constant *> methods;
1212 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001213 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001214 b.GenerateVtableForVBases(RD);
Mike Stumpf0070db2009-08-26 20:46:33 +00001215 register_index(RD, b.getIndex());
1216 I = IndexFor.find(RD);
1217 }
1218 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1219 return (*I->second)[MD];
1220 }
1221};
1222
1223// FIXME: Move to Context.
1224VtableInfo::MapTy VtableInfo::IndexFor;
1225
Mike Stumpf1216772009-07-31 18:25:34 +00001226llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001227 llvm::SmallString<256> OutName;
1228 llvm::raw_svector_ostream Out(OutName);
1229 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001230 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001231 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001232 llvm::GlobalVariable::LinkageTypes linktype;
1233 linktype = llvm::GlobalValue::WeakAnyLinkage;
1234 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001235 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001236 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001237
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001238 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001239
Mike Stump276b9f12009-08-16 01:46:26 +00001240 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001241 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001242
Mike Stump276b9f12009-08-16 01:46:26 +00001243 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001244 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001245
Mike Stump82b56962009-07-31 21:43:43 +00001246 llvm::Constant *C;
1247 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1248 C = llvm::ConstantArray::get(type, methods);
1249 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001250 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001251 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001252 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001253 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001254 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001255 return vtable;
1256}
1257
Mike Stumpf0070db2009-08-26 20:46:33 +00001258// FIXME: move to Context
1259static VtableInfo *vtableinfo;
1260
Mike Stumped032eb2009-09-04 18:27:16 +00001261llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1262 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001263 bool Extern, int64_t nv,
1264 int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001265 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1266
1267 FunctionArgList Args;
1268 ImplicitParamDecl *ThisDecl =
1269 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1270 MD->getThisType(getContext()));
1271 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1272 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1273 e = MD->param_end();
1274 i != e; ++i) {
1275 ParmVarDecl *D = *i;
1276 Args.push_back(std::make_pair(D, D->getType()));
1277 }
1278 IdentifierInfo *II
1279 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1280 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1281 getContext().getTranslationUnitDecl(),
1282 SourceLocation(), II, R, 0,
1283 Extern
1284 ? FunctionDecl::Extern
1285 : FunctionDecl::Static,
1286 false, true);
1287 StartFunction(FD, R, Fn, Args, SourceLocation());
1288 // FIXME: generate body
1289 FinishFunction();
1290 return Fn;
1291}
1292
Mike Stump6e319f62009-09-11 23:25:56 +00001293llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1294 const CXXMethodDecl *MD,
1295 bool Extern,
1296 int64_t nv_t,
1297 int64_t v_t,
1298 int64_t nv_r,
1299 int64_t v_r) {
1300 QualType R = MD->getType()->getAsFunctionType()->getResultType();
1301
1302 FunctionArgList Args;
1303 ImplicitParamDecl *ThisDecl =
1304 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1305 MD->getThisType(getContext()));
1306 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1307 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1308 e = MD->param_end();
1309 i != e; ++i) {
1310 ParmVarDecl *D = *i;
1311 Args.push_back(std::make_pair(D, D->getType()));
1312 }
1313 IdentifierInfo *II
1314 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1315 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1316 getContext().getTranslationUnitDecl(),
1317 SourceLocation(), II, R, 0,
1318 Extern
1319 ? FunctionDecl::Extern
1320 : FunctionDecl::Static,
1321 false, true);
1322 StartFunction(FD, R, Fn, Args, SourceLocation());
1323 // FIXME: generate body
1324 FinishFunction();
1325 return Fn;
1326}
1327
Mike Stump77ca8f62009-09-05 07:20:32 +00001328llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1329 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001330 llvm::SmallString<256> OutName;
1331 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001332 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001333 llvm::GlobalVariable::LinkageTypes linktype;
1334 linktype = llvm::GlobalValue::WeakAnyLinkage;
1335 if (!Extern)
1336 linktype = llvm::GlobalValue::InternalLinkage;
1337 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1338 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1339 const llvm::FunctionType *FTy =
1340 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1341 FPT->isVariadic());
1342
1343 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1344 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001345 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001346 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1347 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1348 return m;
1349}
1350
Mike Stump6e319f62009-09-11 23:25:56 +00001351llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1352 bool Extern, int64_t nv_t,
1353 int64_t v_t, int64_t nv_r,
1354 int64_t v_r) {
1355 llvm::SmallString<256> OutName;
1356 llvm::raw_svector_ostream Out(OutName);
1357 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1358 llvm::GlobalVariable::LinkageTypes linktype;
1359 linktype = llvm::GlobalValue::WeakAnyLinkage;
1360 if (!Extern)
1361 linktype = llvm::GlobalValue::InternalLinkage;
1362 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1363 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1364 const llvm::FunctionType *FTy =
1365 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1366 FPT->isVariadic());
1367
1368 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1369 &getModule());
1370 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1371 v_r);
1372 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1373 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1374 return m;
1375}
1376
Mike Stumpf0070db2009-08-26 20:46:33 +00001377llvm::Value *
1378CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1379 const llvm::Type *Ty) {
1380 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Mike Stumpf0070db2009-08-26 20:46:33 +00001382 // FIXME: move to Context
1383 if (vtableinfo == 0)
1384 vtableinfo = new VtableInfo(CGM);
1385
1386 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1387
1388 Ty = llvm::PointerType::get(Ty, 0);
1389 Ty = llvm::PointerType::get(Ty, 0);
1390 Ty = llvm::PointerType::get(Ty, 0);
1391 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1392 vtbl = Builder.CreateLoad(vtbl);
1393 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1394 Idx, "vfn");
1395 vfn = Builder.CreateLoad(vfn);
1396 return vfn;
1397}
1398
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001399/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1400/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1401/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001402// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001403void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001404 llvm::Value *Src,
1405 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001406 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001407 QualType Ty) {
1408 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1409 assert(CA && "VLA cannot be copied over");
1410 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001412 // Create a temporary for the loop index and initialize it with 0.
1413 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1414 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001415 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001416 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1417 Builder.CreateStore(zeroConstant, IndexPtr, false);
1418 // Start the loop with a block that tests the condition.
1419 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1420 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001422 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001424 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1425 // Generate: if (loop-index < number-of-elements fall to the loop body,
1426 // otherwise, go to the block after the for-loop.
1427 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001428 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001429 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1430 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001431 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001432 "isless");
1433 // If the condition is true, execute the body.
1434 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001436 EmitBlock(ForBody);
1437 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1438 // Inside the loop body, emit the constructor call on the array element.
1439 Counter = Builder.CreateLoad(IndexPtr);
1440 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1441 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1442 if (BitwiseCopy)
1443 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001444 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001445 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001446 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001447 Ctor_Complete);
1448 CallArgList CallArgs;
1449 // Push the this (Dest) ptr.
1450 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1451 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001453 // Push the Src ptr.
1454 CallArgs.push_back(std::make_pair(RValue::get(Src),
1455 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001456 QualType ResultType =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001457 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1458 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1459 Callee, CallArgs, BaseCopyCtor);
1460 }
1461 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001463 // Emit the increment of the loop counter.
1464 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1465 Counter = Builder.CreateLoad(IndexPtr);
1466 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1467 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001469 // Finally, branch back up to the condition for the next iteration.
1470 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001472 // Emit the fall-through block.
1473 EmitBlock(AfterFor, true);
1474}
1475
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001476/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001477/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001478/// bitwise assignment or via a copy assignment operator function call.
1479/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001480void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001481 llvm::Value *Src,
1482 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001483 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001484 QualType Ty) {
1485 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1486 assert(CA && "VLA cannot be asssigned");
1487 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001489 // Create a temporary for the loop index and initialize it with 0.
1490 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1491 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001492 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001493 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1494 Builder.CreateStore(zeroConstant, IndexPtr, false);
1495 // Start the loop with a block that tests the condition.
1496 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1497 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001499 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001501 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1502 // Generate: if (loop-index < number-of-elements fall to the loop body,
1503 // otherwise, go to the block after the for-loop.
1504 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001505 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001506 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1507 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001508 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001509 "isless");
1510 // If the condition is true, execute the body.
1511 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001513 EmitBlock(ForBody);
1514 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1515 // Inside the loop body, emit the assignment operator call on array element.
1516 Counter = Builder.CreateLoad(IndexPtr);
1517 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1518 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1519 const CXXMethodDecl *MD = 0;
1520 if (BitwiseAssign)
1521 EmitAggregateCopy(Dest, Src, Ty);
1522 else {
1523 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1524 MD);
1525 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1526 (void)hasCopyAssign;
1527 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
1528 const llvm::Type *LTy =
1529 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1530 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001531 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001533 CallArgList CallArgs;
1534 // Push the this (Dest) ptr.
1535 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1536 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001538 // Push the Src ptr.
1539 CallArgs.push_back(std::make_pair(RValue::get(Src),
1540 MD->getParamDecl(0)->getType()));
Mike Stumped032eb2009-09-04 18:27:16 +00001541 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001542 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1543 Callee, CallArgs, MD);
1544 }
1545 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001547 // Emit the increment of the loop counter.
1548 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1549 Counter = Builder.CreateLoad(IndexPtr);
1550 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1551 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001553 // Finally, branch back up to the condition for the next iteration.
1554 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001556 // Emit the fall-through block.
1557 EmitBlock(AfterFor, true);
1558}
1559
Fariborz Jahanianca283612009-08-07 23:51:33 +00001560/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1561/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001562/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001563void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001564 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001565 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001566 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1567 if (ClassDecl) {
1568 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl);
1569 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ;
1570 }
1571 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1572 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001573 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001574 }
Mike Stump1eb44332009-09-09 15:08:12 +00001575
1576 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001577 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001578 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001579 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001580 CallArgList CallArgs;
1581 // Push the this (Dest) ptr.
1582 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1583 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Fariborz Jahanianca283612009-08-07 23:51:33 +00001585 // Push the Src ptr.
1586 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001587 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001588 QualType ResultType =
Fariborz Jahanianca283612009-08-07 23:51:33 +00001589 BaseCopyCtor->getType()->getAsFunctionType()->getResultType();
1590 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1591 Callee, CallArgs, BaseCopyCtor);
1592 }
1593}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001594
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001595/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001596/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001597/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001598// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001599void CodeGenFunction::EmitClassCopyAssignment(
1600 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001601 const CXXRecordDecl *ClassDecl,
1602 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001603 QualType Ty) {
1604 if (ClassDecl) {
1605 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl);
1606 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ;
1607 }
1608 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1609 EmitAggregateCopy(Dest, Src, Ty);
1610 return;
1611 }
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001613 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001614 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001615 MD);
1616 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1617 (void)ConstCopyAssignOp;
1618
1619 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
Mike Stump1eb44332009-09-09 15:08:12 +00001620 const llvm::Type *LTy =
1621 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001622 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001623 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001625 CallArgList CallArgs;
1626 // Push the this (Dest) ptr.
1627 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1628 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001630 // Push the Src ptr.
1631 CallArgs.push_back(std::make_pair(RValue::get(Src),
1632 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001633 QualType ResultType =
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001634 MD->getType()->getAsFunctionType()->getResultType();
1635 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1636 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001637}
1638
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001639/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001640void
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001641CodeGenFunction::SynthesizeDefaultConstructor(GlobalDecl GD,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001642 const FunctionDecl *FD,
1643 llvm::Function *Fn,
1644 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001645 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(GD.getDecl());
1646
1647 StartFunction(GD, FD->getResultType(), Fn, Args, SourceLocation());
1648 EmitCtorPrologue(Ctor);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001649 FinishFunction();
1650}
1651
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001652/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001653/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001654/// The implicitly-defined copy constructor for class X performs a memberwise
1655/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001656/// of initialization of bases and members in a user-defined constructor
1657/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001658/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001659/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001660/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001661/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001662/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001663/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001664/// Virtual base class subobjects shall be copied only once by the
1665/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001666
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001667void CodeGenFunction::SynthesizeCXXCopyConstructor(GlobalDecl GD,
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001668 const FunctionDecl *FD,
1669 llvm::Function *Fn,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001670 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001671 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(GD.getDecl());
1672 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001673 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001674 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001675 StartFunction(GD, Ctor->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001677 FunctionArgList::const_iterator i = Args.begin();
1678 const VarDecl *ThisArg = i->first;
1679 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1680 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1681 const VarDecl *SrcArg = (i+1)->first;
1682 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1683 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001685 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1686 Base != ClassDecl->bases_end(); ++Base) {
1687 // FIXME. copy constrution of virtual base NYI
1688 if (Base->isVirtual())
1689 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001691 CXXRecordDecl *BaseClassDecl
1692 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001693 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1694 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001695 }
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001697 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1698 FieldEnd = ClassDecl->field_end();
1699 Field != FieldEnd; ++Field) {
1700 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001701 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001702 getContext().getAsConstantArrayType(FieldType);
1703 if (Array)
1704 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001706 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1707 CXXRecordDecl *FieldClassDecl
1708 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1709 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1710 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001711 if (Array) {
1712 const llvm::Type *BasePtr = ConvertType(FieldType);
1713 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001714 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001715 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001716 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001717 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1718 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1719 FieldClassDecl, FieldType);
1720 }
Mike Stump1eb44332009-09-09 15:08:12 +00001721 else
1722 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001723 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001724 continue;
1725 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001726 // Do a built-in assignment of scalar data members.
1727 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1728 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1729 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1730 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001731 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001732 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001733}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001734
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001735/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001736/// Before the implicitly-declared copy assignment operator for a class is
1737/// implicitly defined, all implicitly- declared copy assignment operators for
1738/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001739/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001740/// The implicitly-defined copy assignment operator for class X performs
1741/// memberwise assignment of its subob- jects. The direct base classes of X are
1742/// assigned first, in the order of their declaration in
1743/// the base-specifier-list, and then the immediate nonstatic data members of X
1744/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001745/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001746/// if the subobject is of class type, the copy assignment operator for the
1747/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001748/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001749///
Mike Stump1eb44332009-09-09 15:08:12 +00001750/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001751/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001752///
Mike Stump1eb44332009-09-09 15:08:12 +00001753/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001754/// used.
1755void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
1756 const FunctionDecl *FD,
1757 llvm::Function *Fn,
1758 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001759
1760 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1761 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1762 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001763 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001765 FunctionArgList::const_iterator i = Args.begin();
1766 const VarDecl *ThisArg = i->first;
1767 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1768 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1769 const VarDecl *SrcArg = (i+1)->first;
1770 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1771 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001773 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1774 Base != ClassDecl->bases_end(); ++Base) {
1775 // FIXME. copy assignment of virtual base NYI
1776 if (Base->isVirtual())
1777 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001779 CXXRecordDecl *BaseClassDecl
1780 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1781 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1782 Base->getType());
1783 }
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001785 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1786 FieldEnd = ClassDecl->field_end();
1787 Field != FieldEnd; ++Field) {
1788 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001789 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001790 getContext().getAsConstantArrayType(FieldType);
1791 if (Array)
1792 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001794 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1795 CXXRecordDecl *FieldClassDecl
1796 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1797 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1798 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001799 if (Array) {
1800 const llvm::Type *BasePtr = ConvertType(FieldType);
1801 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1802 llvm::Value *DestBaseAddrPtr =
1803 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1804 llvm::Value *SrcBaseAddrPtr =
1805 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1806 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1807 FieldClassDecl, FieldType);
1808 }
1809 else
Mike Stump1eb44332009-09-09 15:08:12 +00001810 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001811 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001812 continue;
1813 }
1814 // Do a built-in assignment of scalar data members.
1815 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1816 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1817 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1818 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001819 }
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001821 // return *this;
1822 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001824 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001825}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001826
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001827/// EmitCtorPrologue - This routine generates necessary code to initialize
1828/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001829/// FIXME: This needs to take a CXXCtorType.
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001830void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001831 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001832 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001833 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001835 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001836 E = CD->init_end();
1837 B != E; ++B) {
1838 CXXBaseOrMemberInitializer *Member = (*B);
1839 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001840 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001841 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001842 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001843 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001844 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001845 BaseClassDecl);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001846 EmitCXXConstructorCall(Member->getConstructor(),
1847 Ctor_Complete, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001848 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001849 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001850 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001851 // non-static data member initilaizers.
1852 FieldDecl *Field = Member->getMember();
1853 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001854 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001855 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001856 if (Array)
1857 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Mike Stumpf1216772009-07-31 18:25:34 +00001859 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001860 LValue LHS;
1861 if (FieldType->isReferenceType()) {
1862 // FIXME: This is really ugly; should be refactored somehow
1863 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1864 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
1865 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(),
1866 QualType::GCNone, FieldType.getAddressSpace());
1867 } else {
1868 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1869 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001870 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001871 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001872 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001873 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001874 if (Array) {
1875 const llvm::Type *BasePtr = ConvertType(FieldType);
1876 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001877 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001878 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001879 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001880 Array, BaseAddrPtr);
1881 }
1882 else
1883 EmitCXXConstructorCall(Member->getConstructor(),
1884 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001885 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001886 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001887 continue;
1888 }
1889 else {
1890 // Initializing an anonymous union data member.
1891 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001892 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001893 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001894 FieldType = anonMember->getType();
1895 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001896 }
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001898 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001899 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001900 RValue RHS;
1901 if (FieldType->isReferenceType())
1902 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1903 /*IsInitializer=*/true);
1904 else
1905 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1906 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001907 }
1908 }
Mike Stumpf1216772009-07-31 18:25:34 +00001909
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001910 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001911 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001912 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001913 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001914 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001915 ClassDecl->bases_begin();
1916 Base != ClassDecl->bases_end(); ++Base) {
1917 // FIXME. copy assignment of virtual base NYI
1918 if (Base->isVirtual())
1919 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001921 CXXRecordDecl *BaseClassDecl
1922 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1923 if (BaseClassDecl->hasTrivialConstructor())
1924 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001925 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001926 BaseClassDecl->getDefaultConstructor(getContext())) {
1927 LoadOfThis = LoadCXXThis();
1928 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1929 BaseClassDecl);
1930 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1931 }
1932 }
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001934 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1935 FieldEnd = ClassDecl->field_end();
1936 Field != FieldEnd; ++Field) {
1937 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001938 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001939 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001940 if (Array)
1941 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001942 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1943 continue;
1944 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001945 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001946 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1947 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1948 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001949 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001950 MemberClassDecl->getDefaultConstructor(getContext())) {
1951 LoadOfThis = LoadCXXThis();
1952 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001953 if (Array) {
1954 const llvm::Type *BasePtr = ConvertType(FieldType);
1955 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001956 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001957 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1958 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1959 }
1960 else
Mike Stump1eb44332009-09-09 15:08:12 +00001961 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001962 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001963 }
1964 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001965 }
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Mike Stumpf1216772009-07-31 18:25:34 +00001967 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001968 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001969 if (!LoadOfThis)
1970 LoadOfThis = LoadCXXThis();
1971 llvm::Value *VtableField;
1972 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001973 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001974 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1975 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1976 llvm::Value *vtable = GenerateVtable(ClassDecl);
1977 Builder.CreateStore(vtable, VtableField);
1978 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001979}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001980
1981/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001982/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001983/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001984/// FIXME: This needs to take a CXXDtorType.
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001985void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) {
1986 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001987 assert(!ClassDecl->getNumVBases() &&
1988 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001989 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001991 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1992 *E = DD->destr_end(); B != E; ++B) {
1993 uintptr_t BaseOrMember = (*B);
1994 if (DD->isMemberToDestroy(BaseOrMember)) {
1995 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1996 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001997 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001998 getContext().getAsConstantArrayType(FieldType);
1999 if (Array)
2000 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002001 const RecordType *RT = FieldType->getAs<RecordType>();
2002 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2003 if (FieldClassDecl->hasTrivialDestructor())
2004 continue;
2005 llvm::Value *LoadOfThis = LoadCXXThis();
2006 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002007 if (Array) {
2008 const llvm::Type *BasePtr = ConvertType(FieldType);
2009 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002010 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002011 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002012 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002013 Array, BaseAddrPtr);
2014 }
2015 else
2016 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
2017 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00002018 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002019 const RecordType *RT =
2020 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
2021 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2022 if (BaseClassDecl->hasTrivialDestructor())
2023 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002024 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002025 ClassDecl,BaseClassDecl);
2026 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2027 Dtor_Complete, V);
2028 }
2029 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002030 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
2031 return;
2032 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00002033 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002034 // reverse order of their construction.
2035 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00002036
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002037 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
2038 FieldEnd = ClassDecl->field_end();
2039 Field != FieldEnd; ++Field) {
2040 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002041 if (getContext().getAsConstantArrayType(FieldType))
2042 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002043 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
2044 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2045 if (FieldClassDecl->hasTrivialDestructor())
2046 continue;
2047 DestructedFields.push_back(*Field);
2048 }
2049 }
2050 if (!DestructedFields.empty())
2051 for (int i = DestructedFields.size() -1; i >= 0; --i) {
2052 FieldDecl *Field = DestructedFields[i];
2053 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002054 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002055 getContext().getAsConstantArrayType(FieldType);
2056 if (Array)
2057 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002058 const RecordType *RT = FieldType->getAs<RecordType>();
2059 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2060 llvm::Value *LoadOfThis = LoadCXXThis();
2061 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002062 if (Array) {
2063 const llvm::Type *BasePtr = ConvertType(FieldType);
2064 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002065 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002066 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002067 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002068 Array, BaseAddrPtr);
2069 }
2070 else
2071 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
2072 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002073 }
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002075 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
2076 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
2077 Base != ClassDecl->bases_end(); ++Base) {
2078 // FIXME. copy assignment of virtual base NYI
2079 if (Base->isVirtual())
2080 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002082 CXXRecordDecl *BaseClassDecl
2083 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2084 if (BaseClassDecl->hasTrivialDestructor())
2085 continue;
2086 DestructedBases.push_back(BaseClassDecl);
2087 }
2088 if (DestructedBases.empty())
2089 return;
2090 for (int i = DestructedBases.size() -1; i >= 0; --i) {
2091 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Mike Stump1eb44332009-09-09 15:08:12 +00002092 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(),
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002093 ClassDecl,BaseClassDecl);
2094 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2095 Dtor_Complete, V);
2096 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002097}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002098
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002099void CodeGenFunction::SynthesizeDefaultDestructor(GlobalDecl GD,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002100 const FunctionDecl *FD,
2101 llvm::Function *Fn,
2102 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002104 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(GD.getDecl());
2105
2106 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002107 assert(!ClassDecl->hasUserDeclaredDestructor() &&
2108 "SynthesizeDefaultDestructor - destructor has user declaration");
2109 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002111 StartFunction(GD, Dtor->getResultType(), Fn, Args, SourceLocation());
2112 EmitDtorEpilogue(Dtor);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002113 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00002114}