blob: 8f5cff4efafe3b5bc36de2fdb6af26f99a106b12 [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump1eb44332009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlssone1b29ef2008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson6815e942009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Mike Stump1eb44332009-09-09 15:08:12 +000029void
Fariborz Jahanian88f42802009-11-10 19:24:06 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000031 llvm::Constant *DeclPtr) {
Anders Carlsson6815e942009-09-27 18:58:34 +000032 const llvm::Type *Int8PtrTy =
33 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Mike Stump1eb44332009-09-09 15:08:12 +000034
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000035 std::vector<const llvm::Type *> Params;
36 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000038 // Get the destructor function type
Mike Stump1eb44332009-09-09 15:08:12 +000039 const llvm::Type *DtorFnTy =
Owen Anderson0032b272009-08-13 21:57:51 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000041 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000043 Params.clear();
44 Params.push_back(DtorFnTy);
45 Params.push_back(Int8PtrTy);
46 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000048 // Get the __cxa_atexit function type
49 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump1eb44332009-09-09 15:08:12 +000050 const llvm::FunctionType *AtExitFnTy =
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000051 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000053 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54 "__cxa_atexit");
Mike Stump1eb44332009-09-09 15:08:12 +000055
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000056 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57 "__dso_handle");
Anders Carlsson3b2e16b2009-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 Stump1eb44332009-09-09 15:08:12 +000064void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000065 llvm::Constant *DeclPtr) {
66 assert(D.hasGlobalStorage() &&
67 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000068
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000069 const Expr *Init = D.getInit();
70 QualType T = D.getType();
Mike Stumpdf317bf2009-11-03 23:25:48 +000071 bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
Mike Stump1eb44332009-09-09 15:08:12 +000072
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000073 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000074 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000075 } else if (!hasAggregateLLVMType(T)) {
76 llvm::Value *V = EmitScalarExpr(Init);
Mike Stumpdf317bf2009-11-03 23:25:48 +000077 EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000078 } else if (T->isAnyComplexType()) {
Mike Stumpdf317bf2009-11-03 23:25:48 +000079 EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000080 } else {
Mike Stumpdf317bf2009-11-03 23:25:48 +000081 EmitAggExpr(Init, DeclPtr, isVolatile);
Fariborz Jahanian88b11de2009-11-11 01:13:34 +000082 // Avoid generating destructor(s) for initialized objects.
83 if (!isa<CXXConstructExpr>(Init))
84 return;
Fariborz Jahanian88f42802009-11-10 19:24:06 +000085 const ConstantArrayType *Array = getContext().getAsConstantArrayType(T);
86 if (Array)
87 T = getContext().getBaseElementType(Array);
88
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000089 if (const RecordType *RT = T->getAs<RecordType>()) {
90 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian88f42802009-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 Carlsson3b2e16b2009-08-08 21:45:14 +0000105 }
106 }
107}
108
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000109void
110CodeGenModule::EmitCXXGlobalInitFunc() {
111 if (CXXGlobalInits.empty())
112 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Mike Stump79d57682009-11-04 01:11:15 +0000114 const llvm::FunctionType *FTy
115 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
116 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000118 // Create our global initialization function.
119 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000120 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000121 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
122 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000124 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000125 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-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 Carlsson0ff8baf2009-09-11 00:07:24 +0000133 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000134 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000136 for (unsigned i = 0; i != NumDecls; ++i) {
137 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000139 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
140 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
141 }
142 FinishFunction();
143}
144
Mike Stump1eb44332009-09-09 15:08:12 +0000145void
146CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000147 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000148 // FIXME: This should use __cxa_guard_{acquire,release}?
149
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000150 assert(!getContext().getLangOptions().ThreadsafeStatics &&
151 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000152
Anders Carlsson283a0622009-04-13 18:03:33 +0000153 llvm::SmallString<256> GuardVName;
154 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000155 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000157 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000158 llvm::GlobalValue *GuardV =
Mike Stump79d57682009-11-04 01:11:15 +0000159 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
160 false, GV->getLinkage(),
161 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000162 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000164 // Load the first byte of the guard variable.
Mike Stump79d57682009-11-04 01:11:15 +0000165 const llvm::Type *PtrTy
166 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000167 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000168 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170 // Compare it against 0.
Mike Stump79d57682009-11-04 01:11:15 +0000171 llvm::Value *nullValue
172 = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000173 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Daniel Dunbar55e87422008-11-11 02:29:29 +0000175 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000176 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000177
178 // If the guard variable is 0, jump to the initializer code.
179 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000181 EmitBlock(InitBlock);
182
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000183 EmitCXXGlobalVarDeclInit(D, GV);
184
Mike Stump79d57682009-11-04 01:11:15 +0000185 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
186 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000187 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000189 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000190}
191
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000192RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
193 llvm::Value *Callee,
194 llvm::Value *This,
195 CallExpr::const_arg_iterator ArgBeg,
196 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000197 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000198 "Trying to emit a member call expr on a static method!");
199
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000200 // A call to a trivial destructor requires no code generation.
201 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
202 if (Destructor->isTrivial())
203 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
John McCall183700f2009-09-21 23:43:11 +0000205 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000207 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000209 // Push the this ptr.
210 Args.push_back(std::make_pair(RValue::get(This),
211 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000213 // And the rest of the call args
214 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
John McCall183700f2009-09-21 23:43:11 +0000216 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000217 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
218 Callee, Args, MD);
219}
220
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000221/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
222/// expr can be devirtualized.
223static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
224 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
225 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
226 // This is a record decl. We know the type and can devirtualize it.
227 return VD->getType()->isRecordType();
228 }
Anders Carlsson76366482009-10-12 19:45:47 +0000229
230 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000231 }
232
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000233 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000234 if (isa<CXXTemporaryObjectExpr>(Base))
235 return true;
236
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000237 // And calls on bound temporaries.
238 if (isa<CXXBindTemporaryExpr>(Base))
239 return true;
240
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000241 // Check if this is a call expr that returns a record type.
242 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
243 return CE->getCallReturnType()->isRecordType();
244
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000245 // We can't devirtualize the call.
246 return false;
247}
248
Anders Carlsson774e7c62009-04-03 22:50:24 +0000249RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000250 if (isa<BinaryOperator>(CE->getCallee()))
251 return EmitCXXMemberPointerCallExpr(CE);
252
Anders Carlsson774e7c62009-04-03 22:50:24 +0000253 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
254 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000255
Anders Carlsson2472bf02009-09-29 03:54:11 +0000256 if (MD->isStatic()) {
257 // The method is static, emit it as we would a regular call.
258 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
259 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
260 CE->arg_begin(), CE->arg_end(), 0);
261
262 }
263
John McCall183700f2009-09-21 23:43:11 +0000264 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000265
Mike Stump1eb44332009-09-09 15:08:12 +0000266 const llvm::Type *Ty =
267 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000268 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000269 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson774e7c62009-04-03 22:50:24 +0000271 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000272 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000273 else {
274 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000275 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000276 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000277
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000278 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000279 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000280 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000281 //
282 // We also don't emit a virtual call if the base expression has a record type
283 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000284 llvm::Value *Callee;
Eli Friedman8dfa2b32009-11-16 05:31:29 +0000285 if (const CXXDestructorDecl *Destructor
286 = dyn_cast<CXXDestructorDecl>(MD)) {
287 if (MD->isVirtual() && !ME->hasQualifier() &&
288 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
289 Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty);
290 } else {
291 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
292 }
293 } else if (MD->isVirtual() && !ME->hasQualifier() &&
294 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
295 Callee = BuildVirtualCall(MD, This, Ty);
296 } else {
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000297 Callee = CGM.GetAddrOfFunction(MD, Ty);
Eli Friedman8dfa2b32009-11-16 05:31:29 +0000298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
300 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000301 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000302}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000303
Mike Stump1eb44332009-09-09 15:08:12 +0000304RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000305CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
306 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000307 const Expr *BaseExpr = BO->getLHS();
308 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000309
Anders Carlsson3eea6352009-10-13 17:41:28 +0000310 const MemberPointerType *MPT =
311 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000312 const FunctionProtoType *FPT =
313 MPT->getPointeeType()->getAs<FunctionProtoType>();
314 const CXXRecordDecl *RD =
Douglas Gregor87c12c42009-11-04 16:49:01 +0000315 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000316
317 const llvm::FunctionType *FTy =
318 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
319 FPT->isVariadic());
320
321 const llvm::Type *Int8PtrTy =
322 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
323
324 // Get the member function pointer.
325 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000326 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
327 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000328
329 // Emit the 'this' pointer.
330 llvm::Value *This;
331
332 if (BO->getOpcode() == BinaryOperator::PtrMemI)
333 This = EmitScalarExpr(BaseExpr);
334 else
335 This = EmitLValue(BaseExpr).getAddress();
336
337 // Adjust it.
338 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
339 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
340
341 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
342 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
343
344 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
345
346 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
347
348 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
349
350 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
351
352 // If the LSB in the function pointer is 1, the function pointer points to
353 // a virtual function.
354 llvm::Value *IsVirtual
355 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
356 "and");
357
358 IsVirtual = Builder.CreateTrunc(IsVirtual,
359 llvm::Type::getInt1Ty(VMContext));
360
361 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
362 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
363 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
364
365 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
366 EmitBlock(FnVirtual);
367
368 const llvm::Type *VTableTy =
369 FTy->getPointerTo()->getPointerTo()->getPointerTo();
370
371 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
372 VTable = Builder.CreateLoad(VTable);
373
374 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
375
376 // Since the function pointer is 1 plus the virtual table offset, we
377 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000378 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000379
380 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
381
382 EmitBranch(FnEnd);
383 EmitBlock(FnNonVirtual);
384
385 // If the function is not virtual, just load the pointer.
386 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
387 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
388
389 EmitBlock(FnEnd);
390
391 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
392 Callee->reserveOperandSpace(2);
393 Callee->addIncoming(VirtualFn, FnVirtual);
394 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
395
396 CallArgList Args;
397
398 QualType ThisType =
399 getContext().getPointerType(getContext().getTagDeclType(RD));
400
401 // Push the this ptr.
402 Args.push_back(std::make_pair(RValue::get(This), ThisType));
403
404 // And the rest of the call args
405 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
406 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
407 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
408 Callee, Args, 0);
409}
410
411RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000412CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
413 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000414 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000415 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Fariborz Jahanianad258832009-08-13 21:09:41 +0000417 if (MD->isCopyAssignment()) {
418 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
419 if (ClassDecl->hasTrivialCopyAssignment()) {
420 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
421 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
422 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
423 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
424 QualType Ty = E->getType();
425 EmitAggregateCopy(This, Src, Ty);
426 return RValue::get(This);
427 }
428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
John McCall183700f2009-09-21 23:43:11 +0000430 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000431 const llvm::Type *Ty =
432 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000433 FPT->isVariadic());
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Anders Carlsson0f294632009-05-27 04:18:27 +0000435 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Eli Friedman8dfa2b32009-11-16 05:31:29 +0000437 llvm::Value *Callee;
438 if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
439 Callee = BuildVirtualCall(MD, This, Ty);
440 else
441 Callee = CGM.GetAddrOfFunction(MD, Ty);
442
Anders Carlsson0f294632009-05-27 04:18:27 +0000443 return EmitCXXMemberCall(MD, Callee, This,
444 E->arg_begin() + 1, E->arg_end());
445}
446
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000447llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000448 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000449 "Must be in a C++ member function decl to load 'this'");
450 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
451 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000453 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000454 // ans: See how CodeGenFunction::LoadObjCSelf() uses
455 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000456 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
457}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000458
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000459/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
460/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000461/// array.
462/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
463/// array type and 'ArrayPtr' points to the beginning fo the array.
464/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000465void
466CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000467 const ConstantArrayType *ArrayTy,
468 llvm::Value *ArrayPtr) {
469 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
470 llvm::Value * NumElements =
471 llvm::ConstantInt::get(SizeTy,
472 getContext().getConstantArrayElementCount(ArrayTy));
473
474 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
475}
476
477void
478CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
479 llvm::Value *NumElements,
480 llvm::Value *ArrayPtr) {
481 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000483 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000484 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
485 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
486 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000488 // Start the loop with a block that tests the condition.
489 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
490 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000492 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000494 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000496 // Generate: if (loop-index < number-of-elements fall to the loop body,
497 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000498 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000499 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000500 // If the condition is true, execute the body.
501 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000503 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000505 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000506 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000507 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000508 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
509 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000510 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000512 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000514 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000515 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000516 Counter = Builder.CreateLoad(IndexPtr);
517 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
518 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000520 // Finally, branch back up to the condition for the next iteration.
521 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000523 // Emit the fall-through block.
524 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000525}
526
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000527/// EmitCXXAggrDestructorCall - calls the default destructor on array
528/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000529void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000530CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
531 const ArrayType *Array,
532 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000533 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
534 assert(CA && "Do we support VLA for destruction ?");
Fariborz Jahanian72c21532009-11-13 19:27:47 +0000535 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
536 llvm::Value* ElementCountPtr =
537 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
538 EmitCXXAggrDestructorCall(D, ElementCountPtr, This);
539}
540
541/// EmitCXXAggrDestructorCall - calls the default destructor on array
542/// elements in reverse order of construction.
543void
544CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
545 llvm::Value *UpperCount,
546 llvm::Value *This) {
Mike Stump1eb44332009-09-09 15:08:12 +0000547 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000548 1);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000549 // Create a temporary for the loop index and initialize it with count of
550 // array elements.
551 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
552 "loop.index");
553 // Index = ElementCount;
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000554 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000556 // Start the loop with a block that tests the condition.
557 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
558 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000560 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000562 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000564 // Generate: if (loop-index != 0 fall to the loop body,
565 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000566 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000567 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
568 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
569 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
570 "isne");
571 // If the condition is true, execute the body.
572 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000574 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000576 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
577 // Inside the loop body, emit the constructor call on the array element.
578 Counter = Builder.CreateLoad(IndexPtr);
579 Counter = Builder.CreateSub(Counter, One);
580 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
Fariborz Jahanian534ba902009-11-13 22:29:45 +0000581 if (D->isVirtual()) {
582 const llvm::Type *Ty =
583 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(D),
584 /*isVariadic=*/false);
585
586 llvm::Value *Callee = BuildVirtualCall(D, Dtor_Deleting, Address, Ty);
587 EmitCXXMemberCall(D, Callee, Address, 0, 0);
588 }
589 else
590 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000592 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000594 // Emit the decrement of the loop counter.
595 Counter = Builder.CreateLoad(IndexPtr);
596 Counter = Builder.CreateSub(Counter, One, "dec");
597 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000599 // Finally, branch back up to the condition for the next iteration.
600 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000602 // Emit the fall-through block.
603 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000604}
605
Fariborz Jahanian72c21532009-11-13 19:27:47 +0000606/// GenerateCXXAggrDestructorHelper - Generates a helper function which when invoked,
Fariborz Jahanian88f42802009-11-10 19:24:06 +0000607/// calls the default destructor on array elements in reverse order of
608/// construction.
609llvm::Constant *
610CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
611 const ArrayType *Array,
612 llvm::Value *This) {
613 static int UniqueCount;
614 FunctionArgList Args;
615 ImplicitParamDecl *Dst =
616 ImplicitParamDecl::Create(getContext(), 0,
617 SourceLocation(), 0,
618 getContext().getPointerType(getContext().VoidTy));
619 Args.push_back(std::make_pair(Dst, Dst->getType()));
620
621 llvm::SmallString<16> Name;
622 llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueCount);
623 QualType R = getContext().VoidTy;
624 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
625 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
626 llvm::Function *Fn =
627 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
628 Name.c_str(),
629 &CGM.getModule());
630 IdentifierInfo *II
631 = &CGM.getContext().Idents.get(Name.c_str());
632 FunctionDecl *FD = FunctionDecl::Create(getContext(),
633 getContext().getTranslationUnitDecl(),
634 SourceLocation(), II, R, 0,
635 FunctionDecl::Static,
636 false, true);
637 StartFunction(FD, R, Fn, Args, SourceLocation());
638 QualType BaseElementTy = getContext().getBaseElementType(Array);
639 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
640 BasePtr = llvm::PointerType::getUnqual(BasePtr);
641 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
642 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
643 FinishFunction();
644 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
645 0);
646 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
647 return m;
648}
649
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000650void
Mike Stump1eb44332009-09-09 15:08:12 +0000651CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
652 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000653 llvm::Value *This,
654 CallExpr::const_arg_iterator ArgBeg,
655 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000656 if (D->isCopyConstructor(getContext())) {
657 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
658 if (ClassDecl->hasTrivialCopyConstructor()) {
659 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
660 "EmitCXXConstructorCall - user declared copy constructor");
661 const Expr *E = (*ArgBeg);
662 QualType Ty = E->getType();
663 llvm::Value *Src = EmitLValue(E).getAddress();
664 EmitAggregateCopy(This, Src, Ty);
665 return;
666 }
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000669 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
670
671 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000672}
673
Mike Stump1eb44332009-09-09 15:08:12 +0000674void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000675 CXXDtorType Type,
676 llvm::Value *This) {
677 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Anders Carlsson7267c162009-05-29 21:03:38 +0000679 EmitCXXMemberCall(D, Callee, This, 0, 0);
680}
681
Mike Stump1eb44332009-09-09 15:08:12 +0000682void
683CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000684 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000685 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000686 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000687 const ConstantArrayType *Array =
688 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000689 // For a copy constructor, even if it is trivial, must fall thru so
690 // its argument is code-gen'ed.
691 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000692 QualType InitType = E->getType();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000693 if (Array)
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000694 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000695 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000696 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000697 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000698 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000701 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000702 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Anders Carlsson03d8ed42009-11-13 04:34:45 +0000703 const Expr *Arg = E->getArg(0);
704
705 if (const CXXBindTemporaryExpr *BindExpr =
706 dyn_cast<CXXBindTemporaryExpr>(Arg))
707 Arg = BindExpr->getSubExpr();
708
709 EmitAggExpr(Arg, Dest, false);
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000710 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000711 }
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000712 if (Array) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000713 QualType BaseElementTy = getContext().getBaseElementType(Array);
714 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
715 BasePtr = llvm::PointerType::getUnqual(BasePtr);
716 llvm::Value *BaseAddrPtr =
717 Builder.CreateBitCast(Dest, BasePtr);
718 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
719 }
720 else
721 // Call the constructor.
722 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
723 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000724}
725
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000726void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000727 EmitGlobal(GlobalDecl(D, Ctor_Complete));
728 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000729}
Anders Carlsson363c1842009-04-16 23:57:24 +0000730
Mike Stump1eb44332009-09-09 15:08:12 +0000731void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000732 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Anders Carlsson27ae5362009-04-17 01:58:57 +0000734 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000736 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Anders Carlsson27ae5362009-04-17 01:58:57 +0000738 SetFunctionDefinitionAttributes(D, Fn);
739 SetLLVMFunctionAttributesForDefinition(D, Fn);
740}
741
Anders Carlsson363c1842009-04-16 23:57:24 +0000742llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000743CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000744 CXXCtorType Type) {
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000745 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000746 const llvm::FunctionType *FTy =
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000747 getTypes().GetFunctionType(getTypes().getFunctionInfo(D),
748 FPT->isVariadic());
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Anders Carlsson363c1842009-04-16 23:57:24 +0000750 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000751 return cast<llvm::Function>(
752 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000753}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000754
Mike Stump1eb44332009-09-09 15:08:12 +0000755const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000756 CXXCtorType Type) {
757 llvm::SmallString<256> Name;
758 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000759 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Anders Carlsson27ae5362009-04-17 01:58:57 +0000761 Name += '\0';
762 return UniqueMangledName(Name.begin(), Name.end());
763}
764
765void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Eli Friedmanea9a2082009-11-14 04:19:37 +0000766 if (D->isVirtual())
767 EmitCXXDestructor(D, Dtor_Deleting);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000768 EmitCXXDestructor(D, Dtor_Complete);
769 EmitCXXDestructor(D, Dtor_Base);
770}
771
Mike Stump1eb44332009-09-09 15:08:12 +0000772void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000773 CXXDtorType Type) {
774 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000776 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Anders Carlsson27ae5362009-04-17 01:58:57 +0000778 SetFunctionDefinitionAttributes(D, Fn);
779 SetLLVMFunctionAttributesForDefinition(D, Fn);
780}
781
782llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000783CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000784 CXXDtorType Type) {
785 const llvm::FunctionType *FTy =
786 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Anders Carlsson27ae5362009-04-17 01:58:57 +0000788 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000789 return cast<llvm::Function>(
790 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000791}
792
Mike Stump1eb44332009-09-09 15:08:12 +0000793const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000794 CXXDtorType Type) {
795 llvm::SmallString<256> Name;
796 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000797 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Anders Carlsson27ae5362009-04-17 01:58:57 +0000799 Name += '\0';
800 return UniqueMangledName(Name.begin(), Name.end());
801}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000802
Mike Stumped032eb2009-09-04 18:27:16 +0000803llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
804 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000805 bool Extern, int64_t nv,
806 int64_t v) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000807 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stumped032eb2009-09-04 18:27:16 +0000808}
809
Mike Stumpc902d222009-11-03 16:59:27 +0000810llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
811 int64_t v) {
812 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
813 0);
814 const llvm::Type *OrigTy = V->getType();
815 if (nv) {
816 // Do the non-virtual adjustment
817 V = Builder.CreateBitCast(V, Ptr8Ty);
818 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
819 V = Builder.CreateBitCast(V, OrigTy);
820 }
821 if (v) {
822 // Do the virtual this adjustment
823 const llvm::Type *PtrDiffTy =
824 ConvertType(getContext().getPointerDiffType());
825 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
826 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
827 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
828 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
829 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
830 V = Builder.CreateLoad(V, "vtable");
831 llvm::Value *VTablePtr = V;
832 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
833 v /= LLVMPointerWidth/8;
834 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
835 V = Builder.CreateLoad(V);
836 V = Builder.CreateGEP(ThisVal, V);
837 V = Builder.CreateBitCast(V, OrigTy);
838 }
839 return V;
840}
841
Mike Stump6e319f62009-09-11 23:25:56 +0000842llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
843 const CXXMethodDecl *MD,
844 bool Extern,
845 int64_t nv_t,
846 int64_t v_t,
847 int64_t nv_r,
848 int64_t v_r) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000849 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000850
851 FunctionArgList Args;
852 ImplicitParamDecl *ThisDecl =
853 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
854 MD->getThisType(getContext()));
855 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
856 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
857 e = MD->param_end();
858 i != e; ++i) {
859 ParmVarDecl *D = *i;
860 Args.push_back(std::make_pair(D, D->getType()));
861 }
862 IdentifierInfo *II
863 = &CGM.getContext().Idents.get("__thunk_named_foo_");
864 FunctionDecl *FD = FunctionDecl::Create(getContext(),
865 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000866 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000867 Extern
868 ? FunctionDecl::Extern
869 : FunctionDecl::Static,
870 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000871 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
872
873 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000874 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
875 const llvm::Type *Ty =
876 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
877 FPT->isVariadic());
878 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000879 CallArgList CallArgs;
880
Mike Stump736529e2009-11-03 02:12:59 +0000881 QualType ArgType = MD->getThisType(getContext());
882 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stumpd0fe5362009-11-04 00:53:51 +0000883 if (nv_t || v_t) {
Mike Stumpc902d222009-11-03 16:59:27 +0000884 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000885 const llvm::Type *OrigTy = Callee->getType();
Mike Stumpc902d222009-11-03 16:59:27 +0000886 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stumpd0fe5362009-11-04 00:53:51 +0000887 if (nv_r || v_r) {
888 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
889 Callee = Builder.CreateBitCast(Callee, OrigTy);
890 nv_r = v_r = 0;
891 }
892 }
893
Mike Stump736529e2009-11-03 02:12:59 +0000894 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
895
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000896 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
897 e = MD->param_end();
898 i != e; ++i) {
899 ParmVarDecl *D = *i;
900 QualType ArgType = D->getType();
901
902 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
903 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000904 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
905 }
906
Mike Stumpf49ed942009-11-02 23:47:45 +0000907 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
908 Callee, CallArgs, MD);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000909 if (nv_r || v_r) {
Mike Stump03e777e2009-11-05 06:32:02 +0000910 bool CanBeZero = !(ResultType->isReferenceType()
911 // FIXME: attr nonnull can't be zero either
912 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000913 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000914 if (CanBeZero) {
915 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
916 llvm::BasicBlock *ZeroBlock = createBasicBlock();
917 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000918
Mike Stump03e777e2009-11-05 06:32:02 +0000919 const llvm::Type *Ty = RV.getScalarVal()->getType();
920 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
921 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
922 NonZeroBlock, ZeroBlock);
923 EmitBlock(NonZeroBlock);
924 llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r);
925 EmitBranch(ContBlock);
926 EmitBlock(ZeroBlock);
927 llvm::Value *Z = RV.getScalarVal();
928 EmitBlock(ContBlock);
929 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
930 RVOrZero->reserveOperandSpace(2);
931 RVOrZero->addIncoming(NZ, NonZeroBlock);
932 RVOrZero->addIncoming(Z, ZeroBlock);
933 RV = RValue::get(RVOrZero);
934 } else
935 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000936 }
937
Mike Stumpf49ed942009-11-02 23:47:45 +0000938 if (!ResultType->isVoidType())
939 EmitReturnOfRValue(RV, ResultType);
940
Mike Stump6e319f62009-09-11 23:25:56 +0000941 FinishFunction();
942 return Fn;
943}
944
Mike Stump77ca8f62009-09-05 07:20:32 +0000945llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
946 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000947 llvm::SmallString<256> OutName;
948 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000949 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000950 llvm::GlobalVariable::LinkageTypes linktype;
951 linktype = llvm::GlobalValue::WeakAnyLinkage;
952 if (!Extern)
953 linktype = llvm::GlobalValue::InternalLinkage;
954 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000955 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000956 const llvm::FunctionType *FTy =
957 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
958 FPT->isVariadic());
959
960 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
961 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000962 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000963 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
964 return m;
965}
966
Mike Stump6e319f62009-09-11 23:25:56 +0000967llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
968 bool Extern, int64_t nv_t,
969 int64_t v_t, int64_t nv_r,
970 int64_t v_r) {
971 llvm::SmallString<256> OutName;
972 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000973 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000974 llvm::GlobalVariable::LinkageTypes linktype;
975 linktype = llvm::GlobalValue::WeakAnyLinkage;
976 if (!Extern)
977 linktype = llvm::GlobalValue::InternalLinkage;
978 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000979 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000980 const llvm::FunctionType *FTy =
981 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
982 FPT->isVariadic());
983
984 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
985 &getModule());
986 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
987 v_r);
Mike Stump6e319f62009-09-11 23:25:56 +0000988 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
989 return m;
990}
991
Mike Stumpf0070db2009-08-26 20:46:33 +0000992llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000993CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
994 const CXXRecordDecl *ClassDecl,
995 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000996 const llvm::Type *Int8PtrTy =
997 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
998
999 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
1000 Int8PtrTy->getPointerTo());
1001 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
1002
Anders Carlssondbd920c2009-10-11 22:13:54 +00001003 int64_t VBaseOffsetIndex =
1004 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
1005
Anders Carlsson2f1986b2009-10-06 22:43:30 +00001006 llvm::Value *VBaseOffsetPtr =
Mike Stump79d57682009-11-04 01:11:15 +00001007 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +00001008 const llvm::Type *PtrDiffTy =
1009 ConvertType(getContext().getPointerDiffType());
1010
1011 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1012 PtrDiffTy->getPointerTo());
1013
1014 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1015
1016 return VBaseOffset;
1017}
1018
Anders Carlsson566abee2009-11-13 04:45:41 +00001019static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, int64_t VtableIndex,
1020 llvm::Value *This, const llvm::Type *Ty) {
1021 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +00001022
Anders Carlsson566abee2009-11-13 04:45:41 +00001023 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
1024 Vtable = CGF.Builder.CreateLoad(Vtable);
1025
1026 llvm::Value *VFuncPtr =
1027 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
1028 return CGF.Builder.CreateLoad(VFuncPtr);
1029}
1030
1031llvm::Value *
1032CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1033 const llvm::Type *Ty) {
1034 MD = MD->getCanonicalDecl();
1035 int64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
1036
1037 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1038}
1039
1040llvm::Value *
1041CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
1042 llvm::Value *&This, const llvm::Type *Ty) {
1043 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
1044 int64_t VtableIndex =
Anders Carlssona0fdd912009-11-13 17:08:56 +00001045 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +00001046
1047 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +00001048}
1049
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001050/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1051/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1052/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001053// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001054void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001055 llvm::Value *Src,
1056 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001057 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001058 QualType Ty) {
1059 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1060 assert(CA && "VLA cannot be copied over");
1061 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001063 // Create a temporary for the loop index and initialize it with 0.
1064 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1065 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001066 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001067 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +00001068 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001069 // Start the loop with a block that tests the condition.
1070 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1071 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001073 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001075 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1076 // Generate: if (loop-index < number-of-elements fall to the loop body,
1077 // otherwise, go to the block after the for-loop.
1078 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001079 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001080 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1081 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001082 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001083 "isless");
1084 // If the condition is true, execute the body.
1085 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001087 EmitBlock(ForBody);
1088 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1089 // Inside the loop body, emit the constructor call on the array element.
1090 Counter = Builder.CreateLoad(IndexPtr);
1091 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1092 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1093 if (BitwiseCopy)
1094 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001095 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001096 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001097 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001098 Ctor_Complete);
1099 CallArgList CallArgs;
1100 // Push the this (Dest) ptr.
1101 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1102 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001104 // Push the Src ptr.
1105 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stump79d57682009-11-04 01:11:15 +00001106 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001107 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001108 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001109 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1110 Callee, CallArgs, BaseCopyCtor);
1111 }
1112 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001114 // Emit the increment of the loop counter.
1115 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1116 Counter = Builder.CreateLoad(IndexPtr);
1117 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1118 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001120 // Finally, branch back up to the condition for the next iteration.
1121 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001123 // Emit the fall-through block.
1124 EmitBlock(AfterFor, true);
1125}
1126
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001127/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001128/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001129/// bitwise assignment or via a copy assignment operator function call.
1130/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001131void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001132 llvm::Value *Src,
1133 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001134 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001135 QualType Ty) {
1136 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1137 assert(CA && "VLA cannot be asssigned");
1138 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001140 // Create a temporary for the loop index and initialize it with 0.
1141 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1142 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001143 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001144 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1145 Builder.CreateStore(zeroConstant, IndexPtr, false);
1146 // Start the loop with a block that tests the condition.
1147 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1148 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001150 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001152 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1153 // Generate: if (loop-index < number-of-elements fall to the loop body,
1154 // otherwise, go to the block after the for-loop.
1155 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001156 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001157 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1158 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001159 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001160 "isless");
1161 // If the condition is true, execute the body.
1162 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001164 EmitBlock(ForBody);
1165 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1166 // Inside the loop body, emit the assignment operator call on array element.
1167 Counter = Builder.CreateLoad(IndexPtr);
1168 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1169 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1170 const CXXMethodDecl *MD = 0;
1171 if (BitwiseAssign)
1172 EmitAggregateCopy(Dest, Src, Ty);
1173 else {
1174 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1175 MD);
1176 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1177 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001178 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001179 const llvm::Type *LTy =
1180 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1181 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001182 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001184 CallArgList CallArgs;
1185 // Push the this (Dest) ptr.
1186 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1187 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001189 // Push the Src ptr.
1190 CallArgs.push_back(std::make_pair(RValue::get(Src),
1191 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001192 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001193 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1194 Callee, CallArgs, MD);
1195 }
1196 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001198 // Emit the increment of the loop counter.
1199 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1200 Counter = Builder.CreateLoad(IndexPtr);
1201 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1202 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001204 // Finally, branch back up to the condition for the next iteration.
1205 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001207 // Emit the fall-through block.
1208 EmitBlock(AfterFor, true);
1209}
1210
Fariborz Jahanianca283612009-08-07 23:51:33 +00001211/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1212/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001213/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001214void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001215 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001216 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001217 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1218 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001219 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1220 /*NullCheckValue=*/false);
1221 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1222 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001223 }
1224 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1225 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001226 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001227 }
Mike Stump1eb44332009-09-09 15:08:12 +00001228
1229 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001230 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001231 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001232 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001233 CallArgList CallArgs;
1234 // Push the this (Dest) ptr.
1235 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1236 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Fariborz Jahanianca283612009-08-07 23:51:33 +00001238 // Push the Src ptr.
1239 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001240 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001241 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001242 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001243 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1244 Callee, CallArgs, BaseCopyCtor);
1245 }
1246}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001247
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001248/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001249/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001250/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001251// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001252void CodeGenFunction::EmitClassCopyAssignment(
1253 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001254 const CXXRecordDecl *ClassDecl,
1255 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001256 QualType Ty) {
1257 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001258 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1259 /*NullCheckValue=*/false);
1260 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1261 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001262 }
1263 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1264 EmitAggregateCopy(Dest, Src, Ty);
1265 return;
1266 }
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001268 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001269 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001270 MD);
1271 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1272 (void)ConstCopyAssignOp;
1273
John McCall183700f2009-09-21 23:43:11 +00001274 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001275 const llvm::Type *LTy =
1276 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001277 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001278 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001280 CallArgList CallArgs;
1281 // Push the this (Dest) ptr.
1282 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1283 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001285 // Push the Src ptr.
1286 CallArgs.push_back(std::make_pair(RValue::get(Src),
1287 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001288 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001289 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001290 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1291 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001292}
1293
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001294/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001295void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001296CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1297 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001298 llvm::Function *Fn,
1299 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001300 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1301 SourceLocation());
1302 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001303 FinishFunction();
1304}
1305
Mike Stump79d57682009-11-04 01:11:15 +00001306/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1307/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001308/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stump79d57682009-11-04 01:11:15 +00001309/// copy of its subobjects. The order of copying is the same as the order of
1310/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001311/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001312/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001313/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001314/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001315/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001316/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001317/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001318/// Virtual base class subobjects shall be copied only once by the
1319/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001320
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001321void
1322CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1323 CXXCtorType Type,
1324 llvm::Function *Fn,
1325 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001326 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001327 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stump79d57682009-11-04 01:11:15 +00001328 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001329 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1330 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001332 FunctionArgList::const_iterator i = Args.begin();
1333 const VarDecl *ThisArg = i->first;
1334 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1335 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1336 const VarDecl *SrcArg = (i+1)->first;
1337 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1338 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001340 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1341 Base != ClassDecl->bases_end(); ++Base) {
1342 // FIXME. copy constrution of virtual base NYI
1343 if (Base->isVirtual())
1344 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001346 CXXRecordDecl *BaseClassDecl
1347 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001348 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1349 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001350 }
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001352 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1353 FieldEnd = ClassDecl->field_end();
1354 Field != FieldEnd; ++Field) {
1355 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001356 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001357 getContext().getAsConstantArrayType(FieldType);
1358 if (Array)
1359 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001361 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1362 CXXRecordDecl *FieldClassDecl
1363 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1364 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1365 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001366 if (Array) {
1367 const llvm::Type *BasePtr = ConvertType(FieldType);
1368 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001369 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001370 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001371 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001372 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1373 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1374 FieldClassDecl, FieldType);
1375 }
Mike Stump1eb44332009-09-09 15:08:12 +00001376 else
1377 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001378 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001379 continue;
1380 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001381 // Do a built-in assignment of scalar data members.
1382 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1383 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Eli Friedman6d10ac92009-11-16 21:47:41 +00001384 if (!hasAggregateLLVMType(Field->getType())) {
1385 RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType());
1386 EmitStoreThroughLValue(RVRHS, LHS, Field->getType());
1387 } else if (Field->getType()->isAnyComplexType()) {
1388 ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(),
1389 RHS.isVolatileQualified());
1390 StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified());
1391 } else {
1392 EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType());
1393 }
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001394 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001395 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001396}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001397
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001398/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001399/// Before the implicitly-declared copy assignment operator for a class is
1400/// implicitly defined, all implicitly- declared copy assignment operators for
1401/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001402/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001403/// The implicitly-defined copy assignment operator for class X performs
1404/// memberwise assignment of its subob- jects. The direct base classes of X are
1405/// assigned first, in the order of their declaration in
1406/// the base-specifier-list, and then the immediate nonstatic data members of X
1407/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001408/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001409/// if the subobject is of class type, the copy assignment operator for the
1410/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001411/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001412///
Mike Stump1eb44332009-09-09 15:08:12 +00001413/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001414/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001415///
Mike Stump1eb44332009-09-09 15:08:12 +00001416/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001417/// used.
1418void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001419 llvm::Function *Fn,
1420 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001421
1422 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1423 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1424 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001425 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001427 FunctionArgList::const_iterator i = Args.begin();
1428 const VarDecl *ThisArg = i->first;
1429 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1430 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1431 const VarDecl *SrcArg = (i+1)->first;
1432 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1433 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001435 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1436 Base != ClassDecl->bases_end(); ++Base) {
1437 // FIXME. copy assignment of virtual base NYI
1438 if (Base->isVirtual())
1439 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001441 CXXRecordDecl *BaseClassDecl
1442 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1443 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1444 Base->getType());
1445 }
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001447 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1448 FieldEnd = ClassDecl->field_end();
1449 Field != FieldEnd; ++Field) {
1450 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001451 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001452 getContext().getAsConstantArrayType(FieldType);
1453 if (Array)
1454 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001456 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1457 CXXRecordDecl *FieldClassDecl
1458 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1459 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1460 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001461 if (Array) {
1462 const llvm::Type *BasePtr = ConvertType(FieldType);
1463 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1464 llvm::Value *DestBaseAddrPtr =
1465 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1466 llvm::Value *SrcBaseAddrPtr =
1467 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1468 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1469 FieldClassDecl, FieldType);
1470 }
1471 else
Mike Stump1eb44332009-09-09 15:08:12 +00001472 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001473 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001474 continue;
1475 }
1476 // Do a built-in assignment of scalar data members.
1477 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1478 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1479 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1480 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001481 }
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001483 // return *this;
1484 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001486 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001487}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001488
Anders Carlssonb1156b92009-11-06 03:23:06 +00001489static void EmitBaseInitializer(CodeGenFunction &CGF,
1490 const CXXRecordDecl *ClassDecl,
1491 CXXBaseOrMemberInitializer *BaseInit,
1492 CXXCtorType CtorType) {
1493 assert(BaseInit->isBaseInitializer() &&
1494 "Must have base initializer!");
1495
1496 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1497
1498 const Type *BaseType = BaseInit->getBaseClass();
1499 CXXRecordDecl *BaseClassDecl =
1500 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
1501 llvm::Value *V = CGF.GetAddressCXXOfBaseClass(ThisPtr, ClassDecl,
1502 BaseClassDecl,
1503 /*NullCheckValue=*/false);
1504 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1505 CtorType, V,
1506 BaseInit->const_arg_begin(),
1507 BaseInit->const_arg_end());
1508}
1509
1510static void EmitMemberInitializer(CodeGenFunction &CGF,
1511 const CXXRecordDecl *ClassDecl,
1512 CXXBaseOrMemberInitializer *MemberInit) {
1513 assert(MemberInit->isMemberInitializer() &&
1514 "Must have member initializer!");
1515
1516 // non-static data member initializers.
1517 FieldDecl *Field = MemberInit->getMember();
Eli Friedmanebf50652009-11-16 23:34:11 +00001518 QualType FieldType = CGF.getContext().getCanonicalType(Field->getType());
Anders Carlssonb1156b92009-11-06 03:23:06 +00001519
1520 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1521 LValue LHS;
1522 if (FieldType->isReferenceType()) {
1523 // FIXME: This is really ugly; should be refactored somehow
1524 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1525 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1526 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1527 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1528 } else {
Eli Friedman1db53452009-11-16 22:58:01 +00001529 LHS = CGF.EmitLValueForField(ThisPtr, Field, ClassDecl->isUnion(), 0);
Anders Carlssonb1156b92009-11-06 03:23:06 +00001530 }
Eli Friedmanebf50652009-11-16 23:34:11 +00001531
1532 // If we are initializing an anonymous union field, drill down to the field.
1533 if (MemberInit->getAnonUnionMember()) {
1534 Field = MemberInit->getAnonUnionMember();
1535 LHS = CGF.EmitLValueForField(LHS.getAddress(), Field,
1536 /*IsUnion=*/true, 0);
1537 FieldType = Field->getType();
1538 }
1539
1540 // If the field is an array, branch based on the element type.
1541 const ConstantArrayType *Array =
1542 CGF.getContext().getAsConstantArrayType(FieldType);
1543 if (Array)
1544 FieldType = CGF.getContext().getBaseElementType(FieldType);
1545
Eli Friedman4d26b432009-11-16 23:53:01 +00001546 // We lose the constructor for anonymous union members, so handle them
1547 // explicitly.
1548 // FIXME: This is somwhat ugly.
1549 if (MemberInit->getAnonUnionMember() && FieldType->getAs<RecordType>()) {
1550 if (MemberInit->getNumArgs())
1551 CGF.EmitAggExpr(*MemberInit->arg_begin(), LHS.getAddress(),
1552 LHS.isVolatileQualified());
1553 else
1554 CGF.EmitAggregateClear(LHS.getAddress(), Field->getType());
1555 return;
1556 }
1557
Anders Carlssonb1156b92009-11-06 03:23:06 +00001558 if (FieldType->getAs<RecordType>()) {
Eli Friedmanebf50652009-11-16 23:34:11 +00001559 assert(MemberInit->getConstructor() &&
1560 "EmitCtorPrologue - no constructor to initialize member");
1561 if (Array) {
1562 const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1563 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1564 llvm::Value *BaseAddrPtr =
1565 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1566 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
1567 Array, BaseAddrPtr);
Anders Carlssonb1156b92009-11-06 03:23:06 +00001568 }
Eli Friedmanebf50652009-11-16 23:34:11 +00001569 else
1570 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1571 Ctor_Complete, LHS.getAddress(),
1572 MemberInit->const_arg_begin(),
1573 MemberInit->const_arg_end());
1574 return;
Anders Carlssonb1156b92009-11-06 03:23:06 +00001575 }
1576
1577 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1578 Expr *RhsExpr = *MemberInit->arg_begin();
1579 RValue RHS;
Eli Friedman4d26b432009-11-16 23:53:01 +00001580 if (FieldType->isReferenceType()) {
Anders Carlssonb1156b92009-11-06 03:23:06 +00001581 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1582 /*IsInitializer=*/true);
Fariborz Jahaniane8b31cc2009-11-11 17:55:25 +00001583 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
Eli Friedman4d26b432009-11-16 23:53:01 +00001584 } else if (Array) {
1585 CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType());
1586 } else if (!CGF.hasAggregateLLVMType(RhsExpr->getType())) {
1587 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
1588 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1589 } else if (RhsExpr->getType()->isAnyComplexType()) {
1590 CGF.EmitComplexExprIntoAddr(RhsExpr, LHS.getAddress(),
1591 LHS.isVolatileQualified());
1592 } else {
1593 // Handle member function pointers; other aggregates shouldn't get this far.
1594 CGF.EmitAggExpr(RhsExpr, LHS.getAddress(), LHS.isVolatileQualified());
1595 }
Anders Carlssonb1156b92009-11-06 03:23:06 +00001596}
1597
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001598/// EmitCtorPrologue - This routine generates necessary code to initialize
1599/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001600/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001601void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1602 CXXCtorType CtorType) {
Anders Carlssonb1156b92009-11-06 03:23:06 +00001603 const CXXRecordDecl *ClassDecl = CD->getParent();
1604
Mike Stumpeb19fa92009-08-06 13:41:24 +00001605 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001606 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001608 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001609 E = CD->init_end();
1610 B != E; ++B) {
1611 CXXBaseOrMemberInitializer *Member = (*B);
Anders Carlssonb1156b92009-11-06 03:23:06 +00001612
Anders Carlsson1faf6742009-11-06 04:11:09 +00001613 assert(LiveTemporaries.empty() &&
1614 "Should not have any live temporaries at initializer start!");
1615
Anders Carlssonb1156b92009-11-06 03:23:06 +00001616 if (Member->isBaseInitializer())
1617 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1618 else
1619 EmitMemberInitializer(*this, ClassDecl, Member);
Anders Carlsson1faf6742009-11-06 04:11:09 +00001620
1621 // Pop any live temporaries that the initializers might have pushed.
1622 while (!LiveTemporaries.empty())
1623 PopCXXTemporary();
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001624 }
Mike Stumpf1216772009-07-31 18:25:34 +00001625
1626 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001627 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001628 if (!LoadOfThis)
1629 LoadOfThis = LoadCXXThis();
1630 llvm::Value *VtableField;
1631 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001632 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001633 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1634 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
Mike Stump380dd752009-11-10 07:44:33 +00001635 llvm::Value *vtable = CGM.getVtableInfo().getVtable(ClassDecl);
Mike Stumpf1216772009-07-31 18:25:34 +00001636 Builder.CreateStore(vtable, VtableField);
1637 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001638}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001639
1640/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001641/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001642/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001643/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001644void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1645 CXXDtorType DtorType) {
Anders Carlsson9f853df2009-11-17 04:44:12 +00001646 assert(!DD->isTrivial() &&
1647 "Should not emit dtor epilogue for trivial dtor!");
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Anders Carlsson9f853df2009-11-17 04:44:12 +00001649 const CXXRecordDecl *ClassDecl = DD->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Anders Carlsson9f853df2009-11-17 04:44:12 +00001651 // Collect the fields.
1652 llvm::SmallVector<const FieldDecl *, 16> FieldDecls;
1653 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1654 E = ClassDecl->field_end(); I != E; ++I) {
1655 const FieldDecl *Field = *I;
1656
1657 QualType FieldType = getContext().getCanonicalType(Field->getType());
1658 FieldType = getContext().getBaseElementType(FieldType);
1659
1660 const RecordType *RT = FieldType->getAs<RecordType>();
1661 if (!RT)
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001662 continue;
Anders Carlsson9f853df2009-11-17 04:44:12 +00001663
1664 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1665 if (FieldClassDecl->hasTrivialDestructor())
1666 continue;
1667
1668 FieldDecls.push_back(Field);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001669 }
Anders Carlssonbc0d83b2009-11-15 23:03:25 +00001670
Anders Carlsson9f853df2009-11-17 04:44:12 +00001671 // Now destroy the fields.
1672 for (size_t i = FieldDecls.size(); i > 0; --i) {
1673 const FieldDecl *Field = FieldDecls[i - 1];
1674
1675 QualType FieldType = Field->getType();
1676 const ConstantArrayType *Array =
1677 getContext().getAsConstantArrayType(FieldType);
1678 if (Array)
1679 FieldType = getContext().getBaseElementType(FieldType);
1680
1681 const RecordType *RT = FieldType->getAs<RecordType>();
1682 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1683
1684 llvm::Value *ThisPtr = LoadCXXThis();
1685
1686 LValue LHS = EmitLValueForField(ThisPtr, Field,
1687 /*isUnion=*/false,
1688 // FIXME: Qualifiers?
1689 /*CVRQualifiers=*/0);
1690 if (Array) {
1691 const llvm::Type *BasePtr = ConvertType(FieldType);
1692 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1693 llvm::Value *BaseAddrPtr =
1694 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1695 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
1696 Array, BaseAddrPtr);
1697 } else
1698 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1699 Dtor_Complete, LHS.getAddress());
1700 }
1701
1702 // Destroy non-virtual bases.
1703 for (CXXRecordDecl::reverse_base_class_const_iterator I =
1704 ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) {
1705 const CXXBaseSpecifier &Base = *I;
1706
1707 // Ignore virtual bases.
1708 if (Base.isVirtual())
1709 continue;
1710
1711 CXXRecordDecl *BaseClassDecl
1712 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1713
1714 // Ignore trivial destructors.
1715 if (BaseClassDecl->hasTrivialDestructor())
1716 continue;
1717
1718 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1719 ClassDecl, BaseClassDecl,
1720 /*NullCheckValue=*/false);
1721 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1722 Dtor_Base, V);
1723 }
1724
1725 // If we're emitting a base destructor, we don't want to emit calls to the
1726 // virtual bases.
1727 if (DtorType == Dtor_Base)
1728 return;
1729
1730 // FIXME: Handle virtual bases.
1731 for (CXXRecordDecl::reverse_base_class_const_iterator I =
1732 ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); I != E; ++I) {
1733 assert(false && "FIXME: Handle virtual bases.");
1734 }
1735
1736 // If we have a deleting destructor, emit a call to the delete operator.
Eli Friedman5fe05982009-11-18 00:50:08 +00001737 if (DtorType == Dtor_Deleting)
1738 EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(),
1739 getContext().getTagDeclType(ClassDecl));
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001740}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001741
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001742void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1743 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001744 llvm::Function *Fn,
1745 const FunctionArgList &Args) {
Anders Carlsson9f853df2009-11-17 04:44:12 +00001746 assert(!Dtor->getParent()->hasUserDeclaredDestructor() &&
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001747 "SynthesizeDefaultDestructor - destructor has user declaration");
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001749 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1750 SourceLocation());
Anders Carlsson9f853df2009-11-17 04:44:12 +00001751
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001752 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001753 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001754}
Anders Carlsson6815e942009-09-27 18:58:34 +00001755
Anders Carlsson9f853df2009-11-17 04:44:12 +00001756// FIXME: Move this to CGStmtCXX.cpp
Anders Carlsson6815e942009-09-27 18:58:34 +00001757void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1758 // FIXME: We need to do more here.
1759 EmitStmt(S.getTryBlock());
1760}