blob: 38345e474190304809e727eb77205c9c9dcec970 [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
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000048 QualType T = D.getType();
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000049
John McCall85aca0f2010-07-30 04:56:58 +000050 // Drill down past array types.
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000051 const ConstantArrayType *Array = Context.getAsConstantArrayType(T);
52 if (Array)
53 T = Context.getBaseElementType(Array);
54
John McCall85aca0f2010-07-30 04:56:58 +000055 /// If that's not a record, we're done.
56 /// FIXME: __attribute__((cleanup)) ?
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000057 const RecordType *RT = T->getAs<RecordType>();
58 if (!RT)
59 return;
60
61 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
62 if (RD->hasTrivialDestructor())
63 return;
64
Douglas Gregor1d110e02010-07-01 14:13:13 +000065 CXXDestructorDecl *Dtor = RD->getDestructor();
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000066
67 llvm::Constant *DtorFn;
68 if (Array) {
69 DtorFn =
Anders Carlsson02e370a2010-06-08 22:14:59 +000070 CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, Array,
71 DeclPtr);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000072 const llvm::Type *Int8PtrTy =
Anders Carlsson02e370a2010-06-08 22:14:59 +000073 llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000074 DeclPtr = llvm::Constant::getNullValue(Int8PtrTy);
75 } else
76 DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
77
78 CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
79}
80
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000081void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
82 llvm::Constant *DeclPtr) {
83
84 const Expr *Init = D.getInit();
85 QualType T = D.getType();
86
87 if (!T->isReferenceType()) {
88 EmitDeclInit(*this, D, DeclPtr);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000089 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000090 return;
91 }
Anders Carlsson045a6d82010-06-27 17:52:15 +000092
93 RValue RV = EmitReferenceBindingToExpr(Init, &D);
94 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, T);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000095}
Anders Carlssoneb4072e2009-12-10 00:30:05 +000096
Daniel Dunbarefb0fa92010-03-20 04:15:41 +000097void
98CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
99 llvm::Constant *DeclPtr) {
100 // Generate a global destructor entry if not using __cxa_atexit.
101 if (!CGM.getCodeGenOpts().CXAAtExit) {
102 CGM.AddCXXDtorEntry(DtorFn, DeclPtr);
103 return;
104 }
105
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000106 const llvm::Type *Int8PtrTy =
107 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
108
109 std::vector<const llvm::Type *> Params;
110 Params.push_back(Int8PtrTy);
111
112 // Get the destructor function type
113 const llvm::Type *DtorFnTy =
114 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
115 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
116
117 Params.clear();
118 Params.push_back(DtorFnTy);
119 Params.push_back(Int8PtrTy);
120 Params.push_back(Int8PtrTy);
121
122 // Get the __cxa_atexit function type
123 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
124 const llvm::FunctionType *AtExitFnTy =
125 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
126
127 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
128 "__cxa_atexit");
129
130 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
131 "__dso_handle");
132 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
133 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
134 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
135 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
136}
137
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000138static llvm::Function *
139CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
140 const llvm::FunctionType *FTy,
141 llvm::StringRef Name) {
142 llvm::Function *Fn =
143 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
144 Name, &CGM.getModule());
145
Anders Carlsson18af3682010-06-08 22:47:50 +0000146 // Set the section if needed.
147 if (const char *Section =
148 CGM.getContext().Target.getStaticInitSectionSpecifier())
149 Fn->setSection(Section);
150
John McCall044cc542010-07-06 04:38:10 +0000151 if (!CGM.getLangOptions().Exceptions)
152 Fn->setDoesNotThrow();
153
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000154 return Fn;
155}
156
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000157void
158CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) {
Eli Friedman6c6bda32010-01-08 00:50:11 +0000159 const llvm::FunctionType *FTy
160 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
161 false);
162
163 // Create a variable initialization function.
164 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000165 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
Eli Friedman6c6bda32010-01-08 00:50:11 +0000166
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000167 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000168
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000169 if (D->hasAttr<InitPriorityAttr>()) {
170 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
Chris Lattnerec2830d2010-06-27 06:32:58 +0000171 OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000172 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
John McCallbf40cb52010-07-15 23:40:35 +0000173 DelayedCXXInitPosition.erase(D);
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000174 }
John McCallbf40cb52010-07-15 23:40:35 +0000175 else {
176 llvm::DenseMap<const Decl *, unsigned>::iterator I =
177 DelayedCXXInitPosition.find(D);
178 if (I == DelayedCXXInitPosition.end()) {
179 CXXGlobalInits.push_back(Fn);
180 } else {
181 assert(CXXGlobalInits[I->second] == 0);
182 CXXGlobalInits[I->second] = Fn;
183 DelayedCXXInitPosition.erase(I);
184 }
185 }
Eli Friedman6c6bda32010-01-08 00:50:11 +0000186}
187
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000188void
189CodeGenModule::EmitCXXGlobalInitFunc() {
John McCallbf40cb52010-07-15 23:40:35 +0000190 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
191 CXXGlobalInits.pop_back();
192
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000193 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000194 return;
195
196 const llvm::FunctionType *FTy
197 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
198 false);
199
200 // Create our global initialization function.
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000201 llvm::Function *Fn =
202 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000203
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000204 if (!PrioritizedCXXGlobalInits.empty()) {
Fariborz Jahanian027d7ed2010-06-21 19:49:38 +0000205 llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
206 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
Fariborz Jahanianf4896882010-06-22 00:23:08 +0000207 PrioritizedCXXGlobalInits.end());
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000208 for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) {
209 llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second;
210 LocalCXXGlobalInits.push_back(Fn);
211 }
John McCallbf40cb52010-07-15 23:40:35 +0000212 LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end());
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000213 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
214 &LocalCXXGlobalInits[0],
215 LocalCXXGlobalInits.size());
216 }
217 else
218 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
219 &CXXGlobalInits[0],
220 CXXGlobalInits.size());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000221 AddGlobalCtor(Fn);
222}
223
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000224void CodeGenModule::EmitCXXGlobalDtorFunc() {
225 if (CXXGlobalDtors.empty())
226 return;
227
228 const llvm::FunctionType *FTy
229 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
230 false);
231
232 // Create our global destructor function.
233 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000234 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000235
236 CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors);
237 AddGlobalDtor(Fn);
238}
239
240void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
241 const VarDecl *D) {
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000242 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
243 SourceLocation());
244
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000245 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
246 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000247
248 FinishFunction();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000249}
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000250
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000251void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
252 llvm::Constant **Decls,
253 unsigned NumDecls) {
254 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
255 SourceLocation());
256
257 for (unsigned i = 0; i != NumDecls; ++i)
John McCallbf40cb52010-07-15 23:40:35 +0000258 if (Decls[i])
259 Builder.CreateCall(Decls[i]);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000260
261 FinishFunction();
262}
263
264void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
Chris Lattner810112e2010-06-19 05:52:45 +0000265 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000266 &DtorsAndObjects) {
267 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
268 SourceLocation());
269
270 // Emit the dtors, in reverse order from construction.
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000271 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
Chris Lattner810112e2010-06-19 05:52:45 +0000272 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000273 llvm::CallInst *CI = Builder.CreateCall(Callee,
274 DtorsAndObjects[e - i - 1].second);
275 // Make sure the call and the callee agree on calling convention.
276 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
277 CI->setCallingConv(F->getCallingConv());
278 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000279
280 FinishFunction();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000281}
282
Anders Carlssona508b7d2010-02-06 23:23:06 +0000283static llvm::Constant *getGuardAcquireFn(CodeGenFunction &CGF) {
284 // int __cxa_guard_acquire(__int64_t *guard_object);
285
286 const llvm::Type *Int64PtrTy =
287 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
288
289 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
290
291 const llvm::FunctionType *FTy =
292 llvm::FunctionType::get(CGF.ConvertType(CGF.getContext().IntTy),
293 Args, /*isVarArg=*/false);
294
295 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire");
296}
297
298static llvm::Constant *getGuardReleaseFn(CodeGenFunction &CGF) {
299 // void __cxa_guard_release(__int64_t *guard_object);
300
301 const llvm::Type *Int64PtrTy =
302 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
303
304 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
305
306 const llvm::FunctionType *FTy =
307 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
308 Args, /*isVarArg=*/false);
309
310 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release");
311}
312
313static llvm::Constant *getGuardAbortFn(CodeGenFunction &CGF) {
314 // void __cxa_guard_abort(__int64_t *guard_object);
315
316 const llvm::Type *Int64PtrTy =
317 llvm::Type::getInt64PtrTy(CGF.getLLVMContext());
318
319 std::vector<const llvm::Type*> Args(1, Int64PtrTy);
320
321 const llvm::FunctionType *FTy =
322 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
323 Args, /*isVarArg=*/false);
324
325 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort");
326}
327
John McCalle540e632010-07-21 06:20:50 +0000328namespace {
John McCall1f0fca52010-07-21 07:22:38 +0000329 struct CallGuardAbort : EHScopeStack::Cleanup {
John McCalle540e632010-07-21 06:20:50 +0000330 llvm::GlobalVariable *Guard;
331 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
332
333 void Emit(CodeGenFunction &CGF, bool IsForEH) {
334 // It shouldn't be possible for this to throw, but if it can,
335 // this should allow for the possibility of an invoke.
336 CGF.Builder.CreateCall(getGuardAbortFn(CGF), Guard)
337 ->setDoesNotThrow();
338 }
339 };
340}
341
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000342void
343CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
344 llvm::GlobalVariable *GV) {
John McCallfe67f3b2010-05-04 20:45:42 +0000345 // Bail out early if this initializer isn't reachable.
346 if (!Builder.GetInsertBlock()) return;
347
Anders Carlssona508b7d2010-02-06 23:23:06 +0000348 bool ThreadsafeStatics = getContext().getLangOptions().ThreadsafeStatics;
349
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000350 llvm::SmallString<256> GuardVName;
351 CGM.getMangleContext().mangleGuardVariable(&D, GuardVName);
352
353 // Create the guard variable.
John McCalle540e632010-07-21 06:20:50 +0000354 llvm::GlobalVariable *GuardVariable =
Anders Carlssona508b7d2010-02-06 23:23:06 +0000355 new llvm::GlobalVariable(CGM.getModule(), Int64Ty,
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000356 false, GV->getLinkage(),
Anders Carlssona508b7d2010-02-06 23:23:06 +0000357 llvm::Constant::getNullValue(Int64Ty),
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000358 GuardVName.str());
359
360 // Load the first byte of the guard variable.
361 const llvm::Type *PtrTy
362 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlssona508b7d2010-02-06 23:23:06 +0000363 llvm::Value *V =
364 Builder.CreateLoad(Builder.CreateBitCast(GuardVariable, PtrTy), "tmp");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000365
Anders Carlssona508b7d2010-02-06 23:23:06 +0000366 llvm::BasicBlock *InitCheckBlock = createBasicBlock("init.check");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000367 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
368
Anders Carlssona508b7d2010-02-06 23:23:06 +0000369 // Check if the first byte of the guard variable is zero.
370 Builder.CreateCondBr(Builder.CreateIsNull(V, "tobool"),
371 InitCheckBlock, EndBlock);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000372
Anders Carlssona508b7d2010-02-06 23:23:06 +0000373 EmitBlock(InitCheckBlock);
374
Douglas Gregor86a3a032010-05-16 01:24:12 +0000375 // Variables used when coping with thread-safe statics and exceptions.
Douglas Gregor86a3a032010-05-16 01:24:12 +0000376 if (ThreadsafeStatics) {
Anders Carlssona508b7d2010-02-06 23:23:06 +0000377 // Call __cxa_guard_acquire.
378 V = Builder.CreateCall(getGuardAcquireFn(*this), GuardVariable);
379
380 llvm::BasicBlock *InitBlock = createBasicBlock("init");
381
382 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
383 InitBlock, EndBlock);
384
John McCallf1549f62010-07-06 01:34:17 +0000385 // Call __cxa_guard_abort along the exceptional edge.
John McCalle540e632010-07-21 06:20:50 +0000386 if (Exceptions)
John McCall1f0fca52010-07-21 07:22:38 +0000387 EHStack.pushCleanup<CallGuardAbort>(EHCleanup, GuardVariable);
Douglas Gregor86a3a032010-05-16 01:24:12 +0000388
389 EmitBlock(InitBlock);
Anders Carlssona508b7d2010-02-06 23:23:06 +0000390 }
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000391
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000392 if (D.getType()->isReferenceType()) {
Anders Carlsson864143f2009-12-10 01:58:33 +0000393 QualType T = D.getType();
Anders Carlsson045a6d82010-06-27 17:52:15 +0000394 RValue RV = EmitReferenceBindingToExpr(D.getInit(), &D);
Anders Carlsson864143f2009-12-10 01:58:33 +0000395 EmitStoreOfScalar(RV.getScalarVal(), GV, /*Volatile=*/false, T);
396
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000397 } else
398 EmitDeclInit(*this, D, GV);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000399
Anders Carlssona508b7d2010-02-06 23:23:06 +0000400 if (ThreadsafeStatics) {
John McCall224124c2010-08-10 18:51:44 +0000401 // Pop the guard-abort cleanup if we pushed one.
402 if (Exceptions)
403 PopCleanupBlock();
404
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}