blob: 4c1f6ad4cd3bf19479cbdad2479f1f3aa9e83d6c [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 Carlsson6815e942009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Mike Stump1eb44332009-09-09 15:08:12 +000029void
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor,
31 llvm::Constant *DeclPtr) {
Anders Carlsson6815e942009-09-27 18:58:34 +000032 const llvm::Type *Int8PtrTy =
33 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Mike Stump1eb44332009-09-09 15:08:12 +000034
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000035 std::vector<const llvm::Type *> Params;
36 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000038 // Get the destructor function type
Mike Stump1eb44332009-09-09 15:08:12 +000039 const llvm::Type *DtorFnTy =
Owen Anderson0032b272009-08-13 21:57:51 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000041 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000043 Params.clear();
44 Params.push_back(DtorFnTy);
45 Params.push_back(Int8PtrTy);
46 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000048 // Get the __cxa_atexit function type
49 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump1eb44332009-09-09 15:08:12 +000050 const llvm::FunctionType *AtExitFnTy =
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000051 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000053 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54 "__cxa_atexit");
Mike Stump1eb44332009-09-09 15:08:12 +000055
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000056 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57 "__dso_handle");
Mike Stump1eb44332009-09-09 15:08:12 +000058
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000059 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000061 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
62 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
63 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
64 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
65}
66
Mike Stump1eb44332009-09-09 15:08:12 +000067void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000068 llvm::Constant *DeclPtr) {
69 assert(D.hasGlobalStorage() &&
70 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000071
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000072 const Expr *Init = D.getInit();
73 QualType T = D.getType();
Mike Stump1eb44332009-09-09 15:08:12 +000074
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000075 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000076 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000077 } else if (!hasAggregateLLVMType(T)) {
78 llvm::Value *V = EmitScalarExpr(Init);
79 EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T);
80 } else if (T->isAnyComplexType()) {
81 EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified());
82 } else {
83 EmitAggExpr(Init, DeclPtr, T.isVolatileQualified());
Mike Stump1eb44332009-09-09 15:08:12 +000084
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000085 if (const RecordType *RT = T->getAs<RecordType>()) {
86 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
87 if (!RD->hasTrivialDestructor())
88 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
89 }
90 }
91}
92
Anders Carlsson89ed31d2009-08-08 23:24:23 +000093void
94CodeGenModule::EmitCXXGlobalInitFunc() {
95 if (CXXGlobalInits.empty())
96 return;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Owen Anderson0032b272009-08-13 21:57:51 +000098 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Anders Carlsson89ed31d2009-08-08 23:24:23 +000099 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000101 // Create our global initialization function.
102 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000103 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000104 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
105 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000107 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000108 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000109 CXXGlobalInits.size());
110 AddGlobalCtor(Fn);
111}
112
113void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
114 const VarDecl **Decls,
115 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000116 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000117 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000119 for (unsigned i = 0; i != NumDecls; ++i) {
120 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000122 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
123 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
124 }
125 FinishFunction();
126}
127
Mike Stump1eb44332009-09-09 15:08:12 +0000128void
129CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000130 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000131 // FIXME: This should use __cxa_guard_{acquire,release}?
132
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000133 assert(!getContext().getLangOptions().ThreadsafeStatics &&
134 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000135
Anders Carlsson283a0622009-04-13 18:03:33 +0000136 llvm::SmallString<256> GuardVName;
137 llvm::raw_svector_ostream GuardVOut(GuardVName);
138 mangleGuardVariable(&D, getContext(), GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000140 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000141 llvm::GlobalValue *GuardV =
Owen Anderson0032b272009-08-13 21:57:51 +0000142 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000143 GV->getLinkage(),
Owen Anderson0032b272009-08-13 21:57:51 +0000144 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000145 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000147 // Load the first byte of the guard variable.
Owen Anderson0032b272009-08-13 21:57:51 +0000148 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000149 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000150 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000152 // Compare it against 0.
Owen Anderson0032b272009-08-13 21:57:51 +0000153 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000154 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbar55e87422008-11-11 02:29:29 +0000156 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000157 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000158
159 // If the guard variable is 0, jump to the initializer code.
160 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000162 EmitBlock(InitBlock);
163
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000164 EmitCXXGlobalVarDeclInit(D, GV);
165
Owen Anderson0032b272009-08-13 21:57:51 +0000166 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000167 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000169 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170}
171
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000172RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
173 llvm::Value *Callee,
174 llvm::Value *This,
175 CallExpr::const_arg_iterator ArgBeg,
176 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000177 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000178 "Trying to emit a member call expr on a static method!");
179
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000180 // A call to a trivial destructor requires no code generation.
181 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
182 if (Destructor->isTrivial())
183 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
John McCall183700f2009-09-21 23:43:11 +0000185 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000187 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000189 // Push the this ptr.
190 Args.push_back(std::make_pair(RValue::get(This),
191 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000193 // And the rest of the call args
194 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
John McCall183700f2009-09-21 23:43:11 +0000196 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000197 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
198 Callee, Args, MD);
199}
200
Anders Carlsson774e7c62009-04-03 22:50:24 +0000201RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
202 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
203 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000204
John McCall183700f2009-09-21 23:43:11 +0000205 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000206
Mike Stump1eb44332009-09-09 15:08:12 +0000207 const llvm::Type *Ty =
208 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000209 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000210 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Anders Carlsson774e7c62009-04-03 22:50:24 +0000212 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000213 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000214 else {
215 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000216 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000217 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000218
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000219 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000220 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000221 // virtual call mechanism.
Mike Stumpf0070db2009-08-26 20:46:33 +0000222 llvm::Value *Callee;
Douglas Gregor0979c802009-08-31 21:41:48 +0000223 if (MD->isVirtual() && !ME->hasQualifier())
Mike Stumpf0070db2009-08-26 20:46:33 +0000224 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000225 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000226 = dyn_cast<CXXDestructorDecl>(MD))
227 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000228 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000229 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000230
231 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000232 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000233}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000234
Mike Stump1eb44332009-09-09 15:08:12 +0000235RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000236CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
237 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000238 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000239 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Fariborz Jahanianad258832009-08-13 21:09:41 +0000241 if (MD->isCopyAssignment()) {
242 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
243 if (ClassDecl->hasTrivialCopyAssignment()) {
244 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
245 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
246 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
247 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
248 QualType Ty = E->getType();
249 EmitAggregateCopy(This, Src, Ty);
250 return RValue::get(This);
251 }
252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
John McCall183700f2009-09-21 23:43:11 +0000254 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000255 const llvm::Type *Ty =
256 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000257 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000258 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Anders Carlsson0f294632009-05-27 04:18:27 +0000260 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Anders Carlsson0f294632009-05-27 04:18:27 +0000262 return EmitCXXMemberCall(MD, Callee, This,
263 E->arg_begin() + 1, E->arg_end());
264}
265
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000266llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000267 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000268 "Must be in a C++ member function decl to load 'this'");
269 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
270 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000272 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000273 // ans: See how CodeGenFunction::LoadObjCSelf() uses
274 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000275 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
276}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000277
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000278/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
279/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000280/// array.
281/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
282/// array type and 'ArrayPtr' points to the beginning fo the array.
283/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000284void
285CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000286 const ConstantArrayType *ArrayTy,
287 llvm::Value *ArrayPtr) {
288 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
289 llvm::Value * NumElements =
290 llvm::ConstantInt::get(SizeTy,
291 getContext().getConstantArrayElementCount(ArrayTy));
292
293 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
294}
295
296void
297CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
298 llvm::Value *NumElements,
299 llvm::Value *ArrayPtr) {
300 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000302 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000303 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
304 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
305 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000307 // Start the loop with a block that tests the condition.
308 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
309 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000311 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000313 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000315 // Generate: if (loop-index < number-of-elements fall to the loop body,
316 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000317 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000318 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000319 // If the condition is true, execute the body.
320 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000322 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000324 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000325 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000326 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000327 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
328 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000329 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000331 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000333 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000334 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000335 Counter = Builder.CreateLoad(IndexPtr);
336 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
337 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000339 // Finally, branch back up to the condition for the next iteration.
340 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000342 // Emit the fall-through block.
343 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000344}
345
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000346/// EmitCXXAggrDestructorCall - calls the default destructor on array
347/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000348void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000349CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
350 const ArrayType *Array,
351 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000352 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
353 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000354 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000355 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000356 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000357 // Create a temporary for the loop index and initialize it with count of
358 // array elements.
359 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
360 "loop.index");
361 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000362 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000363 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
364 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +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 Jahanian1c536bf2009-08-20 23:02:58 +0000370 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000372 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000374 // Generate: if (loop-index != 0 fall to the loop body,
375 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000376 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000377 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
378 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
379 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
380 "isne");
381 // If the condition is true, execute the body.
382 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000384 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000386 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
387 // Inside the loop body, emit the constructor call on the array element.
388 Counter = Builder.CreateLoad(IndexPtr);
389 Counter = Builder.CreateSub(Counter, One);
390 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
391 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000393 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000395 // Emit the decrement of the loop counter.
396 Counter = Builder.CreateLoad(IndexPtr);
397 Counter = Builder.CreateSub(Counter, One, "dec");
398 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000400 // Finally, branch back up to the condition for the next iteration.
401 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000403 // Emit the fall-through block.
404 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000405}
406
407void
Mike Stump1eb44332009-09-09 15:08:12 +0000408CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
409 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000410 llvm::Value *This,
411 CallExpr::const_arg_iterator ArgBeg,
412 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000413 if (D->isCopyConstructor(getContext())) {
414 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
415 if (ClassDecl->hasTrivialCopyConstructor()) {
416 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
417 "EmitCXXConstructorCall - user declared copy constructor");
418 const Expr *E = (*ArgBeg);
419 QualType Ty = E->getType();
420 llvm::Value *Src = EmitLValue(E).getAddress();
421 EmitAggregateCopy(This, Src, Ty);
422 return;
423 }
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000426 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
427
428 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000429}
430
Mike Stump1eb44332009-09-09 15:08:12 +0000431void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000432 CXXDtorType Type,
433 llvm::Value *This) {
434 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson7267c162009-05-29 21:03:38 +0000436 EmitCXXMemberCall(D, Callee, This, 0, 0);
437}
438
Mike Stump1eb44332009-09-09 15:08:12 +0000439void
440CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000441 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000442 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000443
444 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000445 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000446 if (RD->hasTrivialConstructor())
447 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000448
Mike Stump1eb44332009-09-09 15:08:12 +0000449 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000450 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000451 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000452 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000453 EmitAggExpr((*i), Dest, false);
454 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000455 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000456 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000457 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000458 E->arg_begin(), E->arg_end());
459}
460
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000461void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000462 EmitGlobal(GlobalDecl(D, Ctor_Complete));
463 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000464}
Anders Carlsson363c1842009-04-16 23:57:24 +0000465
Mike Stump1eb44332009-09-09 15:08:12 +0000466void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000467 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Anders Carlsson27ae5362009-04-17 01:58:57 +0000469 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000471 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Anders Carlsson27ae5362009-04-17 01:58:57 +0000473 SetFunctionDefinitionAttributes(D, Fn);
474 SetLLVMFunctionAttributesForDefinition(D, Fn);
475}
476
Anders Carlsson363c1842009-04-16 23:57:24 +0000477llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000478CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000479 CXXCtorType Type) {
480 const llvm::FunctionType *FTy =
481 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Anders Carlsson363c1842009-04-16 23:57:24 +0000483 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000484 return cast<llvm::Function>(
485 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000486}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000487
Mike Stump1eb44332009-09-09 15:08:12 +0000488const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000489 CXXCtorType Type) {
490 llvm::SmallString<256> Name;
491 llvm::raw_svector_ostream Out(Name);
492 mangleCXXCtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anders Carlsson27ae5362009-04-17 01:58:57 +0000494 Name += '\0';
495 return UniqueMangledName(Name.begin(), Name.end());
496}
497
498void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000499 EmitCXXDestructor(D, Dtor_Complete);
500 EmitCXXDestructor(D, Dtor_Base);
501}
502
Mike Stump1eb44332009-09-09 15:08:12 +0000503void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000504 CXXDtorType Type) {
505 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000507 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Anders Carlsson27ae5362009-04-17 01:58:57 +0000509 SetFunctionDefinitionAttributes(D, Fn);
510 SetLLVMFunctionAttributesForDefinition(D, Fn);
511}
512
513llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000514CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000515 CXXDtorType Type) {
516 const llvm::FunctionType *FTy =
517 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Anders Carlsson27ae5362009-04-17 01:58:57 +0000519 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000520 return cast<llvm::Function>(
521 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000522}
523
Mike Stump1eb44332009-09-09 15:08:12 +0000524const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000525 CXXDtorType Type) {
526 llvm::SmallString<256> Name;
527 llvm::raw_svector_ostream Out(Name);
528 mangleCXXDtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Anders Carlsson27ae5362009-04-17 01:58:57 +0000530 Name += '\0';
531 return UniqueMangledName(Name.begin(), Name.end());
532}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000533
Mike Stump32f37012009-08-18 21:49:00 +0000534llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000535 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000536 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000537 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000538
539 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000540 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000541
542 llvm::SmallString<256> OutName;
543 llvm::raw_svector_ostream Out(OutName);
544 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000545 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000546 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000547 llvm::GlobalVariable::LinkageTypes linktype;
548 linktype = llvm::GlobalValue::WeakAnyLinkage;
549 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000550 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000551 // FIXME: descriptor
552 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000553 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000554 // FIXME: TS
555 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
556
557 llvm::Constant *C;
558 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
559 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000560 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000561 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000562 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
563 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000564}
565
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000566class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000567public:
568 /// Index_t - Vtable index type.
569 typedef uint64_t Index_t;
570private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000571 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000572 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000573 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000574 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000575 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000576 /// BLayout - Layout for the most derived class that this vtable is being
577 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000578 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000579 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000580 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000581 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000582 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000583 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000584 /// Index - Maps a method decl into a vtable index. Useful for virtual
585 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000586 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000587 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
588 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump97f4d462009-09-18 19:06:35 +0000589 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump6e319f62009-09-11 23:25:56 +0000590 typedef std::pair<Index_t, Index_t> CallOffset;
591 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
Mike Stump77ca8f62009-09-05 07:20:32 +0000592 Thunks_t Thunks;
Mike Stump6e319f62009-09-11 23:25:56 +0000593 typedef llvm::DenseMap<const CXXMethodDecl *,
594 std::pair<CallOffset, CallOffset> > CovariantThunks_t;
595 CovariantThunks_t CovariantThunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000596 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000597 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000598 // FIXME: Linkage should follow vtable
599 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000600 const uint32_t LLVMPointerWidth;
601 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000602public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000603 VtableBuilder(std::vector<llvm::Constant *> &meth,
604 const CXXRecordDecl *c,
605 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000606 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
607 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000608 CGM(cgm), Extern(true),
609 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000610 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
611 }
Mike Stump32f37012009-08-18 21:49:00 +0000612
Mike Stumpf0070db2009-08-26 20:46:33 +0000613 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stump97f4d462009-09-18 19:06:35 +0000614 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
615 { return VBIndex; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000616
Mike Stump15a24e02009-08-28 23:22:54 +0000617 llvm::Constant *wrap(Index_t i) {
618 llvm::Constant *m;
619 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
620 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000621 }
622
Mike Stump15a24e02009-08-28 23:22:54 +0000623 llvm::Constant *wrap(llvm::Constant *m) {
624 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000625 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000626
Mike Stump7fa0d932009-08-20 02:11:48 +0000627 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000628 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump97f4d462009-09-18 19:06:35 +0000629 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Mike Stump7fa0d932009-08-20 02:11:48 +0000630 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000631 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000632 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
633 if (i->isVirtual() && !SeenVBase.count(Base)) {
634 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000635 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000636 llvm::Constant *m = wrap(BaseOffset);
637 m = wrap((0?700:0) + BaseOffset);
Mike Stump97f4d462009-09-18 19:06:35 +0000638 VBIndex[Base] = -(offsets.size()*LLVMPointerWidth/8)
639 - 3*LLVMPointerWidth/8;
Mike Stump7fa0d932009-08-20 02:11:48 +0000640 offsets.push_back(m);
641 }
Mike Stumpb9837442009-08-20 07:22:17 +0000642 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000643 }
644 }
645
Mike Stumpb9871a22009-08-21 01:45:00 +0000646 void StartNewTable() {
647 SeenVBase.clear();
648 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000649
Mike Stump97f4d462009-09-18 19:06:35 +0000650 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
651
652 /// getVbaseOffset - Returns the index into the vtable for the virtual base
653 /// offset for the given (B) virtual base of the derived class D.
654 Index_t getVbaseOffset(QualType qB, QualType qD) {
655 qD = qD->getAs<PointerType>()->getPointeeType();
656 qB = qB->getAs<PointerType>()->getPointeeType();
657 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
658 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
659 if (D != Class)
660 return VBlookup(D, B);
661 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
662 i = VBIndex.find(B);
663 if (i != VBIndex.end())
664 return i->second;
665 // FIXME: temporal botch, is this data here, by the time we need it?
666
667 // FIXME: Locate the containing virtual base first.
668 return 42;
669 }
670
Mike Stump35191b62009-09-01 22:20:28 +0000671 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000672 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000673 typedef CXXMethodDecl::method_iterator meth_iter;
674
Mike Stumpb9871a22009-08-21 01:45:00 +0000675 // FIXME: Don't like the nested loops. For very large inheritance
676 // heirarchies we could have a table on the side with the final overridder
677 // and just replace each instance of an overridden method once. Would be
678 // nice to measure the cost/benefit on real code.
679
Mike Stumpb9871a22009-08-21 01:45:00 +0000680 for (meth_iter mi = MD->begin_overridden_methods(),
681 e = MD->end_overridden_methods();
682 mi != e; ++mi) {
683 const CXXMethodDecl *OMD = *mi;
684 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000685 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000686 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
687
Mike Stumpdec025b2009-09-07 04:27:52 +0000688 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000689 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000690 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000691 if (submethods[i] != om)
692 continue;
John McCall183700f2009-09-21 23:43:11 +0000693 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000694 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
John McCall183700f2009-09-21 23:43:11 +0000695 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000696 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
697 CallOffset ReturnOffset = std::make_pair(0, 0);
698 if (oret != ret) {
699 // FIXME: calculate offsets for covariance
Mike Stump97f4d462009-09-18 19:06:35 +0000700 ReturnOffset = std::make_pair(42,getVbaseOffset(oret, ret));
Mike Stump6e319f62009-09-11 23:25:56 +0000701 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000702 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000703 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000704
705 Thunks.erase(OMD);
706 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000707 Index_t &idx = VCall[OMD];
708 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000709 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000710 idx = VCalls.size()+1;
711 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000712 } else {
713 VCallOffset[MD] = VCallOffset[OMD];
714 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000715 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000716 VCall[MD] = idx;
Mike Stump6e319f62009-09-11 23:25:56 +0000717 CallOffset ThisOffset;
718 // FIXME: calculate non-virtual offset
719 ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
720 if (ReturnOffset.first || ReturnOffset.second)
721 CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset);
722 else
723 Thunks[MD] = ThisOffset;
Mike Stump35191b62009-09-01 22:20:28 +0000724 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000725 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000726#if 0
727 // FIXME: finish off
728 int64_t O = VCallOffset[OMD] - Offset/8;
729 if (O) {
730 Thunks[MD] = std::make_pair(O, 0);
731 }
732#endif
733 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000734 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000735 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000736
Mike Stump35191b62009-09-01 22:20:28 +0000737 return false;
738 }
739
Mike Stump98cc7102009-09-05 11:28:33 +0000740 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000741 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
742 i != e; ++i) {
743 const CXXMethodDecl *MD = i->first;
744 Index_t idx = Index[MD];
745 Index_t nv_O = i->second.first;
746 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000747 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000748 }
749 Thunks.clear();
Mike Stump6e319f62009-09-11 23:25:56 +0000750 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
751 e = CovariantThunks.end();
752 i != e; ++i) {
753 const CXXMethodDecl *MD = i->first;
754 Index_t idx = Index[MD];
755 Index_t nv_t = i->second.first.first;
756 Index_t v_t = i->second.first.second;
757 Index_t nv_r = i->second.second.first;
758 Index_t v_r = i->second.second.second;
759 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
760 v_r);
761 }
762 CovariantThunks.clear();
Mike Stump77ca8f62009-09-05 07:20:32 +0000763 }
764
Mike Stumpdec025b2009-09-07 04:27:52 +0000765 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
766 int64_t> > *Path, bool MorallyVirtual) {
767 for (std::vector<std::pair<const CXXRecordDecl *,
768 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000769 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000770 const CXXRecordDecl *RD = i->first;
771 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000772 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
773 ++mi)
774 if (mi->isVirtual()) {
775 const CXXMethodDecl *MD = *mi;
Anders Carlssonc7cba152009-09-12 00:00:29 +0000776 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD));
Mike Stumpdec025b2009-09-07 04:27:52 +0000777 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000778 }
779 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000780 }
781
Mike Stump6d10eb82009-09-05 07:49:12 +0000782 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlssonc7cba152009-09-12 00:00:29 +0000783 llvm::Constant *m = 0;
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000784 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonc7cba152009-09-12 00:00:29 +0000785 m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000786 else
Anders Carlssonc7cba152009-09-12 00:00:29 +0000787 m = wrap(CGM.GetAddrOfFunction(MD));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000788
Mike Stump77ca8f62009-09-05 07:20:32 +0000789 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000790 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000791 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Mike Stumpb9871a22009-08-21 01:45:00 +0000793 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000794 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000795 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000796 if (MorallyVirtual) {
797 VCallOffset[MD] = Offset/8;
798 Index_t &idx = VCall[MD];
799 // Allocate the first one, after that, we reuse the previous one.
800 if (idx == 0) {
801 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000802 VCalls.push_back(0);
803 }
804 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000805 }
806
Mike Stump6d10eb82009-09-05 07:49:12 +0000807 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
808 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000809 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
810 ++mi)
811 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +0000812 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +0000813 }
Mike Stump65defe32009-08-18 21:03:28 +0000814
Mike Stump77ca8f62009-09-05 07:20:32 +0000815 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
816 const CXXRecordDecl *PrimaryBase,
817 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
818 int64_t Offset) {
819 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
820 e = RD->bases_end(); i != e; ++i) {
821 if (i->isVirtual())
822 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000823 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +0000824 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
825 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
826 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
827 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +0000828 std::vector<std::pair<const CXXRecordDecl *,
829 int64_t> > S;
830 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +0000831 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +0000832 }
833 }
834 }
835
Mike Stump6d10eb82009-09-05 07:49:12 +0000836 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
837 const ASTRecordLayout &Layout,
838 const CXXRecordDecl *PrimaryBase,
839 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
840 int64_t Offset, bool ForVirtualBase) {
841 StartNewTable();
842 extra = 0;
843 // FIXME: Cleanup.
844 if (!ForVirtualBase) {
845 // then virtual base offsets...
846 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
847 e = offsets.rend(); i != e; ++i)
848 methods.push_back(*i);
849 }
850
851 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +0000852 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
853 e=VCalls.rend();
854 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +0000855 methods.push_back(wrap((0?600:0) + *i));
856 VCalls.clear();
857
858 if (ForVirtualBase) {
859 // then virtual base offsets...
860 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
861 e = offsets.rend(); i != e; ++i)
862 methods.push_back(*i);
863 }
864
865 methods.push_back(wrap(-(Offset/8)));
866 methods.push_back(rtti);
867 Index_t AddressPoint = methods.size();
868
Mike Stump98cc7102009-09-05 11:28:33 +0000869 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +0000870 methods.insert(methods.end(), submethods.begin(), submethods.end());
871 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +0000872
873 // and then the non-virtual bases.
874 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
875 MorallyVirtual, Offset);
876 return AddressPoint;
877 }
878
Mike Stump078d7782009-09-05 08:40:18 +0000879 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +0000880 if (!RD->isDynamicClass())
881 return;
882
883 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000884 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +0000885 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
886
Mike Stump9bbe9622009-09-05 08:37:03 +0000887 // vtables are composed from the chain of primaries.
888 if (PrimaryBase) {
889 if (PrimaryBaseWasVirtual)
890 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +0000891 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +0000892 }
893
894 // And add the virtuals for the class to the primary vtable.
895 AddMethods(RD, MorallyVirtual, Offset);
896 }
897
Mike Stumpe45c90f2009-09-05 09:10:58 +0000898 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +0000899 bool MorallyVirtual = false, int64_t Offset = 0,
900 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +0000901 std::vector<std::pair<const CXXRecordDecl *,
902 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +0000903 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +0000904 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +0000905
906 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000907 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +0000908 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
909
Mike Stump15a24e02009-08-28 23:22:54 +0000910 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +0000911 extra = 0;
912 GenerateVBaseOffsets(offsets, RD, Offset);
913 if (ForVirtualBase)
914 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +0000915
916 // vtables are composed from the chain of primaries.
917 if (PrimaryBase) {
918 if (PrimaryBaseWasVirtual)
919 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +0000920 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +0000921 }
922
Mike Stump15a24e02009-08-28 23:22:54 +0000923 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +0000924 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +0000925
Mike Stump98cc7102009-09-05 11:28:33 +0000926 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +0000927 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +0000928
Mike Stump6d10eb82009-09-05 07:49:12 +0000929 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
930 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +0000931 }
932
Mike Stump98cc7102009-09-05 11:28:33 +0000933 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +0000934 int64_t Offset = 0,
935 std::vector<std::pair<const CXXRecordDecl *,
936 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +0000937 bool alloc = false;
938 if (Path == 0) {
939 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +0000940 Path = new std::vector<std::pair<const CXXRecordDecl *,
941 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +0000942 }
943 // FIXME: We also need to override using all paths to a virtual base,
944 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +0000945 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +0000946 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
947 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000948 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +0000949 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
950 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
951 // Mark it so we don't output it twice.
952 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +0000953 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +0000954 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +0000955 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +0000956 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000957 int64_t BaseOffset = Offset;
958 if (i->isVirtual())
959 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +0000960 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +0000961 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +0000962 }
Mike Stump98cc7102009-09-05 11:28:33 +0000963 Path->pop_back();
964 if (alloc)
965 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +0000966 }
Mike Stump109b13d2009-08-18 21:30:21 +0000967};
Mike Stump8a12b562009-08-06 15:50:11 +0000968
Mike Stumpf0070db2009-08-26 20:46:33 +0000969class VtableInfo {
970public:
971 typedef VtableBuilder::Index_t Index_t;
972private:
973 CodeGenModule &CGM; // Per-module state.
974 /// Index_t - Vtable index type.
975 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
976 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
977 // FIXME: Move to Context.
978 static MapTy IndexFor;
Mike Stump97f4d462009-09-18 19:06:35 +0000979
980 typedef llvm::DenseMap<const CXXRecordDecl *, Index_t> VBElTy;
981 typedef llvm::DenseMap<const CXXRecordDecl *, VBElTy *> VBMapTy;
982 // FIXME: Move to Context.
983 static VBMapTy VBIndexFor;
Mike Stumpf0070db2009-08-26 20:46:33 +0000984public:
985 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
Mike Stump97f4d462009-09-18 19:06:35 +0000986 void RegisterIndex(const CXXRecordDecl *RD, const ElTy &e) {
Mike Stumpf0070db2009-08-26 20:46:33 +0000987 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
988 // We own a copy of this, it will go away shortly.
Mike Stumpf0070db2009-08-26 20:46:33 +0000989 IndexFor[RD] = new ElTy (e);
990 }
Mike Stump97f4d462009-09-18 19:06:35 +0000991 void RegisterVBIndex(const CXXRecordDecl *RD, const VBElTy &e) {
992 assert(VBIndexFor.find(RD) == VBIndexFor.end() && "Don't compute vtbl twice");
993 // We own a copy of this, it will go away shortly.
994 VBIndexFor[RD] = new VBElTy (e);
995 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000996 Index_t lookup(const CXXMethodDecl *MD) {
997 const CXXRecordDecl *RD = MD->getParent();
998 MapTy::iterator I = IndexFor.find(RD);
999 if (I == IndexFor.end()) {
1000 std::vector<llvm::Constant *> methods;
Mike Stump97f4d462009-09-18 19:06:35 +00001001 // FIXME: This seems expensive. Can we do a partial job to get
1002 // just this data.
Mike Stumpf0070db2009-08-26 20:46:33 +00001003 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001004 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001005 b.GenerateVtableForVBases(RD);
Mike Stump97f4d462009-09-18 19:06:35 +00001006 RegisterIndex(RD, b.getIndex());
Mike Stumpf0070db2009-08-26 20:46:33 +00001007 I = IndexFor.find(RD);
1008 }
1009 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1010 return (*I->second)[MD];
1011 }
Mike Stump97f4d462009-09-18 19:06:35 +00001012 Index_t VBlookup(const CXXRecordDecl *RD, const CXXRecordDecl *BD) {
1013 VBMapTy::iterator I = VBIndexFor.find(RD);
1014 if (I == VBIndexFor.end()) {
1015 std::vector<llvm::Constant *> methods;
1016 // FIXME: This seems expensive. Can we do a partial job to get
1017 // just this data.
1018 VtableBuilder b(methods, RD, CGM);
1019 b.GenerateVtableForBase(RD);
1020 b.GenerateVtableForVBases(RD);
1021 RegisterVBIndex(RD, b.getVBIndex());
1022 I = VBIndexFor.find(RD);
1023 }
1024 assert(I->second->find(BD)!=I->second->end() && "Can't find vtable index");
1025 return (*I->second)[BD];
1026 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001027};
1028
Mike Stump97f4d462009-09-18 19:06:35 +00001029// FIXME: move to Context
1030static VtableInfo *vtableinfo;
1031
1032VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
1033 CXXRecordDecl *B) {
1034 if (vtableinfo == 0)
1035 vtableinfo = new VtableInfo(CGM);
1036
1037 return vtableinfo->VBlookup(D, B);
1038}
1039
1040
Mike Stumpf0070db2009-08-26 20:46:33 +00001041// FIXME: Move to Context.
1042VtableInfo::MapTy VtableInfo::IndexFor;
1043
Mike Stump97f4d462009-09-18 19:06:35 +00001044// FIXME: Move to Context.
1045VtableInfo::VBMapTy VtableInfo::VBIndexFor;
1046
Mike Stumpf1216772009-07-31 18:25:34 +00001047llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001048 llvm::SmallString<256> OutName;
1049 llvm::raw_svector_ostream Out(OutName);
1050 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001051 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001052 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001053 llvm::GlobalVariable::LinkageTypes linktype;
1054 linktype = llvm::GlobalValue::WeakAnyLinkage;
1055 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001056 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001057 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001058
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001059 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001060
Mike Stump276b9f12009-08-16 01:46:26 +00001061 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001062 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001063
Mike Stump276b9f12009-08-16 01:46:26 +00001064 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001065 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001066
Mike Stump82b56962009-07-31 21:43:43 +00001067 llvm::Constant *C;
1068 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1069 C = llvm::ConstantArray::get(type, methods);
1070 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001071 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001072 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001073 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001074 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001075 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001076 return vtable;
1077}
1078
Mike Stumped032eb2009-09-04 18:27:16 +00001079llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1080 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001081 bool Extern, int64_t nv,
1082 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +00001083 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +00001084
1085 FunctionArgList Args;
1086 ImplicitParamDecl *ThisDecl =
1087 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1088 MD->getThisType(getContext()));
1089 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1090 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1091 e = MD->param_end();
1092 i != e; ++i) {
1093 ParmVarDecl *D = *i;
1094 Args.push_back(std::make_pair(D, D->getType()));
1095 }
1096 IdentifierInfo *II
1097 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1098 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1099 getContext().getTranslationUnitDecl(),
1100 SourceLocation(), II, R, 0,
1101 Extern
1102 ? FunctionDecl::Extern
1103 : FunctionDecl::Static,
1104 false, true);
1105 StartFunction(FD, R, Fn, Args, SourceLocation());
1106 // FIXME: generate body
1107 FinishFunction();
1108 return Fn;
1109}
1110
Mike Stump6e319f62009-09-11 23:25:56 +00001111llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1112 const CXXMethodDecl *MD,
1113 bool Extern,
1114 int64_t nv_t,
1115 int64_t v_t,
1116 int64_t nv_r,
1117 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +00001118 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +00001119
1120 FunctionArgList Args;
1121 ImplicitParamDecl *ThisDecl =
1122 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1123 MD->getThisType(getContext()));
1124 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1125 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1126 e = MD->param_end();
1127 i != e; ++i) {
1128 ParmVarDecl *D = *i;
1129 Args.push_back(std::make_pair(D, D->getType()));
1130 }
1131 IdentifierInfo *II
1132 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1133 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1134 getContext().getTranslationUnitDecl(),
1135 SourceLocation(), II, R, 0,
1136 Extern
1137 ? FunctionDecl::Extern
1138 : FunctionDecl::Static,
1139 false, true);
1140 StartFunction(FD, R, Fn, Args, SourceLocation());
1141 // FIXME: generate body
1142 FinishFunction();
1143 return Fn;
1144}
1145
Mike Stump77ca8f62009-09-05 07:20:32 +00001146llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1147 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001148 llvm::SmallString<256> OutName;
1149 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001150 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001151 llvm::GlobalVariable::LinkageTypes linktype;
1152 linktype = llvm::GlobalValue::WeakAnyLinkage;
1153 if (!Extern)
1154 linktype = llvm::GlobalValue::InternalLinkage;
1155 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001156 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +00001157 const llvm::FunctionType *FTy =
1158 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1159 FPT->isVariadic());
1160
1161 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1162 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001163 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001164 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1165 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1166 return m;
1167}
1168
Mike Stump6e319f62009-09-11 23:25:56 +00001169llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1170 bool Extern, int64_t nv_t,
1171 int64_t v_t, int64_t nv_r,
1172 int64_t v_r) {
1173 llvm::SmallString<256> OutName;
1174 llvm::raw_svector_ostream Out(OutName);
1175 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1176 llvm::GlobalVariable::LinkageTypes linktype;
1177 linktype = llvm::GlobalValue::WeakAnyLinkage;
1178 if (!Extern)
1179 linktype = llvm::GlobalValue::InternalLinkage;
1180 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001181 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +00001182 const llvm::FunctionType *FTy =
1183 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1184 FPT->isVariadic());
1185
1186 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1187 &getModule());
1188 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1189 v_r);
1190 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1191 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1192 return m;
1193}
1194
Mike Stumpf0070db2009-08-26 20:46:33 +00001195llvm::Value *
1196CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1197 const llvm::Type *Ty) {
1198 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Mike Stumpf0070db2009-08-26 20:46:33 +00001200 // FIXME: move to Context
1201 if (vtableinfo == 0)
1202 vtableinfo = new VtableInfo(CGM);
1203
1204 VtableInfo::Index_t Idx = vtableinfo->lookup(MD);
1205
1206 Ty = llvm::PointerType::get(Ty, 0);
1207 Ty = llvm::PointerType::get(Ty, 0);
1208 Ty = llvm::PointerType::get(Ty, 0);
1209 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1210 vtbl = Builder.CreateLoad(vtbl);
1211 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
1212 Idx, "vfn");
1213 vfn = Builder.CreateLoad(vfn);
1214 return vfn;
1215}
1216
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001217/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1218/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1219/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001220// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001221void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001222 llvm::Value *Src,
1223 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001224 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001225 QualType Ty) {
1226 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1227 assert(CA && "VLA cannot be copied over");
1228 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001230 // Create a temporary for the loop index and initialize it with 0.
1231 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1232 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001233 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001234 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1235 Builder.CreateStore(zeroConstant, IndexPtr, false);
1236 // Start the loop with a block that tests the condition.
1237 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1238 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001240 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001242 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1243 // Generate: if (loop-index < number-of-elements fall to the loop body,
1244 // otherwise, go to the block after the for-loop.
1245 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001246 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001247 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1248 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001249 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001250 "isless");
1251 // If the condition is true, execute the body.
1252 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001254 EmitBlock(ForBody);
1255 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1256 // Inside the loop body, emit the constructor call on the array element.
1257 Counter = Builder.CreateLoad(IndexPtr);
1258 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1259 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1260 if (BitwiseCopy)
1261 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001262 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001263 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001264 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001265 Ctor_Complete);
1266 CallArgList CallArgs;
1267 // Push the this (Dest) ptr.
1268 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1269 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001271 // Push the Src ptr.
1272 CallArgs.push_back(std::make_pair(RValue::get(Src),
1273 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001274 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001275 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001276 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1277 Callee, CallArgs, BaseCopyCtor);
1278 }
1279 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001281 // Emit the increment of the loop counter.
1282 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1283 Counter = Builder.CreateLoad(IndexPtr);
1284 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1285 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001287 // Finally, branch back up to the condition for the next iteration.
1288 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001290 // Emit the fall-through block.
1291 EmitBlock(AfterFor, true);
1292}
1293
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001294/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001295/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001296/// bitwise assignment or via a copy assignment operator function call.
1297/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001298void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001299 llvm::Value *Src,
1300 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001301 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001302 QualType Ty) {
1303 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1304 assert(CA && "VLA cannot be asssigned");
1305 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001307 // Create a temporary for the loop index and initialize it with 0.
1308 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1309 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001310 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001311 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1312 Builder.CreateStore(zeroConstant, IndexPtr, false);
1313 // Start the loop with a block that tests the condition.
1314 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1315 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001317 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001319 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1320 // Generate: if (loop-index < number-of-elements fall to the loop body,
1321 // otherwise, go to the block after the for-loop.
1322 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001323 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001324 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1325 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001326 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001327 "isless");
1328 // If the condition is true, execute the body.
1329 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001331 EmitBlock(ForBody);
1332 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1333 // Inside the loop body, emit the assignment operator call on array element.
1334 Counter = Builder.CreateLoad(IndexPtr);
1335 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1336 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1337 const CXXMethodDecl *MD = 0;
1338 if (BitwiseAssign)
1339 EmitAggregateCopy(Dest, Src, Ty);
1340 else {
1341 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1342 MD);
1343 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1344 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001345 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001346 const llvm::Type *LTy =
1347 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1348 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001349 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001351 CallArgList CallArgs;
1352 // Push the this (Dest) ptr.
1353 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1354 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001356 // Push the Src ptr.
1357 CallArgs.push_back(std::make_pair(RValue::get(Src),
1358 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001359 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001360 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1361 Callee, CallArgs, MD);
1362 }
1363 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001365 // Emit the increment of the loop counter.
1366 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1367 Counter = Builder.CreateLoad(IndexPtr);
1368 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1369 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001371 // Finally, branch back up to the condition for the next iteration.
1372 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001374 // Emit the fall-through block.
1375 EmitBlock(AfterFor, true);
1376}
1377
Fariborz Jahanianca283612009-08-07 23:51:33 +00001378/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1379/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001380/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001381void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001382 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001383 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001384 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1385 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001386 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1387 /*NullCheckValue=*/false);
1388 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1389 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001390 }
1391 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1392 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001393 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001394 }
Mike Stump1eb44332009-09-09 15:08:12 +00001395
1396 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001397 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001398 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001399 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001400 CallArgList CallArgs;
1401 // Push the this (Dest) ptr.
1402 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1403 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Fariborz Jahanianca283612009-08-07 23:51:33 +00001405 // Push the Src ptr.
1406 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001407 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001408 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001409 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001410 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1411 Callee, CallArgs, BaseCopyCtor);
1412 }
1413}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001414
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001415/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001416/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001417/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001418// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001419void CodeGenFunction::EmitClassCopyAssignment(
1420 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001421 const CXXRecordDecl *ClassDecl,
1422 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001423 QualType Ty) {
1424 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001425 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1426 /*NullCheckValue=*/false);
1427 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1428 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001429 }
1430 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1431 EmitAggregateCopy(Dest, Src, Ty);
1432 return;
1433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001435 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001436 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001437 MD);
1438 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1439 (void)ConstCopyAssignOp;
1440
John McCall183700f2009-09-21 23:43:11 +00001441 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001442 const llvm::Type *LTy =
1443 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001444 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001445 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001447 CallArgList CallArgs;
1448 // Push the this (Dest) ptr.
1449 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1450 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001452 // Push the Src ptr.
1453 CallArgs.push_back(std::make_pair(RValue::get(Src),
1454 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001455 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001456 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001457 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1458 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001459}
1460
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001461/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001462void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001463CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1464 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001465 llvm::Function *Fn,
1466 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001467 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1468 SourceLocation());
1469 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001470 FinishFunction();
1471}
1472
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001473/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001474/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001475/// The implicitly-defined copy constructor for class X performs a memberwise
1476/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001477/// of initialization of bases and members in a user-defined constructor
1478/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001479/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001480/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001481/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001482/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001483/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001484/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001485/// Virtual base class subobjects shall be copied only once by the
1486/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001487
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001488void
1489CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1490 CXXCtorType Type,
1491 llvm::Function *Fn,
1492 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001493 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001494 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001495 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001496 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1497 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001499 FunctionArgList::const_iterator i = Args.begin();
1500 const VarDecl *ThisArg = i->first;
1501 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1502 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1503 const VarDecl *SrcArg = (i+1)->first;
1504 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1505 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001507 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1508 Base != ClassDecl->bases_end(); ++Base) {
1509 // FIXME. copy constrution of virtual base NYI
1510 if (Base->isVirtual())
1511 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001513 CXXRecordDecl *BaseClassDecl
1514 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001515 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1516 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001517 }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001519 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1520 FieldEnd = ClassDecl->field_end();
1521 Field != FieldEnd; ++Field) {
1522 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001523 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001524 getContext().getAsConstantArrayType(FieldType);
1525 if (Array)
1526 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001528 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1529 CXXRecordDecl *FieldClassDecl
1530 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1531 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1532 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001533 if (Array) {
1534 const llvm::Type *BasePtr = ConvertType(FieldType);
1535 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001536 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001537 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001538 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001539 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1540 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1541 FieldClassDecl, FieldType);
1542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543 else
1544 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001545 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001546 continue;
1547 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001548 // Do a built-in assignment of scalar data members.
1549 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1550 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1551 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1552 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001553 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001554 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001555}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001556
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001557/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001558/// Before the implicitly-declared copy assignment operator for a class is
1559/// implicitly defined, all implicitly- declared copy assignment operators for
1560/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001561/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001562/// The implicitly-defined copy assignment operator for class X performs
1563/// memberwise assignment of its subob- jects. The direct base classes of X are
1564/// assigned first, in the order of their declaration in
1565/// the base-specifier-list, and then the immediate nonstatic data members of X
1566/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001567/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001568/// if the subobject is of class type, the copy assignment operator for the
1569/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001570/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001571///
Mike Stump1eb44332009-09-09 15:08:12 +00001572/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001573/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001574///
Mike Stump1eb44332009-09-09 15:08:12 +00001575/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001576/// used.
1577void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001578 llvm::Function *Fn,
1579 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001580
1581 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1582 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1583 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001584 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001586 FunctionArgList::const_iterator i = Args.begin();
1587 const VarDecl *ThisArg = i->first;
1588 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1589 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1590 const VarDecl *SrcArg = (i+1)->first;
1591 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1592 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001594 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1595 Base != ClassDecl->bases_end(); ++Base) {
1596 // FIXME. copy assignment of virtual base NYI
1597 if (Base->isVirtual())
1598 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001600 CXXRecordDecl *BaseClassDecl
1601 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1602 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1603 Base->getType());
1604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001606 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1607 FieldEnd = ClassDecl->field_end();
1608 Field != FieldEnd; ++Field) {
1609 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001610 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001611 getContext().getAsConstantArrayType(FieldType);
1612 if (Array)
1613 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001615 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1616 CXXRecordDecl *FieldClassDecl
1617 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1618 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1619 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001620 if (Array) {
1621 const llvm::Type *BasePtr = ConvertType(FieldType);
1622 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1623 llvm::Value *DestBaseAddrPtr =
1624 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1625 llvm::Value *SrcBaseAddrPtr =
1626 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1627 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1628 FieldClassDecl, FieldType);
1629 }
1630 else
Mike Stump1eb44332009-09-09 15:08:12 +00001631 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001632 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001633 continue;
1634 }
1635 // Do a built-in assignment of scalar data members.
1636 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1637 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1638 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1639 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001640 }
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001642 // return *this;
1643 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001645 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001646}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001647
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001648/// EmitCtorPrologue - This routine generates necessary code to initialize
1649/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001650/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001651void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1652 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001653 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001654 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001655 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001657 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001658 E = CD->init_end();
1659 B != E; ++B) {
1660 CXXBaseOrMemberInitializer *Member = (*B);
1661 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001662 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001663 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001664 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001665 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001666 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1667 BaseClassDecl,
1668 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001669 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001670 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001671 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001672 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001673 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001674 // non-static data member initilaizers.
1675 FieldDecl *Field = Member->getMember();
1676 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001677 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001678 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001679 if (Array)
1680 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Mike Stumpf1216772009-07-31 18:25:34 +00001682 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001683 LValue LHS;
1684 if (FieldType->isReferenceType()) {
1685 // FIXME: This is really ugly; should be refactored somehow
1686 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1687 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001688 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1689 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001690 } else {
1691 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1692 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001693 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001694 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001695 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001696 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001697 if (Array) {
1698 const llvm::Type *BasePtr = ConvertType(FieldType);
1699 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001700 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001701 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001702 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001703 Array, BaseAddrPtr);
1704 }
1705 else
1706 EmitCXXConstructorCall(Member->getConstructor(),
1707 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001708 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001709 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001710 continue;
1711 }
1712 else {
1713 // Initializing an anonymous union data member.
1714 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001715 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001716 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001717 FieldType = anonMember->getType();
1718 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001719 }
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001721 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001722 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001723 RValue RHS;
1724 if (FieldType->isReferenceType())
1725 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1726 /*IsInitializer=*/true);
1727 else
1728 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1729 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001730 }
1731 }
Mike Stumpf1216772009-07-31 18:25:34 +00001732
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001733 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001734 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001735 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001736 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001737 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001738 ClassDecl->bases_begin();
1739 Base != ClassDecl->bases_end(); ++Base) {
1740 // FIXME. copy assignment of virtual base NYI
1741 if (Base->isVirtual())
1742 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001744 CXXRecordDecl *BaseClassDecl
1745 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1746 if (BaseClassDecl->hasTrivialConstructor())
1747 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001748 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001749 BaseClassDecl->getDefaultConstructor(getContext())) {
1750 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001751 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1752 BaseClassDecl,
1753 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001754 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1755 }
1756 }
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001758 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1759 FieldEnd = ClassDecl->field_end();
1760 Field != FieldEnd; ++Field) {
1761 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001762 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001763 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001764 if (Array)
1765 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001766 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1767 continue;
1768 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001769 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001770 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1771 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1772 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001773 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001774 MemberClassDecl->getDefaultConstructor(getContext())) {
1775 LoadOfThis = LoadCXXThis();
1776 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001777 if (Array) {
1778 const llvm::Type *BasePtr = ConvertType(FieldType);
1779 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001780 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001781 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1782 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1783 }
1784 else
Mike Stump1eb44332009-09-09 15:08:12 +00001785 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001786 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001787 }
1788 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001789 }
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Mike Stumpf1216772009-07-31 18:25:34 +00001791 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001792 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001793 if (!LoadOfThis)
1794 LoadOfThis = LoadCXXThis();
1795 llvm::Value *VtableField;
1796 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001797 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001798 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1799 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1800 llvm::Value *vtable = GenerateVtable(ClassDecl);
1801 Builder.CreateStore(vtable, VtableField);
1802 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001803}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001804
1805/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001806/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001807/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001808/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001809void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1810 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001811 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001812 assert(!ClassDecl->getNumVBases() &&
1813 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001814 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001816 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1817 *E = DD->destr_end(); B != E; ++B) {
1818 uintptr_t BaseOrMember = (*B);
1819 if (DD->isMemberToDestroy(BaseOrMember)) {
1820 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1821 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001822 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001823 getContext().getAsConstantArrayType(FieldType);
1824 if (Array)
1825 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001826 const RecordType *RT = FieldType->getAs<RecordType>();
1827 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1828 if (FieldClassDecl->hasTrivialDestructor())
1829 continue;
1830 llvm::Value *LoadOfThis = LoadCXXThis();
1831 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001832 if (Array) {
1833 const llvm::Type *BasePtr = ConvertType(FieldType);
1834 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001835 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001836 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001837 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001838 Array, BaseAddrPtr);
1839 }
1840 else
1841 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1842 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001843 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001844 const RecordType *RT =
1845 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1846 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1847 if (BaseClassDecl->hasTrivialDestructor())
1848 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001849 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1850 ClassDecl, BaseClassDecl,
1851 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001852 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001853 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001854 }
1855 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001856 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1857 return;
1858 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001859 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001860 // reverse order of their construction.
1861 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001863 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1864 FieldEnd = ClassDecl->field_end();
1865 Field != FieldEnd; ++Field) {
1866 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001867 if (getContext().getAsConstantArrayType(FieldType))
1868 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001869 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1870 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1871 if (FieldClassDecl->hasTrivialDestructor())
1872 continue;
1873 DestructedFields.push_back(*Field);
1874 }
1875 }
1876 if (!DestructedFields.empty())
1877 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1878 FieldDecl *Field = DestructedFields[i];
1879 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001880 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001881 getContext().getAsConstantArrayType(FieldType);
1882 if (Array)
1883 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001884 const RecordType *RT = FieldType->getAs<RecordType>();
1885 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1886 llvm::Value *LoadOfThis = LoadCXXThis();
1887 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001888 if (Array) {
1889 const llvm::Type *BasePtr = ConvertType(FieldType);
1890 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001891 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001892 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001893 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001894 Array, BaseAddrPtr);
1895 }
1896 else
1897 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1898 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001899 }
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001901 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1902 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1903 Base != ClassDecl->bases_end(); ++Base) {
1904 // FIXME. copy assignment of virtual base NYI
1905 if (Base->isVirtual())
1906 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001908 CXXRecordDecl *BaseClassDecl
1909 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1910 if (BaseClassDecl->hasTrivialDestructor())
1911 continue;
1912 DestructedBases.push_back(BaseClassDecl);
1913 }
1914 if (DestructedBases.empty())
1915 return;
1916 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1917 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001918 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1919 ClassDecl,BaseClassDecl,
1920 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001921 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1922 Dtor_Complete, V);
1923 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001924}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001925
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001926void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1927 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001928 llvm::Function *Fn,
1929 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001931 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001932 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1933 "SynthesizeDefaultDestructor - destructor has user declaration");
1934 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001936 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1937 SourceLocation());
1938 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001939 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001940}
Anders Carlsson6815e942009-09-27 18:58:34 +00001941
1942// FIXME: Move this to CGCXXStmt.cpp
1943void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1944 // FIXME: We need to do more here.
1945 EmitStmt(S.getTryBlock());
1946}