blob: a59ed0abe6212be55fb8d5b0634ba208812ba495 [file] [log] [blame]
Anders Carlssonbc49cfe2009-12-10 00:16:00 +00001//===--- CGDeclCXX.cpp - Emit LLVM Code for C++ 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 code generation of C++ declarations
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chandler Carruth85098242010-06-15 23:19:56 +000015#include "clang/Frontend/CodeGenOptions.h"
Douglas Gregor51150ab2010-05-16 01:24:12 +000016#include "llvm/Intrinsics.h"
17
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000018using namespace clang;
19using namespace CodeGen;
20
Anders Carlsson364051c2009-12-10 00:57:45 +000021static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
22 llvm::Constant *DeclPtr) {
23 assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
24 assert(!D.getType()->isReferenceType() &&
25 "Should not call EmitDeclInit on a reference!");
26
Anders Carlsson364051c2009-12-10 00:57:45 +000027 ASTContext &Context = CGF.getContext();
28
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000029 const Expr *Init = D.getInit();
30 QualType T = D.getType();
Anders Carlsson364051c2009-12-10 00:57:45 +000031 bool isVolatile = Context.getCanonicalType(T).isVolatileQualified();
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000032
Anders Carlsson364051c2009-12-10 00:57:45 +000033 if (!CGF.hasAggregateLLVMType(T)) {
34 llvm::Value *V = CGF.EmitScalarExpr(Init);
35 CGF.EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000036 } else if (T->isAnyComplexType()) {
Anders Carlsson364051c2009-12-10 00:57:45 +000037 CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000038 } else {
Anders Carlsson364051c2009-12-10 00:57:45 +000039 CGF.EmitAggExpr(Init, DeclPtr, isVolatile);
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000040 }
41}
42
Douglas Gregor370eadf2010-05-05 15:38:32 +000043static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
44 llvm::Constant *DeclPtr) {
45 CodeGenModule &CGM = CGF.CGM;
46 ASTContext &Context = CGF.getContext();
47
48 const Expr *Init = D.getInit();
49 QualType T = D.getType();
50 if (!CGF.hasAggregateLLVMType(T) || T->isAnyComplexType())
51 return;
52
53 // Avoid generating destructor(s) for initialized objects.
54 if (!isa<CXXConstructExpr>(Init))
55 return;
56
57 const ConstantArrayType *Array = Context.getAsConstantArrayType(T);
58 if (Array)
59 T = Context.getBaseElementType(Array);
60
61 const RecordType *RT = T->getAs<RecordType>();
62 if (!RT)
63 return;
64
65 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
66 if (RD->hasTrivialDestructor())
67 return;
68
69 CXXDestructorDecl *Dtor = RD->getDestructor(Context);
70
71 llvm::Constant *DtorFn;
72 if (Array) {
73 DtorFn =
Anders Carlsson165ec0a2010-06-08 22:14:59 +000074 CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, Array,
75 DeclPtr);
Douglas Gregor370eadf2010-05-05 15:38:32 +000076 const llvm::Type *Int8PtrTy =
Anders Carlsson165ec0a2010-06-08 22:14:59 +000077 llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Douglas Gregor370eadf2010-05-05 15:38:32 +000078 DeclPtr = llvm::Constant::getNullValue(Int8PtrTy);
79 } else
80 DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
81
82 CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
83}
84
Anders Carlsson364051c2009-12-10 00:57:45 +000085void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
86 llvm::Constant *DeclPtr) {
87
88 const Expr *Init = D.getInit();
89 QualType T = D.getType();
90
91 if (!T->isReferenceType()) {
92 EmitDeclInit(*this, D, DeclPtr);
Douglas Gregor370eadf2010-05-05 15:38:32 +000093 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlsson364051c2009-12-10 00:57:45 +000094 return;
95 }
Fariborz Jahanian5cda9282010-01-25 21:40:39 +000096 if (Init->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson3b227bd2010-02-03 16:38:03 +000097 RValue RV = EmitReferenceBindingToExpr(Init, /*IsInitializer=*/true);
Fariborz Jahanian5cda9282010-01-25 21:40:39 +000098 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T);
99 return;
100 }
101 ErrorUnsupported(Init,
102 "global variable that binds reference to a non-lvalue");
Anders Carlsson364051c2009-12-10 00:57:45 +0000103}
Anders Carlsson633c6f62009-12-10 00:30:05 +0000104
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000105void
106CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
107 llvm::Constant *DeclPtr) {
108 // Generate a global destructor entry if not using __cxa_atexit.
109 if (!CGM.getCodeGenOpts().CXAAtExit) {
110 CGM.AddCXXDtorEntry(DtorFn, DeclPtr);
111 return;
112 }
113
Anders Carlsson633c6f62009-12-10 00:30:05 +0000114 const llvm::Type *Int8PtrTy =
115 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
116
117 std::vector<const llvm::Type *> Params;
118 Params.push_back(Int8PtrTy);
119
120 // Get the destructor function type
121 const llvm::Type *DtorFnTy =
122 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
123 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
124
125 Params.clear();
126 Params.push_back(DtorFnTy);
127 Params.push_back(Int8PtrTy);
128 Params.push_back(Int8PtrTy);
129
130 // Get the __cxa_atexit function type
131 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
132 const llvm::FunctionType *AtExitFnTy =
133 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
134
135 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
136 "__cxa_atexit");
137
138 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
139 "__dso_handle");
140 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
141 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
142 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
143 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
144}
145
Anders Carlssonb2839e42010-06-08 22:40:05 +0000146static llvm::Function *
147CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
148 const llvm::FunctionType *FTy,
149 llvm::StringRef Name) {
150 llvm::Function *Fn =
151 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
152 Name, &CGM.getModule());
153
Anders Carlsson851318a2010-06-08 22:47:50 +0000154 // Set the section if needed.
155 if (const char *Section =
156 CGM.getContext().Target.getStaticInitSectionSpecifier())
157 Fn->setSection(Section);
158
Anders Carlssonb2839e42010-06-08 22:40:05 +0000159 return Fn;
160}
161
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000162void
163CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) {
Eli Friedman5866fe32010-01-08 00:50:11 +0000164 const llvm::FunctionType *FTy
165 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
166 false);
167
168 // Create a variable initialization function.
169 llvm::Function *Fn =
Anders Carlssonb2839e42010-06-08 22:40:05 +0000170 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
Eli Friedman5866fe32010-01-08 00:50:11 +0000171
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000172 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D);
Eli Friedman5866fe32010-01-08 00:50:11 +0000173
174 CXXGlobalInits.push_back(Fn);
175}
176
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000177void
178CodeGenModule::EmitCXXGlobalInitFunc() {
Anders Carlsson633c6f62009-12-10 00:30:05 +0000179 if (CXXGlobalInits.empty())
180 return;
181
182 const llvm::FunctionType *FTy
183 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
184 false);
185
186 // Create our global initialization function.
Anders Carlssonb2839e42010-06-08 22:40:05 +0000187 llvm::Function *Fn =
188 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
Anders Carlsson633c6f62009-12-10 00:30:05 +0000189
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000190 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
191 &CXXGlobalInits[0],
192 CXXGlobalInits.size());
193 AddGlobalCtor(Fn);
194}
195
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000196void CodeGenModule::EmitCXXGlobalDtorFunc() {
197 if (CXXGlobalDtors.empty())
198 return;
199
200 const llvm::FunctionType *FTy
201 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
202 false);
203
204 // Create our global destructor function.
205 llvm::Function *Fn =
Anders Carlssonb2839e42010-06-08 22:40:05 +0000206 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000207
208 CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors);
209 AddGlobalDtor(Fn);
210}
211
212void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
213 const VarDecl *D) {
Daniel Dunbar75722842010-03-20 04:15:29 +0000214 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
215 SourceLocation());
216
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000217 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
218 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
Daniel Dunbar75722842010-03-20 04:15:29 +0000219
220 FinishFunction();
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000221}
Daniel Dunbar75722842010-03-20 04:15:29 +0000222
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000223void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
224 llvm::Constant **Decls,
225 unsigned NumDecls) {
226 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
227 SourceLocation());
228
229 for (unsigned i = 0; i != NumDecls; ++i)
230 Builder.CreateCall(Decls[i]);
231
232 FinishFunction();
233}
234
235void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
Chris Lattner87233f72010-06-19 05:52:45 +0000236 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000237 &DtorsAndObjects) {
238 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
239 SourceLocation());
240
241 // Emit the dtors, in reverse order from construction.
Chris Lattner5e8416a2010-04-26 20:35:54 +0000242 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
Chris Lattner87233f72010-06-19 05:52:45 +0000243 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
Chris Lattner5e8416a2010-04-26 20:35:54 +0000244 llvm::CallInst *CI = Builder.CreateCall(Callee,
245 DtorsAndObjects[e - i - 1].second);
246 // Make sure the call and the callee agree on calling convention.
247 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
248 CI->setCallingConv(F->getCallingConv());
249 }
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000250
251 FinishFunction();
Anders Carlsson633c6f62009-12-10 00:30:05 +0000252}
253
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000254static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) {
255 // int __cxa_guard_acquire(__int64_t *guard_object);
256
257 const llvm::Type *Int64PtrTy =
258 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
259
260 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
261
262 const llvm::FunctionType *FTy =
263 llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy),
264 Args, /*isVarArg=*/false);
265
266 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire");
267}
268
269static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) {
270 // void __cxa_guard_release(__int64_t *guard_object);
271
272 const llvm::Type *Int64PtrTy =
273 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
274
275 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
276
277 const llvm::FunctionType *FTy =
278 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
279 Args, /*isVarArg=*/false);
280
281 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release");
282}
283
284static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) {
285 // void __cxa_guard_abort(__int64_t *guard_object);
286
287 const llvm::Type *Int64PtrTy =
288 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
289
290 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
291
292 const llvm::FunctionType *FTy =
293 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
294 Args, /*isVarArg=*/false);
295
296 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort");
297}
298
Anders Carlsson633c6f62009-12-10 00:30:05 +0000299void
300CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
301 llvm::GlobalVariable *GV) {
John McCall4a39ab82010-05-04 20:45:42 +0000302 // Bail out early if this initializer isn't reachable.
303 if (!Builder.GetInsertBlock()) return;
304
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000305 bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics;
306
Anders Carlsson633c6f62009-12-10 00:30:05 +0000307 llvm::SmallString<256> GuardVName;
308 CGM.getMangleContext().mangleGuardVariable(&D, GuardVName);
309
310 // Create the guard variable.
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000311 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(VMContext);
312 llvm::GlobalValue *GuardVariable =
313 new llvm::GlobalVariable(CGM.getModule(), Int64Ty,
Anders Carlsson633c6f62009-12-10 00:30:05 +0000314 false, GV->getLinkage(),
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000315 llvm::Constant::getNullValue(Int64Ty),
Anders Carlsson633c6f62009-12-10 00:30:05 +0000316 GuardVName.str());
317
318 // Load the first byte of the guard variable.
319 const llvm::Type *PtrTy
320 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000321 llvm::Value *V =
322 Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp");
Anders Carlsson633c6f62009-12-10 00:30:05 +0000323
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000324 llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check");
Anders Carlsson633c6f62009-12-10 00:30:05 +0000325 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
326
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000327 // Check if the first byte of the guard variable is zero.
328 Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"),
329 InitCheckBlock, EndBlock);
Anders Carlsson633c6f62009-12-10 00:30:05 +0000330
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000331 EmitBlock(InitCheckBlock);
332
Douglas Gregor51150ab2010-05-16 01:24:12 +0000333 // Variables used when coping with thread-safe statics and exceptions.
334 llvm::BasicBlock *SavedLandingPad = 0;
335 llvm::BasicBlock *LandingPad = 0;
336 if (ThreadsafeStatics) {
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000337 // Call __cxa_guard_acquire.
338 V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable);
339
340 llvm::BasicBlock *InitBlock = createBasicBlock("init");
341
342 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
343 InitBlock, EndBlock);
344
Douglas Gregorc278d1b2010-05-16 00:44:00 +0000345 if (Exceptions) {
Douglas Gregor51150ab2010-05-16 01:24:12 +0000346 SavedLandingPad = getInvokeDest();
347 LandingPad = createBasicBlock("guard.lpad");
348 setInvokeDest(LandingPad);
Douglas Gregorc278d1b2010-05-16 00:44:00 +0000349 }
Douglas Gregor51150ab2010-05-16 01:24:12 +0000350
351 EmitBlock(InitBlock);
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000352 }
Anders Carlsson633c6f62009-12-10 00:30:05 +0000353
Anders Carlsson364051c2009-12-10 00:57:45 +0000354 if (D.getType()->isReferenceType()) {
Anders Carlssonfefc7b82009-12-10 01:58:33 +0000355 QualType T = D.getType();
Anders Carlssona72ddd42009-12-10 01:05:11 +0000356 // We don't want to pass true for IsInitializer here, because a static
357 // reference to a temporary does not extend its lifetime.
Anders Carlsson3b227bd2010-02-03 16:38:03 +0000358 RValue RV = EmitReferenceBindingToExpr(D.getInit(),
Anders Carlssonfefc7b82009-12-10 01:58:33 +0000359 /*IsInitializer=*/false);
360 EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T);
361
Anders Carlsson364051c2009-12-10 00:57:45 +0000362 } else
363 EmitDeclInit(*this, D, GV);
Anders Carlsson633c6f62009-12-10 00:30:05 +0000364
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000365 if (ThreadsafeStatics) {
366 // Call __cxa_guard_release.
Douglas Gregor51150ab2010-05-16 01:24:12 +0000367 Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable);
Anders Carlssonfcd764a2010-02-06 23:23:06 +0000368 } else {
369 llvm::Value *One =
370 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1);
371 Builder.CreateStore(One, Builder.CreateBitCast(GuardVariable, PtrTy));
372 }
Anders Carlsson633c6f62009-12-10 00:30:05 +0000373
Douglas Gregor370eadf2010-05-05 15:38:32 +0000374 // Register the call to the destructor.
375 if (!D.getType()->isReferenceType())
376 EmitDeclDestroy(*this, D, GV);
377
Douglas Gregor51150ab2010-05-16 01:24:12 +0000378 if (ThreadsafeStatics && Exceptions) {
379 // If an exception is thrown during initialization, call __cxa_guard_abort
380 // along the exceptional edge.
381 EmitBranch(EndBlock);
382
383 // Construct the landing pad.
384 EmitBlock(LandingPad);
385
386 // Personality function and LLVM intrinsics.
387 llvm::Constant *Personality =
388 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
389 (VMContext),
390 true),
391 "__gxx_personality_v0");
392 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
393 llvm::Value *llvm_eh_exception =
394 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
395 llvm::Value *llvm_eh_selector =
396 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
397
398 // Exception object
399 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
400 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
401
402 // Call the selector function.
403 const llvm::PointerType *PtrToInt8Ty
404 = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
405 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
406 llvm::Value* SelectorArgs[3] = { Exc, Personality, Null };
407 Builder.CreateCall(llvm_eh_selector, SelectorArgs, SelectorArgs + 3,
408 "selector");
409 Builder.CreateStore(Exc, RethrowPtr);
410
411 // Call __cxa_guard_abort along the exceptional edge.
412 Builder.CreateCall(getGuardAbortFn(*this), GuardVariable);
413
414 setInvokeDest(SavedLandingPad);
415
416 // Rethrow the current exception.
417 if (getInvokeDest()) {
418 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
419 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
420 getInvokeDest(),
421 Builder.CreateLoad(RethrowPtr));
422 EmitBlock(Cont);
423 } else
424 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
425 Builder.CreateLoad(RethrowPtr));
426
427 Builder.CreateUnreachable();
428 }
429
Anders Carlsson633c6f62009-12-10 00:30:05 +0000430 EmitBlock(EndBlock);
431}
Anders Carlsson282bc102010-06-08 22:17:27 +0000432
433/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
434/// invoked, calls the default destructor on array elements in reverse order of
435/// construction.
436llvm::Function *
437CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
438 const ArrayType *Array,
439 llvm::Value *This) {
440 FunctionArgList Args;
441 ImplicitParamDecl *Dst =
442 ImplicitParamDecl::Create(getContext(), 0,
443 SourceLocation(), 0,
444 getContext().getPointerType(getContext().VoidTy));
445 Args.push_back(std::make_pair(Dst, Dst->getType()));
446
Anders Carlsson282bc102010-06-08 22:17:27 +0000447 const CGFunctionInfo &FI =
448 CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args,
449 FunctionType::ExtInfo());
450 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
Anders Carlssonb2839e42010-06-08 22:40:05 +0000451 llvm::Function *Fn =
452 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
Anders Carlsson282bc102010-06-08 22:17:27 +0000453
454 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, Args, SourceLocation());
455
456 QualType BaseElementTy = getContext().getBaseElementType(Array);
457 const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo();
458 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
459
460 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
461
462 FinishFunction();
463
464 return Fn;
465}