blob: 636cc57b2a505c94c6ac753abddef0dc20cce563 [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
John McCall9dd450b2009-09-21 23:43:11 +0000199 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +0000200
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000201 CallArgList Args;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000203 // Push the this ptr.
204 Args.push_back(std::make_pair(RValue::get(This),
205 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +0000206
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000207 // And the rest of the call args
208 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000209
John McCall9dd450b2009-09-21 23:43:11 +0000210 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000211 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
212 Callee, Args, MD);
213}
214
Anders Carlssond7432df2009-10-12 19:41:04 +0000215/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
216/// expr can be devirtualized.
217static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
218 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
219 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
220 // This is a record decl. We know the type and can devirtualize it.
221 return VD->getType()->isRecordType();
222 }
Anders Carlssonb61301f2009-10-12 19:45:47 +0000223
224 return false;
Anders Carlssond7432df2009-10-12 19:41:04 +0000225 }
226
Anders Carlssona1b54fd2009-10-12 19:59:15 +0000227 // We can always devirtualize calls on temporary object expressions.
Anders Carlssonb61301f2009-10-12 19:45:47 +0000228 if (isa<CXXTemporaryObjectExpr>(Base))
229 return true;
230
Anders Carlssona1b54fd2009-10-12 19:59:15 +0000231 // And calls on bound temporaries.
232 if (isa<CXXBindTemporaryExpr>(Base))
233 return true;
234
Anders Carlsson2a017092009-10-12 19:51:33 +0000235 // Check if this is a call expr that returns a record type.
236 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
237 return CE->getCallReturnType()->isRecordType();
238
Anders Carlssond7432df2009-10-12 19:41:04 +0000239 // We can't devirtualize the call.
240 return false;
241}
242
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000243RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000244 if (isa<BinaryOperator>(CE->getCallee()))
245 return EmitCXXMemberPointerCallExpr(CE);
246
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000247 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
248 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000249
Anders Carlsson8f4fd602009-09-29 03:54:11 +0000250 if (MD->isStatic()) {
251 // The method is static, emit it as we would a regular call.
252 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
253 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
254 CE->arg_begin(), CE->arg_end(), 0);
255
256 }
257
John McCall9dd450b2009-09-21 23:43:11 +0000258 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumpa523b2d2009-07-30 21:47:44 +0000259
Mike Stump11289f42009-09-09 15:08:12 +0000260 const llvm::Type *Ty =
261 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlsson03a409f2009-04-08 20:31:57 +0000262 FPT->isVariadic());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000263 llvm::Value *This;
Mike Stump11289f42009-09-09 15:08:12 +0000264
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000265 if (ME->isArrow())
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000266 This = EmitScalarExpr(ME->getBase());
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000267 else {
268 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000269 This = BaseLV.getAddress();
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000270 }
Mike Stumpa5588bf2009-08-26 20:46:33 +0000271
Eli Friedmanffc066f2009-11-26 07:45:48 +0000272 if (MD->isCopyAssignment() && MD->isTrivial()) {
273 // We don't like to generate the trivial copy assignment operator when
274 // it isn't necessary; just produce the proper effect here.
275 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
276 EmitAggregateCopy(This, RHS, CE->getType());
277 return RValue::get(This);
278 }
279
Douglas Gregorc1905232009-08-26 22:36:53 +0000280 // C++ [class.virtual]p12:
Mike Stump11289f42009-09-09 15:08:12 +0000281 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorc1905232009-08-26 22:36:53 +0000282 // virtual call mechanism.
Anders Carlssonb5296552009-10-11 23:55:52 +0000283 //
284 // We also don't emit a virtual call if the base expression has a record type
285 // because then we know what the type is.
Mike Stumpa5588bf2009-08-26 20:46:33 +0000286 llvm::Value *Callee;
Eli Friedmane6ce3542009-11-16 05:31:29 +0000287 if (const CXXDestructorDecl *Destructor
288 = dyn_cast<CXXDestructorDecl>(MD)) {
Eli Friedman84a7e342009-11-26 07:40:08 +0000289 if (Destructor->isTrivial())
290 return RValue::get(0);
Eli Friedmane6ce3542009-11-16 05:31:29 +0000291 if (MD->isVirtual() && !ME->hasQualifier() &&
292 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
293 Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty);
294 } else {
295 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
296 }
297 } else if (MD->isVirtual() && !ME->hasQualifier() &&
298 !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
299 Callee = BuildVirtualCall(MD, This, Ty);
300 } else {
Anders Carlssonecf9bf02009-09-10 23:43:36 +0000301 Callee = CGM.GetAddrOfFunction(MD, Ty);
Eli Friedmane6ce3542009-11-16 05:31:29 +0000302 }
Mike Stump11289f42009-09-09 15:08:12 +0000303
304 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000305 CE->arg_begin(), CE->arg_end());
Anders Carlssone5fd6f22009-04-03 22:50:24 +0000306}
Anders Carlssona5d077d2009-04-14 16:58:56 +0000307
Mike Stump11289f42009-09-09 15:08:12 +0000308RValue
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000309CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
310 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000311 const Expr *BaseExpr = BO->getLHS();
312 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000313
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000314 const MemberPointerType *MPT =
315 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000316 const FunctionProtoType *FPT =
317 MPT->getPointeeType()->getAs<FunctionProtoType>();
318 const CXXRecordDecl *RD =
Douglas Gregor615ac672009-11-04 16:49:01 +0000319 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000320
321 const llvm::FunctionType *FTy =
322 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
323 FPT->isVariadic());
324
325 const llvm::Type *Int8PtrTy =
326 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
327
328 // Get the member function pointer.
329 llvm::Value *MemFnPtr =
Anders Carlsson6bfee8f2009-10-13 17:41:28 +0000330 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
331 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000332
333 // Emit the 'this' pointer.
334 llvm::Value *This;
335
336 if (BO->getOpcode() == BinaryOperator::PtrMemI)
337 This = EmitScalarExpr(BaseExpr);
338 else
339 This = EmitLValue(BaseExpr).getAddress();
340
341 // Adjust it.
342 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
343 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
344
345 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
346 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
347
348 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
349
350 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
351
352 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
353
354 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
355
356 // If the LSB in the function pointer is 1, the function pointer points to
357 // a virtual function.
358 llvm::Value *IsVirtual
359 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
360 "and");
361
362 IsVirtual = Builder.CreateTrunc(IsVirtual,
363 llvm::Type::getInt1Ty(VMContext));
364
365 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
366 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
367 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
368
369 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
370 EmitBlock(FnVirtual);
371
372 const llvm::Type *VTableTy =
373 FTy->getPointerTo()->getPointerTo()->getPointerTo();
374
375 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
376 VTable = Builder.CreateLoad(VTable);
377
378 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
379
380 // Since the function pointer is 1 plus the virtual table offset, we
381 // subtract 1 by using a GEP.
Mike Stump0d479e62009-10-09 01:25:47 +0000382 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000383
384 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
385
386 EmitBranch(FnEnd);
387 EmitBlock(FnNonVirtual);
388
389 // If the function is not virtual, just load the pointer.
390 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
391 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
392
393 EmitBlock(FnEnd);
394
395 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
396 Callee->reserveOperandSpace(2);
397 Callee->addIncoming(VirtualFn, FnVirtual);
398 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
399
400 CallArgList Args;
401
402 QualType ThisType =
403 getContext().getPointerType(getContext().getTagDeclType(RD));
404
405 // Push the this ptr.
406 Args.push_back(std::make_pair(RValue::get(This), ThisType));
407
408 // And the rest of the call args
409 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
410 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
411 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
412 Callee, Args, 0);
413}
414
415RValue
Anders Carlsson4034a952009-05-27 04:18:27 +0000416CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
417 const CXXMethodDecl *MD) {
Mike Stump11289f42009-09-09 15:08:12 +0000418 assert(MD->isInstance() &&
Anders Carlsson4034a952009-05-27 04:18:27 +0000419 "Trying to emit a member call expr on a static method!");
Mike Stump11289f42009-09-09 15:08:12 +0000420
Fariborz Jahanian4985b332009-08-13 21:09:41 +0000421 if (MD->isCopyAssignment()) {
422 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
423 if (ClassDecl->hasTrivialCopyAssignment()) {
424 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
425 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
426 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
427 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
428 QualType Ty = E->getType();
429 EmitAggregateCopy(This, Src, Ty);
430 return RValue::get(This);
431 }
432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
John McCall9dd450b2009-09-21 23:43:11 +0000434 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +0000435 const llvm::Type *Ty =
436 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stump5a522352009-09-04 18:27:16 +0000437 FPT->isVariadic());
Mike Stump11289f42009-09-09 15:08:12 +0000438
Anders Carlsson4034a952009-05-27 04:18:27 +0000439 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump11289f42009-09-09 15:08:12 +0000440
Eli Friedmane6ce3542009-11-16 05:31:29 +0000441 llvm::Value *Callee;
442 if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
443 Callee = BuildVirtualCall(MD, This, Ty);
444 else
445 Callee = CGM.GetAddrOfFunction(MD, Ty);
446
Anders Carlsson4034a952009-05-27 04:18:27 +0000447 return EmitCXXMemberCall(MD, Callee, This,
448 E->arg_begin() + 1, E->arg_end());
449}
450
Anders Carlssona5d077d2009-04-14 16:58:56 +0000451llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump11289f42009-09-09 15:08:12 +0000452 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlssona5d077d2009-04-14 16:58:56 +0000453 "Must be in a C++ member function decl to load 'this'");
454 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
455 "Must be in a C++ member function decl to load 'this'");
Mike Stump11289f42009-09-09 15:08:12 +0000456
Anders Carlssona5d077d2009-04-14 16:58:56 +0000457 // FIXME: What if we're inside a block?
Mike Stump18bb9282009-05-16 07:57:57 +0000458 // ans: See how CodeGenFunction::LoadObjCSelf() uses
459 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlssona5d077d2009-04-14 16:58:56 +0000460 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
461}
Anders Carlssonf7475242009-04-15 15:55:24 +0000462
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000463/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
464/// for-loop to call the default constructor on individual members of the
Anders Carlssond49844b2009-09-23 02:45:36 +0000465/// array.
466/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
467/// array type and 'ArrayPtr' points to the beginning fo the array.
468/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000469void
470CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson3a202f62009-11-24 18:43:52 +0000471 const ConstantArrayType *ArrayTy,
472 llvm::Value *ArrayPtr,
473 CallExpr::const_arg_iterator ArgBeg,
474 CallExpr::const_arg_iterator ArgEnd) {
475
Anders Carlssond49844b2009-09-23 02:45:36 +0000476 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
477 llvm::Value * NumElements =
478 llvm::ConstantInt::get(SizeTy,
479 getContext().getConstantArrayElementCount(ArrayTy));
480
Anders Carlsson3a202f62009-11-24 18:43:52 +0000481 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd);
Anders Carlssond49844b2009-09-23 02:45:36 +0000482}
483
484void
485CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson3a202f62009-11-24 18:43:52 +0000486 llvm::Value *NumElements,
487 llvm::Value *ArrayPtr,
488 CallExpr::const_arg_iterator ArgBeg,
489 CallExpr::const_arg_iterator ArgEnd) {
Anders Carlssond49844b2009-09-23 02:45:36 +0000490 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump11289f42009-09-09 15:08:12 +0000491
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000492 // Create a temporary for the loop index and initialize it with 0.
Anders Carlssond49844b2009-09-23 02:45:36 +0000493 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
494 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
Daniel Dunbardacbe6b2009-11-29 21:11:41 +0000495 Builder.CreateStore(Zero, IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000496
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000497 // Start the loop with a block that tests the condition.
498 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
499 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +0000500
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000501 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000502
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000503 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump11289f42009-09-09 15:08:12 +0000504
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000505 // Generate: if (loop-index < number-of-elements fall to the loop body,
506 // otherwise, go to the block after the for-loop.
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000507 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlssond49844b2009-09-23 02:45:36 +0000508 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000509 // If the condition is true, execute the body.
510 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +0000511
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000512 EmitBlock(ForBody);
Mike Stump11289f42009-09-09 15:08:12 +0000513
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000514 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000515 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahaniandd46eb72009-08-20 01:01:06 +0000516 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlssond49844b2009-09-23 02:45:36 +0000517 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
518 "arrayidx");
Mike Stump11289f42009-09-09 15:08:12 +0000519
Anders Carlsson3a202f62009-11-24 18:43:52 +0000520 // C++ [class.temporary]p4:
521 // There are two contexts in which temporaries are destroyed at a different
522 // point than the end of the full- expression. The first context is when a
523 // default constructor is called to initialize an element of an array.
524 // If the constructor has one or more default arguments, the destruction of
525 // every temporary created in a default argument expression is sequenced
526 // before the construction of the next array element, if any.
527
528 // Keep track of the current number of live temporaries.
529 unsigned OldNumLiveTemporaries = LiveTemporaries.size();
530
531 EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd);
532
533 // Pop temporaries.
534 while (LiveTemporaries.size() > OldNumLiveTemporaries)
535 PopCXXTemporary();
536
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000537 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000538
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000539 // Emit the increment of the loop counter.
Anders Carlssond49844b2009-09-23 02:45:36 +0000540 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000541 Counter = Builder.CreateLoad(IndexPtr);
542 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
Daniel Dunbardacbe6b2009-11-29 21:11:41 +0000543 Builder.CreateStore(NextVal, IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000544
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000545 // Finally, branch back up to the condition for the next iteration.
546 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000547
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000548 // Emit the fall-through block.
549 EmitBlock(AfterFor, true);
Fariborz Jahanian431c8832009-08-19 20:55:16 +0000550}
551
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000552/// EmitCXXAggrDestructorCall - calls the default destructor on array
553/// elements in reverse order of construction.
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000554void
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000555CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
556 const ArrayType *Array,
557 llvm::Value *This) {
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000558 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
559 assert(CA && "Do we support VLA for destruction ?");
Fariborz Jahanian6814eaa2009-11-13 19:27:47 +0000560 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
561 llvm::Value* ElementCountPtr =
562 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
563 EmitCXXAggrDestructorCall(D, ElementCountPtr, This);
564}
565
566/// EmitCXXAggrDestructorCall - calls the default destructor on array
567/// elements in reverse order of construction.
568void
569CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
570 llvm::Value *UpperCount,
571 llvm::Value *This) {
Mike Stump11289f42009-09-09 15:08:12 +0000572 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000573 1);
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000574 // Create a temporary for the loop index and initialize it with count of
575 // array elements.
576 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
577 "loop.index");
578 // Index = ElementCount;
Daniel Dunbardacbe6b2009-11-29 21:11:41 +0000579 Builder.CreateStore(UpperCount, IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000580
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000581 // Start the loop with a block that tests the condition.
582 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
583 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +0000584
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000585 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000586
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000587 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump11289f42009-09-09 15:08:12 +0000588
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000589 // Generate: if (loop-index != 0 fall to the loop body,
590 // otherwise, go to the block after the for-loop.
Mike Stump11289f42009-09-09 15:08:12 +0000591 llvm::Value* zeroConstant =
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000592 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
593 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
594 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
595 "isne");
596 // If the condition is true, execute the body.
597 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +0000598
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000599 EmitBlock(ForBody);
Mike Stump11289f42009-09-09 15:08:12 +0000600
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000601 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
602 // Inside the loop body, emit the constructor call on the array element.
603 Counter = Builder.CreateLoad(IndexPtr);
604 Counter = Builder.CreateSub(Counter, One);
605 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
Fariborz Jahanianbe641492009-11-30 22:07:18 +0000606 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000608 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000609
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000610 // Emit the decrement of the loop counter.
611 Counter = Builder.CreateLoad(IndexPtr);
612 Counter = Builder.CreateSub(Counter, One, "dec");
Daniel Dunbardacbe6b2009-11-29 21:11:41 +0000613 Builder.CreateStore(Counter, IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000615 // Finally, branch back up to the condition for the next iteration.
616 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000617
Fariborz Jahanian1a606ab2009-08-20 23:02:58 +0000618 // Emit the fall-through block.
619 EmitBlock(AfterFor, true);
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000620}
621
Mike Stumpea950e22009-11-18 18:57:56 +0000622/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
623/// invoked, calls the default destructor on array elements in reverse order of
Fariborz Jahanian1254a092009-11-10 19:24:06 +0000624/// construction.
625llvm::Constant *
626CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
627 const ArrayType *Array,
628 llvm::Value *This) {
629 static int UniqueCount;
630 FunctionArgList Args;
631 ImplicitParamDecl *Dst =
632 ImplicitParamDecl::Create(getContext(), 0,
633 SourceLocation(), 0,
634 getContext().getPointerType(getContext().VoidTy));
635 Args.push_back(std::make_pair(Dst, Dst->getType()));
636
637 llvm::SmallString<16> Name;
638 llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueCount);
639 QualType R = getContext().VoidTy;
640 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
641 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
642 llvm::Function *Fn =
643 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
644 Name.c_str(),
645 &CGM.getModule());
646 IdentifierInfo *II
647 = &CGM.getContext().Idents.get(Name.c_str());
648 FunctionDecl *FD = FunctionDecl::Create(getContext(),
649 getContext().getTranslationUnitDecl(),
650 SourceLocation(), II, R, 0,
651 FunctionDecl::Static,
652 false, true);
653 StartFunction(FD, R, Fn, Args, SourceLocation());
654 QualType BaseElementTy = getContext().getBaseElementType(Array);
655 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
656 BasePtr = llvm::PointerType::getUnqual(BasePtr);
657 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
658 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
659 FinishFunction();
660 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
661 0);
662 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
663 return m;
664}
665
Fariborz Jahanian9c837202009-08-20 20:54:15 +0000666void
Mike Stump11289f42009-09-09 15:08:12 +0000667CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
668 CXXCtorType Type,
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000669 llvm::Value *This,
670 CallExpr::const_arg_iterator ArgBeg,
671 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian42af5ba2009-08-14 20:11:43 +0000672 if (D->isCopyConstructor(getContext())) {
673 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
674 if (ClassDecl->hasTrivialCopyConstructor()) {
675 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
676 "EmitCXXConstructorCall - user declared copy constructor");
677 const Expr *E = (*ArgBeg);
678 QualType Ty = E->getType();
679 llvm::Value *Src = EmitLValue(E).getAddress();
680 EmitAggregateCopy(This, Src, Ty);
681 return;
682 }
Eli Friedman84a7e342009-11-26 07:40:08 +0000683 } else if (D->isTrivial()) {
684 // FIXME: Track down why we're trying to generate calls to the trivial
685 // default constructor!
686 return;
Fariborz Jahanian42af5ba2009-08-14 20:11:43 +0000687 }
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) {
Fariborz Jahanianbe641492009-11-30 22:07:18 +0000697 if (D->isVirtual()) {
698 const llvm::Type *Ty =
699 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(D),
700 /*isVariadic=*/false);
701
702 llvm::Value *Callee = BuildVirtualCall(D, Dtor_Deleting, This, Ty);
703 EmitCXXMemberCall(D, Callee, This, 0, 0);
704 return;
705 }
Anders Carlsson0a637412009-05-29 21:03:38 +0000706 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000707
Anders Carlsson0a637412009-05-29 21:03:38 +0000708 EmitCXXMemberCall(D, Callee, This, 0, 0);
709}
710
Mike Stump11289f42009-09-09 15:08:12 +0000711void
712CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson1619a5042009-05-03 17:47:16 +0000713 const CXXConstructExpr *E) {
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000714 assert(Dest && "Must have a destination!");
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000715 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000716 const ConstantArrayType *Array =
717 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000718 // For a copy constructor, even if it is trivial, must fall thru so
719 // its argument is code-gen'ed.
720 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000721 QualType InitType = E->getType();
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000722 if (Array)
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000723 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000724 const CXXRecordDecl *RD =
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000725 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000726 if (RD->hasTrivialConstructor())
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000727 return;
Fariborz Jahanianda21efb2009-10-16 19:20:59 +0000728 }
Mike Stump11289f42009-09-09 15:08:12 +0000729 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanianeb869762009-08-06 01:02:49 +0000730 // its first argument instead.
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000731 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Anders Carlsson78cfaa92009-11-13 04:34:45 +0000732 const Expr *Arg = E->getArg(0);
733
734 if (const CXXBindTemporaryExpr *BindExpr =
735 dyn_cast<CXXBindTemporaryExpr>(Arg))
736 Arg = BindExpr->getSubExpr();
737
738 EmitAggExpr(Arg, Dest, false);
Fariborz Jahanian00130932009-08-06 19:12:38 +0000739 return;
Fariborz Jahanianeb869762009-08-06 01:02:49 +0000740 }
Fariborz Jahanian29baa2b2009-10-28 21:07:28 +0000741 if (Array) {
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000742 QualType BaseElementTy = getContext().getBaseElementType(Array);
743 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
744 BasePtr = llvm::PointerType::getUnqual(BasePtr);
745 llvm::Value *BaseAddrPtr =
746 Builder.CreateBitCast(Dest, BasePtr);
Anders Carlsson3a202f62009-11-24 18:43:52 +0000747 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
748 E->arg_begin(), E->arg_end());
Fariborz Jahanianf1639ff2009-10-28 20:55:41 +0000749 }
750 else
751 // Call the constructor.
752 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
753 E->arg_begin(), E->arg_end());
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000754}
755
Anders Carlssonf7475242009-04-15 15:55:24 +0000756void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlssondae1abc2009-05-05 04:44:02 +0000757 EmitGlobal(GlobalDecl(D, Ctor_Complete));
758 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlssonf7475242009-04-15 15:55:24 +0000759}
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000760
Mike Stump11289f42009-09-09 15:08:12 +0000761void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000762 CXXCtorType Type) {
Mike Stump11289f42009-09-09 15:08:12 +0000763
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000764 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000765
Anders Carlsson73fcc952009-09-11 00:07:24 +0000766 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump11289f42009-09-09 15:08:12 +0000767
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000768 SetFunctionDefinitionAttributes(D, Fn);
769 SetLLVMFunctionAttributesForDefinition(D, Fn);
770}
771
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000772llvm::Function *
Mike Stump11289f42009-09-09 15:08:12 +0000773CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000774 CXXCtorType Type) {
Fariborz Jahanianc2d71b52009-11-06 18:47:57 +0000775 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000776 const llvm::FunctionType *FTy =
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000777 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanianc2d71b52009-11-06 18:47:57 +0000778 FPT->isVariadic());
Mike Stump11289f42009-09-09 15:08:12 +0000779
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000780 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnere0be0df2009-05-12 21:21:08 +0000781 return cast<llvm::Function>(
782 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000783}
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000784
Mike Stump11289f42009-09-09 15:08:12 +0000785const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000786 CXXCtorType Type) {
787 llvm::SmallString<256> Name;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000788 getMangleContext().mangleCXXCtor(D, Type, Name);
Mike Stump11289f42009-09-09 15:08:12 +0000789
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000790 Name += '\0';
791 return UniqueMangledName(Name.begin(), Name.end());
792}
793
794void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Eli Friedmanb572c922009-11-14 04:19:37 +0000795 if (D->isVirtual())
Eli Friedman250534c2009-11-27 01:42:12 +0000796 EmitGlobalDefinition(GlobalDecl(D, Dtor_Deleting));
797 EmitGlobalDefinition(GlobalDecl(D, Dtor_Complete));
798 EmitGlobalDefinition(GlobalDecl(D, Dtor_Base));
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000799}
800
Mike Stump11289f42009-09-09 15:08:12 +0000801void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000802 CXXDtorType Type) {
803 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump11289f42009-09-09 15:08:12 +0000804
Anders Carlsson73fcc952009-09-11 00:07:24 +0000805 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump11289f42009-09-09 15:08:12 +0000806
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000807 SetFunctionDefinitionAttributes(D, Fn);
808 SetLLVMFunctionAttributesForDefinition(D, Fn);
809}
810
811llvm::Function *
Mike Stump11289f42009-09-09 15:08:12 +0000812CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000813 CXXDtorType Type) {
814 const llvm::FunctionType *FTy =
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000815 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump11289f42009-09-09 15:08:12 +0000816
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000817 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnere0be0df2009-05-12 21:21:08 +0000818 return cast<llvm::Function>(
819 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000820}
821
Mike Stump11289f42009-09-09 15:08:12 +0000822const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000823 CXXDtorType Type) {
824 llvm::SmallString<256> Name;
Daniel Dunbare128dd12009-11-21 09:06:22 +0000825 getMangleContext().mangleCXXDtor(D, Type, Name);
Mike Stump11289f42009-09-09 15:08:12 +0000826
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000827 Name += '\0';
828 return UniqueMangledName(Name.begin(), Name.end());
829}
Fariborz Jahanian83381cc2009-07-20 23:18:55 +0000830
Anders Carlssonc7785402009-11-26 02:32:05 +0000831llvm::Constant *
Eli Friedman551fe842009-12-03 04:27:05 +0000832CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
Anders Carlssonc7785402009-11-26 02:32:05 +0000833 bool Extern,
834 const ThunkAdjustment &ThisAdjustment) {
Mike Stumpe2d4a2c2009-12-03 03:47:56 +0000835 return GenerateCovariantThunk(Fn, GD, Extern,
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000836 CovariantThunkAdjustment(ThisAdjustment,
837 ThunkAdjustment()));
Mike Stump5a522352009-09-04 18:27:16 +0000838}
839
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000840llvm::Value *
841CodeGenFunction::DynamicTypeAdjust(llvm::Value *V,
842 const ThunkAdjustment &Adjustment) {
843 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
844
Mike Stump77738202009-11-03 16:59:27 +0000845 const llvm::Type *OrigTy = V->getType();
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000846 if (Adjustment.NonVirtual) {
Mike Stump77738202009-11-03 16:59:27 +0000847 // Do the non-virtual adjustment
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000848 V = Builder.CreateBitCast(V, Int8PtrTy);
849 V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual);
Mike Stump77738202009-11-03 16:59:27 +0000850 V = Builder.CreateBitCast(V, OrigTy);
851 }
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000852
853 if (!Adjustment.Virtual)
854 return V;
855
856 assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 &&
857 "vtable entry unaligned");
858
859 // Do the virtual this adjustment
860 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
861 const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo();
862
863 llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy);
864 V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo());
865 V = Builder.CreateLoad(V, "vtable");
866
867 llvm::Value *VTablePtr = V;
868 uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8);
869 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
870 V = Builder.CreateLoad(V);
871 V = Builder.CreateGEP(ThisVal, V);
872
873 return Builder.CreateBitCast(V, OrigTy);
Mike Stump77738202009-11-03 16:59:27 +0000874}
875
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000876llvm::Constant *
877CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
Eli Friedman551fe842009-12-03 04:27:05 +0000878 GlobalDecl GD, bool Extern,
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000879 const CovariantThunkAdjustment &Adjustment) {
Mike Stumpe2d4a2c2009-12-03 03:47:56 +0000880 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump33ccd9e2009-11-02 23:22:01 +0000881 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump80f6ac52009-09-11 23:25:56 +0000882
883 FunctionArgList Args;
884 ImplicitParamDecl *ThisDecl =
885 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
886 MD->getThisType(getContext()));
887 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
888 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
889 e = MD->param_end();
890 i != e; ++i) {
891 ParmVarDecl *D = *i;
892 Args.push_back(std::make_pair(D, D->getType()));
893 }
894 IdentifierInfo *II
895 = &CGM.getContext().Idents.get("__thunk_named_foo_");
896 FunctionDecl *FD = FunctionDecl::Create(getContext(),
897 getContext().getTranslationUnitDecl(),
Mike Stump33ccd9e2009-11-02 23:22:01 +0000898 SourceLocation(), II, ResultType, 0,
Mike Stump80f6ac52009-09-11 23:25:56 +0000899 Extern
900 ? FunctionDecl::Extern
901 : FunctionDecl::Static,
902 false, true);
Mike Stump33ccd9e2009-11-02 23:22:01 +0000903 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
904
905 // generate body
Mike Stumpf3589722009-11-03 02:12:59 +0000906 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
907 const llvm::Type *Ty =
908 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
909 FPT->isVariadic());
Eli Friedman551fe842009-12-03 04:27:05 +0000910 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
Mike Stumpe2d4a2c2009-12-03 03:47:56 +0000911
Mike Stump33ccd9e2009-11-02 23:22:01 +0000912 CallArgList CallArgs;
913
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000914 bool ShouldAdjustReturnPointer = true;
Mike Stumpf3589722009-11-03 02:12:59 +0000915 QualType ArgType = MD->getThisType(getContext());
916 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000917 if (!Adjustment.ThisAdjustment.isEmpty()) {
Mike Stump77738202009-11-03 16:59:27 +0000918 // Do the this adjustment.
Mike Stump71609a22009-11-04 00:53:51 +0000919 const llvm::Type *OrigTy = Callee->getType();
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000920 Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment);
921
922 if (!Adjustment.ReturnAdjustment.isEmpty()) {
923 const CovariantThunkAdjustment &ReturnAdjustment =
924 CovariantThunkAdjustment(ThunkAdjustment(),
925 Adjustment.ReturnAdjustment);
926
Mike Stumpe2d4a2c2009-12-03 03:47:56 +0000927 Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment);
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000928
Mike Stump71609a22009-11-04 00:53:51 +0000929 Callee = Builder.CreateBitCast(Callee, OrigTy);
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000930 ShouldAdjustReturnPointer = false;
Mike Stump71609a22009-11-04 00:53:51 +0000931 }
932 }
933
Mike Stumpf3589722009-11-03 02:12:59 +0000934 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
935
Mike Stump33ccd9e2009-11-02 23:22:01 +0000936 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
937 e = MD->param_end();
938 i != e; ++i) {
939 ParmVarDecl *D = *i;
940 QualType ArgType = D->getType();
941
942 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
Eli Friedman4039f352009-12-03 04:49:52 +0000943 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(),
944 SourceLocation());
Mike Stump33ccd9e2009-11-02 23:22:01 +0000945 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
946 }
947
Mike Stump31e1d432009-11-02 23:47:45 +0000948 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
949 Callee, CallArgs, MD);
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000950 if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) {
Mike Stumpc5507682009-11-05 06:32:02 +0000951 bool CanBeZero = !(ResultType->isReferenceType()
952 // FIXME: attr nonnull can't be zero either
953 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stump77738202009-11-03 16:59:27 +0000954 // Do the return result adjustment.
Mike Stumpc5507682009-11-05 06:32:02 +0000955 if (CanBeZero) {
956 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
957 llvm::BasicBlock *ZeroBlock = createBasicBlock();
958 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stumpb8da7a02009-11-05 06:12:26 +0000959
Mike Stumpc5507682009-11-05 06:32:02 +0000960 const llvm::Type *Ty = RV.getScalarVal()->getType();
961 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
962 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
963 NonZeroBlock, ZeroBlock);
964 EmitBlock(NonZeroBlock);
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000965 llvm::Value *NZ =
966 DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment);
Mike Stumpc5507682009-11-05 06:32:02 +0000967 EmitBranch(ContBlock);
968 EmitBlock(ZeroBlock);
969 llvm::Value *Z = RV.getScalarVal();
970 EmitBlock(ContBlock);
971 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
972 RVOrZero->reserveOperandSpace(2);
973 RVOrZero->addIncoming(NZ, NonZeroBlock);
974 RVOrZero->addIncoming(Z, ZeroBlock);
975 RV = RValue::get(RVOrZero);
976 } else
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000977 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(),
978 Adjustment.ReturnAdjustment));
Mike Stump33ccd9e2009-11-02 23:22:01 +0000979 }
980
Mike Stump31e1d432009-11-02 23:47:45 +0000981 if (!ResultType->isVoidType())
982 EmitReturnOfRValue(RV, ResultType);
983
Mike Stump80f6ac52009-09-11 23:25:56 +0000984 FinishFunction();
985 return Fn;
986}
987
Anders Carlssonc7785402009-11-26 02:32:05 +0000988llvm::Constant *
Eli Friedman551fe842009-12-03 04:27:05 +0000989CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern,
Anders Carlssonc7785402009-11-26 02:32:05 +0000990 const ThunkAdjustment &ThisAdjustment) {
Mike Stumpe2d4a2c2009-12-03 03:47:56 +0000991 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump5a522352009-09-04 18:27:16 +0000992 llvm::SmallString<256> OutName;
Mike Stumpe2d4a2c2009-12-03 03:47:56 +0000993 if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) {
994 getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment,
995 OutName);
996 } else
997 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
Anders Carlssonc7785402009-11-26 02:32:05 +0000998
Mike Stump5a522352009-09-04 18:27:16 +0000999 llvm::GlobalVariable::LinkageTypes linktype;
1000 linktype = llvm::GlobalValue::WeakAnyLinkage;
1001 if (!Extern)
1002 linktype = llvm::GlobalValue::InternalLinkage;
1003 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall9dd450b2009-09-21 23:43:11 +00001004 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump5a522352009-09-04 18:27:16 +00001005 const llvm::FunctionType *FTy =
1006 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1007 FPT->isVariadic());
1008
Daniel Dunbare128dd12009-11-21 09:06:22 +00001009 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump5a522352009-09-04 18:27:16 +00001010 &getModule());
Mike Stumpe2d4a2c2009-12-03 03:47:56 +00001011 CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment);
Mike Stump5a522352009-09-04 18:27:16 +00001012 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1013 return m;
1014}
1015
Anders Carlsson2f87c4f2009-11-26 03:09:37 +00001016llvm::Constant *
Mike Stumpe2d4a2c2009-12-03 03:47:56 +00001017CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern,
Anders Carlsson2f87c4f2009-11-26 03:09:37 +00001018 const CovariantThunkAdjustment &Adjustment) {
Mike Stumpe2d4a2c2009-12-03 03:47:56 +00001019 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump80f6ac52009-09-11 23:25:56 +00001020 llvm::SmallString<256> OutName;
Anders Carlsson2f87c4f2009-11-26 03:09:37 +00001021 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
Mike Stump80f6ac52009-09-11 23:25:56 +00001022 llvm::GlobalVariable::LinkageTypes linktype;
1023 linktype = llvm::GlobalValue::WeakAnyLinkage;
1024 if (!Extern)
1025 linktype = llvm::GlobalValue::InternalLinkage;
1026 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall9dd450b2009-09-21 23:43:11 +00001027 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump80f6ac52009-09-11 23:25:56 +00001028 const llvm::FunctionType *FTy =
1029 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1030 FPT->isVariadic());
1031
Daniel Dunbare128dd12009-11-21 09:06:22 +00001032 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump80f6ac52009-09-11 23:25:56 +00001033 &getModule());
Anders Carlsson2f87c4f2009-11-26 03:09:37 +00001034 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment);
Mike Stump80f6ac52009-09-11 23:25:56 +00001035 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1036 return m;
1037}
1038
Mike Stumpa5588bf2009-08-26 20:46:33 +00001039llvm::Value *
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001040CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
1041 const CXXRecordDecl *ClassDecl,
1042 const CXXRecordDecl *BaseClassDecl) {
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001043 const llvm::Type *Int8PtrTy =
1044 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
1045
1046 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
1047 Int8PtrTy->getPointerTo());
1048 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
1049
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001050 int64_t VBaseOffsetIndex =
1051 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
1052
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001053 llvm::Value *VBaseOffsetPtr =
Mike Stumpb9c9b352009-11-04 01:11:15 +00001054 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlssonc6d171e2009-10-06 22:43:30 +00001055 const llvm::Type *PtrDiffTy =
1056 ConvertType(getContext().getPointerDiffType());
1057
1058 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1059 PtrDiffTy->getPointerTo());
1060
1061 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1062
1063 return VBaseOffset;
1064}
1065
Anders Carlssonf942ee02009-11-27 20:47:55 +00001066static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex,
Anders Carlssone828c362009-11-13 04:45:41 +00001067 llvm::Value *This, const llvm::Type *Ty) {
1068 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson32bfb1c2009-10-03 14:56:57 +00001069
Anders Carlssone828c362009-11-13 04:45:41 +00001070 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
1071 Vtable = CGF.Builder.CreateLoad(Vtable);
1072
1073 llvm::Value *VFuncPtr =
1074 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
1075 return CGF.Builder.CreateLoad(VFuncPtr);
1076}
1077
1078llvm::Value *
1079CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1080 const llvm::Type *Ty) {
1081 MD = MD->getCanonicalDecl();
Anders Carlssonf942ee02009-11-27 20:47:55 +00001082 uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlssone828c362009-11-13 04:45:41 +00001083
1084 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1085}
1086
1087llvm::Value *
1088CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
1089 llvm::Value *&This, const llvm::Type *Ty) {
1090 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssonf942ee02009-11-27 20:47:55 +00001091 uint64_t VtableIndex =
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001092 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlssone828c362009-11-13 04:45:41 +00001093
1094 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpa5588bf2009-08-26 20:46:33 +00001095}
1096
Fariborz Jahanian56263842009-08-21 18:30:26 +00001097/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1098/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1099/// copy or via a copy constructor call.
Fariborz Jahanian2c73b1d2009-08-26 00:23:27 +00001100// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump11289f42009-09-09 15:08:12 +00001101void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001102 llvm::Value *Src,
1103 const ArrayType *Array,
Mike Stump11289f42009-09-09 15:08:12 +00001104 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001105 QualType Ty) {
1106 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1107 assert(CA && "VLA cannot be copied over");
1108 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump11289f42009-09-09 15:08:12 +00001109
Fariborz Jahanian56263842009-08-21 18:30:26 +00001110 // Create a temporary for the loop index and initialize it with 0.
1111 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1112 "loop.index");
Mike Stump11289f42009-09-09 15:08:12 +00001113 llvm::Value* zeroConstant =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001114 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Daniel Dunbardacbe6b2009-11-29 21:11:41 +00001115 Builder.CreateStore(zeroConstant, IndexPtr);
Fariborz Jahanian56263842009-08-21 18:30:26 +00001116 // Start the loop with a block that tests the condition.
1117 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1118 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +00001119
Fariborz Jahanian56263842009-08-21 18:30:26 +00001120 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001121
Fariborz Jahanian56263842009-08-21 18:30:26 +00001122 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1123 // Generate: if (loop-index < number-of-elements fall to the loop body,
1124 // otherwise, go to the block after the for-loop.
1125 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump11289f42009-09-09 15:08:12 +00001126 llvm::Value * NumElementsPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001127 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1128 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001129 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001130 "isless");
1131 // If the condition is true, execute the body.
1132 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +00001133
Fariborz Jahanian56263842009-08-21 18:30:26 +00001134 EmitBlock(ForBody);
1135 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1136 // Inside the loop body, emit the constructor call on the array element.
1137 Counter = Builder.CreateLoad(IndexPtr);
1138 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1139 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1140 if (BitwiseCopy)
1141 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001142 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001143 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump11289f42009-09-09 15:08:12 +00001144 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanian56263842009-08-21 18:30:26 +00001145 Ctor_Complete);
1146 CallArgList CallArgs;
1147 // Push the this (Dest) ptr.
1148 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1149 BaseCopyCtor->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001150
Fariborz Jahanian56263842009-08-21 18:30:26 +00001151 // Push the Src ptr.
1152 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stumpb9c9b352009-11-04 01:11:15 +00001153 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001154 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001155 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian56263842009-08-21 18:30:26 +00001156 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1157 Callee, CallArgs, BaseCopyCtor);
1158 }
1159 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001160
Fariborz Jahanian56263842009-08-21 18:30:26 +00001161 // Emit the increment of the loop counter.
1162 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1163 Counter = Builder.CreateLoad(IndexPtr);
1164 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
Daniel Dunbardacbe6b2009-11-29 21:11:41 +00001165 Builder.CreateStore(NextVal, IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001166
Fariborz Jahanian56263842009-08-21 18:30:26 +00001167 // Finally, branch back up to the condition for the next iteration.
1168 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001169
Fariborz Jahanian56263842009-08-21 18:30:26 +00001170 // Emit the fall-through block.
1171 EmitBlock(AfterFor, true);
1172}
1173
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001174/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump11289f42009-09-09 15:08:12 +00001175/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001176/// bitwise assignment or via a copy assignment operator function call.
1177/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump11289f42009-09-09 15:08:12 +00001178void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001179 llvm::Value *Src,
1180 const ArrayType *Array,
Mike Stump11289f42009-09-09 15:08:12 +00001181 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001182 QualType Ty) {
1183 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1184 assert(CA && "VLA cannot be asssigned");
1185 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump11289f42009-09-09 15:08:12 +00001186
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001187 // Create a temporary for the loop index and initialize it with 0.
1188 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1189 "loop.index");
Mike Stump11289f42009-09-09 15:08:12 +00001190 llvm::Value* zeroConstant =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001191 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Daniel Dunbardacbe6b2009-11-29 21:11:41 +00001192 Builder.CreateStore(zeroConstant, IndexPtr);
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001193 // Start the loop with a block that tests the condition.
1194 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1195 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump11289f42009-09-09 15:08:12 +00001196
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001197 EmitBlock(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001198
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001199 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1200 // Generate: if (loop-index < number-of-elements fall to the loop body,
1201 // otherwise, go to the block after the for-loop.
1202 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump11289f42009-09-09 15:08:12 +00001203 llvm::Value * NumElementsPtr =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001204 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1205 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001206 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001207 "isless");
1208 // If the condition is true, execute the body.
1209 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump11289f42009-09-09 15:08:12 +00001210
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001211 EmitBlock(ForBody);
1212 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1213 // Inside the loop body, emit the assignment operator call on array element.
1214 Counter = Builder.CreateLoad(IndexPtr);
1215 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1216 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1217 const CXXMethodDecl *MD = 0;
1218 if (BitwiseAssign)
1219 EmitAggregateCopy(Dest, Src, Ty);
1220 else {
1221 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1222 MD);
1223 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1224 (void)hasCopyAssign;
John McCall9dd450b2009-09-21 23:43:11 +00001225 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001226 const llvm::Type *LTy =
1227 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1228 FPT->isVariadic());
Anders Carlssonecf9bf02009-09-10 23:43:36 +00001229 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump11289f42009-09-09 15:08:12 +00001230
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001231 CallArgList CallArgs;
1232 // Push the this (Dest) ptr.
1233 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1234 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001235
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001236 // Push the Src ptr.
1237 CallArgs.push_back(std::make_pair(RValue::get(Src),
1238 MD->getParamDecl(0)->getType()));
John McCall9dd450b2009-09-21 23:43:11 +00001239 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001240 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1241 Callee, CallArgs, MD);
1242 }
1243 EmitBlock(ContinueBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001244
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001245 // Emit the increment of the loop counter.
1246 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1247 Counter = Builder.CreateLoad(IndexPtr);
1248 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
Daniel Dunbardacbe6b2009-11-29 21:11:41 +00001249 Builder.CreateStore(NextVal, IndexPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001250
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001251 // Finally, branch back up to the condition for the next iteration.
1252 EmitBranch(CondBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001253
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001254 // Emit the fall-through block.
1255 EmitBlock(AfterFor, true);
1256}
1257
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001258/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1259/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanian56263842009-08-21 18:30:26 +00001260/// or via a copy constructor call.
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001261void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001262 llvm::Value *Dest, llvm::Value *Src,
Mike Stump11289f42009-09-09 15:08:12 +00001263 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001264 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1265 if (ClassDecl) {
Anders Carlsson8c793172009-11-23 17:57:54 +00001266 Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1267 /*NullCheckValue=*/false);
1268 Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl,
1269 /*NullCheckValue=*/false);
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001270 }
1271 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1272 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001273 return;
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001274 }
Mike Stump11289f42009-09-09 15:08:12 +00001275
1276 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian7c3d7f62009-08-08 00:59:58 +00001277 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump11289f42009-09-09 15:08:12 +00001278 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001279 Ctor_Complete);
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001280 CallArgList CallArgs;
1281 // Push the this (Dest) ptr.
1282 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1283 BaseCopyCtor->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001284
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001285 // Push the Src ptr.
1286 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian2a26e352009-08-10 17:20:45 +00001287 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001288 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001289 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianb68df0b2009-08-07 23:51:33 +00001290 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1291 Callee, CallArgs, BaseCopyCtor);
1292 }
1293}
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001294
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001295/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump11289f42009-09-09 15:08:12 +00001296/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001297/// assignment of via an assignment operator call.
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001298// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001299void CodeGenFunction::EmitClassCopyAssignment(
1300 llvm::Value *Dest, llvm::Value *Src,
Mike Stump11289f42009-09-09 15:08:12 +00001301 const CXXRecordDecl *ClassDecl,
1302 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001303 QualType Ty) {
1304 if (ClassDecl) {
Anders Carlsson8c793172009-11-23 17:57:54 +00001305 Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1306 /*NullCheckValue=*/false);
1307 Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl,
1308 /*NullCheckValue=*/false);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001309 }
1310 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1311 EmitAggregateCopy(Dest, Src, Ty);
1312 return;
1313 }
Mike Stump11289f42009-09-09 15:08:12 +00001314
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001315 const CXXMethodDecl *MD = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001316 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001317 MD);
1318 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1319 (void)ConstCopyAssignOp;
1320
John McCall9dd450b2009-09-21 23:43:11 +00001321 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump11289f42009-09-09 15:08:12 +00001322 const llvm::Type *LTy =
1323 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001324 FPT->isVariadic());
Anders Carlssonecf9bf02009-09-10 23:43:36 +00001325 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump11289f42009-09-09 15:08:12 +00001326
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001327 CallArgList CallArgs;
1328 // Push the this (Dest) ptr.
1329 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1330 MD->getThisType(getContext())));
Mike Stump11289f42009-09-09 15:08:12 +00001331
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001332 // Push the Src ptr.
1333 CallArgs.push_back(std::make_pair(RValue::get(Src),
1334 MD->getParamDecl(0)->getType()));
Mike Stump11289f42009-09-09 15:08:12 +00001335 QualType ResultType =
John McCall9dd450b2009-09-21 23:43:11 +00001336 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanian9b630c32009-08-13 00:53:36 +00001337 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1338 Callee, CallArgs, MD);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001339}
1340
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001341/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump11289f42009-09-09 15:08:12 +00001342void
Anders Carlssonddf57d32009-09-14 05:32:02 +00001343CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1344 CXXCtorType Type,
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001345 llvm::Function *Fn,
1346 const FunctionArgList &Args) {
Eli Friedman84a7e342009-11-26 07:40:08 +00001347 assert(!Ctor->isTrivial() && "shouldn't need to generate trivial ctor");
Anders Carlssonddf57d32009-09-14 05:32:02 +00001348 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1349 SourceLocation());
1350 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian9d3405c2009-08-10 18:46:38 +00001351 FinishFunction();
1352}
1353
Mike Stumpb9c9b352009-11-04 01:11:15 +00001354/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1355/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump11289f42009-09-09 15:08:12 +00001356/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stumpb9c9b352009-11-04 01:11:15 +00001357/// copy of its subobjects. The order of copying is the same as the order of
1358/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001359/// Each subobject is copied in the manner appropriate to its type:
Mike Stump11289f42009-09-09 15:08:12 +00001360/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001361/// used;
Mike Stump11289f42009-09-09 15:08:12 +00001362/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001363/// appropriate to the element type;
Mike Stump11289f42009-09-09 15:08:12 +00001364/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001365/// used.
Mike Stump11289f42009-09-09 15:08:12 +00001366/// Virtual base class subobjects shall be copied only once by the
1367/// implicitly-defined copy constructor
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001368
Anders Carlssonddf57d32009-09-14 05:32:02 +00001369void
1370CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1371 CXXCtorType Type,
1372 llvm::Function *Fn,
1373 const FunctionArgList &Args) {
Anders Carlsson73fcc952009-09-11 00:07:24 +00001374 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001375 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stumpb9c9b352009-11-04 01:11:15 +00001376 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Eli Friedman84a7e342009-11-26 07:40:08 +00001377 assert(!Ctor->isTrivial() && "shouldn't need to generate trivial ctor");
Anders Carlssonddf57d32009-09-14 05:32:02 +00001378 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1379 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001380
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001381 FunctionArgList::const_iterator i = Args.begin();
1382 const VarDecl *ThisArg = i->first;
1383 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1384 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1385 const VarDecl *SrcArg = (i+1)->first;
1386 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1387 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump11289f42009-09-09 15:08:12 +00001388
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001389 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1390 Base != ClassDecl->bases_end(); ++Base) {
1391 // FIXME. copy constrution of virtual base NYI
1392 if (Base->isVirtual())
1393 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001394
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001395 CXXRecordDecl *BaseClassDecl
1396 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian24f38962009-08-08 23:32:22 +00001397 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1398 Base->getType());
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001399 }
Mike Stump11289f42009-09-09 15:08:12 +00001400
Anders Carlsson3c9beab2009-11-24 21:08:10 +00001401 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1402 E = ClassDecl->field_end(); I != E; ++I) {
1403 const FieldDecl *Field = *I;
1404
1405 QualType FieldType = getContext().getCanonicalType(Field->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001406 const ConstantArrayType *Array =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001407 getContext().getAsConstantArrayType(FieldType);
1408 if (Array)
1409 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump11289f42009-09-09 15:08:12 +00001410
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001411 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1412 CXXRecordDecl *FieldClassDecl
1413 = cast<CXXRecordDecl>(FieldClassType->getDecl());
Anders Carlsson3c9beab2009-11-24 21:08:10 +00001414 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1415 LValue RHS = EmitLValueForField(LoadOfSrc, Field, false, 0);
Fariborz Jahanian56263842009-08-21 18:30:26 +00001416 if (Array) {
1417 const llvm::Type *BasePtr = ConvertType(FieldType);
1418 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump11289f42009-09-09 15:08:12 +00001419 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001420 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump11289f42009-09-09 15:08:12 +00001421 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanian56263842009-08-21 18:30:26 +00001422 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1423 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1424 FieldClassDecl, FieldType);
1425 }
Mike Stump11289f42009-09-09 15:08:12 +00001426 else
1427 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanian56263842009-08-21 18:30:26 +00001428 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001429 continue;
1430 }
Anders Carlsson3c9beab2009-11-24 21:08:10 +00001431
1432 if (Field->getType()->isReferenceType()) {
1433 unsigned FieldIndex = CGM.getTypes().getLLVMFieldNo(Field);
1434
1435 llvm::Value *LHS = Builder.CreateStructGEP(LoadOfThis, FieldIndex,
1436 "lhs.ref");
1437
1438 llvm::Value *RHS = Builder.CreateStructGEP(LoadOfThis, FieldIndex,
1439 "rhs.ref");
1440
1441 // Load the value in RHS.
1442 RHS = Builder.CreateLoad(RHS);
1443
1444 // And store it in the LHS
1445 Builder.CreateStore(RHS, LHS);
1446
1447 continue;
1448 }
Fariborz Jahanian7cc52cf2009-08-10 18:34:26 +00001449 // Do a built-in assignment of scalar data members.
Anders Carlsson3c9beab2009-11-24 21:08:10 +00001450 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1451 LValue RHS = EmitLValueForField(LoadOfSrc, Field, false, 0);
1452
Eli Friedmanc2ef2152009-11-16 21:47:41 +00001453 if (!hasAggregateLLVMType(Field->getType())) {
1454 RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType());
1455 EmitStoreThroughLValue(RVRHS, LHS, Field->getType());
1456 } else if (Field->getType()->isAnyComplexType()) {
1457 ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(),
1458 RHS.isVolatileQualified());
1459 StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified());
1460 } else {
1461 EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType());
1462 }
Fariborz Jahanian59b32592009-08-08 00:15:41 +00001463 }
Fariborz Jahanianf6bda5d2009-08-08 19:31:03 +00001464 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001465}
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001466
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001467/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump11289f42009-09-09 15:08:12 +00001468/// Before the implicitly-declared copy assignment operator for a class is
1469/// implicitly defined, all implicitly- declared copy assignment operators for
1470/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001471/// implicitly defined. [12.8-p12]
Mike Stump11289f42009-09-09 15:08:12 +00001472/// The implicitly-defined copy assignment operator for class X performs
1473/// memberwise assignment of its subob- jects. The direct base classes of X are
1474/// assigned first, in the order of their declaration in
1475/// the base-specifier-list, and then the immediate nonstatic data members of X
1476/// are assigned, in the order in which they were declared in the class
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001477/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump11289f42009-09-09 15:08:12 +00001478/// if the subobject is of class type, the copy assignment operator for the
1479/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001480/// possible virtual overriding functions in more derived classes);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001481///
Mike Stump11289f42009-09-09 15:08:12 +00001482/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001483/// appropriate to the element type;
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001484///
Mike Stump11289f42009-09-09 15:08:12 +00001485/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001486/// used.
1487void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001488 llvm::Function *Fn,
1489 const FunctionArgList &Args) {
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001490
1491 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1492 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1493 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonddf57d32009-09-14 05:32:02 +00001494 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001495
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001496 FunctionArgList::const_iterator i = Args.begin();
1497 const VarDecl *ThisArg = i->first;
1498 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1499 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1500 const VarDecl *SrcArg = (i+1)->first;
1501 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1502 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump11289f42009-09-09 15:08:12 +00001503
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001504 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1505 Base != ClassDecl->bases_end(); ++Base) {
1506 // FIXME. copy assignment of virtual base NYI
1507 if (Base->isVirtual())
1508 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001509
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001510 CXXRecordDecl *BaseClassDecl
1511 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1512 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1513 Base->getType());
1514 }
Mike Stump11289f42009-09-09 15:08:12 +00001515
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001516 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1517 FieldEnd = ClassDecl->field_end();
1518 Field != FieldEnd; ++Field) {
1519 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001520 const ConstantArrayType *Array =
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001521 getContext().getAsConstantArrayType(FieldType);
1522 if (Array)
1523 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump11289f42009-09-09 15:08:12 +00001524
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001525 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1526 CXXRecordDecl *FieldClassDecl
1527 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1528 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1529 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001530 if (Array) {
1531 const llvm::Type *BasePtr = ConvertType(FieldType);
1532 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1533 llvm::Value *DestBaseAddrPtr =
1534 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1535 llvm::Value *SrcBaseAddrPtr =
1536 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1537 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1538 FieldClassDecl, FieldType);
1539 }
1540 else
Mike Stump11289f42009-09-09 15:08:12 +00001541 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanian8adc9732009-08-21 22:34:55 +00001542 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanianbbd5e8c2009-08-12 23:34:46 +00001543 continue;
1544 }
1545 // Do a built-in assignment of scalar data members.
1546 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1547 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1548 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1549 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian92b3f472009-08-14 00:01:54 +00001550 }
Mike Stump11289f42009-09-09 15:08:12 +00001551
Fariborz Jahanian92b3f472009-08-14 00:01:54 +00001552 // return *this;
1553 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump11289f42009-09-09 15:08:12 +00001554
Fariborz Jahaniande7d4c22009-08-12 21:14:35 +00001555 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001556}
Fariborz Jahanian40134e72009-08-07 20:22:40 +00001557
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001558static void EmitBaseInitializer(CodeGenFunction &CGF,
1559 const CXXRecordDecl *ClassDecl,
1560 CXXBaseOrMemberInitializer *BaseInit,
1561 CXXCtorType CtorType) {
1562 assert(BaseInit->isBaseInitializer() &&
1563 "Must have base initializer!");
1564
1565 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1566
1567 const Type *BaseType = BaseInit->getBaseClass();
1568 CXXRecordDecl *BaseClassDecl =
1569 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson8c793172009-11-23 17:57:54 +00001570 llvm::Value *V = CGF.GetAddressOfBaseClass(ThisPtr, ClassDecl,
1571 BaseClassDecl,
1572 /*NullCheckValue=*/false);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001573 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1574 CtorType, V,
1575 BaseInit->const_arg_begin(),
1576 BaseInit->const_arg_end());
1577}
1578
1579static void EmitMemberInitializer(CodeGenFunction &CGF,
1580 const CXXRecordDecl *ClassDecl,
1581 CXXBaseOrMemberInitializer *MemberInit) {
1582 assert(MemberInit->isMemberInitializer() &&
1583 "Must have member initializer!");
1584
1585 // non-static data member initializers.
1586 FieldDecl *Field = MemberInit->getMember();
Eli Friedman5e7d9692009-11-16 23:34:11 +00001587 QualType FieldType = CGF.getContext().getCanonicalType(Field->getType());
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001588
1589 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1590 LValue LHS;
1591 if (FieldType->isReferenceType()) {
1592 // FIXME: This is really ugly; should be refactored somehow
1593 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1594 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1595 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1596 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1597 } else {
Eli Friedmanc1daba32009-11-16 22:58:01 +00001598 LHS = CGF.EmitLValueForField(ThisPtr, Field, ClassDecl->isUnion(), 0);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001599 }
Eli Friedman5e7d9692009-11-16 23:34:11 +00001600
1601 // If we are initializing an anonymous union field, drill down to the field.
1602 if (MemberInit->getAnonUnionMember()) {
1603 Field = MemberInit->getAnonUnionMember();
1604 LHS = CGF.EmitLValueForField(LHS.getAddress(), Field,
1605 /*IsUnion=*/true, 0);
1606 FieldType = Field->getType();
1607 }
1608
1609 // If the field is an array, branch based on the element type.
1610 const ConstantArrayType *Array =
1611 CGF.getContext().getAsConstantArrayType(FieldType);
1612 if (Array)
1613 FieldType = CGF.getContext().getBaseElementType(FieldType);
1614
Eli Friedmane85ef712009-11-16 23:53:01 +00001615 // We lose the constructor for anonymous union members, so handle them
1616 // explicitly.
1617 // FIXME: This is somwhat ugly.
1618 if (MemberInit->getAnonUnionMember() && FieldType->getAs<RecordType>()) {
1619 if (MemberInit->getNumArgs())
1620 CGF.EmitAggExpr(*MemberInit->arg_begin(), LHS.getAddress(),
1621 LHS.isVolatileQualified());
1622 else
1623 CGF.EmitAggregateClear(LHS.getAddress(), Field->getType());
1624 return;
1625 }
1626
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001627 if (FieldType->getAs<RecordType>()) {
Eli Friedman5e7d9692009-11-16 23:34:11 +00001628 assert(MemberInit->getConstructor() &&
1629 "EmitCtorPrologue - no constructor to initialize member");
1630 if (Array) {
1631 const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1632 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1633 llvm::Value *BaseAddrPtr =
1634 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1635 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
Anders Carlsson3a202f62009-11-24 18:43:52 +00001636 Array, BaseAddrPtr,
1637 MemberInit->const_arg_begin(),
1638 MemberInit->const_arg_end());
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001639 }
Eli Friedman5e7d9692009-11-16 23:34:11 +00001640 else
1641 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1642 Ctor_Complete, LHS.getAddress(),
1643 MemberInit->const_arg_begin(),
1644 MemberInit->const_arg_end());
1645 return;
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001646 }
1647
1648 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1649 Expr *RhsExpr = *MemberInit->arg_begin();
1650 RValue RHS;
Eli Friedmane85ef712009-11-16 23:53:01 +00001651 if (FieldType->isReferenceType()) {
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001652 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1653 /*IsInitializer=*/true);
Fariborz Jahanian03f62ed2009-11-11 17:55:25 +00001654 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
Eli Friedmane85ef712009-11-16 23:53:01 +00001655 } else if (Array) {
1656 CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType());
1657 } else if (!CGF.hasAggregateLLVMType(RhsExpr->getType())) {
1658 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
1659 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1660 } else if (RhsExpr->getType()->isAnyComplexType()) {
1661 CGF.EmitComplexExprIntoAddr(RhsExpr, LHS.getAddress(),
1662 LHS.isVolatileQualified());
1663 } else {
1664 // Handle member function pointers; other aggregates shouldn't get this far.
1665 CGF.EmitAggExpr(RhsExpr, LHS.getAddress(), LHS.isVolatileQualified());
1666 }
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001667}
1668
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001669/// EmitCtorPrologue - This routine generates necessary code to initialize
1670/// base classes and non-static data members belonging to this constructor.
Anders Carlsson6b8b4b42009-09-01 18:33:46 +00001671/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonddf57d32009-09-14 05:32:02 +00001672void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1673 CXXCtorType CtorType) {
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001674 const CXXRecordDecl *ClassDecl = CD->getParent();
1675
Mike Stump6b2556f2009-08-06 13:41:24 +00001676 // FIXME: Add vbase initialization
Mike Stumpbc78a722009-07-31 18:25:34 +00001677 llvm::Value *LoadOfThis = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001678
Fariborz Jahaniandedf1e42009-07-25 21:12:28 +00001679 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001680 E = CD->init_end();
1681 B != E; ++B) {
1682 CXXBaseOrMemberInitializer *Member = (*B);
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001683
Anders Carlsson5852b132009-11-06 04:11:09 +00001684 assert(LiveTemporaries.empty() &&
1685 "Should not have any live temporaries at initializer start!");
1686
Anders Carlssona7cb98b2009-11-06 03:23:06 +00001687 if (Member->isBaseInitializer())
1688 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1689 else
1690 EmitMemberInitializer(*this, ClassDecl, Member);
Anders Carlsson5852b132009-11-06 04:11:09 +00001691
1692 // Pop any live temporaries that the initializers might have pushed.
1693 while (!LiveTemporaries.empty())
1694 PopCXXTemporary();
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001695 }
Mike Stumpbc78a722009-07-31 18:25:34 +00001696
1697 // Initialize the vtable pointer
Mike Stumpa19718a2009-08-05 22:59:44 +00001698 if (ClassDecl->isDynamicClass()) {
Mike Stumpbc78a722009-07-31 18:25:34 +00001699 if (!LoadOfThis)
1700 LoadOfThis = LoadCXXThis();
1701 llvm::Value *VtableField;
1702 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson41a75022009-08-13 21:57:51 +00001703 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpbc78a722009-07-31 18:25:34 +00001704 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1705 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +00001706 llvm::Value *vtable = CGM.getVtableInfo().getVtable(ClassDecl);
Mike Stumpbc78a722009-07-31 18:25:34 +00001707 Builder.CreateStore(vtable, VtableField);
1708 }
Fariborz Jahanian83381cc2009-07-20 23:18:55 +00001709}
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001710
1711/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump11289f42009-09-09 15:08:12 +00001712/// destructor. This is to call destructors on members and base classes
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001713/// in reverse order of their construction.
Anders Carlsson6b8b4b42009-09-01 18:33:46 +00001714/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonddf57d32009-09-14 05:32:02 +00001715void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1716 CXXDtorType DtorType) {
Anders Carlssondee9a302009-11-17 04:44:12 +00001717 assert(!DD->isTrivial() &&
1718 "Should not emit dtor epilogue for trivial dtor!");
Mike Stump11289f42009-09-09 15:08:12 +00001719
Anders Carlssondee9a302009-11-17 04:44:12 +00001720 const CXXRecordDecl *ClassDecl = DD->getParent();
Mike Stump11289f42009-09-09 15:08:12 +00001721
Anders Carlssondee9a302009-11-17 04:44:12 +00001722 // Collect the fields.
1723 llvm::SmallVector<const FieldDecl *, 16> FieldDecls;
1724 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1725 E = ClassDecl->field_end(); I != E; ++I) {
1726 const FieldDecl *Field = *I;
1727
1728 QualType FieldType = getContext().getCanonicalType(Field->getType());
1729 FieldType = getContext().getBaseElementType(FieldType);
1730
1731 const RecordType *RT = FieldType->getAs<RecordType>();
1732 if (!RT)
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001733 continue;
Anders Carlssondee9a302009-11-17 04:44:12 +00001734
1735 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1736 if (FieldClassDecl->hasTrivialDestructor())
1737 continue;
1738
1739 FieldDecls.push_back(Field);
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001740 }
Anders Carlssond7872042009-11-15 23:03:25 +00001741
Anders Carlssondee9a302009-11-17 04:44:12 +00001742 // Now destroy the fields.
1743 for (size_t i = FieldDecls.size(); i > 0; --i) {
1744 const FieldDecl *Field = FieldDecls[i - 1];
1745
1746 QualType FieldType = Field->getType();
1747 const ConstantArrayType *Array =
1748 getContext().getAsConstantArrayType(FieldType);
1749 if (Array)
1750 FieldType = getContext().getBaseElementType(FieldType);
1751
1752 const RecordType *RT = FieldType->getAs<RecordType>();
1753 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1754
1755 llvm::Value *ThisPtr = LoadCXXThis();
1756
1757 LValue LHS = EmitLValueForField(ThisPtr, Field,
1758 /*isUnion=*/false,
1759 // FIXME: Qualifiers?
1760 /*CVRQualifiers=*/0);
1761 if (Array) {
1762 const llvm::Type *BasePtr = ConvertType(FieldType);
1763 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1764 llvm::Value *BaseAddrPtr =
1765 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1766 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
1767 Array, BaseAddrPtr);
1768 } else
1769 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1770 Dtor_Complete, LHS.getAddress());
1771 }
1772
1773 // Destroy non-virtual bases.
1774 for (CXXRecordDecl::reverse_base_class_const_iterator I =
1775 ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) {
1776 const CXXBaseSpecifier &Base = *I;
1777
1778 // Ignore virtual bases.
1779 if (Base.isVirtual())
1780 continue;
1781
1782 CXXRecordDecl *BaseClassDecl
1783 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1784
1785 // Ignore trivial destructors.
1786 if (BaseClassDecl->hasTrivialDestructor())
1787 continue;
Fariborz Jahanianbe641492009-11-30 22:07:18 +00001788 const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext());
1789
Anders Carlsson8c793172009-11-23 17:57:54 +00001790 llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(),
1791 ClassDecl, BaseClassDecl,
1792 /*NullCheckValue=*/false);
Fariborz Jahanianbe641492009-11-30 22:07:18 +00001793 EmitCXXDestructorCall(D, Dtor_Base, V);
Anders Carlssondee9a302009-11-17 04:44:12 +00001794 }
1795
1796 // If we're emitting a base destructor, we don't want to emit calls to the
1797 // virtual bases.
1798 if (DtorType == Dtor_Base)
1799 return;
1800
Fariborz Jahanianbe641492009-11-30 22:07:18 +00001801 // Handle virtual bases.
Anders Carlssondee9a302009-11-17 04:44:12 +00001802 for (CXXRecordDecl::reverse_base_class_const_iterator I =
1803 ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); I != E; ++I) {
Fariborz Jahanianbe641492009-11-30 22:07:18 +00001804 const CXXBaseSpecifier &Base = *I;
1805 CXXRecordDecl *BaseClassDecl
1806 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1807
1808 // Ignore trivial destructors.
1809 if (BaseClassDecl->hasTrivialDestructor())
1810 continue;
1811 const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext());
1812 llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(),
1813 ClassDecl, BaseClassDecl,
1814 /*NullCheckValue=*/false);
1815 EmitCXXDestructorCall(D, Dtor_Base, V);
Anders Carlssondee9a302009-11-17 04:44:12 +00001816 }
1817
1818 // If we have a deleting destructor, emit a call to the delete operator.
Fariborz Jahanian037bcb52009-12-01 23:35:33 +00001819 if (DtorType == Dtor_Deleting) {
1820 assert(DD->getOperatorDelete() &&
1821 "operator delete missing - EmitDtorEpilogue");
Eli Friedmanfe81e3f2009-11-18 00:50:08 +00001822 EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(),
1823 getContext().getTagDeclType(ClassDecl));
Fariborz Jahanian037bcb52009-12-01 23:35:33 +00001824 }
Fariborz Jahanianaa01d2a2009-07-30 17:49:11 +00001825}
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001826
Anders Carlssonddf57d32009-09-14 05:32:02 +00001827void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1828 CXXDtorType DtorType,
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001829 llvm::Function *Fn,
1830 const FunctionArgList &Args) {
Anders Carlssondee9a302009-11-17 04:44:12 +00001831 assert(!Dtor->getParent()->hasUserDeclaredDestructor() &&
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001832 "SynthesizeDefaultDestructor - destructor has user declaration");
Mike Stump11289f42009-09-09 15:08:12 +00001833
Anders Carlssonddf57d32009-09-14 05:32:02 +00001834 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1835 SourceLocation());
Anders Carlssondee9a302009-11-17 04:44:12 +00001836
Anders Carlssonddf57d32009-09-14 05:32:02 +00001837 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahaniand172e912009-08-17 19:04:50 +00001838 FinishFunction();
Mike Stump11289f42009-09-09 15:08:12 +00001839}