blob: b7670dfb45f1a674342836da8bc1f5204ee013fd [file] [log] [blame]
Anders Carlsson87fc5a52008-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 Stump11289f42009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlsson87fc5a52008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson1235bbc2009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahaniandedf1e42009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlssone5fd6f22009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson131be8b2008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson52d78a52009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Mike Stump11289f42009-09-09 15:08:12 +000029void
Fariborz Jahanian1254a092009-11-10 19:24:06 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
Anders Carlssonf40886a2009-08-08 21:45:14 +000031 llvm::Constant *DeclPtr) {
Anders Carlsson52d78a52009-09-27 18:58:34 +000032 const llvm::Type *Int8PtrTy =
33 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Mike Stump11289f42009-09-09 15:08:12 +000034
Anders Carlssonf40886a2009-08-08 21:45:14 +000035 std::vector<const llvm::Type *> Params;
36 Params.push_back(Int8PtrTy);
Mike Stump11289f42009-09-09 15:08:12 +000037
Anders Carlssonf40886a2009-08-08 21:45:14 +000038 // Get the destructor function type
Mike Stump11289f42009-09-09 15:08:12 +000039 const llvm::Type *DtorFnTy =
Owen Anderson41a75022009-08-13 21:57:51 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlssonf40886a2009-08-08 21:45:14 +000041 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump11289f42009-09-09 15:08:12 +000042
Anders Carlssonf40886a2009-08-08 21:45:14 +000043 Params.clear();
44 Params.push_back(DtorFnTy);
45 Params.push_back(Int8PtrTy);
46 Params.push_back(Int8PtrTy);
Mike Stump11289f42009-09-09 15:08:12 +000047
Anders Carlssonf40886a2009-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 Stump11289f42009-09-09 15:08:12 +000050 const llvm::FunctionType *AtExitFnTy =
Anders Carlssonf40886a2009-08-08 21:45:14 +000051 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump11289f42009-09-09 15:08:12 +000052
Anders Carlssonf40886a2009-08-08 21:45:14 +000053 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54 "__cxa_atexit");
Mike Stump11289f42009-09-09 15:08:12 +000055
Anders Carlssonf40886a2009-08-08 21:45:14 +000056 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57 "__dso_handle");
Anders Carlssonf40886a2009-08-08 21:45:14 +000058 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
59 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
60 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
61 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
62}
63
Mike Stump11289f42009-09-09 15:08:12 +000064void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlssonf40886a2009-08-08 21:45:14 +000065 llvm::Constant *DeclPtr) {
66 assert(D.hasGlobalStorage() &&
67 "VarDecl must have global storage!");
Mike Stump11289f42009-09-09 15:08:12 +000068
Anders Carlssonf40886a2009-08-08 21:45:14 +000069 const Expr *Init = D.getInit();
70 QualType T = D.getType();
Mike Stump53f9ded2009-11-03 23:25:48 +000071 bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
Mike Stump11289f42009-09-09 15:08:12 +000072
Anders Carlssonf40886a2009-08-08 21:45:14 +000073 if (T->isReferenceType()) {
Anders Carlsson49033712009-08-17 18:24:57 +000074 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlssonf40886a2009-08-08 21:45:14 +000075 } else if (!hasAggregateLLVMType(T)) {
76 llvm::Value *V = EmitScalarExpr(Init);
Mike Stump53f9ded2009-11-03 23:25:48 +000077 EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlssonf40886a2009-08-08 21:45:14 +000078 } else if (T->isAnyComplexType()) {
Mike Stump53f9ded2009-11-03 23:25:48 +000079 EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlssonf40886a2009-08-08 21:45:14 +000080 } else {
Mike Stump53f9ded2009-11-03 23:25:48 +000081 EmitAggExpr(Init, DeclPtr, isVolatile);
Fariborz Jahaniane6c81122009-11-11 01:13:34 +000082 // Avoid generating destructor(s) for initialized objects.
83 if (!isa<CXXConstructExpr>(Init))
84 return;
Fariborz Jahanian1254a092009-11-10 19:24:06 +000085 const ConstantArrayType *Array = getContext().getAsConstantArrayType(T);
86 if (Array)
87 T = getContext().getBaseElementType(Array);
88
Anders Carlssonf40886a2009-08-08 21:45:14 +000089 if (const RecordType *RT = T->getAs<RecordType>()) {
90 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian1254a092009-11-10 19:24:06 +000091 if (!RD->hasTrivialDestructor()) {
92 llvm::Constant *DtorFn;
93 if (Array) {
94 DtorFn = CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(
95 RD->getDestructor(getContext()),
96 Array, DeclPtr);
97 DeclPtr =
98 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
99 }
100 else
101 DtorFn = CGM.GetAddrOfCXXDestructor(RD->getDestructor(getContext()),
102 Dtor_Complete);
103 EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
104 }
Anders Carlssonf40886a2009-08-08 21:45:14 +0000105 }
106 }
107}
108
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000109void
110CodeGenModule::EmitCXXGlobalInitFunc() {
111 if (CXXGlobalInits.empty())
112 return;
Mike Stump11289f42009-09-09 15:08:12 +0000113
Mike Stumpb9c9b352009-11-04 01:11:15 +0000114 const llvm::FunctionType *FTy
115 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
116 false);
Mike Stump11289f42009-09-09 15:08:12 +0000117
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000118 // Create our global initialization function.
119 // FIXME: Should this be tweakable by targets?
Mike Stump11289f42009-09-09 15:08:12 +0000120 llvm::Function *Fn =
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000121 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
122 "__cxx_global_initialization", &TheModule);
Mike Stump11289f42009-09-09 15:08:12 +0000123
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000124 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramerb57e5d22009-08-08 23:43:26 +0000125 &CXXGlobalInits[0],
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000126 CXXGlobalInits.size());
127 AddGlobalCtor(Fn);
128}
129
130void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
131 const VarDecl **Decls,
132 unsigned NumDecls) {
Anders Carlsson73fcc952009-09-11 00:07:24 +0000133 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000134 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000135
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000136 for (unsigned i = 0; i != NumDecls; ++i) {
137 const VarDecl *D = Decls[i];
Mike Stump11289f42009-09-09 15:08:12 +0000138
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000139 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
140 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
141 }
142 FinishFunction();
143}
144
Mike Stump11289f42009-09-09 15:08:12 +0000145void
146CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlssonf40886a2009-08-08 21:45:14 +0000147 llvm::GlobalVariable *GV) {
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000148 // FIXME: This should use __cxa_guard_{acquire,release}?
149
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000150 assert(!getContext().getLangOptions().ThreadsafeStatics &&
151 "thread safe statics are currently not supported!");
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000152
Anders Carlsson1235bbc2009-04-13 18:03:33 +0000153 llvm::SmallString<256> GuardVName;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000154 CGM.getMangleContext().mangleGuardVariable(&D, GuardVName);
Mike Stump11289f42009-09-09 15:08:12 +0000155
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000156 // Create the guard variable.
Mike Stump11289f42009-09-09 15:08:12 +0000157 llvm::GlobalValue *GuardV =
Mike Stumpb9c9b352009-11-04 01:11:15 +0000158 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
159 false, GV->getLinkage(),
160 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar58bc48c2009-08-19 20:04:03 +0000161 GuardVName.str());
Mike Stump11289f42009-09-09 15:08:12 +0000162
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000163 // Load the first byte of the guard variable.
Mike Stumpb9c9b352009-11-04 01:11:15 +0000164 const llvm::Type *PtrTy
165 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump11289f42009-09-09 15:08:12 +0000166 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000167 "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000168
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000169 // Compare it against 0.
Mike Stumpb9c9b352009-11-04 01:11:15 +0000170 llvm::Value *nullValue
171 = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000172 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump11289f42009-09-09 15:08:12 +0000173
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000174 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbara612e792008-11-13 01:38:36 +0000175 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000176
177 // If the guard variable is 0, jump to the initializer code.
178 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000179
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000180 EmitBlock(InitBlock);
181
Anders Carlssonf40886a2009-08-08 21:45:14 +0000182 EmitCXXGlobalVarDeclInit(D, GV);
183
Mike Stumpb9c9b352009-11-04 01:11:15 +0000184 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
185 1),
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000186 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump11289f42009-09-09 15:08:12 +0000187
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000188 EmitBlock(EndBlock);
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000189}
190
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000191RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
192 llvm::Value *Callee,
193 llvm::Value *This,
194 CallExpr::const_arg_iterator ArgBeg,
195 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump11289f42009-09-09 15:08:12 +0000196 assert(MD->isInstance() &&
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000197 "Trying to emit a member call expr on a static method!");
198
Douglas Gregord94105a2009-09-04 19:04:08 +0000199 // A call to a trivial destructor requires no code generation.
200 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
201 if (Destructor->isTrivial())
202 return RValue::get(0);
Mike Stump11289f42009-09-09 15:08:12 +0000203
John McCall9dd450b2009-09-21 23:43:11 +0000204 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +0000205
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000206 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000207
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000208 // Push the this ptr.
209 Args.push_back(std::make_pair(RValue::get(This),
210 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +0000211
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000212 // And the rest of the call args
213 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000214
John McCall9dd450b2009-09-21 23:43:11 +0000215 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000216 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
217 Callee, Args, MD);
218}
219
Anders Carlssond7432df2009-10-12 19:41:04 +0000220/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
221/// expr can be devirtualized.
222static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
223 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
224 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
225 // This is a record decl. We know the type and can devirtualize it.
226 return VD->getType()->isRecordType();
227 }
Anders Carlssonb61301f2009-10-12 19:45:47 +0000228
229 return false;
Anders Carlssond7432df2009-10-12 19:41:04 +0000230 }
231
Anders Carlssona1b54fd2009-10-12 19:59:15 +0000232 // We can always devirtualize calls on temporary object expressions.
Anders Carlssonb61301f2009-10-12 19:45:47 +0000233 if (isa<CXXTemporaryObjectExpr>(Base))
234 return true;
235
Anders Carlssona1b54fd2009-10-12 19:59:15 +0000236 // And calls on bound temporaries.
237 if (isa<CXXBindTemporaryExpr>(Base))
238 return true;
239
Anders Carlsson2a017092009-10-12 19:51:33 +0000240 // Check if this is a call expr that returns a record type.
241 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
242 return CE->getCallReturnType()->isRecordType();
243
Anders Carlssond7432df2009-10-12 19:41:04 +0000244 // We can't devirtualize the call.
245 return false;
246}
247
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000248RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000249 if (isa<BinaryOperator>(CE->getCallee()))
250 return EmitCXXMemberPointerCallExpr(CE);
251
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000252 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
253 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000254
Anders Carlsson8f4fd602009-09-29 03:54:11 +0000255 if (MD->isStatic()) {
256 // The method is static, emit it as we would a regular call.
257 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
258 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
259 CE->arg_begin(), CE->arg_end(), 0);
260
261 }
262
John McCall9dd450b2009-09-21 23:43:11 +0000263 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumpa523b2d2009-07-30 21:47:44 +0000264
Mike Stump11289f42009-09-09 15:08:12 +0000265 const llvm::Type *Ty =
266 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlsson03a409f2009-04-08 20:31:57 +0000267 FPT->isVariadic());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000268 llvm::Value *This;
Mike Stump11289f42009-09-09 15:08:12 +0000269
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000270 if (ME->isArrow())
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000271 This = EmitScalarExpr(ME->getBase());
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000272 else {
273 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000274 This = BaseLV.getAddress();
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000275 }
Mike Stumpa5588bf2009-08-26 20:46:33 +0000276
Douglas Gregorc1905232009-08-26 22:36:53 +0000277 // C++ [class.virtual]p12:
Mike Stump11289f42009-09-09 15:08:12 +0000278 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorc1905232009-08-26 22:36:53 +0000279 // virtual call mechanism.
Anders Carlssonb5296552009-10-11 23:55:52 +0000280 //
281 // We also don't emit a virtual call if the base expression has a record type
282 // because then we know what the type is.
Mike Stumpa5588bf2009-08-26 20:46:33 +0000283 llvm::Value *Callee;
Eli Friedmane6ce3542009-11-16 05:31:29 +0000284 if (const CXXDestructorDecl *Destructor
285 = dyn_cast<CXXDestructorDecl>(MD)) {
286 if (MD->isVirtual() && !ME->hasQualifier() &&
287 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
288 Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty);
289 } else {
290 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
291 }
292 } else if (MD->isVirtual() && !ME->hasQualifier() &&
293 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
294 Callee = BuildVirtualCall(MD, This, Ty);
295 } else {
Anders Carlssonecf9bf02009-09-10 23:43:36 +0000296 Callee = CGM.GetAddrOfFunction(MD, Ty);
Eli Friedmane6ce3542009-11-16 05:31:29 +0000297 }
Mike Stump11289f42009-09-09 15:08:12 +0000298
299 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000300 CE->arg_begin(), CE->arg_end());
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000301}
Anders Carlssona5d077d2009-04-14 16:58:56 +0000302
Mike Stump11289f42009-09-09 15:08:12 +0000303RValue
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000304CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
305 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000306 const Expr *BaseExpr = BO->getLHS();
307 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000308
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000309 const MemberPointerType *MPT =
310 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000311 const FunctionProtoType *FPT =
312 MPT->getPointeeType()->getAs<FunctionProtoType>();
313 const CXXRecordDecl *RD =
Douglas Gregor615ac672009-11-04 16:49:01 +0000314 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000315
316 const llvm::FunctionType *FTy =
317 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
318 FPT->isVariadic());
319
320 const llvm::Type *Int8PtrTy =
321 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
322
323 // Get the member function pointer.
324 llvm::Value *MemFnPtr =
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000325 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
326 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000327
328 // Emit the 'this' pointer.
329 llvm::Value *This;
330
331 if (BO->getOpcode() == BinaryOperator::PtrMemI)
332 This = EmitScalarExpr(BaseExpr);
333 else
334 This = EmitLValue(BaseExpr).getAddress();
335
336 // Adjust it.
337 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
338 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
339
340 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
341 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
342
343 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
344
345 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
346
347 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
348
349 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
350
351 // If the LSB in the function pointer is 1, the function pointer points to
352 // a virtual function.
353 llvm::Value *IsVirtual
354 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
355 "and");
356
357 IsVirtual = Builder.CreateTrunc(IsVirtual,
358 llvm::Type::getInt1Ty(VMContext));
359
360 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
361 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
362 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
363
364 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
365 EmitBlock(FnVirtual);
366
367 const llvm::Type *VTableTy =
368 FTy->getPointerTo()->getPointerTo()->getPointerTo();
369
370 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
371 VTable = Builder.CreateLoad(VTable);
372
373 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
374
375 // Since the function pointer is 1 plus the virtual table offset, we
376 // subtract 1 by using a GEP.
Mike Stump0d479e62009-10-09 01:25:47 +0000377 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000378
379 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
380
381 EmitBranch(FnEnd);
382 EmitBlock(FnNonVirtual);
383
384 // If the function is not virtual, just load the pointer.
385 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
386 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
387
388 EmitBlock(FnEnd);
389
390 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
391 Callee->reserveOperandSpace(2);
392 Callee->addIncoming(VirtualFn, FnVirtual);
393 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
394
395 CallArgList Args;
396
397 QualType ThisType =
398 getContext().getPointerType(getContext().getTagDeclType(RD));
399
400 // Push the this ptr.
401 Args.push_back(std::make_pair(RValue::get(This), ThisType));
402
403 // And the rest of the call args
404 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
405 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
406 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
407 Callee, Args, 0);
408}
409
410RValue
Anders Carlsson4034a952009-05-27 04:18:27 +0000411CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
412 const CXXMethodDecl *MD) {
Mike Stump11289f42009-09-09 15:08:12 +0000413 assert(MD->isInstance() &&
Anders Carlsson4034a952009-05-27 04:18:27 +0000414 "Trying to emit a member call expr on a static method!");
Mike Stump11289f42009-09-09 15:08:12 +0000415
Fariborz Jahanian4985b332009-08-13 21:09:41 +0000416 if (MD->isCopyAssignment()) {
417 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
418 if (ClassDecl->hasTrivialCopyAssignment()) {
419 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
420 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
421 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
422 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
423 QualType Ty = E->getType();
424 EmitAggregateCopy(This, Src, Ty);
425 return RValue::get(This);
426 }
427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
John McCall9dd450b2009-09-21 23:43:11 +0000429 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +0000430 const llvm::Type *Ty =
431 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stump5a522352009-09-04 18:27:16 +0000432 FPT->isVariadic());
Mike Stump11289f42009-09-09 15:08:12 +0000433
Anders Carlsson4034a952009-05-27 04:18:27 +0000434 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump11289f42009-09-09 15:08:12 +0000435
Eli Friedmane6ce3542009-11-16 05:31:29 +0000436 llvm::Value *Callee;
437 if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
438 Callee = BuildVirtualCall(MD, This, Ty);
439 else
440 Callee = CGM.GetAddrOfFunction(MD, Ty);
441
Anders Carlsson4034a952009-05-27 04:18:27 +0000442 return EmitCXXMemberCall(MD, Callee, This,
443 E->arg_begin() + 1, E->arg_end());
444}
445
Anders Carlssona5d077d2009-04-14 16:58:56 +0000446llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump11289f42009-09-09 15:08:12 +0000447 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlssona5d077d2009-04-14 16:58:56 +0000448 "Must be in a C++ member function decl to load 'this'");
449 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
450 "Must be in a C++ member function decl to load 'this'");
Mike Stump11289f42009-09-09 15:08:12 +0000451
Anders Carlssona5d077d2009-04-14 16:58:56 +0000452 // FIXME: What if we're inside a block?
Mike Stump18bb9282009-05-16 07:57:57 +0000453 // ans: See how CodeGenFunction::LoadObjCSelf() uses
454 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlssona5d077d2009-04-14 16:58:56 +0000455 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
456}
Anders Carlssonf7475242009-04-15 15:55:24 +0000457
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000458/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
459/// for-loop to call the default constructor on individual members of the
Anders Carlssond49844b2009-09-23 02:45:36 +0000460/// array.
461/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
462/// array type and 'ArrayPtr' points to the beginning fo the array.
463/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000464void
465CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlssond49844b2009-09-23 02:45:36 +0000466 const ConstantArrayType *ArrayTy,
467 llvm::Value *ArrayPtr) {
468 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
469 llvm::Value * NumElements =
470 llvm::ConstantInt::get(SizeTy,
471 getContext().getConstantArrayElementCount(ArrayTy));
472
473 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
474}
475
476void
477CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
478 llvm::Value *NumElements,
479 llvm::Value *ArrayPtr) {
480 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump11289f42009-09-09 15:08:12 +0000481
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000482 // Create a temporary for the loop index and initialize it with 0.
Anders Carlssond49844b2009-09-23 02:45:36 +0000483 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
484 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
485 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000486
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000487 // Start the loop with a block that tests the condition.
488 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
489 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +0000490
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000491 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000492
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000493 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump11289f42009-09-09 15:08:12 +0000494
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000495 // Generate: if (loop-index < number-of-elements fall to the loop body,
496 // otherwise, go to the block after the for-loop.
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000497 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlssond49844b2009-09-23 02:45:36 +0000498 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000499 // If the condition is true, execute the body.
500 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +0000501
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000502 EmitBlock(ForBody);
Mike Stump11289f42009-09-09 15:08:12 +0000503
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000504 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000505 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahaniandd46eb72009-08-20 01:01:06 +0000506 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlssond49844b2009-09-23 02:45:36 +0000507 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
508 "arrayidx");
Fariborz Jahanian2c73b1d2009-08-26 00:23:27 +0000509 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000510
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000511 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000512
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000513 // Emit the increment of the loop counter.
Anders Carlssond49844b2009-09-23 02:45:36 +0000514 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000515 Counter = Builder.CreateLoad(IndexPtr);
516 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
517 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000518
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000519 // Finally, branch back up to the condition for the next iteration.
520 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000521
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000522 // Emit the fall-through block.
523 EmitBlock(AfterFor, true);
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000524}
525
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000526/// EmitCXXAggrDestructorCall - calls the default destructor on array
527/// elements in reverse order of construction.
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000528void
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000529CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
530 const ArrayType *Array,
531 llvm::Value *This) {
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000532 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
533 assert(CA && "Do we support VLA for destruction ?");
Fariborz Jahanian6814eaa2009-11-13 19:27:47 +0000534 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
535 llvm::Value* ElementCountPtr =
536 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
537 EmitCXXAggrDestructorCall(D, ElementCountPtr, This);
538}
539
540/// EmitCXXAggrDestructorCall - calls the default destructor on array
541/// elements in reverse order of construction.
542void
543CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
544 llvm::Value *UpperCount,
545 llvm::Value *This) {
Mike Stump11289f42009-09-09 15:08:12 +0000546 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000547 1);
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000548 // Create a temporary for the loop index and initialize it with count of
549 // array elements.
550 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
551 "loop.index");
552 // Index = ElementCount;
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000553 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000554
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000555 // Start the loop with a block that tests the condition.
556 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
557 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +0000558
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000559 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000560
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000561 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump11289f42009-09-09 15:08:12 +0000562
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000563 // Generate: if (loop-index != 0 fall to the loop body,
564 // otherwise, go to the block after the for-loop.
Mike Stump11289f42009-09-09 15:08:12 +0000565 llvm::Value* zeroConstant =
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000566 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
567 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
568 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
569 "isne");
570 // If the condition is true, execute the body.
571 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +0000572
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000573 EmitBlock(ForBody);
Mike Stump11289f42009-09-09 15:08:12 +0000574
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000575 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
576 // Inside the loop body, emit the constructor call on the array element.
577 Counter = Builder.CreateLoad(IndexPtr);
578 Counter = Builder.CreateSub(Counter, One);
579 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
Fariborz Jahanianebea0052009-11-13 22:29:45 +0000580 if (D->isVirtual()) {
581 const llvm::Type *Ty =
582 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(D),
583 /*isVariadic=*/false);
584
585 llvm::Value *Callee = BuildVirtualCall(D, Dtor_Deleting, Address, Ty);
586 EmitCXXMemberCall(D, Callee, Address, 0, 0);
587 }
588 else
589 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump11289f42009-09-09 15:08:12 +0000590
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000591 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000592
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000593 // Emit the decrement of the loop counter.
594 Counter = Builder.CreateLoad(IndexPtr);
595 Counter = Builder.CreateSub(Counter, One, "dec");
596 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000597
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000598 // Finally, branch back up to the condition for the next iteration.
599 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000600
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000601 // Emit the fall-through block.
602 EmitBlock(AfterFor, true);
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000603}
604
Mike Stumpea950e22009-11-18 18:57:56 +0000605/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
606/// invoked, calls the default destructor on array elements in reverse order of
Fariborz Jahanian1254a092009-11-10 19:24:06 +0000607/// construction.
608llvm::Constant *
609CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
610 const ArrayType *Array,
611 llvm::Value *This) {
612 static int UniqueCount;
613 FunctionArgList Args;
614 ImplicitParamDecl *Dst =
615 ImplicitParamDecl::Create(getContext(), 0,
616 SourceLocation(), 0,
617 getContext().getPointerType(getContext().VoidTy));
618 Args.push_back(std::make_pair(Dst, Dst->getType()));
619
620 llvm::SmallString<16> Name;
621 llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueCount);
622 QualType R = getContext().VoidTy;
623 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
624 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
625 llvm::Function *Fn =
626 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
627 Name.c_str(),
628 &CGM.getModule());
629 IdentifierInfo *II
630 = &CGM.getContext().Idents.get(Name.c_str());
631 FunctionDecl *FD = FunctionDecl::Create(getContext(),
632 getContext().getTranslationUnitDecl(),
633 SourceLocation(), II, R, 0,
634 FunctionDecl::Static,
635 false, true);
636 StartFunction(FD, R, Fn, Args, SourceLocation());
637 QualType BaseElementTy = getContext().getBaseElementType(Array);
638 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
639 BasePtr = llvm::PointerType::getUnqual(BasePtr);
640 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
641 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
642 FinishFunction();
643 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
644 0);
645 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
646 return m;
647}
648
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000649void
Mike Stump11289f42009-09-09 15:08:12 +0000650CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
651 CXXCtorType Type,
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000652 llvm::Value *This,
653 CallExpr::const_arg_iterator ArgBeg,
654 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian42af5ba2009-08-14 20:11:43 +0000655 if (D->isCopyConstructor(getContext())) {
656 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
657 if (ClassDecl->hasTrivialCopyConstructor()) {
658 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
659 "EmitCXXConstructorCall - user declared copy constructor");
660 const Expr *E = (*ArgBeg);
661 QualType Ty = E->getType();
662 llvm::Value *Src = EmitLValue(E).getAddress();
663 EmitAggregateCopy(This, Src, Ty);
664 return;
665 }
666 }
Mike Stump11289f42009-09-09 15:08:12 +0000667
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000668 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
669
670 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000671}
672
Mike Stump11289f42009-09-09 15:08:12 +0000673void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson0a637412009-05-29 21:03:38 +0000674 CXXDtorType Type,
675 llvm::Value *This) {
676 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000677
Anders Carlsson0a637412009-05-29 21:03:38 +0000678 EmitCXXMemberCall(D, Callee, This, 0, 0);
679}
680
Mike Stump11289f42009-09-09 15:08:12 +0000681void
682CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson1619a5042009-05-03 17:47:16 +0000683 const CXXConstructExpr *E) {
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000684 assert(Dest && "Must have a destination!");
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000685 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000686 const ConstantArrayType *Array =
687 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000688 // For a copy constructor, even if it is trivial, must fall thru so
689 // its argument is code-gen'ed.
690 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000691 QualType InitType = E->getType();
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000692 if (Array)
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000693 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000694 const CXXRecordDecl *RD =
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000695 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000696 if (RD->hasTrivialConstructor())
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000697 return;
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000698 }
Mike Stump11289f42009-09-09 15:08:12 +0000699 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanianeb869762009-08-06 01:02:49 +0000700 // its first argument instead.
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000701 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Anders Carlsson78cfaa92009-11-13 04:34:45 +0000702 const Expr *Arg = E->getArg(0);
703
704 if (const CXXBindTemporaryExpr *BindExpr =
705 dyn_cast<CXXBindTemporaryExpr>(Arg))
706 Arg = BindExpr->getSubExpr();
707
708 EmitAggExpr(Arg, Dest, false);
Fariborz Jahanian00130932009-08-06 19:12:38 +0000709 return;
Fariborz Jahanianeb869762009-08-06 01:02:49 +0000710 }
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000711 if (Array) {
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000712 QualType BaseElementTy = getContext().getBaseElementType(Array);
713 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
714 BasePtr = llvm::PointerType::getUnqual(BasePtr);
715 llvm::Value *BaseAddrPtr =
716 Builder.CreateBitCast(Dest, BasePtr);
717 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
718 }
719 else
720 // Call the constructor.
721 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
722 E->arg_begin(), E->arg_end());
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000723}
724
Anders Carlssonf7475242009-04-15 15:55:24 +0000725void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlssondae1abc2009-05-05 04:44:02 +0000726 EmitGlobal(GlobalDecl(D, Ctor_Complete));
727 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlssonf7475242009-04-15 15:55:24 +0000728}
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000729
Mike Stump11289f42009-09-09 15:08:12 +0000730void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000731 CXXCtorType Type) {
Mike Stump11289f42009-09-09 15:08:12 +0000732
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000733 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000734
Anders Carlsson73fcc952009-09-11 00:07:24 +0000735 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump11289f42009-09-09 15:08:12 +0000736
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000737 SetFunctionDefinitionAttributes(D, Fn);
738 SetLLVMFunctionAttributesForDefinition(D, Fn);
739}
740
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000741llvm::Function *
Mike Stump11289f42009-09-09 15:08:12 +0000742CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000743 CXXCtorType Type) {
Fariborz Jahanianc2d71b52009-11-06 18:47:57 +0000744 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000745 const llvm::FunctionType *FTy =
Fariborz Jahanianc2d71b52009-11-06 18:47:57 +0000746 getTypes().GetFunctionType(getTypes().getFunctionInfo(D),
747 FPT->isVariadic());
Mike Stump11289f42009-09-09 15:08:12 +0000748
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000749 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnere0be0df2009-05-12 21:21:08 +0000750 return cast<llvm::Function>(
751 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000752}
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000753
Mike Stump11289f42009-09-09 15:08:12 +0000754const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000755 CXXCtorType Type) {
756 llvm::SmallString<256> Name;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000757 getMangleContext().mangleCXXCtor(D, Type, Name);
Mike Stump11289f42009-09-09 15:08:12 +0000758
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000759 Name += '\0';
760 return UniqueMangledName(Name.begin(), Name.end());
761}
762
763void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Eli Friedmanb572c922009-11-14 04:19:37 +0000764 if (D->isVirtual())
765 EmitCXXDestructor(D, Dtor_Deleting);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000766 EmitCXXDestructor(D, Dtor_Complete);
767 EmitCXXDestructor(D, Dtor_Base);
768}
769
Mike Stump11289f42009-09-09 15:08:12 +0000770void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000771 CXXDtorType Type) {
772 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000773
Anders Carlsson73fcc952009-09-11 00:07:24 +0000774 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump11289f42009-09-09 15:08:12 +0000775
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000776 SetFunctionDefinitionAttributes(D, Fn);
777 SetLLVMFunctionAttributesForDefinition(D, Fn);
778}
779
780llvm::Function *
Mike Stump11289f42009-09-09 15:08:12 +0000781CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000782 CXXDtorType Type) {
783 const llvm::FunctionType *FTy =
784 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump11289f42009-09-09 15:08:12 +0000785
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000786 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnere0be0df2009-05-12 21:21:08 +0000787 return cast<llvm::Function>(
788 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000789}
790
Mike Stump11289f42009-09-09 15:08:12 +0000791const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000792 CXXDtorType Type) {
793 llvm::SmallString<256> Name;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000794 getMangleContext().mangleCXXDtor(D, Type, Name);
Mike Stump11289f42009-09-09 15:08:12 +0000795
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000796 Name += '\0';
797 return UniqueMangledName(Name.begin(), Name.end());
798}
Fariborz Jahanian83381cc2009-07-20 23:18:55 +0000799
Mike Stump5a522352009-09-04 18:27:16 +0000800llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
801 const CXXMethodDecl *MD,
Mike Stump453fe422009-09-05 07:20:32 +0000802 bool Extern, int64_t nv,
803 int64_t v) {
Mike Stump33ccd9e2009-11-02 23:22:01 +0000804 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stump5a522352009-09-04 18:27:16 +0000805}
806
Mike Stump77738202009-11-03 16:59:27 +0000807llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
808 int64_t v) {
809 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
810 0);
811 const llvm::Type *OrigTy = V->getType();
812 if (nv) {
813 // Do the non-virtual adjustment
814 V = Builder.CreateBitCast(V, Ptr8Ty);
815 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
816 V = Builder.CreateBitCast(V, OrigTy);
817 }
818 if (v) {
819 // Do the virtual this adjustment
820 const llvm::Type *PtrDiffTy =
821 ConvertType(getContext().getPointerDiffType());
822 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
823 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
824 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
825 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
826 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
827 V = Builder.CreateLoad(V, "vtable");
828 llvm::Value *VTablePtr = V;
829 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
830 v /= LLVMPointerWidth/8;
831 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
832 V = Builder.CreateLoad(V);
833 V = Builder.CreateGEP(ThisVal, V);
834 V = Builder.CreateBitCast(V, OrigTy);
835 }
836 return V;
837}
838
Mike Stump80f6ac52009-09-11 23:25:56 +0000839llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
840 const CXXMethodDecl *MD,
841 bool Extern,
842 int64_t nv_t,
843 int64_t v_t,
844 int64_t nv_r,
845 int64_t v_r) {
Mike Stump33ccd9e2009-11-02 23:22:01 +0000846 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump80f6ac52009-09-11 23:25:56 +0000847
848 FunctionArgList Args;
849 ImplicitParamDecl *ThisDecl =
850 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
851 MD->getThisType(getContext()));
852 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
853 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
854 e = MD->param_end();
855 i != e; ++i) {
856 ParmVarDecl *D = *i;
857 Args.push_back(std::make_pair(D, D->getType()));
858 }
859 IdentifierInfo *II
860 = &CGM.getContext().Idents.get("__thunk_named_foo_");
861 FunctionDecl *FD = FunctionDecl::Create(getContext(),
862 getContext().getTranslationUnitDecl(),
Mike Stump33ccd9e2009-11-02 23:22:01 +0000863 SourceLocation(), II, ResultType, 0,
Mike Stump80f6ac52009-09-11 23:25:56 +0000864 Extern
865 ? FunctionDecl::Extern
866 : FunctionDecl::Static,
867 false, true);
Mike Stump33ccd9e2009-11-02 23:22:01 +0000868 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
869
870 // generate body
Mike Stumpf3589722009-11-03 02:12:59 +0000871 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
872 const llvm::Type *Ty =
873 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
874 FPT->isVariadic());
875 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump33ccd9e2009-11-02 23:22:01 +0000876 CallArgList CallArgs;
877
Mike Stumpf3589722009-11-03 02:12:59 +0000878 QualType ArgType = MD->getThisType(getContext());
879 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stump71609a22009-11-04 00:53:51 +0000880 if (nv_t || v_t) {
Mike Stump77738202009-11-03 16:59:27 +0000881 // Do the this adjustment.
Mike Stump71609a22009-11-04 00:53:51 +0000882 const llvm::Type *OrigTy = Callee->getType();
Mike Stump77738202009-11-03 16:59:27 +0000883 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stump71609a22009-11-04 00:53:51 +0000884 if (nv_r || v_r) {
885 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
886 Callee = Builder.CreateBitCast(Callee, OrigTy);
887 nv_r = v_r = 0;
888 }
889 }
890
Mike Stumpf3589722009-11-03 02:12:59 +0000891 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
892
Mike Stump33ccd9e2009-11-02 23:22:01 +0000893 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
894 e = MD->param_end();
895 i != e; ++i) {
896 ParmVarDecl *D = *i;
897 QualType ArgType = D->getType();
898
899 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
900 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stump33ccd9e2009-11-02 23:22:01 +0000901 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
902 }
903
Mike Stump31e1d432009-11-02 23:47:45 +0000904 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
905 Callee, CallArgs, MD);
Mike Stump33ccd9e2009-11-02 23:22:01 +0000906 if (nv_r || v_r) {
Mike Stumpc5507682009-11-05 06:32:02 +0000907 bool CanBeZero = !(ResultType->isReferenceType()
908 // FIXME: attr nonnull can't be zero either
909 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stump77738202009-11-03 16:59:27 +0000910 // Do the return result adjustment.
Mike Stumpc5507682009-11-05 06:32:02 +0000911 if (CanBeZero) {
912 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
913 llvm::BasicBlock *ZeroBlock = createBasicBlock();
914 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stumpb8da7a02009-11-05 06:12:26 +0000915
Mike Stumpc5507682009-11-05 06:32:02 +0000916 const llvm::Type *Ty = RV.getScalarVal()->getType();
917 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
918 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
919 NonZeroBlock, ZeroBlock);
920 EmitBlock(NonZeroBlock);
921 llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r);
922 EmitBranch(ContBlock);
923 EmitBlock(ZeroBlock);
924 llvm::Value *Z = RV.getScalarVal();
925 EmitBlock(ContBlock);
926 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
927 RVOrZero->reserveOperandSpace(2);
928 RVOrZero->addIncoming(NZ, NonZeroBlock);
929 RVOrZero->addIncoming(Z, ZeroBlock);
930 RV = RValue::get(RVOrZero);
931 } else
932 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stump33ccd9e2009-11-02 23:22:01 +0000933 }
934
Mike Stump31e1d432009-11-02 23:47:45 +0000935 if (!ResultType->isVoidType())
936 EmitReturnOfRValue(RV, ResultType);
937
Mike Stump80f6ac52009-09-11 23:25:56 +0000938 FinishFunction();
939 return Fn;
940}
941
Mike Stump453fe422009-09-05 07:20:32 +0000942llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
943 int64_t nv, int64_t v) {
Mike Stump5a522352009-09-04 18:27:16 +0000944 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000945 getMangleContext().mangleThunk(MD, nv, v, OutName);
Mike Stump5a522352009-09-04 18:27:16 +0000946 llvm::GlobalVariable::LinkageTypes linktype;
947 linktype = llvm::GlobalValue::WeakAnyLinkage;
948 if (!Extern)
949 linktype = llvm::GlobalValue::InternalLinkage;
950 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall9dd450b2009-09-21 23:43:11 +0000951 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump5a522352009-09-04 18:27:16 +0000952 const llvm::FunctionType *FTy =
953 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
954 FPT->isVariadic());
955
Daniel Dunbare128dd12009-11-21 09:06:22 +0000956 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump5a522352009-09-04 18:27:16 +0000957 &getModule());
Mike Stump453fe422009-09-05 07:20:32 +0000958 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stump5a522352009-09-04 18:27:16 +0000959 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
960 return m;
961}
962
Mike Stump80f6ac52009-09-11 23:25:56 +0000963llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
964 bool Extern, int64_t nv_t,
965 int64_t v_t, int64_t nv_r,
966 int64_t v_r) {
967 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000968 getMangleContext().mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, OutName);
Mike Stump80f6ac52009-09-11 23:25:56 +0000969 llvm::GlobalVariable::LinkageTypes linktype;
970 linktype = llvm::GlobalValue::WeakAnyLinkage;
971 if (!Extern)
972 linktype = llvm::GlobalValue::InternalLinkage;
973 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall9dd450b2009-09-21 23:43:11 +0000974 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump80f6ac52009-09-11 23:25:56 +0000975 const llvm::FunctionType *FTy =
976 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
977 FPT->isVariadic());
978
Daniel Dunbare128dd12009-11-21 09:06:22 +0000979 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump80f6ac52009-09-11 23:25:56 +0000980 &getModule());
981 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
982 v_r);
Mike Stump80f6ac52009-09-11 23:25:56 +0000983 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
984 return m;
985}
986
Mike Stumpa5588bf2009-08-26 20:46:33 +0000987llvm::Value *
Anders Carlssonc6d171e2009-10-06 22:43:30 +0000988CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
989 const CXXRecordDecl *ClassDecl,
990 const CXXRecordDecl *BaseClassDecl) {
Anders Carlssonc6d171e2009-10-06 22:43:30 +0000991 const llvm::Type *Int8PtrTy =
992 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
993
994 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
995 Int8PtrTy->getPointerTo());
996 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
997
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000998 int64_t VBaseOffsetIndex =
999 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
1000
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001001 llvm::Value *VBaseOffsetPtr =
Mike Stumpb9c9b352009-11-04 01:11:15 +00001002 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001003 const llvm::Type *PtrDiffTy =
1004 ConvertType(getContext().getPointerDiffType());
1005
1006 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1007 PtrDiffTy->getPointerTo());
1008
1009 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1010
1011 return VBaseOffset;
1012}
1013
Anders Carlssone828c362009-11-13 04:45:41 +00001014static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, int64_t VtableIndex,
1015 llvm::Value *This, const llvm::Type *Ty) {
1016 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson32bfb1c2009-10-03 14:56:57 +00001017
Anders Carlssone828c362009-11-13 04:45:41 +00001018 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
1019 Vtable = CGF.Builder.CreateLoad(Vtable);
1020
1021 llvm::Value *VFuncPtr =
1022 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
1023 return CGF.Builder.CreateLoad(VFuncPtr);
1024}
1025
1026llvm::Value *
1027CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1028 const llvm::Type *Ty) {
1029 MD = MD->getCanonicalDecl();
1030 int64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
1031
1032 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1033}
1034
1035llvm::Value *
1036CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
1037 llvm::Value *&This, const llvm::Type *Ty) {
1038 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
1039 int64_t VtableIndex =
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001040 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlssone828c362009-11-13 04:45:41 +00001041
1042 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpa5588bf2009-08-26 20:46:33 +00001043}
1044
Fariborz Jahanian56263842009-08-21 18:30:26 +00001045/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1046/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1047/// copy or via a copy constructor call.
Fariborz Jahanian2c73b1d2009-08-26 00:23:27 +00001048// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump11289f42009-09-09 15:08:12 +00001049void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001050 llvm::Value *Src,
1051 const ArrayType *Array,
Mike Stump11289f42009-09-09 15:08:12 +00001052 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001053 QualType Ty) {
1054 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1055 assert(CA && "VLA cannot be copied over");
1056 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump11289f42009-09-09 15:08:12 +00001057
Fariborz Jahanian56263842009-08-21 18:30:26 +00001058 // Create a temporary for the loop index and initialize it with 0.
1059 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1060 "loop.index");
Mike Stump11289f42009-09-09 15:08:12 +00001061 llvm::Value* zeroConstant =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001062 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson32bfb1c2009-10-03 14:56:57 +00001063 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanian56263842009-08-21 18:30:26 +00001064 // Start the loop with a block that tests the condition.
1065 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1066 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +00001067
Fariborz Jahanian56263842009-08-21 18:30:26 +00001068 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001069
Fariborz Jahanian56263842009-08-21 18:30:26 +00001070 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1071 // Generate: if (loop-index < number-of-elements fall to the loop body,
1072 // otherwise, go to the block after the for-loop.
1073 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump11289f42009-09-09 15:08:12 +00001074 llvm::Value * NumElementsPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001075 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1076 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001077 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001078 "isless");
1079 // If the condition is true, execute the body.
1080 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +00001081
Fariborz Jahanian56263842009-08-21 18:30:26 +00001082 EmitBlock(ForBody);
1083 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1084 // Inside the loop body, emit the constructor call on the array element.
1085 Counter = Builder.CreateLoad(IndexPtr);
1086 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1087 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1088 if (BitwiseCopy)
1089 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001090 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001091 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump11289f42009-09-09 15:08:12 +00001092 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001093 Ctor_Complete);
1094 CallArgList CallArgs;
1095 // Push the this (Dest) ptr.
1096 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1097 BaseCopyCtor->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001098
Fariborz Jahanian56263842009-08-21 18:30:26 +00001099 // Push the Src ptr.
1100 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stumpb9c9b352009-11-04 01:11:15 +00001101 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001102 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001103 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian56263842009-08-21 18:30:26 +00001104 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1105 Callee, CallArgs, BaseCopyCtor);
1106 }
1107 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001108
Fariborz Jahanian56263842009-08-21 18:30:26 +00001109 // Emit the increment of the loop counter.
1110 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1111 Counter = Builder.CreateLoad(IndexPtr);
1112 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1113 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001114
Fariborz Jahanian56263842009-08-21 18:30:26 +00001115 // Finally, branch back up to the condition for the next iteration.
1116 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001117
Fariborz Jahanian56263842009-08-21 18:30:26 +00001118 // Emit the fall-through block.
1119 EmitBlock(AfterFor, true);
1120}
1121
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001122/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump11289f42009-09-09 15:08:12 +00001123/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001124/// bitwise assignment or via a copy assignment operator function call.
1125/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump11289f42009-09-09 15:08:12 +00001126void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001127 llvm::Value *Src,
1128 const ArrayType *Array,
Mike Stump11289f42009-09-09 15:08:12 +00001129 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001130 QualType Ty) {
1131 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1132 assert(CA && "VLA cannot be asssigned");
1133 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump11289f42009-09-09 15:08:12 +00001134
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001135 // Create a temporary for the loop index and initialize it with 0.
1136 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1137 "loop.index");
Mike Stump11289f42009-09-09 15:08:12 +00001138 llvm::Value* zeroConstant =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001139 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1140 Builder.CreateStore(zeroConstant, IndexPtr, false);
1141 // Start the loop with a block that tests the condition.
1142 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1143 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +00001144
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001145 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001146
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001147 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1148 // Generate: if (loop-index < number-of-elements fall to the loop body,
1149 // otherwise, go to the block after the for-loop.
1150 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump11289f42009-09-09 15:08:12 +00001151 llvm::Value * NumElementsPtr =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001152 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1153 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001154 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001155 "isless");
1156 // If the condition is true, execute the body.
1157 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +00001158
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001159 EmitBlock(ForBody);
1160 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1161 // Inside the loop body, emit the assignment operator call on array element.
1162 Counter = Builder.CreateLoad(IndexPtr);
1163 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1164 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1165 const CXXMethodDecl *MD = 0;
1166 if (BitwiseAssign)
1167 EmitAggregateCopy(Dest, Src, Ty);
1168 else {
1169 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1170 MD);
1171 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1172 (void)hasCopyAssign;
John McCall9dd450b2009-09-21 23:43:11 +00001173 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001174 const llvm::Type *LTy =
1175 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1176 FPT->isVariadic());
Anders Carlssonecf9bf02009-09-10 23:43:36 +00001177 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001179 CallArgList CallArgs;
1180 // Push the this (Dest) ptr.
1181 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1182 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001183
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001184 // Push the Src ptr.
1185 CallArgs.push_back(std::make_pair(RValue::get(Src),
1186 MD->getParamDecl(0)->getType()));
John McCall9dd450b2009-09-21 23:43:11 +00001187 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001188 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1189 Callee, CallArgs, MD);
1190 }
1191 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001192
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001193 // Emit the increment of the loop counter.
1194 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1195 Counter = Builder.CreateLoad(IndexPtr);
1196 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1197 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001198
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001199 // Finally, branch back up to the condition for the next iteration.
1200 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001201
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001202 // Emit the fall-through block.
1203 EmitBlock(AfterFor, true);
1204}
1205
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001206/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1207/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanian56263842009-08-21 18:30:26 +00001208/// or via a copy constructor call.
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001209void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001210 llvm::Value *Dest, llvm::Value *Src,
Mike Stump11289f42009-09-09 15:08:12 +00001211 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001212 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1213 if (ClassDecl) {
Anders Carlsson8c793172009-11-23 17:57:54 +00001214 Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1215 /*NullCheckValue=*/false);
1216 Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl,
1217 /*NullCheckValue=*/false);
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001218 }
1219 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1220 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001221 return;
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001222 }
Mike Stump11289f42009-09-09 15:08:12 +00001223
1224 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian7c3d7f62009-08-08 00:59:58 +00001225 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump11289f42009-09-09 15:08:12 +00001226 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001227 Ctor_Complete);
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001228 CallArgList CallArgs;
1229 // Push the this (Dest) ptr.
1230 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1231 BaseCopyCtor->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001232
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001233 // Push the Src ptr.
1234 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian2a26e352009-08-10 17:20:45 +00001235 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001236 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001237 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001238 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1239 Callee, CallArgs, BaseCopyCtor);
1240 }
1241}
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001242
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001243/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump11289f42009-09-09 15:08:12 +00001244/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001245/// assignment of via an assignment operator call.
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001246// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001247void CodeGenFunction::EmitClassCopyAssignment(
1248 llvm::Value *Dest, llvm::Value *Src,
Mike Stump11289f42009-09-09 15:08:12 +00001249 const CXXRecordDecl *ClassDecl,
1250 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001251 QualType Ty) {
1252 if (ClassDecl) {
Anders Carlsson8c793172009-11-23 17:57:54 +00001253 Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1254 /*NullCheckValue=*/false);
1255 Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl,
1256 /*NullCheckValue=*/false);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001257 }
1258 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1259 EmitAggregateCopy(Dest, Src, Ty);
1260 return;
1261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001263 const CXXMethodDecl *MD = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001264 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001265 MD);
1266 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1267 (void)ConstCopyAssignOp;
1268
John McCall9dd450b2009-09-21 23:43:11 +00001269 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +00001270 const llvm::Type *LTy =
1271 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001272 FPT->isVariadic());
Anders Carlssonecf9bf02009-09-10 23:43:36 +00001273 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump11289f42009-09-09 15:08:12 +00001274
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001275 CallArgList CallArgs;
1276 // Push the this (Dest) ptr.
1277 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1278 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001279
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001280 // Push the Src ptr.
1281 CallArgs.push_back(std::make_pair(RValue::get(Src),
1282 MD->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001283 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001284 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001285 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1286 Callee, CallArgs, MD);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001287}
1288
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001289/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump11289f42009-09-09 15:08:12 +00001290void
Anders Carlssonddf57d32009-09-14 05:32:02 +00001291CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1292 CXXCtorType Type,
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001293 llvm::Function *Fn,
1294 const FunctionArgList &Args) {
Anders Carlssonddf57d32009-09-14 05:32:02 +00001295 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1296 SourceLocation());
1297 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001298 FinishFunction();
1299}
1300
Mike Stumpb9c9b352009-11-04 01:11:15 +00001301/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1302/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump11289f42009-09-09 15:08:12 +00001303/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stumpb9c9b352009-11-04 01:11:15 +00001304/// copy of its subobjects. The order of copying is the same as the order of
1305/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001306/// Each subobject is copied in the manner appropriate to its type:
Mike Stump11289f42009-09-09 15:08:12 +00001307/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001308/// used;
Mike Stump11289f42009-09-09 15:08:12 +00001309/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001310/// appropriate to the element type;
Mike Stump11289f42009-09-09 15:08:12 +00001311/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001312/// used.
Mike Stump11289f42009-09-09 15:08:12 +00001313/// Virtual base class subobjects shall be copied only once by the
1314/// implicitly-defined copy constructor
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001315
Anders Carlssonddf57d32009-09-14 05:32:02 +00001316void
1317CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1318 CXXCtorType Type,
1319 llvm::Function *Fn,
1320 const FunctionArgList &Args) {
Anders Carlsson73fcc952009-09-11 00:07:24 +00001321 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001322 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stumpb9c9b352009-11-04 01:11:15 +00001323 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonddf57d32009-09-14 05:32:02 +00001324 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1325 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001326
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001327 FunctionArgList::const_iterator i = Args.begin();
1328 const VarDecl *ThisArg = i->first;
1329 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1330 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1331 const VarDecl *SrcArg = (i+1)->first;
1332 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1333 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump11289f42009-09-09 15:08:12 +00001334
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001335 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1336 Base != ClassDecl->bases_end(); ++Base) {
1337 // FIXME. copy constrution of virtual base NYI
1338 if (Base->isVirtual())
1339 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001340
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001341 CXXRecordDecl *BaseClassDecl
1342 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001343 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1344 Base->getType());
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001345 }
Mike Stump11289f42009-09-09 15:08:12 +00001346
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001347 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1348 FieldEnd = ClassDecl->field_end();
1349 Field != FieldEnd; ++Field) {
1350 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001351 const ConstantArrayType *Array =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001352 getContext().getAsConstantArrayType(FieldType);
1353 if (Array)
1354 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump11289f42009-09-09 15:08:12 +00001355
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001356 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1357 CXXRecordDecl *FieldClassDecl
1358 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1359 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1360 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanian56263842009-08-21 18:30:26 +00001361 if (Array) {
1362 const llvm::Type *BasePtr = ConvertType(FieldType);
1363 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump11289f42009-09-09 15:08:12 +00001364 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001365 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump11289f42009-09-09 15:08:12 +00001366 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001367 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1368 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1369 FieldClassDecl, FieldType);
1370 }
Mike Stump11289f42009-09-09 15:08:12 +00001371 else
1372 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanian56263842009-08-21 18:30:26 +00001373 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001374 continue;
1375 }
Fariborz Jahanian7cc52cf2009-08-10 18:34:26 +00001376 // Do a built-in assignment of scalar data members.
1377 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1378 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Eli Friedmanc2ef2152009-11-16 21:47:41 +00001379 if (!hasAggregateLLVMType(Field->getType())) {
1380 RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType());
1381 EmitStoreThroughLValue(RVRHS, LHS, Field->getType());
1382 } else if (Field->getType()->isAnyComplexType()) {
1383 ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(),
1384 RHS.isVolatileQualified());
1385 StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified());
1386 } else {
1387 EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType());
1388 }
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001389 }
Fariborz Jahanianf6bda5d2009-08-08 19:31:03 +00001390 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001391}
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001392
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001393/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump11289f42009-09-09 15:08:12 +00001394/// Before the implicitly-declared copy assignment operator for a class is
1395/// implicitly defined, all implicitly- declared copy assignment operators for
1396/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001397/// implicitly defined. [12.8-p12]
Mike Stump11289f42009-09-09 15:08:12 +00001398/// The implicitly-defined copy assignment operator for class X performs
1399/// memberwise assignment of its subob- jects. The direct base classes of X are
1400/// assigned first, in the order of their declaration in
1401/// the base-specifier-list, and then the immediate nonstatic data members of X
1402/// are assigned, in the order in which they were declared in the class
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001403/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump11289f42009-09-09 15:08:12 +00001404/// if the subobject is of class type, the copy assignment operator for the
1405/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001406/// possible virtual overriding functions in more derived classes);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001407///
Mike Stump11289f42009-09-09 15:08:12 +00001408/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001409/// appropriate to the element type;
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001410///
Mike Stump11289f42009-09-09 15:08:12 +00001411/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001412/// used.
1413void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001414 llvm::Function *Fn,
1415 const FunctionArgList &Args) {
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001416
1417 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1418 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1419 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonddf57d32009-09-14 05:32:02 +00001420 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001421
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001422 FunctionArgList::const_iterator i = Args.begin();
1423 const VarDecl *ThisArg = i->first;
1424 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1425 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1426 const VarDecl *SrcArg = (i+1)->first;
1427 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1428 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump11289f42009-09-09 15:08:12 +00001429
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001430 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1431 Base != ClassDecl->bases_end(); ++Base) {
1432 // FIXME. copy assignment of virtual base NYI
1433 if (Base->isVirtual())
1434 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001435
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001436 CXXRecordDecl *BaseClassDecl
1437 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1438 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1439 Base->getType());
1440 }
Mike Stump11289f42009-09-09 15:08:12 +00001441
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001442 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1443 FieldEnd = ClassDecl->field_end();
1444 Field != FieldEnd; ++Field) {
1445 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001446 const ConstantArrayType *Array =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001447 getContext().getAsConstantArrayType(FieldType);
1448 if (Array)
1449 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump11289f42009-09-09 15:08:12 +00001450
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001451 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1452 CXXRecordDecl *FieldClassDecl
1453 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1454 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1455 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001456 if (Array) {
1457 const llvm::Type *BasePtr = ConvertType(FieldType);
1458 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1459 llvm::Value *DestBaseAddrPtr =
1460 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1461 llvm::Value *SrcBaseAddrPtr =
1462 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1463 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1464 FieldClassDecl, FieldType);
1465 }
1466 else
Mike Stump11289f42009-09-09 15:08:12 +00001467 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001468 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001469 continue;
1470 }
1471 // Do a built-in assignment of scalar data members.
1472 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1473 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1474 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1475 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian92b3f472009-08-14 00:01:54 +00001476 }
Mike Stump11289f42009-09-09 15:08:12 +00001477
Fariborz Jahanian92b3f472009-08-14 00:01:54 +00001478 // return *this;
1479 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump11289f42009-09-09 15:08:12 +00001480
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001481 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001482}
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001483
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001484static void EmitBaseInitializer(CodeGenFunction &CGF,
1485 const CXXRecordDecl *ClassDecl,
1486 CXXBaseOrMemberInitializer *BaseInit,
1487 CXXCtorType CtorType) {
1488 assert(BaseInit->isBaseInitializer() &&
1489 "Must have base initializer!");
1490
1491 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1492
1493 const Type *BaseType = BaseInit->getBaseClass();
1494 CXXRecordDecl *BaseClassDecl =
1495 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson8c793172009-11-23 17:57:54 +00001496 llvm::Value *V = CGF.GetAddressOfBaseClass(ThisPtr, ClassDecl,
1497 BaseClassDecl,
1498 /*NullCheckValue=*/false);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001499 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1500 CtorType, V,
1501 BaseInit->const_arg_begin(),
1502 BaseInit->const_arg_end());
1503}
1504
1505static void EmitMemberInitializer(CodeGenFunction &CGF,
1506 const CXXRecordDecl *ClassDecl,
1507 CXXBaseOrMemberInitializer *MemberInit) {
1508 assert(MemberInit->isMemberInitializer() &&
1509 "Must have member initializer!");
1510
1511 // non-static data member initializers.
1512 FieldDecl *Field = MemberInit->getMember();
Eli Friedman5e7d9692009-11-16 23:34:11 +00001513 QualType FieldType = CGF.getContext().getCanonicalType(Field->getType());
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001514
1515 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1516 LValue LHS;
1517 if (FieldType->isReferenceType()) {
1518 // FIXME: This is really ugly; should be refactored somehow
1519 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1520 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1521 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1522 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1523 } else {
Eli Friedmanc1daba32009-11-16 22:58:01 +00001524 LHS = CGF.EmitLValueForField(ThisPtr, Field, ClassDecl->isUnion(), 0);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001525 }
Eli Friedman5e7d9692009-11-16 23:34:11 +00001526
1527 // If we are initializing an anonymous union field, drill down to the field.
1528 if (MemberInit->getAnonUnionMember()) {
1529 Field = MemberInit->getAnonUnionMember();
1530 LHS = CGF.EmitLValueForField(LHS.getAddress(), Field,
1531 /*IsUnion=*/true, 0);
1532 FieldType = Field->getType();
1533 }
1534
1535 // If the field is an array, branch based on the element type.
1536 const ConstantArrayType *Array =
1537 CGF.getContext().getAsConstantArrayType(FieldType);
1538 if (Array)
1539 FieldType = CGF.getContext().getBaseElementType(FieldType);
1540
Eli Friedmane85ef712009-11-16 23:53:01 +00001541 // We lose the constructor for anonymous union members, so handle them
1542 // explicitly.
1543 // FIXME: This is somwhat ugly.
1544 if (MemberInit->getAnonUnionMember() && FieldType->getAs<RecordType>()) {
1545 if (MemberInit->getNumArgs())
1546 CGF.EmitAggExpr(*MemberInit->arg_begin(), LHS.getAddress(),
1547 LHS.isVolatileQualified());
1548 else
1549 CGF.EmitAggregateClear(LHS.getAddress(), Field->getType());
1550 return;
1551 }
1552
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001553 if (FieldType->getAs<RecordType>()) {
Eli Friedman5e7d9692009-11-16 23:34:11 +00001554 assert(MemberInit->getConstructor() &&
1555 "EmitCtorPrologue - no constructor to initialize member");
1556 if (Array) {
1557 const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1558 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1559 llvm::Value *BaseAddrPtr =
1560 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1561 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
1562 Array, BaseAddrPtr);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001563 }
Eli Friedman5e7d9692009-11-16 23:34:11 +00001564 else
1565 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1566 Ctor_Complete, LHS.getAddress(),
1567 MemberInit->const_arg_begin(),
1568 MemberInit->const_arg_end());
1569 return;
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001570 }
1571
1572 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1573 Expr *RhsExpr = *MemberInit->arg_begin();
1574 RValue RHS;
Eli Friedmane85ef712009-11-16 23:53:01 +00001575 if (FieldType->isReferenceType()) {
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001576 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1577 /*IsInitializer=*/true);
Fariborz Jahanian03f62ed2009-11-11 17:55:25 +00001578 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
Eli Friedmane85ef712009-11-16 23:53:01 +00001579 } else if (Array) {
1580 CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType());
1581 } else if (!CGF.hasAggregateLLVMType(RhsExpr->getType())) {
1582 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
1583 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1584 } else if (RhsExpr->getType()->isAnyComplexType()) {
1585 CGF.EmitComplexExprIntoAddr(RhsExpr, LHS.getAddress(),
1586 LHS.isVolatileQualified());
1587 } else {
1588 // Handle member function pointers; other aggregates shouldn't get this far.
1589 CGF.EmitAggExpr(RhsExpr, LHS.getAddress(), LHS.isVolatileQualified());
1590 }
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001591}
1592
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001593/// EmitCtorPrologue - This routine generates necessary code to initialize
1594/// base classes and non-static data members belonging to this constructor.
Anders Carlsson6b8b4b42009-09-01 18:33:46 +00001595/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonddf57d32009-09-14 05:32:02 +00001596void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1597 CXXCtorType CtorType) {
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001598 const CXXRecordDecl *ClassDecl = CD->getParent();
1599
Mike Stump6b2556f2009-08-06 13:41:24 +00001600 // FIXME: Add vbase initialization
Mike Stumpbc78a722009-07-31 18:25:34 +00001601 llvm::Value *LoadOfThis = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001602
Fariborz Jahaniandedf1e42009-07-25 21:12:28 +00001603 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001604 E = CD->init_end();
1605 B != E; ++B) {
1606 CXXBaseOrMemberInitializer *Member = (*B);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001607
Anders Carlsson5852b132009-11-06 04:11:09 +00001608 assert(LiveTemporaries.empty() &&
1609 "Should not have any live temporaries at initializer start!");
1610
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001611 if (Member->isBaseInitializer())
1612 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1613 else
1614 EmitMemberInitializer(*this, ClassDecl, Member);
Anders Carlsson5852b132009-11-06 04:11:09 +00001615
1616 // Pop any live temporaries that the initializers might have pushed.
1617 while (!LiveTemporaries.empty())
1618 PopCXXTemporary();
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001619 }
Mike Stumpbc78a722009-07-31 18:25:34 +00001620
1621 // Initialize the vtable pointer
Mike Stumpa19718a2009-08-05 22:59:44 +00001622 if (ClassDecl->isDynamicClass()) {
Mike Stumpbc78a722009-07-31 18:25:34 +00001623 if (!LoadOfThis)
1624 LoadOfThis = LoadCXXThis();
1625 llvm::Value *VtableField;
1626 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson41a75022009-08-13 21:57:51 +00001627 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpbc78a722009-07-31 18:25:34 +00001628 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1629 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +00001630 llvm::Value *vtable = CGM.getVtableInfo().getVtable(ClassDecl);
Mike Stumpbc78a722009-07-31 18:25:34 +00001631 Builder.CreateStore(vtable, VtableField);
1632 }
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001633}
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001634
1635/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump11289f42009-09-09 15:08:12 +00001636/// destructor. This is to call destructors on members and base classes
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001637/// in reverse order of their construction.
Anders Carlsson6b8b4b42009-09-01 18:33:46 +00001638/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonddf57d32009-09-14 05:32:02 +00001639void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1640 CXXDtorType DtorType) {
Anders Carlssondee9a302009-11-17 04:44:12 +00001641 assert(!DD->isTrivial() &&
1642 "Should not emit dtor epilogue for trivial dtor!");
Mike Stump11289f42009-09-09 15:08:12 +00001643
Anders Carlssondee9a302009-11-17 04:44:12 +00001644 const CXXRecordDecl *ClassDecl = DD->getParent();
Mike Stump11289f42009-09-09 15:08:12 +00001645
Anders Carlssondee9a302009-11-17 04:44:12 +00001646 // Collect the fields.
1647 llvm::SmallVector<const FieldDecl *, 16> FieldDecls;
1648 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1649 E = ClassDecl->field_end(); I != E; ++I) {
1650 const FieldDecl *Field = *I;
1651
1652 QualType FieldType = getContext().getCanonicalType(Field->getType());
1653 FieldType = getContext().getBaseElementType(FieldType);
1654
1655 const RecordType *RT = FieldType->getAs<RecordType>();
1656 if (!RT)
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001657 continue;
Anders Carlssondee9a302009-11-17 04:44:12 +00001658
1659 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1660 if (FieldClassDecl->hasTrivialDestructor())
1661 continue;
1662
1663 FieldDecls.push_back(Field);
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001664 }
Anders Carlssond7872042009-11-15 23:03:25 +00001665
Anders Carlssondee9a302009-11-17 04:44:12 +00001666 // Now destroy the fields.
1667 for (size_t i = FieldDecls.size(); i > 0; --i) {
1668 const FieldDecl *Field = FieldDecls[i - 1];
1669
1670 QualType FieldType = Field->getType();
1671 const ConstantArrayType *Array =
1672 getContext().getAsConstantArrayType(FieldType);
1673 if (Array)
1674 FieldType = getContext().getBaseElementType(FieldType);
1675
1676 const RecordType *RT = FieldType->getAs<RecordType>();
1677 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1678
1679 llvm::Value *ThisPtr = LoadCXXThis();
1680
1681 LValue LHS = EmitLValueForField(ThisPtr, Field,
1682 /*isUnion=*/false,
1683 // FIXME: Qualifiers?
1684 /*CVRQualifiers=*/0);
1685 if (Array) {
1686 const llvm::Type *BasePtr = ConvertType(FieldType);
1687 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1688 llvm::Value *BaseAddrPtr =
1689 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1690 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
1691 Array, BaseAddrPtr);
1692 } else
1693 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1694 Dtor_Complete, LHS.getAddress());
1695 }
1696
1697 // Destroy non-virtual bases.
1698 for (CXXRecordDecl::reverse_base_class_const_iterator I =
1699 ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) {
1700 const CXXBaseSpecifier &Base = *I;
1701
1702 // Ignore virtual bases.
1703 if (Base.isVirtual())
1704 continue;
1705
1706 CXXRecordDecl *BaseClassDecl
1707 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1708
1709 // Ignore trivial destructors.
1710 if (BaseClassDecl->hasTrivialDestructor())
1711 continue;
1712
Anders Carlsson8c793172009-11-23 17:57:54 +00001713 llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(),
1714 ClassDecl, BaseClassDecl,
1715 /*NullCheckValue=*/false);
Anders Carlssondee9a302009-11-17 04:44:12 +00001716 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1717 Dtor_Base, V);
1718 }
1719
1720 // If we're emitting a base destructor, we don't want to emit calls to the
1721 // virtual bases.
1722 if (DtorType == Dtor_Base)
1723 return;
1724
1725 // FIXME: Handle virtual bases.
1726 for (CXXRecordDecl::reverse_base_class_const_iterator I =
1727 ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); I != E; ++I) {
1728 assert(false && "FIXME: Handle virtual bases.");
1729 }
1730
1731 // If we have a deleting destructor, emit a call to the delete operator.
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001732 if (DtorType == Dtor_Deleting)
1733 EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(),
1734 getContext().getTagDeclType(ClassDecl));
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001735}
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001736
Anders Carlssonddf57d32009-09-14 05:32:02 +00001737void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1738 CXXDtorType DtorType,
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001739 llvm::Function *Fn,
1740 const FunctionArgList &Args) {
Anders Carlssondee9a302009-11-17 04:44:12 +00001741 assert(!Dtor->getParent()->hasUserDeclaredDestructor() &&
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001742 "SynthesizeDefaultDestructor - destructor has user declaration");
Mike Stump11289f42009-09-09 15:08:12 +00001743
Anders Carlssonddf57d32009-09-14 05:32:02 +00001744 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1745 SourceLocation());
Anders Carlssondee9a302009-11-17 04:44:12 +00001746
Anders Carlssonddf57d32009-09-14 05:32:02 +00001747 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001748 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001749}