blob: 0744b79d45d2e6babaecc0b41d61709aecd5883e [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);
Anders Carlssonb5404912009-10-07 01:06:45 +0000138 mangleGuardVariable(CGM.getMangleContext(), &D, 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 Carlsson8e7670d2009-10-12 19:41:04 +0000201/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
202/// expr can be devirtualized.
203static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
204 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
205 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
206 // This is a record decl. We know the type and can devirtualize it.
207 return VD->getType()->isRecordType();
208 }
209 }
210
211 // We can't devirtualize the call.
212 return false;
213}
214
Anders Carlsson774e7c62009-04-03 22:50:24 +0000215RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000216 if (isa<BinaryOperator>(CE->getCallee()))
217 return EmitCXXMemberPointerCallExpr(CE);
218
Anders Carlsson774e7c62009-04-03 22:50:24 +0000219 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
220 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000221
Anders Carlsson2472bf02009-09-29 03:54:11 +0000222 if (MD->isStatic()) {
223 // The method is static, emit it as we would a regular call.
224 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
225 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
226 CE->arg_begin(), CE->arg_end(), 0);
227
228 }
229
John McCall183700f2009-09-21 23:43:11 +0000230 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000231
Mike Stump1eb44332009-09-09 15:08:12 +0000232 const llvm::Type *Ty =
233 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000234 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000235 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Anders Carlsson774e7c62009-04-03 22:50:24 +0000237 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000238 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000239 else {
240 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000241 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000242 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000243
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000244 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000245 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000246 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000247 //
248 // We also don't emit a virtual call if the base expression has a record type
249 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000250 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000251 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000252 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000253 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000254 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000255 = dyn_cast<CXXDestructorDecl>(MD))
256 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000257 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000258 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
260 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000261 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000262}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000263
Mike Stump1eb44332009-09-09 15:08:12 +0000264RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000265CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
266 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
267 const DeclRefExpr *BaseExpr = cast<DeclRefExpr>(BO->getLHS());
268 const DeclRefExpr *MemFn = cast<DeclRefExpr>(BO->getRHS());
269
270 const MemberPointerType *MPT = MemFn->getType()->getAs<MemberPointerType>();
271 const FunctionProtoType *FPT =
272 MPT->getPointeeType()->getAs<FunctionProtoType>();
273 const CXXRecordDecl *RD =
274 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
275
276 const llvm::FunctionType *FTy =
277 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
278 FPT->isVariadic());
279
280 const llvm::Type *Int8PtrTy =
281 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
282
283 // Get the member function pointer.
284 llvm::Value *MemFnPtr =
285 CreateTempAlloca(ConvertType(MemFn->getType()), "mem.fn");
286 EmitAggExpr(MemFn, MemFnPtr, /*VolatileDest=*/false);
287
288 // Emit the 'this' pointer.
289 llvm::Value *This;
290
291 if (BO->getOpcode() == BinaryOperator::PtrMemI)
292 This = EmitScalarExpr(BaseExpr);
293 else
294 This = EmitLValue(BaseExpr).getAddress();
295
296 // Adjust it.
297 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
298 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
299
300 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
301 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
302
303 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
304
305 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
306
307 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
308
309 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
310
311 // If the LSB in the function pointer is 1, the function pointer points to
312 // a virtual function.
313 llvm::Value *IsVirtual
314 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
315 "and");
316
317 IsVirtual = Builder.CreateTrunc(IsVirtual,
318 llvm::Type::getInt1Ty(VMContext));
319
320 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
321 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
322 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
323
324 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
325 EmitBlock(FnVirtual);
326
327 const llvm::Type *VTableTy =
328 FTy->getPointerTo()->getPointerTo()->getPointerTo();
329
330 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
331 VTable = Builder.CreateLoad(VTable);
332
333 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
334
335 // Since the function pointer is 1 plus the virtual table offset, we
336 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000337 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000338
339 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
340
341 EmitBranch(FnEnd);
342 EmitBlock(FnNonVirtual);
343
344 // If the function is not virtual, just load the pointer.
345 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
346 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
347
348 EmitBlock(FnEnd);
349
350 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
351 Callee->reserveOperandSpace(2);
352 Callee->addIncoming(VirtualFn, FnVirtual);
353 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
354
355 CallArgList Args;
356
357 QualType ThisType =
358 getContext().getPointerType(getContext().getTagDeclType(RD));
359
360 // Push the this ptr.
361 Args.push_back(std::make_pair(RValue::get(This), ThisType));
362
363 // And the rest of the call args
364 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
365 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
366 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
367 Callee, Args, 0);
368}
369
370RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000371CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
372 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000373 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000374 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Fariborz Jahanianad258832009-08-13 21:09:41 +0000376 if (MD->isCopyAssignment()) {
377 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
378 if (ClassDecl->hasTrivialCopyAssignment()) {
379 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
380 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
381 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
382 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
383 QualType Ty = E->getType();
384 EmitAggregateCopy(This, Src, Ty);
385 return RValue::get(This);
386 }
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
John McCall183700f2009-09-21 23:43:11 +0000389 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000390 const llvm::Type *Ty =
391 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000392 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000393 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Anders Carlsson0f294632009-05-27 04:18:27 +0000395 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Anders Carlsson0f294632009-05-27 04:18:27 +0000397 return EmitCXXMemberCall(MD, Callee, This,
398 E->arg_begin() + 1, E->arg_end());
399}
400
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000401llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000402 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000403 "Must be in a C++ member function decl to load 'this'");
404 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
405 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000407 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000408 // ans: See how CodeGenFunction::LoadObjCSelf() uses
409 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000410 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
411}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000412
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000413/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
414/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000415/// array.
416/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
417/// array type and 'ArrayPtr' points to the beginning fo the array.
418/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000419void
420CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000421 const ConstantArrayType *ArrayTy,
422 llvm::Value *ArrayPtr) {
423 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
424 llvm::Value * NumElements =
425 llvm::ConstantInt::get(SizeTy,
426 getContext().getConstantArrayElementCount(ArrayTy));
427
428 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
429}
430
431void
432CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
433 llvm::Value *NumElements,
434 llvm::Value *ArrayPtr) {
435 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000437 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000438 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
439 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
440 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000442 // Start the loop with a block that tests the condition.
443 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
444 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000446 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000448 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000450 // Generate: if (loop-index < number-of-elements fall to the loop body,
451 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000452 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000453 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000454 // If the condition is true, execute the body.
455 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000457 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000459 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000460 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000461 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000462 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
463 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000464 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000466 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000468 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000469 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000470 Counter = Builder.CreateLoad(IndexPtr);
471 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
472 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000474 // Finally, branch back up to the condition for the next iteration.
475 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000477 // Emit the fall-through block.
478 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000479}
480
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000481/// EmitCXXAggrDestructorCall - calls the default destructor on array
482/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000483void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000484CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
485 const ArrayType *Array,
486 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000487 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
488 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000489 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000490 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000491 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000492 // Create a temporary for the loop index and initialize it with count of
493 // array elements.
494 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
495 "loop.index");
496 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000497 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000498 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
499 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000501 // Start the loop with a block that tests the condition.
502 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
503 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000505 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000507 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000509 // Generate: if (loop-index != 0 fall to the loop body,
510 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000511 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000512 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
513 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
514 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
515 "isne");
516 // If the condition is true, execute the body.
517 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000519 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000521 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
522 // Inside the loop body, emit the constructor call on the array element.
523 Counter = Builder.CreateLoad(IndexPtr);
524 Counter = Builder.CreateSub(Counter, One);
525 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
526 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000528 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000530 // Emit the decrement of the loop counter.
531 Counter = Builder.CreateLoad(IndexPtr);
532 Counter = Builder.CreateSub(Counter, One, "dec");
533 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000535 // Finally, branch back up to the condition for the next iteration.
536 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000538 // Emit the fall-through block.
539 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000540}
541
542void
Mike Stump1eb44332009-09-09 15:08:12 +0000543CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
544 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000545 llvm::Value *This,
546 CallExpr::const_arg_iterator ArgBeg,
547 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000548 if (D->isCopyConstructor(getContext())) {
549 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
550 if (ClassDecl->hasTrivialCopyConstructor()) {
551 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
552 "EmitCXXConstructorCall - user declared copy constructor");
553 const Expr *E = (*ArgBeg);
554 QualType Ty = E->getType();
555 llvm::Value *Src = EmitLValue(E).getAddress();
556 EmitAggregateCopy(This, Src, Ty);
557 return;
558 }
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000561 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
562
563 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000564}
565
Mike Stump1eb44332009-09-09 15:08:12 +0000566void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000567 CXXDtorType Type,
568 llvm::Value *This) {
569 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Anders Carlsson7267c162009-05-29 21:03:38 +0000571 EmitCXXMemberCall(D, Callee, This, 0, 0);
572}
573
Mike Stump1eb44332009-09-09 15:08:12 +0000574void
575CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000576 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000577 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000578
579 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000580 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000581 if (RD->hasTrivialConstructor())
582 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000583
Mike Stump1eb44332009-09-09 15:08:12 +0000584 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000585 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000586 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000587 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000588 EmitAggExpr((*i), Dest, false);
589 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000590 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000591 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000592 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000593 E->arg_begin(), E->arg_end());
594}
595
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000596void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000597 EmitGlobal(GlobalDecl(D, Ctor_Complete));
598 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000599}
Anders Carlsson363c1842009-04-16 23:57:24 +0000600
Mike Stump1eb44332009-09-09 15:08:12 +0000601void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000602 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Anders Carlsson27ae5362009-04-17 01:58:57 +0000604 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000606 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Anders Carlsson27ae5362009-04-17 01:58:57 +0000608 SetFunctionDefinitionAttributes(D, Fn);
609 SetLLVMFunctionAttributesForDefinition(D, Fn);
610}
611
Anders Carlsson363c1842009-04-16 23:57:24 +0000612llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000613CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000614 CXXCtorType Type) {
615 const llvm::FunctionType *FTy =
616 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anders Carlsson363c1842009-04-16 23:57:24 +0000618 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000619 return cast<llvm::Function>(
620 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000621}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000622
Mike Stump1eb44332009-09-09 15:08:12 +0000623const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000624 CXXCtorType Type) {
625 llvm::SmallString<256> Name;
626 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000627 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Anders Carlsson27ae5362009-04-17 01:58:57 +0000629 Name += '\0';
630 return UniqueMangledName(Name.begin(), Name.end());
631}
632
633void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000634 EmitCXXDestructor(D, Dtor_Complete);
635 EmitCXXDestructor(D, Dtor_Base);
636}
637
Mike Stump1eb44332009-09-09 15:08:12 +0000638void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000639 CXXDtorType Type) {
640 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000642 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Anders Carlsson27ae5362009-04-17 01:58:57 +0000644 SetFunctionDefinitionAttributes(D, Fn);
645 SetLLVMFunctionAttributesForDefinition(D, Fn);
646}
647
648llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000649CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000650 CXXDtorType Type) {
651 const llvm::FunctionType *FTy =
652 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Anders Carlsson27ae5362009-04-17 01:58:57 +0000654 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000655 return cast<llvm::Function>(
656 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000657}
658
Mike Stump1eb44332009-09-09 15:08:12 +0000659const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000660 CXXDtorType Type) {
661 llvm::SmallString<256> Name;
662 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000663 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Anders Carlsson27ae5362009-04-17 01:58:57 +0000665 Name += '\0';
666 return UniqueMangledName(Name.begin(), Name.end());
667}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000668
Mike Stumped032eb2009-09-04 18:27:16 +0000669llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
670 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000671 bool Extern, int64_t nv,
672 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000673 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000674
675 FunctionArgList Args;
676 ImplicitParamDecl *ThisDecl =
677 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
678 MD->getThisType(getContext()));
679 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
680 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
681 e = MD->param_end();
682 i != e; ++i) {
683 ParmVarDecl *D = *i;
684 Args.push_back(std::make_pair(D, D->getType()));
685 }
686 IdentifierInfo *II
687 = &CGM.getContext().Idents.get("__thunk_named_foo_");
688 FunctionDecl *FD = FunctionDecl::Create(getContext(),
689 getContext().getTranslationUnitDecl(),
690 SourceLocation(), II, R, 0,
691 Extern
692 ? FunctionDecl::Extern
693 : FunctionDecl::Static,
694 false, true);
695 StartFunction(FD, R, Fn, Args, SourceLocation());
696 // FIXME: generate body
697 FinishFunction();
698 return Fn;
699}
700
Mike Stump6e319f62009-09-11 23:25:56 +0000701llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
702 const CXXMethodDecl *MD,
703 bool Extern,
704 int64_t nv_t,
705 int64_t v_t,
706 int64_t nv_r,
707 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000708 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000709
710 FunctionArgList Args;
711 ImplicitParamDecl *ThisDecl =
712 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
713 MD->getThisType(getContext()));
714 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
715 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
716 e = MD->param_end();
717 i != e; ++i) {
718 ParmVarDecl *D = *i;
719 Args.push_back(std::make_pair(D, D->getType()));
720 }
721 IdentifierInfo *II
722 = &CGM.getContext().Idents.get("__thunk_named_foo_");
723 FunctionDecl *FD = FunctionDecl::Create(getContext(),
724 getContext().getTranslationUnitDecl(),
725 SourceLocation(), II, R, 0,
726 Extern
727 ? FunctionDecl::Extern
728 : FunctionDecl::Static,
729 false, true);
730 StartFunction(FD, R, Fn, Args, SourceLocation());
731 // FIXME: generate body
732 FinishFunction();
733 return Fn;
734}
735
Mike Stump77ca8f62009-09-05 07:20:32 +0000736llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
737 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000738 llvm::SmallString<256> OutName;
739 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000740 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000741 llvm::GlobalVariable::LinkageTypes linktype;
742 linktype = llvm::GlobalValue::WeakAnyLinkage;
743 if (!Extern)
744 linktype = llvm::GlobalValue::InternalLinkage;
745 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000746 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000747 const llvm::FunctionType *FTy =
748 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
749 FPT->isVariadic());
750
751 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
752 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000753 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000754 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
755 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
756 return m;
757}
758
Mike Stump6e319f62009-09-11 23:25:56 +0000759llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
760 bool Extern, int64_t nv_t,
761 int64_t v_t, int64_t nv_r,
762 int64_t v_r) {
763 llvm::SmallString<256> OutName;
764 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000765 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000766 llvm::GlobalVariable::LinkageTypes linktype;
767 linktype = llvm::GlobalValue::WeakAnyLinkage;
768 if (!Extern)
769 linktype = llvm::GlobalValue::InternalLinkage;
770 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000771 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000772 const llvm::FunctionType *FTy =
773 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
774 FPT->isVariadic());
775
776 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
777 &getModule());
778 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
779 v_r);
780 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
781 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
782 return m;
783}
784
Mike Stumpf0070db2009-08-26 20:46:33 +0000785llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000786CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
787 const CXXRecordDecl *ClassDecl,
788 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000789 const llvm::Type *Int8PtrTy =
790 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
791
792 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
793 Int8PtrTy->getPointerTo());
794 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
795
Anders Carlssondbd920c2009-10-11 22:13:54 +0000796 int64_t VBaseOffsetIndex =
797 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
798
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000799 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000800 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000801 const llvm::Type *PtrDiffTy =
802 ConvertType(getContext().getPointerDiffType());
803
804 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
805 PtrDiffTy->getPointerTo());
806
807 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
808
809 return VBaseOffset;
810}
811
812llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000813CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
814 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000815 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000816
Mike Stumpf0070db2009-08-26 20:46:33 +0000817 Ty = llvm::PointerType::get(Ty, 0);
818 Ty = llvm::PointerType::get(Ty, 0);
819 Ty = llvm::PointerType::get(Ty, 0);
820 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
821 vtbl = Builder.CreateLoad(vtbl);
822 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000823 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000824 vfn = Builder.CreateLoad(vfn);
825 return vfn;
826}
827
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000828/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
829/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
830/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000831// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000832void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000833 llvm::Value *Src,
834 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000835 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000836 QualType Ty) {
837 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
838 assert(CA && "VLA cannot be copied over");
839 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000841 // Create a temporary for the loop index and initialize it with 0.
842 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
843 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000844 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000845 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000846 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000847 // Start the loop with a block that tests the condition.
848 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
849 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000851 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000853 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
854 // Generate: if (loop-index < number-of-elements fall to the loop body,
855 // otherwise, go to the block after the for-loop.
856 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000857 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000858 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
859 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000860 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000861 "isless");
862 // If the condition is true, execute the body.
863 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000865 EmitBlock(ForBody);
866 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
867 // Inside the loop body, emit the constructor call on the array element.
868 Counter = Builder.CreateLoad(IndexPtr);
869 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
870 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
871 if (BitwiseCopy)
872 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000873 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000874 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000875 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000876 Ctor_Complete);
877 CallArgList CallArgs;
878 // Push the this (Dest) ptr.
879 CallArgs.push_back(std::make_pair(RValue::get(Dest),
880 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000882 // Push the Src ptr.
883 CallArgs.push_back(std::make_pair(RValue::get(Src),
884 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000885 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000886 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000887 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
888 Callee, CallArgs, BaseCopyCtor);
889 }
890 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000892 // Emit the increment of the loop counter.
893 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
894 Counter = Builder.CreateLoad(IndexPtr);
895 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
896 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000898 // Finally, branch back up to the condition for the next iteration.
899 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000901 // Emit the fall-through block.
902 EmitBlock(AfterFor, true);
903}
904
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000905/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000906/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000907/// bitwise assignment or via a copy assignment operator function call.
908/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000909void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000910 llvm::Value *Src,
911 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000912 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000913 QualType Ty) {
914 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
915 assert(CA && "VLA cannot be asssigned");
916 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000918 // Create a temporary for the loop index and initialize it with 0.
919 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
920 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000921 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000922 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
923 Builder.CreateStore(zeroConstant, IndexPtr, false);
924 // Start the loop with a block that tests the condition.
925 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
926 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000928 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000930 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
931 // Generate: if (loop-index < number-of-elements fall to the loop body,
932 // otherwise, go to the block after the for-loop.
933 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000934 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000935 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
936 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000937 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000938 "isless");
939 // If the condition is true, execute the body.
940 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000942 EmitBlock(ForBody);
943 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
944 // Inside the loop body, emit the assignment operator call on array element.
945 Counter = Builder.CreateLoad(IndexPtr);
946 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
947 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
948 const CXXMethodDecl *MD = 0;
949 if (BitwiseAssign)
950 EmitAggregateCopy(Dest, Src, Ty);
951 else {
952 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
953 MD);
954 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
955 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000956 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000957 const llvm::Type *LTy =
958 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
959 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000960 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000962 CallArgList CallArgs;
963 // Push the this (Dest) ptr.
964 CallArgs.push_back(std::make_pair(RValue::get(Dest),
965 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000967 // Push the Src ptr.
968 CallArgs.push_back(std::make_pair(RValue::get(Src),
969 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +0000970 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000971 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
972 Callee, CallArgs, MD);
973 }
974 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000976 // Emit the increment of the loop counter.
977 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
978 Counter = Builder.CreateLoad(IndexPtr);
979 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
980 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000982 // Finally, branch back up to the condition for the next iteration.
983 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000985 // Emit the fall-through block.
986 EmitBlock(AfterFor, true);
987}
988
Fariborz Jahanianca283612009-08-07 23:51:33 +0000989/// EmitClassMemberwiseCopy - This routine generates code to copy a class
990/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000991/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +0000992void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +0000993 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +0000994 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +0000995 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
996 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +0000997 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
998 /*NullCheckValue=*/false);
999 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1000 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001001 }
1002 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1003 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001004 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
1007 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001008 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001009 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001010 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001011 CallArgList CallArgs;
1012 // Push the this (Dest) ptr.
1013 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1014 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Fariborz Jahanianca283612009-08-07 23:51:33 +00001016 // Push the Src ptr.
1017 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001018 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001019 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001020 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001021 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1022 Callee, CallArgs, BaseCopyCtor);
1023 }
1024}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001025
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001026/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001027/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001028/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001029// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001030void CodeGenFunction::EmitClassCopyAssignment(
1031 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001032 const CXXRecordDecl *ClassDecl,
1033 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001034 QualType Ty) {
1035 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001036 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1037 /*NullCheckValue=*/false);
1038 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1039 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001040 }
1041 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1042 EmitAggregateCopy(Dest, Src, Ty);
1043 return;
1044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001046 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001047 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001048 MD);
1049 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1050 (void)ConstCopyAssignOp;
1051
John McCall183700f2009-09-21 23:43:11 +00001052 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001053 const llvm::Type *LTy =
1054 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001055 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001056 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001058 CallArgList CallArgs;
1059 // Push the this (Dest) ptr.
1060 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1061 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001063 // Push the Src ptr.
1064 CallArgs.push_back(std::make_pair(RValue::get(Src),
1065 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001066 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001067 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001068 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1069 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001070}
1071
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001072/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001073void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001074CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1075 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001076 llvm::Function *Fn,
1077 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001078 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1079 SourceLocation());
1080 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001081 FinishFunction();
1082}
1083
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001084/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001085/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001086/// The implicitly-defined copy constructor for class X performs a memberwise
1087/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001088/// of initialization of bases and members in a user-defined constructor
1089/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001090/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001091/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001092/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001093/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001094/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001095/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001096/// Virtual base class subobjects shall be copied only once by the
1097/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001098
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001099void
1100CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1101 CXXCtorType Type,
1102 llvm::Function *Fn,
1103 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001104 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001105 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001106 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001107 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1108 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001110 FunctionArgList::const_iterator i = Args.begin();
1111 const VarDecl *ThisArg = i->first;
1112 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1113 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1114 const VarDecl *SrcArg = (i+1)->first;
1115 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1116 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001118 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1119 Base != ClassDecl->bases_end(); ++Base) {
1120 // FIXME. copy constrution of virtual base NYI
1121 if (Base->isVirtual())
1122 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001124 CXXRecordDecl *BaseClassDecl
1125 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001126 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1127 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001128 }
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001130 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1131 FieldEnd = ClassDecl->field_end();
1132 Field != FieldEnd; ++Field) {
1133 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001134 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001135 getContext().getAsConstantArrayType(FieldType);
1136 if (Array)
1137 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001139 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1140 CXXRecordDecl *FieldClassDecl
1141 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1142 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1143 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001144 if (Array) {
1145 const llvm::Type *BasePtr = ConvertType(FieldType);
1146 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001147 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001148 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001149 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001150 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1151 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1152 FieldClassDecl, FieldType);
1153 }
Mike Stump1eb44332009-09-09 15:08:12 +00001154 else
1155 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001156 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001157 continue;
1158 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001159 // Do a built-in assignment of scalar data members.
1160 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1161 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1162 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1163 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001164 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001165 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001166}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001167
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001168/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001169/// Before the implicitly-declared copy assignment operator for a class is
1170/// implicitly defined, all implicitly- declared copy assignment operators for
1171/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001172/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001173/// The implicitly-defined copy assignment operator for class X performs
1174/// memberwise assignment of its subob- jects. The direct base classes of X are
1175/// assigned first, in the order of their declaration in
1176/// the base-specifier-list, and then the immediate nonstatic data members of X
1177/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001178/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001179/// if the subobject is of class type, the copy assignment operator for the
1180/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001181/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001182///
Mike Stump1eb44332009-09-09 15:08:12 +00001183/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001184/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001185///
Mike Stump1eb44332009-09-09 15:08:12 +00001186/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001187/// used.
1188void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001189 llvm::Function *Fn,
1190 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001191
1192 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1193 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1194 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001195 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001197 FunctionArgList::const_iterator i = Args.begin();
1198 const VarDecl *ThisArg = i->first;
1199 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1200 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1201 const VarDecl *SrcArg = (i+1)->first;
1202 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1203 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001205 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1206 Base != ClassDecl->bases_end(); ++Base) {
1207 // FIXME. copy assignment of virtual base NYI
1208 if (Base->isVirtual())
1209 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001211 CXXRecordDecl *BaseClassDecl
1212 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1213 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1214 Base->getType());
1215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001217 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1218 FieldEnd = ClassDecl->field_end();
1219 Field != FieldEnd; ++Field) {
1220 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001221 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001222 getContext().getAsConstantArrayType(FieldType);
1223 if (Array)
1224 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001226 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1227 CXXRecordDecl *FieldClassDecl
1228 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1229 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1230 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001231 if (Array) {
1232 const llvm::Type *BasePtr = ConvertType(FieldType);
1233 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1234 llvm::Value *DestBaseAddrPtr =
1235 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1236 llvm::Value *SrcBaseAddrPtr =
1237 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1238 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1239 FieldClassDecl, FieldType);
1240 }
1241 else
Mike Stump1eb44332009-09-09 15:08:12 +00001242 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001243 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001244 continue;
1245 }
1246 // Do a built-in assignment of scalar data members.
1247 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1248 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1249 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1250 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001253 // return *this;
1254 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001256 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001257}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001258
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001259/// EmitCtorPrologue - This routine generates necessary code to initialize
1260/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001261/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001262void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1263 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001264 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001265 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001266 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001268 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001269 E = CD->init_end();
1270 B != E; ++B) {
1271 CXXBaseOrMemberInitializer *Member = (*B);
1272 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001273 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001274 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001275 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001276 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001277 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1278 BaseClassDecl,
1279 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001280 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001281 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001282 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001283 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001284 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001285 // non-static data member initilaizers.
1286 FieldDecl *Field = Member->getMember();
1287 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001288 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001289 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001290 if (Array)
1291 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Mike Stumpf1216772009-07-31 18:25:34 +00001293 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001294 LValue LHS;
1295 if (FieldType->isReferenceType()) {
1296 // FIXME: This is really ugly; should be refactored somehow
1297 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1298 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001299 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1300 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001301 } else {
1302 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1303 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001304 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001305 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001306 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001307 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001308 if (Array) {
1309 const llvm::Type *BasePtr = ConvertType(FieldType);
1310 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001311 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001312 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001313 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001314 Array, BaseAddrPtr);
1315 }
1316 else
1317 EmitCXXConstructorCall(Member->getConstructor(),
1318 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001319 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001320 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001321 continue;
1322 }
1323 else {
1324 // Initializing an anonymous union data member.
1325 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001326 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001327 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001328 FieldType = anonMember->getType();
1329 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001330 }
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001332 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001333 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001334 RValue RHS;
1335 if (FieldType->isReferenceType())
1336 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1337 /*IsInitializer=*/true);
1338 else
1339 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1340 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001341 }
1342 }
Mike Stumpf1216772009-07-31 18:25:34 +00001343
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001344 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001345 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001346 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001347 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001348 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001349 ClassDecl->bases_begin();
1350 Base != ClassDecl->bases_end(); ++Base) {
1351 // FIXME. copy assignment of virtual base NYI
1352 if (Base->isVirtual())
1353 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001355 CXXRecordDecl *BaseClassDecl
1356 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1357 if (BaseClassDecl->hasTrivialConstructor())
1358 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001359 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001360 BaseClassDecl->getDefaultConstructor(getContext())) {
1361 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001362 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1363 BaseClassDecl,
1364 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001365 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1366 }
1367 }
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001369 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1370 FieldEnd = ClassDecl->field_end();
1371 Field != FieldEnd; ++Field) {
1372 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001373 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001374 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001375 if (Array)
1376 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001377 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1378 continue;
1379 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001380 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001381 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1382 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1383 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001384 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001385 MemberClassDecl->getDefaultConstructor(getContext())) {
1386 LoadOfThis = LoadCXXThis();
1387 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001388 if (Array) {
1389 const llvm::Type *BasePtr = ConvertType(FieldType);
1390 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001391 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001392 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1393 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1394 }
1395 else
Mike Stump1eb44332009-09-09 15:08:12 +00001396 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001397 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001398 }
1399 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001400 }
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Mike Stumpf1216772009-07-31 18:25:34 +00001402 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001403 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001404 if (!LoadOfThis)
1405 LoadOfThis = LoadCXXThis();
1406 llvm::Value *VtableField;
1407 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001408 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001409 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1410 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1411 llvm::Value *vtable = GenerateVtable(ClassDecl);
1412 Builder.CreateStore(vtable, VtableField);
1413 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001414}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001415
1416/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001417/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001418/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001419/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001420void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1421 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001422 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001423 assert(!ClassDecl->getNumVBases() &&
1424 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001425 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001427 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1428 *E = DD->destr_end(); B != E; ++B) {
1429 uintptr_t BaseOrMember = (*B);
1430 if (DD->isMemberToDestroy(BaseOrMember)) {
1431 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1432 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001433 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001434 getContext().getAsConstantArrayType(FieldType);
1435 if (Array)
1436 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001437 const RecordType *RT = FieldType->getAs<RecordType>();
1438 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1439 if (FieldClassDecl->hasTrivialDestructor())
1440 continue;
1441 llvm::Value *LoadOfThis = LoadCXXThis();
1442 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001443 if (Array) {
1444 const llvm::Type *BasePtr = ConvertType(FieldType);
1445 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001446 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001447 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001448 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001449 Array, BaseAddrPtr);
1450 }
1451 else
1452 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1453 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001454 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001455 const RecordType *RT =
1456 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1457 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1458 if (BaseClassDecl->hasTrivialDestructor())
1459 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001460 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1461 ClassDecl, BaseClassDecl,
1462 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001463 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001464 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001465 }
1466 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001467 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1468 return;
1469 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001470 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001471 // reverse order of their construction.
1472 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001474 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1475 FieldEnd = ClassDecl->field_end();
1476 Field != FieldEnd; ++Field) {
1477 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001478 if (getContext().getAsConstantArrayType(FieldType))
1479 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001480 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1481 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1482 if (FieldClassDecl->hasTrivialDestructor())
1483 continue;
1484 DestructedFields.push_back(*Field);
1485 }
1486 }
1487 if (!DestructedFields.empty())
1488 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1489 FieldDecl *Field = DestructedFields[i];
1490 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001491 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001492 getContext().getAsConstantArrayType(FieldType);
1493 if (Array)
1494 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001495 const RecordType *RT = FieldType->getAs<RecordType>();
1496 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1497 llvm::Value *LoadOfThis = LoadCXXThis();
1498 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001499 if (Array) {
1500 const llvm::Type *BasePtr = ConvertType(FieldType);
1501 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001502 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001503 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001504 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001505 Array, BaseAddrPtr);
1506 }
1507 else
1508 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1509 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001512 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1513 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1514 Base != ClassDecl->bases_end(); ++Base) {
1515 // FIXME. copy assignment of virtual base NYI
1516 if (Base->isVirtual())
1517 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001519 CXXRecordDecl *BaseClassDecl
1520 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1521 if (BaseClassDecl->hasTrivialDestructor())
1522 continue;
1523 DestructedBases.push_back(BaseClassDecl);
1524 }
1525 if (DestructedBases.empty())
1526 return;
1527 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1528 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001529 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1530 ClassDecl,BaseClassDecl,
1531 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001532 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1533 Dtor_Complete, V);
1534 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001535}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001536
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001537void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1538 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001539 llvm::Function *Fn,
1540 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001542 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001543 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1544 "SynthesizeDefaultDestructor - destructor has user declaration");
1545 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001547 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1548 SourceLocation());
1549 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001550 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001551}
Anders Carlsson6815e942009-09-27 18:58:34 +00001552
1553// FIXME: Move this to CGCXXStmt.cpp
1554void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1555 // FIXME: We need to do more here.
1556 EmitStmt(S.getTryBlock());
1557}