blob: 0293c5c125fe71d3bd5829ede39d4ba6162988c7 [file] [log] [blame]
Anders Carlsson5ec2e7c2009-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 Carruth06057ce2010-06-15 23:19:56 +000015#include "clang/Frontend/CodeGenOptions.h"
Douglas Gregor86a3a032010-05-16 01:24:12 +000016#include "llvm/Intrinsics.h"
17
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000018using namespace clang;
19using namespace CodeGen;
20
Anders Carlssonfcbfdc12009-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 Carlssonfcbfdc12009-12-10 00:57:45 +000027 ASTContext &Context = CGF.getContext();
28
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000029 const Expr *Init = D.getInit();
30 QualType T = D.getType();
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000031 bool isVolatile = Context.getCanonicalType(T).isVolatileQualified();
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000032
Anders Carlssonfcbfdc12009-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 Carlsson5ec2e7c2009-12-10 00:16:00 +000036 } else if (T->isAnyComplexType()) {
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000037 CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000038 } else {
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000039 CGF.EmitAggExpr(Init, DeclPtr, isVolatile);
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000040 }
41}
42
Douglas Gregorcc6a44b2010-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
Douglas Gregor1d110e02010-07-01 14:13:13 +000069 CXXDestructorDecl *Dtor = RD->getDestructor();
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000070
71 llvm::Constant *DtorFn;
72 if (Array) {
73 DtorFn =
Anders Carlsson02e370a2010-06-08 22:14:59 +000074 CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, Array,
75 DeclPtr);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000076 const llvm::Type *Int8PtrTy =
Anders Carlsson02e370a2010-06-08 22:14:59 +000077 llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Douglas Gregorcc6a44b2010-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 Carlssonfcbfdc12009-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 Gregorcc6a44b2010-05-05 15:38:32 +000093 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000094 return;
95 }
Anders Carlsson045a6d82010-06-27 17:52:15 +000096
97 RValue RV = EmitReferenceBindingToExpr(Init, &D);
98 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000099}
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000100
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000101void
102CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
103 llvm::Constant *DeclPtr) {
104 // Generate a global destructor entry if not using __cxa_atexit.
105 if (!CGM.getCodeGenOpts().CXAAtExit) {
106 CGM.AddCXXDtorEntry(DtorFn, DeclPtr);
107 return;
108 }
109
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000110 const llvm::Type *Int8PtrTy =
111 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
112
113 std::vector<const llvm::Type *> Params;
114 Params.push_back(Int8PtrTy);
115
116 // Get the destructor function type
117 const llvm::Type *DtorFnTy =
118 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
119 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
120
121 Params.clear();
122 Params.push_back(DtorFnTy);
123 Params.push_back(Int8PtrTy);
124 Params.push_back(Int8PtrTy);
125
126 // Get the __cxa_atexit function type
127 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
128 const llvm::FunctionType *AtExitFnTy =
129 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
130
131 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
132 "__cxa_atexit");
133
134 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
135 "__dso_handle");
136 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
137 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
138 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
139 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
140}
141
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000142static llvm::Function *
143CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
144 const llvm::FunctionType *FTy,
145 llvm::StringRef Name) {
146 llvm::Function *Fn =
147 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
148 Name, &CGM.getModule());
149
Anders Carlsson18af3682010-06-08 22:47:50 +0000150 // Set the section if needed.
151 if (const char *Section =
152 CGM.getContext().Target.getStaticInitSectionSpecifier())
153 Fn->setSection(Section);
154
John McCall044cc542010-07-06 04:38:10 +0000155 if (!CGM.getLangOptions().Exceptions)
156 Fn->setDoesNotThrow();
157
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000158 return Fn;
159}
160
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000161void
162CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) {
Eli Friedman6c6bda32010-01-08 00:50:11 +0000163 const llvm::FunctionType *FTy
164 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
165 false);
166
167 // Create a variable initialization function.
168 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000169 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
Eli Friedman6c6bda32010-01-08 00:50:11 +0000170
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000171 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000172
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000173 if (D->hasAttr<InitPriorityAttr>()) {
174 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
Chris Lattnerec2830d2010-06-27 06:32:58 +0000175 OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000176 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
John McCallbf40cb52010-07-15 23:40:35 +0000177 DelayedCXXInitPosition.erase(D);
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000178 }
John McCallbf40cb52010-07-15 23:40:35 +0000179 else {
180 llvm::DenseMap<const Decl *, unsigned>::iterator I =
181 DelayedCXXInitPosition.find(D);
182 if (I == DelayedCXXInitPosition.end()) {
183 CXXGlobalInits.push_back(Fn);
184 } else {
185 assert(CXXGlobalInits[I->second] == 0);
186 CXXGlobalInits[I->second] = Fn;
187 DelayedCXXInitPosition.erase(I);
188 }
189 }
Eli Friedman6c6bda32010-01-08 00:50:11 +0000190}
191
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000192void
193CodeGenModule::EmitCXXGlobalInitFunc() {
John McCallbf40cb52010-07-15 23:40:35 +0000194 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
195 CXXGlobalInits.pop_back();
196
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000197 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000198 return;
199
200 const llvm::FunctionType *FTy
201 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
202 false);
203
204 // Create our global initialization function.
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000205 llvm::Function *Fn =
206 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000207
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000208 if (!PrioritizedCXXGlobalInits.empty()) {
Fariborz Jahanian027d7ed2010-06-21 19:49:38 +0000209 llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
210 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
Fariborz Jahanianf4896882010-06-22 00:23:08 +0000211 PrioritizedCXXGlobalInits.end());
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000212 for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) {
213 llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second;
214 LocalCXXGlobalInits.push_back(Fn);
215 }
John McCallbf40cb52010-07-15 23:40:35 +0000216 LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end());
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000217 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
218 &LocalCXXGlobalInits[0],
219 LocalCXXGlobalInits.size());
220 }
221 else
222 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
223 &CXXGlobalInits[0],
224 CXXGlobalInits.size());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000225 AddGlobalCtor(Fn);
226}
227
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000228void CodeGenModule::EmitCXXGlobalDtorFunc() {
229 if (CXXGlobalDtors.empty())
230 return;
231
232 const llvm::FunctionType *FTy
233 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
234 false);
235
236 // Create our global destructor function.
237 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000238 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000239
240 CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors);
241 AddGlobalDtor(Fn);
242}
243
244void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
245 const VarDecl *D) {
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000246 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
247 SourceLocation());
248
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000249 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
250 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000251
252 FinishFunction();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000253}
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000254
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000255void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
256 llvm::Constant **Decls,
257 unsigned NumDecls) {
258 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
259 SourceLocation());
260
261 for (unsigned i = 0; i != NumDecls; ++i)
John McCallbf40cb52010-07-15 23:40:35 +0000262 if (Decls[i])
263 Builder.CreateCall(Decls[i]);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000264
265 FinishFunction();
266}
267
268void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
Chris Lattner810112e2010-06-19 05:52:45 +0000269 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000270 &DtorsAndObjects) {
271 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
272 SourceLocation());
273
274 // Emit the dtors, in reverse order from construction.
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000275 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
Chris Lattner810112e2010-06-19 05:52:45 +0000276 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000277 llvm::CallInst *CI = Builder.CreateCall(Callee,
278 DtorsAndObjects[e - i - 1].second);
279 // Make sure the call and the callee agree on calling convention.
280 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
281 CI->setCallingConv(F->getCallingConv());
282 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000283
284 FinishFunction();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000285}
286
Anders Carlssona508b7d2010-02-06 23:23:06 +0000287static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) {
288 // int __cxa_guard_acquire(__int64_t *guard_object);
289
290 const llvm::Type *Int64PtrTy =
291 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
292
293 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
294
295 const llvm::FunctionType *FTy =
296 llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy),
297 Args, /*isVarArg=*/false);
298
299 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire");
300}
301
302static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) {
303 // void __cxa_guard_release(__int64_t *guard_object);
304
305 const llvm::Type *Int64PtrTy =
306 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
307
308 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
309
310 const llvm::FunctionType *FTy =
311 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
312 Args, /*isVarArg=*/false);
313
314 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release");
315}
316
317static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) {
318 // void __cxa_guard_abort(__int64_t *guard_object);
319
320 const llvm::Type *Int64PtrTy =
321 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
322
323 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
324
325 const llvm::FunctionType *FTy =
326 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
327 Args, /*isVarArg=*/false);
328
329 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort");
330}
331
John McCalle540e632010-07-21 06:20:50 +0000332namespace {
John McCall1f0fca52010-07-21 07:22:38 +0000333 struct CallGuardAbort : EHScopeStack::Cleanup {
John McCalle540e632010-07-21 06:20:50 +0000334 llvm::GlobalVariable *Guard;
335 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
336
337 void Emit(CodeGenFunction &CGF, bool IsForEH) {
338 // It shouldn't be possible for this to throw, but if it can,
339 // this should allow for the possibility of an invoke.
340 CGF.Builder.CreateCall(getGuardAbortFn(CGF), Guard)
341 ->setDoesNotThrow();
342 }
343 };
344}
345
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000346void
347CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
348 llvm::GlobalVariable *GV) {
John McCallfe67f3b2010-05-04 20:45:42 +0000349 // Bail out early if this initializer isn't reachable.
350 if (!Builder.GetInsertBlock()) return;
351
Anders Carlssona508b7d2010-02-06 23:23:06 +0000352 bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics;
353
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000354 llvm::SmallString<256> GuardVName;
355 CGM.getMangleContext().mangleGuardVariable(&D, GuardVName);
356
357 // Create the guard variable.
John McCalle540e632010-07-21 06:20:50 +0000358 llvm::GlobalVariable *GuardVariable =
Anders Carlssona508b7d2010-02-06 23:23:06 +0000359 new llvm::GlobalVariable(CGM.getModule(), Int64Ty,
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000360 false, GV->getLinkage(),
Anders Carlssona508b7d2010-02-06 23:23:06 +0000361 llvm::Constant::getNullValue(Int64Ty),
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000362 GuardVName.str());
363
364 // Load the first byte of the guard variable.
365 const llvm::Type *PtrTy
366 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlssona508b7d2010-02-06 23:23:06 +0000367 llvm::Value *V =
368 Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000369
Anders Carlssona508b7d2010-02-06 23:23:06 +0000370 llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000371 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
372
Anders Carlssona508b7d2010-02-06 23:23:06 +0000373 // Check if the first byte of the guard variable is zero.
374 Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"),
375 InitCheckBlock, EndBlock);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000376
Anders Carlssona508b7d2010-02-06 23:23:06 +0000377 EmitBlock(InitCheckBlock);
378
Douglas Gregor86a3a032010-05-16 01:24:12 +0000379 // Variables used when coping with thread-safe statics and exceptions.
Douglas Gregor86a3a032010-05-16 01:24:12 +0000380 if (ThreadsafeStatics) {
Anders Carlssona508b7d2010-02-06 23:23:06 +0000381 // Call __cxa_guard_acquire.
382 V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable);
383
384 llvm::BasicBlock *InitBlock = createBasicBlock("init");
385
386 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
387 InitBlock, EndBlock);
388
John McCallf1549f62010-07-06 01:34:17 +0000389 // Call __cxa_guard_abort along the exceptional edge.
John McCalle540e632010-07-21 06:20:50 +0000390 if (Exceptions)
John McCall1f0fca52010-07-21 07:22:38 +0000391 EHStack.pushCleanup<CallGuardAbort>(EHCleanup, GuardVariable);
Douglas Gregor86a3a032010-05-16 01:24:12 +0000392
393 EmitBlock(InitBlock);
Anders Carlssona508b7d2010-02-06 23:23:06 +0000394 }
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000395
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000396 if (D.getType()->isReferenceType()) {
Anders Carlsson864143f2009-12-10 01:58:33 +0000397 QualType T = D.getType();
Anders Carlsson045a6d82010-06-27 17:52:15 +0000398 RValue RV = EmitReferenceBindingToExpr(D.getInit(), &D);
Anders Carlsson864143f2009-12-10 01:58:33 +0000399 EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T);
400
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000401 } else
402 EmitDeclInit(*this, D, GV);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000403
Anders Carlssona508b7d2010-02-06 23:23:06 +0000404 if (ThreadsafeStatics) {
John McCallf1549f62010-07-06 01:34:17 +0000405 // Call __cxa_guard_release. This cannot throw.
Douglas Gregor86a3a032010-05-16 01:24:12 +0000406 Builder.CreateCall(getGuardReleaseFn(*this), GuardVariable);
Anders Carlssona508b7d2010-02-06 23:23:06 +0000407 } else {
408 llvm::Value *One =
409 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1);
410 Builder.CreateStore(One, Builder.CreateBitCast(GuardVariable, PtrTy));
411 }
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000412
Douglas Gregorcc6a44b2010-05-05 15:38:32 +0000413 // Register the call to the destructor.
414 if (!D.getType()->isReferenceType())
415 EmitDeclDestroy(*this, D, GV);
416
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000417 EmitBlock(EndBlock);
418}
Anders Carlsson77291362010-06-08 22:17:27 +0000419
420/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
421/// invoked, calls the default destructor on array elements in reverse order of
422/// construction.
423llvm::Function *
424CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
425 const ArrayType *Array,
426 llvm::Value *This) {
427 FunctionArgList Args;
428 ImplicitParamDecl *Dst =
429 ImplicitParamDecl::Create(getContext(), 0,
430 SourceLocation(), 0,
431 getContext().getPointerType(getContext().VoidTy));
432 Args.push_back(std::make_pair(Dst, Dst->getType()));
433
Anders Carlsson77291362010-06-08 22:17:27 +0000434 const CGFunctionInfo &FI =
435 CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args,
436 FunctionType::ExtInfo());
437 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000438 llvm::Function *Fn =
439 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
Anders Carlsson77291362010-06-08 22:17:27 +0000440
441 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, Args, SourceLocation());
442
443 QualType BaseElementTy = getContext().getBaseElementType(Array);
444 const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo();
445 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
446
447 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
448
449 FinishFunction();
450
451 return Fn;
452}