blob: 71cdf95f48beff3590ca183125e672fa785be0cb [file] [log] [blame]
Anders Carlsson87fc5a52008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump11289f42009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlsson87fc5a52008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson1235bbc2009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahaniandedf1e42009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlssone5fd6f22009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson131be8b2008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson52d78a52009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Mike Stump11289f42009-09-09 15:08:12 +000029void
Fariborz Jahanian1254a092009-11-10 19:24:06 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
Anders Carlssonf40886a2009-08-08 21:45:14 +000031 llvm::Constant *DeclPtr) {
Anders Carlsson52d78a52009-09-27 18:58:34 +000032 const llvm::Type *Int8PtrTy =
33 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Mike Stump11289f42009-09-09 15:08:12 +000034
Anders Carlssonf40886a2009-08-08 21:45:14 +000035 std::vector<const llvm::Type *> Params;
36 Params.push_back(Int8PtrTy);
Mike Stump11289f42009-09-09 15:08:12 +000037
Anders Carlssonf40886a2009-08-08 21:45:14 +000038 // Get the destructor function type
Mike Stump11289f42009-09-09 15:08:12 +000039 const llvm::Type *DtorFnTy =
Owen Anderson41a75022009-08-13 21:57:51 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlssonf40886a2009-08-08 21:45:14 +000041 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump11289f42009-09-09 15:08:12 +000042
Anders Carlssonf40886a2009-08-08 21:45:14 +000043 Params.clear();
44 Params.push_back(DtorFnTy);
45 Params.push_back(Int8PtrTy);
46 Params.push_back(Int8PtrTy);
Mike Stump11289f42009-09-09 15:08:12 +000047
Anders Carlssonf40886a2009-08-08 21:45:14 +000048 // Get the __cxa_atexit function type
49 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump11289f42009-09-09 15:08:12 +000050 const llvm::FunctionType *AtExitFnTy =
Anders Carlssonf40886a2009-08-08 21:45:14 +000051 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump11289f42009-09-09 15:08:12 +000052
Anders Carlssonf40886a2009-08-08 21:45:14 +000053 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54 "__cxa_atexit");
Mike Stump11289f42009-09-09 15:08:12 +000055
Anders Carlssonf40886a2009-08-08 21:45:14 +000056 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57 "__dso_handle");
Anders Carlssonf40886a2009-08-08 21:45:14 +000058 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
59 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
60 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
61 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
62}
63
Mike Stump11289f42009-09-09 15:08:12 +000064void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlssonf40886a2009-08-08 21:45:14 +000065 llvm::Constant *DeclPtr) {
66 assert(D.hasGlobalStorage() &&
67 "VarDecl must have global storage!");
Mike Stump11289f42009-09-09 15:08:12 +000068
Anders Carlssonf40886a2009-08-08 21:45:14 +000069 const Expr *Init = D.getInit();
70 QualType T = D.getType();
Mike Stump53f9ded2009-11-03 23:25:48 +000071 bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
Mike Stump11289f42009-09-09 15:08:12 +000072
Anders Carlssonf40886a2009-08-08 21:45:14 +000073 if (T->isReferenceType()) {
Anders Carlsson49033712009-08-17 18:24:57 +000074 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlssonf40886a2009-08-08 21:45:14 +000075 } else if (!hasAggregateLLVMType(T)) {
76 llvm::Value *V = EmitScalarExpr(Init);
Mike Stump53f9ded2009-11-03 23:25:48 +000077 EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlssonf40886a2009-08-08 21:45:14 +000078 } else if (T->isAnyComplexType()) {
Mike Stump53f9ded2009-11-03 23:25:48 +000079 EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlssonf40886a2009-08-08 21:45:14 +000080 } else {
Mike Stump53f9ded2009-11-03 23:25:48 +000081 EmitAggExpr(Init, DeclPtr, isVolatile);
Fariborz Jahaniane6c81122009-11-11 01:13:34 +000082 // Avoid generating destructor(s) for initialized objects.
83 if (!isa<CXXConstructExpr>(Init))
84 return;
Fariborz Jahanian1254a092009-11-10 19:24:06 +000085 const ConstantArrayType *Array = getContext().getAsConstantArrayType(T);
86 if (Array)
87 T = getContext().getBaseElementType(Array);
88
Anders Carlssonf40886a2009-08-08 21:45:14 +000089 if (const RecordType *RT = T->getAs<RecordType>()) {
90 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian1254a092009-11-10 19:24:06 +000091 if (!RD->hasTrivialDestructor()) {
92 llvm::Constant *DtorFn;
93 if (Array) {
94 DtorFn = CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(
95 RD->getDestructor(getContext()),
96 Array, DeclPtr);
97 DeclPtr =
98 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
99 }
100 else
101 DtorFn = CGM.GetAddrOfCXXDestructor(RD->getDestructor(getContext()),
102 Dtor_Complete);
103 EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
104 }
Anders Carlssonf40886a2009-08-08 21:45:14 +0000105 }
106 }
107}
108
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000109void
110CodeGenModule::EmitCXXGlobalInitFunc() {
111 if (CXXGlobalInits.empty())
112 return;
Mike Stump11289f42009-09-09 15:08:12 +0000113
Mike Stumpb9c9b352009-11-04 01:11:15 +0000114 const llvm::FunctionType *FTy
115 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
116 false);
Mike Stump11289f42009-09-09 15:08:12 +0000117
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000118 // Create our global initialization function.
119 // FIXME: Should this be tweakable by targets?
Mike Stump11289f42009-09-09 15:08:12 +0000120 llvm::Function *Fn =
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000121 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
122 "__cxx_global_initialization", &TheModule);
Mike Stump11289f42009-09-09 15:08:12 +0000123
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000124 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramerb57e5d22009-08-08 23:43:26 +0000125 &CXXGlobalInits[0],
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000126 CXXGlobalInits.size());
127 AddGlobalCtor(Fn);
128}
129
130void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
131 const VarDecl **Decls,
132 unsigned NumDecls) {
Anders Carlsson73fcc952009-09-11 00:07:24 +0000133 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000134 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000135
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000136 for (unsigned i = 0; i != NumDecls; ++i) {
137 const VarDecl *D = Decls[i];
Mike Stump11289f42009-09-09 15:08:12 +0000138
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000139 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
140 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
141 }
142 FinishFunction();
143}
144
Mike Stump11289f42009-09-09 15:08:12 +0000145void
146CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlssonf40886a2009-08-08 21:45:14 +0000147 llvm::GlobalVariable *GV) {
Daniel Dunbar22a87f92009-02-25 19:24:29 +0000148 // FIXME: This should use __cxa_guard_{acquire,release}?
149
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000150 assert(!getContext().getLangOptions().ThreadsafeStatics &&
151 "thread safe statics are currently not supported!");
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000152
Anders Carlsson1235bbc2009-04-13 18:03:33 +0000153 llvm::SmallString<256> GuardVName;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000154 CGM.getMangleContext().mangleGuardVariable(&D, GuardVName);
Mike Stump11289f42009-09-09 15:08:12 +0000155
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000156 // Create the guard variable.
Mike Stump11289f42009-09-09 15:08:12 +0000157 llvm::GlobalValue *GuardV =
Mike Stumpb9c9b352009-11-04 01:11:15 +0000158 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
159 false, GV->getLinkage(),
160 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar58bc48c2009-08-19 20:04:03 +0000161 GuardVName.str());
Mike Stump11289f42009-09-09 15:08:12 +0000162
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000163 // Load the first byte of the guard variable.
Mike Stumpb9c9b352009-11-04 01:11:15 +0000164 const llvm::Type *PtrTy
165 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump11289f42009-09-09 15:08:12 +0000166 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000167 "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000168
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000169 // Compare it against 0.
Mike Stumpb9c9b352009-11-04 01:11:15 +0000170 llvm::Value *nullValue
171 = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000172 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump11289f42009-09-09 15:08:12 +0000173
Daniel Dunbar75283ff2008-11-11 02:29:29 +0000174 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbara612e792008-11-13 01:38:36 +0000175 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000176
177 // If the guard variable is 0, jump to the initializer code.
178 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000179
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000180 EmitBlock(InitBlock);
181
Anders Carlssonf40886a2009-08-08 21:45:14 +0000182 EmitCXXGlobalVarDeclInit(D, GV);
183
Mike Stumpb9c9b352009-11-04 01:11:15 +0000184 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
185 1),
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000186 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump11289f42009-09-09 15:08:12 +0000187
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000188 EmitBlock(EndBlock);
Anders Carlsson87fc5a52008-08-22 16:00:37 +0000189}
190
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000191RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
192 llvm::Value *Callee,
193 llvm::Value *This,
194 CallExpr::const_arg_iterator ArgBeg,
195 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump11289f42009-09-09 15:08:12 +0000196 assert(MD->isInstance() &&
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000197 "Trying to emit a member call expr on a static method!");
198
Douglas Gregord94105a2009-09-04 19:04:08 +0000199 // A call to a trivial destructor requires no code generation.
200 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
201 if (Destructor->isTrivial())
202 return RValue::get(0);
Mike Stump11289f42009-09-09 15:08:12 +0000203
John McCall9dd450b2009-09-21 23:43:11 +0000204 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +0000205
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000206 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000207
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000208 // Push the this ptr.
209 Args.push_back(std::make_pair(RValue::get(This),
210 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +0000211
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000212 // And the rest of the call args
213 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000214
John McCall9dd450b2009-09-21 23:43:11 +0000215 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000216 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
217 Callee, Args, MD);
218}
219
Anders Carlssond7432df2009-10-12 19:41:04 +0000220/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
221/// expr can be devirtualized.
222static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
223 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
224 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
225 // This is a record decl. We know the type and can devirtualize it.
226 return VD->getType()->isRecordType();
227 }
Anders Carlssonb61301f2009-10-12 19:45:47 +0000228
229 return false;
Anders Carlssond7432df2009-10-12 19:41:04 +0000230 }
231
Anders Carlssona1b54fd2009-10-12 19:59:15 +0000232 // We can always devirtualize calls on temporary object expressions.
Anders Carlssonb61301f2009-10-12 19:45:47 +0000233 if (isa<CXXTemporaryObjectExpr>(Base))
234 return true;
235
Anders Carlssona1b54fd2009-10-12 19:59:15 +0000236 // And calls on bound temporaries.
237 if (isa<CXXBindTemporaryExpr>(Base))
238 return true;
239
Anders Carlsson2a017092009-10-12 19:51:33 +0000240 // Check if this is a call expr that returns a record type.
241 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
242 return CE->getCallReturnType()->isRecordType();
243
Anders Carlssond7432df2009-10-12 19:41:04 +0000244 // We can't devirtualize the call.
245 return false;
246}
247
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000248RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000249 if (isa<BinaryOperator>(CE->getCallee()))
250 return EmitCXXMemberPointerCallExpr(CE);
251
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000252 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
253 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000254
Anders Carlsson8f4fd602009-09-29 03:54:11 +0000255 if (MD->isStatic()) {
256 // The method is static, emit it as we would a regular call.
257 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
258 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
259 CE->arg_begin(), CE->arg_end(), 0);
260
261 }
262
John McCall9dd450b2009-09-21 23:43:11 +0000263 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumpa523b2d2009-07-30 21:47:44 +0000264
Mike Stump11289f42009-09-09 15:08:12 +0000265 const llvm::Type *Ty =
266 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlsson03a409f2009-04-08 20:31:57 +0000267 FPT->isVariadic());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000268 llvm::Value *This;
Mike Stump11289f42009-09-09 15:08:12 +0000269
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000270 if (ME->isArrow())
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000271 This = EmitScalarExpr(ME->getBase());
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000272 else {
273 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000274 This = BaseLV.getAddress();
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000275 }
Mike Stumpa5588bf2009-08-26 20:46:33 +0000276
Douglas Gregorc1905232009-08-26 22:36:53 +0000277 // C++ [class.virtual]p12:
Mike Stump11289f42009-09-09 15:08:12 +0000278 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorc1905232009-08-26 22:36:53 +0000279 // virtual call mechanism.
Anders Carlssonb5296552009-10-11 23:55:52 +0000280 //
281 // We also don't emit a virtual call if the base expression has a record type
282 // because then we know what the type is.
Mike Stumpa5588bf2009-08-26 20:46:33 +0000283 llvm::Value *Callee;
Eli Friedmane6ce3542009-11-16 05:31:29 +0000284 if (const CXXDestructorDecl *Destructor
285 = dyn_cast<CXXDestructorDecl>(MD)) {
286 if (MD->isVirtual() && !ME->hasQualifier() &&
287 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
288 Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty);
289 } else {
290 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
291 }
292 } else if (MD->isVirtual() && !ME->hasQualifier() &&
293 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
294 Callee = BuildVirtualCall(MD, This, Ty);
295 } else {
Anders Carlssonecf9bf02009-09-10 23:43:36 +0000296 Callee = CGM.GetAddrOfFunction(MD, Ty);
Eli Friedmane6ce3542009-11-16 05:31:29 +0000297 }
Mike Stump11289f42009-09-09 15:08:12 +0000298
299 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000300 CE->arg_begin(), CE->arg_end());
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000301}
Anders Carlssona5d077d2009-04-14 16:58:56 +0000302
Mike Stump11289f42009-09-09 15:08:12 +0000303RValue
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000304CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
305 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000306 const Expr *BaseExpr = BO->getLHS();
307 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000308
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000309 const MemberPointerType *MPT =
310 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000311 const FunctionProtoType *FPT =
312 MPT->getPointeeType()->getAs<FunctionProtoType>();
313 const CXXRecordDecl *RD =
Douglas Gregor615ac672009-11-04 16:49:01 +0000314 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000315
316 const llvm::FunctionType *FTy =
317 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
318 FPT->isVariadic());
319
320 const llvm::Type *Int8PtrTy =
321 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
322
323 // Get the member function pointer.
324 llvm::Value *MemFnPtr =
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000325 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
326 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000327
328 // Emit the 'this' pointer.
329 llvm::Value *This;
330
331 if (BO->getOpcode() == BinaryOperator::PtrMemI)
332 This = EmitScalarExpr(BaseExpr);
333 else
334 This = EmitLValue(BaseExpr).getAddress();
335
336 // Adjust it.
337 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
338 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
339
340 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
341 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
342
343 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
344
345 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
346
347 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
348
349 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
350
351 // If the LSB in the function pointer is 1, the function pointer points to
352 // a virtual function.
353 llvm::Value *IsVirtual
354 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
355 "and");
356
357 IsVirtual = Builder.CreateTrunc(IsVirtual,
358 llvm::Type::getInt1Ty(VMContext));
359
360 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
361 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
362 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
363
364 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
365 EmitBlock(FnVirtual);
366
367 const llvm::Type *VTableTy =
368 FTy->getPointerTo()->getPointerTo()->getPointerTo();
369
370 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
371 VTable = Builder.CreateLoad(VTable);
372
373 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
374
375 // Since the function pointer is 1 plus the virtual table offset, we
376 // subtract 1 by using a GEP.
Mike Stump0d479e62009-10-09 01:25:47 +0000377 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000378
379 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
380
381 EmitBranch(FnEnd);
382 EmitBlock(FnNonVirtual);
383
384 // If the function is not virtual, just load the pointer.
385 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
386 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
387
388 EmitBlock(FnEnd);
389
390 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
391 Callee->reserveOperandSpace(2);
392 Callee->addIncoming(VirtualFn, FnVirtual);
393 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
394
395 CallArgList Args;
396
397 QualType ThisType =
398 getContext().getPointerType(getContext().getTagDeclType(RD));
399
400 // Push the this ptr.
401 Args.push_back(std::make_pair(RValue::get(This), ThisType));
402
403 // And the rest of the call args
404 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
405 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
406 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
407 Callee, Args, 0);
408}
409
410RValue
Anders Carlsson4034a952009-05-27 04:18:27 +0000411CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
412 const CXXMethodDecl *MD) {
Mike Stump11289f42009-09-09 15:08:12 +0000413 assert(MD->isInstance() &&
Anders Carlsson4034a952009-05-27 04:18:27 +0000414 "Trying to emit a member call expr on a static method!");
Mike Stump11289f42009-09-09 15:08:12 +0000415
Fariborz Jahanian4985b332009-08-13 21:09:41 +0000416 if (MD->isCopyAssignment()) {
417 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
418 if (ClassDecl->hasTrivialCopyAssignment()) {
419 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
420 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
421 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
422 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
423 QualType Ty = E->getType();
424 EmitAggregateCopy(This, Src, Ty);
425 return RValue::get(This);
426 }
427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
John McCall9dd450b2009-09-21 23:43:11 +0000429 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +0000430 const llvm::Type *Ty =
431 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stump5a522352009-09-04 18:27:16 +0000432 FPT->isVariadic());
Mike Stump11289f42009-09-09 15:08:12 +0000433
Anders Carlsson4034a952009-05-27 04:18:27 +0000434 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump11289f42009-09-09 15:08:12 +0000435
Eli Friedmane6ce3542009-11-16 05:31:29 +0000436 llvm::Value *Callee;
437 if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
438 Callee = BuildVirtualCall(MD, This, Ty);
439 else
440 Callee = CGM.GetAddrOfFunction(MD, Ty);
441
Anders Carlsson4034a952009-05-27 04:18:27 +0000442 return EmitCXXMemberCall(MD, Callee, This,
443 E->arg_begin() + 1, E->arg_end());
444}
445
Anders Carlssona5d077d2009-04-14 16:58:56 +0000446llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump11289f42009-09-09 15:08:12 +0000447 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlssona5d077d2009-04-14 16:58:56 +0000448 "Must be in a C++ member function decl to load 'this'");
449 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
450 "Must be in a C++ member function decl to load 'this'");
Mike Stump11289f42009-09-09 15:08:12 +0000451
Anders Carlssona5d077d2009-04-14 16:58:56 +0000452 // FIXME: What if we're inside a block?
Mike Stump18bb9282009-05-16 07:57:57 +0000453 // ans: See how CodeGenFunction::LoadObjCSelf() uses
454 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlssona5d077d2009-04-14 16:58:56 +0000455 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
456}
Anders Carlssonf7475242009-04-15 15:55:24 +0000457
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000458/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
459/// for-loop to call the default constructor on individual members of the
Anders Carlssond49844b2009-09-23 02:45:36 +0000460/// array.
461/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
462/// array type and 'ArrayPtr' points to the beginning fo the array.
463/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000464void
465CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson3a202f62009-11-24 18:43:52 +0000466 const ConstantArrayType *ArrayTy,
467 llvm::Value *ArrayPtr,
468 CallExpr::const_arg_iterator ArgBeg,
469 CallExpr::const_arg_iterator ArgEnd) {
470
Anders Carlssond49844b2009-09-23 02:45:36 +0000471 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
472 llvm::Value * NumElements =
473 llvm::ConstantInt::get(SizeTy,
474 getContext().getConstantArrayElementCount(ArrayTy));
475
Anders Carlsson3a202f62009-11-24 18:43:52 +0000476 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd);
Anders Carlssond49844b2009-09-23 02:45:36 +0000477}
478
479void
480CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson3a202f62009-11-24 18:43:52 +0000481 llvm::Value *NumElements,
482 llvm::Value *ArrayPtr,
483 CallExpr::const_arg_iterator ArgBeg,
484 CallExpr::const_arg_iterator ArgEnd) {
Anders Carlssond49844b2009-09-23 02:45:36 +0000485 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump11289f42009-09-09 15:08:12 +0000486
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000487 // Create a temporary for the loop index and initialize it with 0.
Anders Carlssond49844b2009-09-23 02:45:36 +0000488 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
489 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
490 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000491
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000492 // Start the loop with a block that tests the condition.
493 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
494 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +0000495
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000496 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000497
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000498 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump11289f42009-09-09 15:08:12 +0000499
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000500 // Generate: if (loop-index < number-of-elements fall to the loop body,
501 // otherwise, go to the block after the for-loop.
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000502 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlssond49844b2009-09-23 02:45:36 +0000503 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000504 // If the condition is true, execute the body.
505 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +0000506
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000507 EmitBlock(ForBody);
Mike Stump11289f42009-09-09 15:08:12 +0000508
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000509 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000510 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahaniandd46eb72009-08-20 01:01:06 +0000511 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlssond49844b2009-09-23 02:45:36 +0000512 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
513 "arrayidx");
Mike Stump11289f42009-09-09 15:08:12 +0000514
Anders Carlsson3a202f62009-11-24 18:43:52 +0000515 // C++ [class.temporary]p4:
516 // There are two contexts in which temporaries are destroyed at a different
517 // point than the end of the full- expression. The first context is when a
518 // default constructor is called to initialize an element of an array.
519 // If the constructor has one or more default arguments, the destruction of
520 // every temporary created in a default argument expression is sequenced
521 // before the construction of the next array element, if any.
522
523 // Keep track of the current number of live temporaries.
524 unsigned OldNumLiveTemporaries = LiveTemporaries.size();
525
526 EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd);
527
528 // Pop temporaries.
529 while (LiveTemporaries.size() > OldNumLiveTemporaries)
530 PopCXXTemporary();
531
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000532 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000533
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000534 // Emit the increment of the loop counter.
Anders Carlssond49844b2009-09-23 02:45:36 +0000535 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000536 Counter = Builder.CreateLoad(IndexPtr);
537 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
538 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000539
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000540 // Finally, branch back up to the condition for the next iteration.
541 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000542
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000543 // Emit the fall-through block.
544 EmitBlock(AfterFor, true);
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000545}
546
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000547/// EmitCXXAggrDestructorCall - calls the default destructor on array
548/// elements in reverse order of construction.
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000549void
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000550CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
551 const ArrayType *Array,
552 llvm::Value *This) {
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000553 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
554 assert(CA && "Do we support VLA for destruction ?");
Fariborz Jahanian6814eaa2009-11-13 19:27:47 +0000555 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
556 llvm::Value* ElementCountPtr =
557 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
558 EmitCXXAggrDestructorCall(D, ElementCountPtr, This);
559}
560
561/// EmitCXXAggrDestructorCall - calls the default destructor on array
562/// elements in reverse order of construction.
563void
564CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
565 llvm::Value *UpperCount,
566 llvm::Value *This) {
Mike Stump11289f42009-09-09 15:08:12 +0000567 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000568 1);
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000569 // Create a temporary for the loop index and initialize it with count of
570 // array elements.
571 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
572 "loop.index");
573 // Index = ElementCount;
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000574 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000575
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000576 // Start the loop with a block that tests the condition.
577 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
578 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +0000579
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000580 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000581
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000582 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump11289f42009-09-09 15:08:12 +0000583
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000584 // Generate: if (loop-index != 0 fall to the loop body,
585 // otherwise, go to the block after the for-loop.
Mike Stump11289f42009-09-09 15:08:12 +0000586 llvm::Value* zeroConstant =
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000587 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
588 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
589 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
590 "isne");
591 // If the condition is true, execute the body.
592 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +0000593
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000594 EmitBlock(ForBody);
Mike Stump11289f42009-09-09 15:08:12 +0000595
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000596 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
597 // Inside the loop body, emit the constructor call on the array element.
598 Counter = Builder.CreateLoad(IndexPtr);
599 Counter = Builder.CreateSub(Counter, One);
600 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
Fariborz Jahanianebea0052009-11-13 22:29:45 +0000601 if (D->isVirtual()) {
602 const llvm::Type *Ty =
603 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(D),
604 /*isVariadic=*/false);
605
606 llvm::Value *Callee = BuildVirtualCall(D, Dtor_Deleting, Address, Ty);
607 EmitCXXMemberCall(D, Callee, Address, 0, 0);
608 }
609 else
610 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump11289f42009-09-09 15:08:12 +0000611
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000612 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000613
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000614 // Emit the decrement of the loop counter.
615 Counter = Builder.CreateLoad(IndexPtr);
616 Counter = Builder.CreateSub(Counter, One, "dec");
617 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000618
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000619 // Finally, branch back up to the condition for the next iteration.
620 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000622 // Emit the fall-through block.
623 EmitBlock(AfterFor, true);
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000624}
625
Mike Stumpea950e22009-11-18 18:57:56 +0000626/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
627/// invoked, calls the default destructor on array elements in reverse order of
Fariborz Jahanian1254a092009-11-10 19:24:06 +0000628/// construction.
629llvm::Constant *
630CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
631 const ArrayType *Array,
632 llvm::Value *This) {
633 static int UniqueCount;
634 FunctionArgList Args;
635 ImplicitParamDecl *Dst =
636 ImplicitParamDecl::Create(getContext(), 0,
637 SourceLocation(), 0,
638 getContext().getPointerType(getContext().VoidTy));
639 Args.push_back(std::make_pair(Dst, Dst->getType()));
640
641 llvm::SmallString<16> Name;
642 llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueCount);
643 QualType R = getContext().VoidTy;
644 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
645 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
646 llvm::Function *Fn =
647 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
648 Name.c_str(),
649 &CGM.getModule());
650 IdentifierInfo *II
651 = &CGM.getContext().Idents.get(Name.c_str());
652 FunctionDecl *FD = FunctionDecl::Create(getContext(),
653 getContext().getTranslationUnitDecl(),
654 SourceLocation(), II, R, 0,
655 FunctionDecl::Static,
656 false, true);
657 StartFunction(FD, R, Fn, Args, SourceLocation());
658 QualType BaseElementTy = getContext().getBaseElementType(Array);
659 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
660 BasePtr = llvm::PointerType::getUnqual(BasePtr);
661 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
662 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
663 FinishFunction();
664 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
665 0);
666 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
667 return m;
668}
669
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000670void
Mike Stump11289f42009-09-09 15:08:12 +0000671CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
672 CXXCtorType Type,
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000673 llvm::Value *This,
674 CallExpr::const_arg_iterator ArgBeg,
675 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian42af5ba2009-08-14 20:11:43 +0000676 if (D->isCopyConstructor(getContext())) {
677 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
678 if (ClassDecl->hasTrivialCopyConstructor()) {
679 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
680 "EmitCXXConstructorCall - user declared copy constructor");
681 const Expr *E = (*ArgBeg);
682 QualType Ty = E->getType();
683 llvm::Value *Src = EmitLValue(E).getAddress();
684 EmitAggregateCopy(This, Src, Ty);
685 return;
686 }
687 }
Mike Stump11289f42009-09-09 15:08:12 +0000688
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000689 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
690
691 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000692}
693
Mike Stump11289f42009-09-09 15:08:12 +0000694void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson0a637412009-05-29 21:03:38 +0000695 CXXDtorType Type,
696 llvm::Value *This) {
697 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000698
Anders Carlsson0a637412009-05-29 21:03:38 +0000699 EmitCXXMemberCall(D, Callee, This, 0, 0);
700}
701
Mike Stump11289f42009-09-09 15:08:12 +0000702void
703CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson1619a5042009-05-03 17:47:16 +0000704 const CXXConstructExpr *E) {
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000705 assert(Dest && "Must have a destination!");
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000706 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000707 const ConstantArrayType *Array =
708 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000709 // For a copy constructor, even if it is trivial, must fall thru so
710 // its argument is code-gen'ed.
711 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000712 QualType InitType = E->getType();
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000713 if (Array)
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000714 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000715 const CXXRecordDecl *RD =
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000716 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000717 if (RD->hasTrivialConstructor())
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000718 return;
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000719 }
Mike Stump11289f42009-09-09 15:08:12 +0000720 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanianeb869762009-08-06 01:02:49 +0000721 // its first argument instead.
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000722 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Anders Carlsson78cfaa92009-11-13 04:34:45 +0000723 const Expr *Arg = E->getArg(0);
724
725 if (const CXXBindTemporaryExpr *BindExpr =
726 dyn_cast<CXXBindTemporaryExpr>(Arg))
727 Arg = BindExpr->getSubExpr();
728
729 EmitAggExpr(Arg, Dest, false);
Fariborz Jahanian00130932009-08-06 19:12:38 +0000730 return;
Fariborz Jahanianeb869762009-08-06 01:02:49 +0000731 }
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000732 if (Array) {
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000733 QualType BaseElementTy = getContext().getBaseElementType(Array);
734 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
735 BasePtr = llvm::PointerType::getUnqual(BasePtr);
736 llvm::Value *BaseAddrPtr =
737 Builder.CreateBitCast(Dest, BasePtr);
Anders Carlsson3a202f62009-11-24 18:43:52 +0000738 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
739 E->arg_begin(), E->arg_end());
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000740 }
741 else
742 // Call the constructor.
743 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
744 E->arg_begin(), E->arg_end());
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000745}
746
Anders Carlssonf7475242009-04-15 15:55:24 +0000747void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlssondae1abc2009-05-05 04:44:02 +0000748 EmitGlobal(GlobalDecl(D, Ctor_Complete));
749 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlssonf7475242009-04-15 15:55:24 +0000750}
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000751
Mike Stump11289f42009-09-09 15:08:12 +0000752void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000753 CXXCtorType Type) {
Mike Stump11289f42009-09-09 15:08:12 +0000754
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000755 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000756
Anders Carlsson73fcc952009-09-11 00:07:24 +0000757 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump11289f42009-09-09 15:08:12 +0000758
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000759 SetFunctionDefinitionAttributes(D, Fn);
760 SetLLVMFunctionAttributesForDefinition(D, Fn);
761}
762
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000763llvm::Function *
Mike Stump11289f42009-09-09 15:08:12 +0000764CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000765 CXXCtorType Type) {
Fariborz Jahanianc2d71b52009-11-06 18:47:57 +0000766 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000767 const llvm::FunctionType *FTy =
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000768 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanianc2d71b52009-11-06 18:47:57 +0000769 FPT->isVariadic());
Mike Stump11289f42009-09-09 15:08:12 +0000770
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000771 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnere0be0df2009-05-12 21:21:08 +0000772 return cast<llvm::Function>(
773 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000774}
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000775
Mike Stump11289f42009-09-09 15:08:12 +0000776const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000777 CXXCtorType Type) {
778 llvm::SmallString<256> Name;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000779 getMangleContext().mangleCXXCtor(D, Type, Name);
Mike Stump11289f42009-09-09 15:08:12 +0000780
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000781 Name += '\0';
782 return UniqueMangledName(Name.begin(), Name.end());
783}
784
785void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Eli Friedmanb572c922009-11-14 04:19:37 +0000786 if (D->isVirtual())
787 EmitCXXDestructor(D, Dtor_Deleting);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000788 EmitCXXDestructor(D, Dtor_Complete);
789 EmitCXXDestructor(D, Dtor_Base);
790}
791
Mike Stump11289f42009-09-09 15:08:12 +0000792void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000793 CXXDtorType Type) {
794 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000795
Anders Carlsson73fcc952009-09-11 00:07:24 +0000796 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump11289f42009-09-09 15:08:12 +0000797
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000798 SetFunctionDefinitionAttributes(D, Fn);
799 SetLLVMFunctionAttributesForDefinition(D, Fn);
800}
801
802llvm::Function *
Mike Stump11289f42009-09-09 15:08:12 +0000803CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000804 CXXDtorType Type) {
805 const llvm::FunctionType *FTy =
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000806 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump11289f42009-09-09 15:08:12 +0000807
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000808 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnere0be0df2009-05-12 21:21:08 +0000809 return cast<llvm::Function>(
810 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000811}
812
Mike Stump11289f42009-09-09 15:08:12 +0000813const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000814 CXXDtorType Type) {
815 llvm::SmallString<256> Name;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000816 getMangleContext().mangleCXXDtor(D, Type, Name);
Mike Stump11289f42009-09-09 15:08:12 +0000817
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000818 Name += '\0';
819 return UniqueMangledName(Name.begin(), Name.end());
820}
Fariborz Jahanian83381cc2009-07-20 23:18:55 +0000821
Mike Stump5a522352009-09-04 18:27:16 +0000822llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
823 const CXXMethodDecl *MD,
Mike Stump453fe422009-09-05 07:20:32 +0000824 bool Extern, int64_t nv,
825 int64_t v) {
Mike Stump33ccd9e2009-11-02 23:22:01 +0000826 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stump5a522352009-09-04 18:27:16 +0000827}
828
Mike Stump77738202009-11-03 16:59:27 +0000829llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
830 int64_t v) {
831 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
832 0);
833 const llvm::Type *OrigTy = V->getType();
834 if (nv) {
835 // Do the non-virtual adjustment
836 V = Builder.CreateBitCast(V, Ptr8Ty);
837 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
838 V = Builder.CreateBitCast(V, OrigTy);
839 }
840 if (v) {
841 // Do the virtual this adjustment
842 const llvm::Type *PtrDiffTy =
843 ConvertType(getContext().getPointerDiffType());
844 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
845 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
846 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
847 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
848 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
849 V = Builder.CreateLoad(V, "vtable");
850 llvm::Value *VTablePtr = V;
851 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
852 v /= LLVMPointerWidth/8;
853 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
854 V = Builder.CreateLoad(V);
855 V = Builder.CreateGEP(ThisVal, V);
856 V = Builder.CreateBitCast(V, OrigTy);
857 }
858 return V;
859}
860
Mike Stump80f6ac52009-09-11 23:25:56 +0000861llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
862 const CXXMethodDecl *MD,
863 bool Extern,
864 int64_t nv_t,
865 int64_t v_t,
866 int64_t nv_r,
867 int64_t v_r) {
Mike Stump33ccd9e2009-11-02 23:22:01 +0000868 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump80f6ac52009-09-11 23:25:56 +0000869
870 FunctionArgList Args;
871 ImplicitParamDecl *ThisDecl =
872 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
873 MD->getThisType(getContext()));
874 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
875 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
876 e = MD->param_end();
877 i != e; ++i) {
878 ParmVarDecl *D = *i;
879 Args.push_back(std::make_pair(D, D->getType()));
880 }
881 IdentifierInfo *II
882 = &CGM.getContext().Idents.get("__thunk_named_foo_");
883 FunctionDecl *FD = FunctionDecl::Create(getContext(),
884 getContext().getTranslationUnitDecl(),
Mike Stump33ccd9e2009-11-02 23:22:01 +0000885 SourceLocation(), II, ResultType, 0,
Mike Stump80f6ac52009-09-11 23:25:56 +0000886 Extern
887 ? FunctionDecl::Extern
888 : FunctionDecl::Static,
889 false, true);
Mike Stump33ccd9e2009-11-02 23:22:01 +0000890 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
891
892 // generate body
Mike Stumpf3589722009-11-03 02:12:59 +0000893 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
894 const llvm::Type *Ty =
895 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
896 FPT->isVariadic());
897 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump33ccd9e2009-11-02 23:22:01 +0000898 CallArgList CallArgs;
899
Mike Stumpf3589722009-11-03 02:12:59 +0000900 QualType ArgType = MD->getThisType(getContext());
901 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stump71609a22009-11-04 00:53:51 +0000902 if (nv_t || v_t) {
Mike Stump77738202009-11-03 16:59:27 +0000903 // Do the this adjustment.
Mike Stump71609a22009-11-04 00:53:51 +0000904 const llvm::Type *OrigTy = Callee->getType();
Mike Stump77738202009-11-03 16:59:27 +0000905 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stump71609a22009-11-04 00:53:51 +0000906 if (nv_r || v_r) {
907 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
908 Callee = Builder.CreateBitCast(Callee, OrigTy);
909 nv_r = v_r = 0;
910 }
911 }
912
Mike Stumpf3589722009-11-03 02:12:59 +0000913 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
914
Mike Stump33ccd9e2009-11-02 23:22:01 +0000915 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
916 e = MD->param_end();
917 i != e; ++i) {
918 ParmVarDecl *D = *i;
919 QualType ArgType = D->getType();
920
921 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
922 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stump33ccd9e2009-11-02 23:22:01 +0000923 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
924 }
925
Mike Stump31e1d432009-11-02 23:47:45 +0000926 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
927 Callee, CallArgs, MD);
Mike Stump33ccd9e2009-11-02 23:22:01 +0000928 if (nv_r || v_r) {
Mike Stumpc5507682009-11-05 06:32:02 +0000929 bool CanBeZero = !(ResultType->isReferenceType()
930 // FIXME: attr nonnull can't be zero either
931 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stump77738202009-11-03 16:59:27 +0000932 // Do the return result adjustment.
Mike Stumpc5507682009-11-05 06:32:02 +0000933 if (CanBeZero) {
934 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
935 llvm::BasicBlock *ZeroBlock = createBasicBlock();
936 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stumpb8da7a02009-11-05 06:12:26 +0000937
Mike Stumpc5507682009-11-05 06:32:02 +0000938 const llvm::Type *Ty = RV.getScalarVal()->getType();
939 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
940 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
941 NonZeroBlock, ZeroBlock);
942 EmitBlock(NonZeroBlock);
943 llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r);
944 EmitBranch(ContBlock);
945 EmitBlock(ZeroBlock);
946 llvm::Value *Z = RV.getScalarVal();
947 EmitBlock(ContBlock);
948 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
949 RVOrZero->reserveOperandSpace(2);
950 RVOrZero->addIncoming(NZ, NonZeroBlock);
951 RVOrZero->addIncoming(Z, ZeroBlock);
952 RV = RValue::get(RVOrZero);
953 } else
954 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stump33ccd9e2009-11-02 23:22:01 +0000955 }
956
Mike Stump31e1d432009-11-02 23:47:45 +0000957 if (!ResultType->isVoidType())
958 EmitReturnOfRValue(RV, ResultType);
959
Mike Stump80f6ac52009-09-11 23:25:56 +0000960 FinishFunction();
961 return Fn;
962}
963
Mike Stump453fe422009-09-05 07:20:32 +0000964llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
965 int64_t nv, int64_t v) {
Mike Stump5a522352009-09-04 18:27:16 +0000966 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000967 getMangleContext().mangleThunk(MD, nv, v, OutName);
Mike Stump5a522352009-09-04 18:27:16 +0000968 llvm::GlobalVariable::LinkageTypes linktype;
969 linktype = llvm::GlobalValue::WeakAnyLinkage;
970 if (!Extern)
971 linktype = llvm::GlobalValue::InternalLinkage;
972 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall9dd450b2009-09-21 23:43:11 +0000973 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump5a522352009-09-04 18:27:16 +0000974 const llvm::FunctionType *FTy =
975 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
976 FPT->isVariadic());
977
Daniel Dunbare128dd12009-11-21 09:06:22 +0000978 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump5a522352009-09-04 18:27:16 +0000979 &getModule());
Mike Stump453fe422009-09-05 07:20:32 +0000980 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stump5a522352009-09-04 18:27:16 +0000981 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
982 return m;
983}
984
Mike Stump80f6ac52009-09-11 23:25:56 +0000985llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
986 bool Extern, int64_t nv_t,
987 int64_t v_t, int64_t nv_r,
988 int64_t v_r) {
989 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000990 getMangleContext().mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, OutName);
Mike Stump80f6ac52009-09-11 23:25:56 +0000991 llvm::GlobalVariable::LinkageTypes linktype;
992 linktype = llvm::GlobalValue::WeakAnyLinkage;
993 if (!Extern)
994 linktype = llvm::GlobalValue::InternalLinkage;
995 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall9dd450b2009-09-21 23:43:11 +0000996 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump80f6ac52009-09-11 23:25:56 +0000997 const llvm::FunctionType *FTy =
998 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
999 FPT->isVariadic());
1000
Daniel Dunbare128dd12009-11-21 09:06:22 +00001001 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump80f6ac52009-09-11 23:25:56 +00001002 &getModule());
1003 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1004 v_r);
Mike Stump80f6ac52009-09-11 23:25:56 +00001005 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1006 return m;
1007}
1008
Mike Stumpa5588bf2009-08-26 20:46:33 +00001009llvm::Value *
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001010CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
1011 const CXXRecordDecl *ClassDecl,
1012 const CXXRecordDecl *BaseClassDecl) {
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001013 const llvm::Type *Int8PtrTy =
1014 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
1015
1016 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
1017 Int8PtrTy->getPointerTo());
1018 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
1019
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001020 int64_t VBaseOffsetIndex =
1021 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
1022
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001023 llvm::Value *VBaseOffsetPtr =
Mike Stumpb9c9b352009-11-04 01:11:15 +00001024 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001025 const llvm::Type *PtrDiffTy =
1026 ConvertType(getContext().getPointerDiffType());
1027
1028 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1029 PtrDiffTy->getPointerTo());
1030
1031 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1032
1033 return VBaseOffset;
1034}
1035
Anders Carlssone828c362009-11-13 04:45:41 +00001036static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, int64_t VtableIndex,
1037 llvm::Value *This, const llvm::Type *Ty) {
1038 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson32bfb1c2009-10-03 14:56:57 +00001039
Anders Carlssone828c362009-11-13 04:45:41 +00001040 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
1041 Vtable = CGF.Builder.CreateLoad(Vtable);
1042
1043 llvm::Value *VFuncPtr =
1044 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
1045 return CGF.Builder.CreateLoad(VFuncPtr);
1046}
1047
1048llvm::Value *
1049CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1050 const llvm::Type *Ty) {
1051 MD = MD->getCanonicalDecl();
1052 int64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
1053
1054 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1055}
1056
1057llvm::Value *
1058CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
1059 llvm::Value *&This, const llvm::Type *Ty) {
1060 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
1061 int64_t VtableIndex =
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001062 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlssone828c362009-11-13 04:45:41 +00001063
1064 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpa5588bf2009-08-26 20:46:33 +00001065}
1066
Fariborz Jahanian56263842009-08-21 18:30:26 +00001067/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1068/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1069/// copy or via a copy constructor call.
Fariborz Jahanian2c73b1d2009-08-26 00:23:27 +00001070// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump11289f42009-09-09 15:08:12 +00001071void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001072 llvm::Value *Src,
1073 const ArrayType *Array,
Mike Stump11289f42009-09-09 15:08:12 +00001074 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001075 QualType Ty) {
1076 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1077 assert(CA && "VLA cannot be copied over");
1078 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump11289f42009-09-09 15:08:12 +00001079
Fariborz Jahanian56263842009-08-21 18:30:26 +00001080 // Create a temporary for the loop index and initialize it with 0.
1081 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1082 "loop.index");
Mike Stump11289f42009-09-09 15:08:12 +00001083 llvm::Value* zeroConstant =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001084 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson32bfb1c2009-10-03 14:56:57 +00001085 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanian56263842009-08-21 18:30:26 +00001086 // Start the loop with a block that tests the condition.
1087 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1088 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +00001089
Fariborz Jahanian56263842009-08-21 18:30:26 +00001090 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001091
Fariborz Jahanian56263842009-08-21 18:30:26 +00001092 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1093 // Generate: if (loop-index < number-of-elements fall to the loop body,
1094 // otherwise, go to the block after the for-loop.
1095 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump11289f42009-09-09 15:08:12 +00001096 llvm::Value * NumElementsPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001097 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1098 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001099 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001100 "isless");
1101 // If the condition is true, execute the body.
1102 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +00001103
Fariborz Jahanian56263842009-08-21 18:30:26 +00001104 EmitBlock(ForBody);
1105 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1106 // Inside the loop body, emit the constructor call on the array element.
1107 Counter = Builder.CreateLoad(IndexPtr);
1108 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1109 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1110 if (BitwiseCopy)
1111 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001112 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001113 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump11289f42009-09-09 15:08:12 +00001114 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001115 Ctor_Complete);
1116 CallArgList CallArgs;
1117 // Push the this (Dest) ptr.
1118 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1119 BaseCopyCtor->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001120
Fariborz Jahanian56263842009-08-21 18:30:26 +00001121 // Push the Src ptr.
1122 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stumpb9c9b352009-11-04 01:11:15 +00001123 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001124 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001125 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian56263842009-08-21 18:30:26 +00001126 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1127 Callee, CallArgs, BaseCopyCtor);
1128 }
1129 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001130
Fariborz Jahanian56263842009-08-21 18:30:26 +00001131 // Emit the increment of the loop counter.
1132 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1133 Counter = Builder.CreateLoad(IndexPtr);
1134 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1135 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001136
Fariborz Jahanian56263842009-08-21 18:30:26 +00001137 // Finally, branch back up to the condition for the next iteration.
1138 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001139
Fariborz Jahanian56263842009-08-21 18:30:26 +00001140 // Emit the fall-through block.
1141 EmitBlock(AfterFor, true);
1142}
1143
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001144/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump11289f42009-09-09 15:08:12 +00001145/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001146/// bitwise assignment or via a copy assignment operator function call.
1147/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump11289f42009-09-09 15:08:12 +00001148void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001149 llvm::Value *Src,
1150 const ArrayType *Array,
Mike Stump11289f42009-09-09 15:08:12 +00001151 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001152 QualType Ty) {
1153 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1154 assert(CA && "VLA cannot be asssigned");
1155 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump11289f42009-09-09 15:08:12 +00001156
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001157 // Create a temporary for the loop index and initialize it with 0.
1158 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1159 "loop.index");
Mike Stump11289f42009-09-09 15:08:12 +00001160 llvm::Value* zeroConstant =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001161 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1162 Builder.CreateStore(zeroConstant, IndexPtr, false);
1163 // Start the loop with a block that tests the condition.
1164 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1165 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +00001166
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001167 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001168
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001169 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1170 // Generate: if (loop-index < number-of-elements fall to the loop body,
1171 // otherwise, go to the block after the for-loop.
1172 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump11289f42009-09-09 15:08:12 +00001173 llvm::Value * NumElementsPtr =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001174 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1175 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001176 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001177 "isless");
1178 // If the condition is true, execute the body.
1179 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +00001180
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001181 EmitBlock(ForBody);
1182 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1183 // Inside the loop body, emit the assignment operator call on array element.
1184 Counter = Builder.CreateLoad(IndexPtr);
1185 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1186 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1187 const CXXMethodDecl *MD = 0;
1188 if (BitwiseAssign)
1189 EmitAggregateCopy(Dest, Src, Ty);
1190 else {
1191 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1192 MD);
1193 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1194 (void)hasCopyAssign;
John McCall9dd450b2009-09-21 23:43:11 +00001195 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001196 const llvm::Type *LTy =
1197 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1198 FPT->isVariadic());
Anders Carlssonecf9bf02009-09-10 23:43:36 +00001199 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump11289f42009-09-09 15:08:12 +00001200
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001201 CallArgList CallArgs;
1202 // Push the this (Dest) ptr.
1203 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1204 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001205
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001206 // Push the Src ptr.
1207 CallArgs.push_back(std::make_pair(RValue::get(Src),
1208 MD->getParamDecl(0)->getType()));
John McCall9dd450b2009-09-21 23:43:11 +00001209 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001210 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1211 Callee, CallArgs, MD);
1212 }
1213 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001214
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001215 // Emit the increment of the loop counter.
1216 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1217 Counter = Builder.CreateLoad(IndexPtr);
1218 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1219 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001220
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001221 // Finally, branch back up to the condition for the next iteration.
1222 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001223
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001224 // Emit the fall-through block.
1225 EmitBlock(AfterFor, true);
1226}
1227
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001228/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1229/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanian56263842009-08-21 18:30:26 +00001230/// or via a copy constructor call.
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001231void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001232 llvm::Value *Dest, llvm::Value *Src,
Mike Stump11289f42009-09-09 15:08:12 +00001233 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001234 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1235 if (ClassDecl) {
Anders Carlsson8c793172009-11-23 17:57:54 +00001236 Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1237 /*NullCheckValue=*/false);
1238 Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl,
1239 /*NullCheckValue=*/false);
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001240 }
1241 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1242 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001243 return;
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001244 }
Mike Stump11289f42009-09-09 15:08:12 +00001245
1246 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian7c3d7f62009-08-08 00:59:58 +00001247 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump11289f42009-09-09 15:08:12 +00001248 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001249 Ctor_Complete);
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001250 CallArgList CallArgs;
1251 // Push the this (Dest) ptr.
1252 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1253 BaseCopyCtor->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001254
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001255 // Push the Src ptr.
1256 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian2a26e352009-08-10 17:20:45 +00001257 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001258 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001259 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001260 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1261 Callee, CallArgs, BaseCopyCtor);
1262 }
1263}
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001264
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001265/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump11289f42009-09-09 15:08:12 +00001266/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001267/// assignment of via an assignment operator call.
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001268// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001269void CodeGenFunction::EmitClassCopyAssignment(
1270 llvm::Value *Dest, llvm::Value *Src,
Mike Stump11289f42009-09-09 15:08:12 +00001271 const CXXRecordDecl *ClassDecl,
1272 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001273 QualType Ty) {
1274 if (ClassDecl) {
Anders Carlsson8c793172009-11-23 17:57:54 +00001275 Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1276 /*NullCheckValue=*/false);
1277 Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl,
1278 /*NullCheckValue=*/false);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001279 }
1280 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1281 EmitAggregateCopy(Dest, Src, Ty);
1282 return;
1283 }
Mike Stump11289f42009-09-09 15:08:12 +00001284
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001285 const CXXMethodDecl *MD = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001286 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001287 MD);
1288 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1289 (void)ConstCopyAssignOp;
1290
John McCall9dd450b2009-09-21 23:43:11 +00001291 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +00001292 const llvm::Type *LTy =
1293 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001294 FPT->isVariadic());
Anders Carlssonecf9bf02009-09-10 23:43:36 +00001295 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump11289f42009-09-09 15:08:12 +00001296
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001297 CallArgList CallArgs;
1298 // Push the this (Dest) ptr.
1299 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1300 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001301
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001302 // Push the Src ptr.
1303 CallArgs.push_back(std::make_pair(RValue::get(Src),
1304 MD->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001305 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001306 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001307 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1308 Callee, CallArgs, MD);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001309}
1310
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001311/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump11289f42009-09-09 15:08:12 +00001312void
Anders Carlssonddf57d32009-09-14 05:32:02 +00001313CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1314 CXXCtorType Type,
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001315 llvm::Function *Fn,
1316 const FunctionArgList &Args) {
Anders Carlssonddf57d32009-09-14 05:32:02 +00001317 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1318 SourceLocation());
1319 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001320 FinishFunction();
1321}
1322
Mike Stumpb9c9b352009-11-04 01:11:15 +00001323/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1324/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump11289f42009-09-09 15:08:12 +00001325/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stumpb9c9b352009-11-04 01:11:15 +00001326/// copy of its subobjects. The order of copying is the same as the order of
1327/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001328/// Each subobject is copied in the manner appropriate to its type:
Mike Stump11289f42009-09-09 15:08:12 +00001329/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001330/// used;
Mike Stump11289f42009-09-09 15:08:12 +00001331/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001332/// appropriate to the element type;
Mike Stump11289f42009-09-09 15:08:12 +00001333/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001334/// used.
Mike Stump11289f42009-09-09 15:08:12 +00001335/// Virtual base class subobjects shall be copied only once by the
1336/// implicitly-defined copy constructor
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001337
Anders Carlssonddf57d32009-09-14 05:32:02 +00001338void
1339CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1340 CXXCtorType Type,
1341 llvm::Function *Fn,
1342 const FunctionArgList &Args) {
Anders Carlsson73fcc952009-09-11 00:07:24 +00001343 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001344 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stumpb9c9b352009-11-04 01:11:15 +00001345 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonddf57d32009-09-14 05:32:02 +00001346 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1347 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001348
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001349 FunctionArgList::const_iterator i = Args.begin();
1350 const VarDecl *ThisArg = i->first;
1351 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1352 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1353 const VarDecl *SrcArg = (i+1)->first;
1354 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1355 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump11289f42009-09-09 15:08:12 +00001356
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001357 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1358 Base != ClassDecl->bases_end(); ++Base) {
1359 // FIXME. copy constrution of virtual base NYI
1360 if (Base->isVirtual())
1361 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001362
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001363 CXXRecordDecl *BaseClassDecl
1364 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001365 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1366 Base->getType());
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001367 }
Mike Stump11289f42009-09-09 15:08:12 +00001368
Anders Carlsson3c9beab2009-11-24 21:08:10 +00001369 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1370 E = ClassDecl->field_end(); I != E; ++I) {
1371 const FieldDecl *Field = *I;
1372
1373 QualType FieldType = getContext().getCanonicalType(Field->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001374 const ConstantArrayType *Array =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001375 getContext().getAsConstantArrayType(FieldType);
1376 if (Array)
1377 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump11289f42009-09-09 15:08:12 +00001378
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001379 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1380 CXXRecordDecl *FieldClassDecl
1381 = cast<CXXRecordDecl>(FieldClassType->getDecl());
Anders Carlsson3c9beab2009-11-24 21:08:10 +00001382 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1383 LValue RHS = EmitLValueForField(LoadOfSrc, Field, false, 0);
Fariborz Jahanian56263842009-08-21 18:30:26 +00001384 if (Array) {
1385 const llvm::Type *BasePtr = ConvertType(FieldType);
1386 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump11289f42009-09-09 15:08:12 +00001387 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001388 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump11289f42009-09-09 15:08:12 +00001389 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001390 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1391 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1392 FieldClassDecl, FieldType);
1393 }
Mike Stump11289f42009-09-09 15:08:12 +00001394 else
1395 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanian56263842009-08-21 18:30:26 +00001396 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001397 continue;
1398 }
Anders Carlsson3c9beab2009-11-24 21:08:10 +00001399
1400 if (Field->getType()->isReferenceType()) {
1401 unsigned FieldIndex = CGM.getTypes().getLLVMFieldNo(Field);
1402
1403 llvm::Value *LHS = Builder.CreateStructGEP(LoadOfThis, FieldIndex,
1404 "lhs.ref");
1405
1406 llvm::Value *RHS = Builder.CreateStructGEP(LoadOfThis, FieldIndex,
1407 "rhs.ref");
1408
1409 // Load the value in RHS.
1410 RHS = Builder.CreateLoad(RHS);
1411
1412 // And store it in the LHS
1413 Builder.CreateStore(RHS, LHS);
1414
1415 continue;
1416 }
Fariborz Jahanian7cc52cf2009-08-10 18:34:26 +00001417 // Do a built-in assignment of scalar data members.
Anders Carlsson3c9beab2009-11-24 21:08:10 +00001418 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1419 LValue RHS = EmitLValueForField(LoadOfSrc, Field, false, 0);
1420
Eli Friedmanc2ef2152009-11-16 21:47:41 +00001421 if (!hasAggregateLLVMType(Field->getType())) {
1422 RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType());
1423 EmitStoreThroughLValue(RVRHS, LHS, Field->getType());
1424 } else if (Field->getType()->isAnyComplexType()) {
1425 ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(),
1426 RHS.isVolatileQualified());
1427 StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified());
1428 } else {
1429 EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType());
1430 }
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001431 }
Fariborz Jahanianf6bda5d2009-08-08 19:31:03 +00001432 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001433}
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001434
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001435/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump11289f42009-09-09 15:08:12 +00001436/// Before the implicitly-declared copy assignment operator for a class is
1437/// implicitly defined, all implicitly- declared copy assignment operators for
1438/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001439/// implicitly defined. [12.8-p12]
Mike Stump11289f42009-09-09 15:08:12 +00001440/// The implicitly-defined copy assignment operator for class X performs
1441/// memberwise assignment of its subob- jects. The direct base classes of X are
1442/// assigned first, in the order of their declaration in
1443/// the base-specifier-list, and then the immediate nonstatic data members of X
1444/// are assigned, in the order in which they were declared in the class
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001445/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump11289f42009-09-09 15:08:12 +00001446/// if the subobject is of class type, the copy assignment operator for the
1447/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001448/// possible virtual overriding functions in more derived classes);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001449///
Mike Stump11289f42009-09-09 15:08:12 +00001450/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001451/// appropriate to the element type;
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001452///
Mike Stump11289f42009-09-09 15:08:12 +00001453/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001454/// used.
1455void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001456 llvm::Function *Fn,
1457 const FunctionArgList &Args) {
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001458
1459 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1460 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1461 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonddf57d32009-09-14 05:32:02 +00001462 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001463
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001464 FunctionArgList::const_iterator i = Args.begin();
1465 const VarDecl *ThisArg = i->first;
1466 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1467 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1468 const VarDecl *SrcArg = (i+1)->first;
1469 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1470 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump11289f42009-09-09 15:08:12 +00001471
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001472 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1473 Base != ClassDecl->bases_end(); ++Base) {
1474 // FIXME. copy assignment of virtual base NYI
1475 if (Base->isVirtual())
1476 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001477
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001478 CXXRecordDecl *BaseClassDecl
1479 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1480 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1481 Base->getType());
1482 }
Mike Stump11289f42009-09-09 15:08:12 +00001483
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001484 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1485 FieldEnd = ClassDecl->field_end();
1486 Field != FieldEnd; ++Field) {
1487 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001488 const ConstantArrayType *Array =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001489 getContext().getAsConstantArrayType(FieldType);
1490 if (Array)
1491 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump11289f42009-09-09 15:08:12 +00001492
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001493 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1494 CXXRecordDecl *FieldClassDecl
1495 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1496 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1497 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001498 if (Array) {
1499 const llvm::Type *BasePtr = ConvertType(FieldType);
1500 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1501 llvm::Value *DestBaseAddrPtr =
1502 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1503 llvm::Value *SrcBaseAddrPtr =
1504 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1505 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1506 FieldClassDecl, FieldType);
1507 }
1508 else
Mike Stump11289f42009-09-09 15:08:12 +00001509 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001510 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001511 continue;
1512 }
1513 // Do a built-in assignment of scalar data members.
1514 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1515 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1516 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1517 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian92b3f472009-08-14 00:01:54 +00001518 }
Mike Stump11289f42009-09-09 15:08:12 +00001519
Fariborz Jahanian92b3f472009-08-14 00:01:54 +00001520 // return *this;
1521 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump11289f42009-09-09 15:08:12 +00001522
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001523 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001524}
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001525
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001526static void EmitBaseInitializer(CodeGenFunction &CGF,
1527 const CXXRecordDecl *ClassDecl,
1528 CXXBaseOrMemberInitializer *BaseInit,
1529 CXXCtorType CtorType) {
1530 assert(BaseInit->isBaseInitializer() &&
1531 "Must have base initializer!");
1532
1533 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1534
1535 const Type *BaseType = BaseInit->getBaseClass();
1536 CXXRecordDecl *BaseClassDecl =
1537 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson8c793172009-11-23 17:57:54 +00001538 llvm::Value *V = CGF.GetAddressOfBaseClass(ThisPtr, ClassDecl,
1539 BaseClassDecl,
1540 /*NullCheckValue=*/false);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001541 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1542 CtorType, V,
1543 BaseInit->const_arg_begin(),
1544 BaseInit->const_arg_end());
1545}
1546
1547static void EmitMemberInitializer(CodeGenFunction &CGF,
1548 const CXXRecordDecl *ClassDecl,
1549 CXXBaseOrMemberInitializer *MemberInit) {
1550 assert(MemberInit->isMemberInitializer() &&
1551 "Must have member initializer!");
1552
1553 // non-static data member initializers.
1554 FieldDecl *Field = MemberInit->getMember();
Eli Friedman5e7d9692009-11-16 23:34:11 +00001555 QualType FieldType = CGF.getContext().getCanonicalType(Field->getType());
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001556
1557 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1558 LValue LHS;
1559 if (FieldType->isReferenceType()) {
1560 // FIXME: This is really ugly; should be refactored somehow
1561 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1562 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1563 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1564 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1565 } else {
Eli Friedmanc1daba32009-11-16 22:58:01 +00001566 LHS = CGF.EmitLValueForField(ThisPtr, Field, ClassDecl->isUnion(), 0);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001567 }
Eli Friedman5e7d9692009-11-16 23:34:11 +00001568
1569 // If we are initializing an anonymous union field, drill down to the field.
1570 if (MemberInit->getAnonUnionMember()) {
1571 Field = MemberInit->getAnonUnionMember();
1572 LHS = CGF.EmitLValueForField(LHS.getAddress(), Field,
1573 /*IsUnion=*/true, 0);
1574 FieldType = Field->getType();
1575 }
1576
1577 // If the field is an array, branch based on the element type.
1578 const ConstantArrayType *Array =
1579 CGF.getContext().getAsConstantArrayType(FieldType);
1580 if (Array)
1581 FieldType = CGF.getContext().getBaseElementType(FieldType);
1582
Eli Friedmane85ef712009-11-16 23:53:01 +00001583 // We lose the constructor for anonymous union members, so handle them
1584 // explicitly.
1585 // FIXME: This is somwhat ugly.
1586 if (MemberInit->getAnonUnionMember() && FieldType->getAs<RecordType>()) {
1587 if (MemberInit->getNumArgs())
1588 CGF.EmitAggExpr(*MemberInit->arg_begin(), LHS.getAddress(),
1589 LHS.isVolatileQualified());
1590 else
1591 CGF.EmitAggregateClear(LHS.getAddress(), Field->getType());
1592 return;
1593 }
1594
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001595 if (FieldType->getAs<RecordType>()) {
Eli Friedman5e7d9692009-11-16 23:34:11 +00001596 assert(MemberInit->getConstructor() &&
1597 "EmitCtorPrologue - no constructor to initialize member");
1598 if (Array) {
1599 const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1600 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1601 llvm::Value *BaseAddrPtr =
1602 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1603 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
Anders Carlsson3a202f62009-11-24 18:43:52 +00001604 Array, BaseAddrPtr,
1605 MemberInit->const_arg_begin(),
1606 MemberInit->const_arg_end());
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001607 }
Eli Friedman5e7d9692009-11-16 23:34:11 +00001608 else
1609 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1610 Ctor_Complete, LHS.getAddress(),
1611 MemberInit->const_arg_begin(),
1612 MemberInit->const_arg_end());
1613 return;
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001614 }
1615
1616 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1617 Expr *RhsExpr = *MemberInit->arg_begin();
1618 RValue RHS;
Eli Friedmane85ef712009-11-16 23:53:01 +00001619 if (FieldType->isReferenceType()) {
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001620 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1621 /*IsInitializer=*/true);
Fariborz Jahanian03f62ed2009-11-11 17:55:25 +00001622 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
Eli Friedmane85ef712009-11-16 23:53:01 +00001623 } else if (Array) {
1624 CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType());
1625 } else if (!CGF.hasAggregateLLVMType(RhsExpr->getType())) {
1626 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
1627 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1628 } else if (RhsExpr->getType()->isAnyComplexType()) {
1629 CGF.EmitComplexExprIntoAddr(RhsExpr, LHS.getAddress(),
1630 LHS.isVolatileQualified());
1631 } else {
1632 // Handle member function pointers; other aggregates shouldn't get this far.
1633 CGF.EmitAggExpr(RhsExpr, LHS.getAddress(), LHS.isVolatileQualified());
1634 }
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001635}
1636
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001637/// EmitCtorPrologue - This routine generates necessary code to initialize
1638/// base classes and non-static data members belonging to this constructor.
Anders Carlsson6b8b4b42009-09-01 18:33:46 +00001639/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonddf57d32009-09-14 05:32:02 +00001640void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1641 CXXCtorType CtorType) {
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001642 const CXXRecordDecl *ClassDecl = CD->getParent();
1643
Mike Stump6b2556f2009-08-06 13:41:24 +00001644 // FIXME: Add vbase initialization
Mike Stumpbc78a722009-07-31 18:25:34 +00001645 llvm::Value *LoadOfThis = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001646
Fariborz Jahaniandedf1e42009-07-25 21:12:28 +00001647 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001648 E = CD->init_end();
1649 B != E; ++B) {
1650 CXXBaseOrMemberInitializer *Member = (*B);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001651
Anders Carlsson5852b132009-11-06 04:11:09 +00001652 assert(LiveTemporaries.empty() &&
1653 "Should not have any live temporaries at initializer start!");
1654
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001655 if (Member->isBaseInitializer())
1656 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1657 else
1658 EmitMemberInitializer(*this, ClassDecl, Member);
Anders Carlsson5852b132009-11-06 04:11:09 +00001659
1660 // Pop any live temporaries that the initializers might have pushed.
1661 while (!LiveTemporaries.empty())
1662 PopCXXTemporary();
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001663 }
Mike Stumpbc78a722009-07-31 18:25:34 +00001664
1665 // Initialize the vtable pointer
Mike Stumpa19718a2009-08-05 22:59:44 +00001666 if (ClassDecl->isDynamicClass()) {
Mike Stumpbc78a722009-07-31 18:25:34 +00001667 if (!LoadOfThis)
1668 LoadOfThis = LoadCXXThis();
1669 llvm::Value *VtableField;
1670 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson41a75022009-08-13 21:57:51 +00001671 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpbc78a722009-07-31 18:25:34 +00001672 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1673 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +00001674 llvm::Value *vtable = CGM.getVtableInfo().getVtable(ClassDecl);
Mike Stumpbc78a722009-07-31 18:25:34 +00001675 Builder.CreateStore(vtable, VtableField);
1676 }
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001677}
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001678
1679/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump11289f42009-09-09 15:08:12 +00001680/// destructor. This is to call destructors on members and base classes
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001681/// in reverse order of their construction.
Anders Carlsson6b8b4b42009-09-01 18:33:46 +00001682/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonddf57d32009-09-14 05:32:02 +00001683void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1684 CXXDtorType DtorType) {
Anders Carlssondee9a302009-11-17 04:44:12 +00001685 assert(!DD->isTrivial() &&
1686 "Should not emit dtor epilogue for trivial dtor!");
Mike Stump11289f42009-09-09 15:08:12 +00001687
Anders Carlssondee9a302009-11-17 04:44:12 +00001688 const CXXRecordDecl *ClassDecl = DD->getParent();
Mike Stump11289f42009-09-09 15:08:12 +00001689
Anders Carlssondee9a302009-11-17 04:44:12 +00001690 // Collect the fields.
1691 llvm::SmallVector<const FieldDecl *, 16> FieldDecls;
1692 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1693 E = ClassDecl->field_end(); I != E; ++I) {
1694 const FieldDecl *Field = *I;
1695
1696 QualType FieldType = getContext().getCanonicalType(Field->getType());
1697 FieldType = getContext().getBaseElementType(FieldType);
1698
1699 const RecordType *RT = FieldType->getAs<RecordType>();
1700 if (!RT)
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001701 continue;
Anders Carlssondee9a302009-11-17 04:44:12 +00001702
1703 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1704 if (FieldClassDecl->hasTrivialDestructor())
1705 continue;
1706
1707 FieldDecls.push_back(Field);
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001708 }
Anders Carlssond7872042009-11-15 23:03:25 +00001709
Anders Carlssondee9a302009-11-17 04:44:12 +00001710 // Now destroy the fields.
1711 for (size_t i = FieldDecls.size(); i > 0; --i) {
1712 const FieldDecl *Field = FieldDecls[i - 1];
1713
1714 QualType FieldType = Field->getType();
1715 const ConstantArrayType *Array =
1716 getContext().getAsConstantArrayType(FieldType);
1717 if (Array)
1718 FieldType = getContext().getBaseElementType(FieldType);
1719
1720 const RecordType *RT = FieldType->getAs<RecordType>();
1721 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1722
1723 llvm::Value *ThisPtr = LoadCXXThis();
1724
1725 LValue LHS = EmitLValueForField(ThisPtr, Field,
1726 /*isUnion=*/false,
1727 // FIXME: Qualifiers?
1728 /*CVRQualifiers=*/0);
1729 if (Array) {
1730 const llvm::Type *BasePtr = ConvertType(FieldType);
1731 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1732 llvm::Value *BaseAddrPtr =
1733 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1734 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
1735 Array, BaseAddrPtr);
1736 } else
1737 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1738 Dtor_Complete, LHS.getAddress());
1739 }
1740
1741 // Destroy non-virtual bases.
1742 for (CXXRecordDecl::reverse_base_class_const_iterator I =
1743 ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) {
1744 const CXXBaseSpecifier &Base = *I;
1745
1746 // Ignore virtual bases.
1747 if (Base.isVirtual())
1748 continue;
1749
1750 CXXRecordDecl *BaseClassDecl
1751 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1752
1753 // Ignore trivial destructors.
1754 if (BaseClassDecl->hasTrivialDestructor())
1755 continue;
1756
Anders Carlsson8c793172009-11-23 17:57:54 +00001757 llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(),
1758 ClassDecl, BaseClassDecl,
1759 /*NullCheckValue=*/false);
Anders Carlssondee9a302009-11-17 04:44:12 +00001760 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1761 Dtor_Base, V);
1762 }
1763
1764 // If we're emitting a base destructor, we don't want to emit calls to the
1765 // virtual bases.
1766 if (DtorType == Dtor_Base)
1767 return;
1768
1769 // FIXME: Handle virtual bases.
1770 for (CXXRecordDecl::reverse_base_class_const_iterator I =
1771 ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); I != E; ++I) {
1772 assert(false && "FIXME: Handle virtual bases.");
1773 }
1774
1775 // If we have a deleting destructor, emit a call to the delete operator.
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001776 if (DtorType == Dtor_Deleting)
1777 EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(),
1778 getContext().getTagDeclType(ClassDecl));
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001779}
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001780
Anders Carlssonddf57d32009-09-14 05:32:02 +00001781void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1782 CXXDtorType DtorType,
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001783 llvm::Function *Fn,
1784 const FunctionArgList &Args) {
Anders Carlssondee9a302009-11-17 04:44:12 +00001785 assert(!Dtor->getParent()->hasUserDeclaredDestructor() &&
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001786 "SynthesizeDefaultDestructor - destructor has user declaration");
Mike Stump11289f42009-09-09 15:08:12 +00001787
Anders Carlssonddf57d32009-09-14 05:32:02 +00001788 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1789 SourceLocation());
Anders Carlssondee9a302009-11-17 04:44:12 +00001790
Anders Carlssonddf57d32009-09-14 05:32:02 +00001791 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001792 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001793}