blob: a5884e35db60d4ad4a9efb975e971dd375e786c9 [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"
John McCall4c40d982010-08-31 07:33:07 +000015#include "CGCXXABI.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "CGObjCRuntime.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000017#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +000018#include "llvm/ADT/StringExtras.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000019#include "llvm/IR/Intrinsics.h"
Douglas Gregor86a3a032010-05-16 01:24:12 +000020
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000021using namespace clang;
22using namespace CodeGen;
23
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000024static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
25 llvm::Constant *DeclPtr) {
26 assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
27 assert(!D.getType()->isReferenceType() &&
28 "Should not call EmitDeclInit on a reference!");
29
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000030 ASTContext &Context = CGF.getContext();
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000031
Eli Friedman6da2c712011-12-03 04:14:32 +000032 CharUnits alignment = Context.getDeclAlign(&D);
John McCalla07398e2011-06-16 04:16:24 +000033 QualType type = D.getType();
34 LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment);
35
36 const Expr *Init = D.getInit();
37 if (!CGF.hasAggregateLLVMType(type)) {
Fariborz Jahanianec805122011-01-13 20:00:54 +000038 CodeGenModule &CGM = CGF.CGM;
John McCalla07398e2011-06-16 04:16:24 +000039 if (lv.isObjCStrong())
John McCallf85e1932011-06-15 23:02:42 +000040 CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
41 DeclPtr, D.isThreadSpecified());
John McCalla07398e2011-06-16 04:16:24 +000042 else if (lv.isObjCWeak())
43 CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
44 DeclPtr);
Fariborz Jahanianec805122011-01-13 20:00:54 +000045 else
John McCalla07398e2011-06-16 04:16:24 +000046 CGF.EmitScalarInit(Init, &D, lv, false);
47 } else if (type->isAnyComplexType()) {
48 CGF.EmitComplexExprIntoAddr(Init, DeclPtr, lv.isVolatile());
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000049 } else {
Chad Rosier649b4a12012-03-29 17:37:10 +000050 CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
51 AggValueSlot::DoesNotNeedGCBarriers,
52 AggValueSlot::IsNotAliased));
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000053 }
54}
55
John McCall5cd91b52010-09-08 01:44:27 +000056/// Emit code to cause the destruction of the given variable with
57/// static storage duration.
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000058static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
John McCalla91f6662011-07-13 03:01:35 +000059 llvm::Constant *addr) {
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000060 CodeGenModule &CGM = CGF.CGM;
John McCalla91f6662011-07-13 03:01:35 +000061
62 // FIXME: __attribute__((cleanup)) ?
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000063
John McCalla91f6662011-07-13 03:01:35 +000064 QualType type = D.getType();
65 QualType::DestructionKind dtorKind = type.isDestructedType();
66
67 switch (dtorKind) {
68 case QualType::DK_none:
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000069 return;
John McCalla91f6662011-07-13 03:01:35 +000070
71 case QualType::DK_cxx_destructor:
72 break;
73
74 case QualType::DK_objc_strong_lifetime:
75 case QualType::DK_objc_weak_lifetime:
76 // We don't care about releasing objects during process teardown.
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000077 return;
John McCalla91f6662011-07-13 03:01:35 +000078 }
79
80 llvm::Constant *function;
81 llvm::Constant *argument;
82
83 // Special-case non-array C++ destructors, where there's a function
84 // with the right signature that we can just call.
85 const CXXRecordDecl *record = 0;
86 if (dtorKind == QualType::DK_cxx_destructor &&
87 (record = type->getAsCXXRecordDecl())) {
88 assert(!record->hasTrivialDestructor());
89 CXXDestructorDecl *dtor = record->getDestructor();
90
91 function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete);
92 argument = addr;
93
94 // Otherwise, the standard logic requires a helper function.
95 } else {
96 function = CodeGenFunction(CGM).generateDestroyHelper(addr, type,
97 CGF.getDestroyer(dtorKind),
98 CGF.needsEHCleanup(dtorKind));
99 argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
100 }
101
John McCall20bb1752012-05-01 06:13:13 +0000102 CGM.getCXXABI().registerGlobalDtor(CGF, function, argument);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +0000103}
104
Richard Smithabb94322012-02-17 07:31:37 +0000105/// Emit code to cause the variable at the given address to be considered as
106/// constant from this point onwards.
Nick Lewyckyef784462012-02-21 00:26:58 +0000107static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
108 llvm::Constant *Addr) {
Richard Smith00a8c3f2012-02-17 20:12:52 +0000109 // Don't emit the intrinsic if we're not optimizing.
110 if (!CGF.CGM.getCodeGenOpts().OptimizationLevel)
111 return;
112
Richard Smithabb94322012-02-17 07:31:37 +0000113 // Grab the llvm.invariant.start intrinsic.
114 llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
115 llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
116
Nick Lewyckyef784462012-02-21 00:26:58 +0000117 // Emit a call with the size in bytes of the object.
118 CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType());
119 uint64_t Width = WidthChars.getQuantity();
120 llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width),
Richard Smithabb94322012-02-17 07:31:37 +0000121 llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
122 CGF.Builder.CreateCall(InvariantStart, Args);
123}
124
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000125void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Richard Smith7ca48502012-02-13 22:16:19 +0000126 llvm::Constant *DeclPtr,
127 bool PerformInit) {
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000128
129 const Expr *Init = D.getInit();
130 QualType T = D.getType();
131
132 if (!T->isReferenceType()) {
Richard Smith7ca48502012-02-13 22:16:19 +0000133 if (PerformInit)
134 EmitDeclInit(*this, D, DeclPtr);
Richard Smithabb94322012-02-17 07:31:37 +0000135 if (CGM.isTypeConstant(D.getType(), true))
Nick Lewyckyef784462012-02-21 00:26:58 +0000136 EmitDeclInvariant(*this, D, DeclPtr);
Richard Smithabb94322012-02-17 07:31:37 +0000137 else
138 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000139 return;
140 }
Anders Carlsson045a6d82010-06-27 17:52:15 +0000141
Richard Smith7ca48502012-02-13 22:16:19 +0000142 assert(PerformInit && "cannot have constant initializer which needs "
143 "destruction for reference");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000144 unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
Anders Carlsson045a6d82010-06-27 17:52:15 +0000145 RValue RV = EmitReferenceBindingToExpr(Init, &D);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000146 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000147}
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000148
John McCall30fa3702012-04-06 18:21:06 +0000149static llvm::Function *
150CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
151 llvm::FunctionType *ty,
152 const Twine &name);
153
154/// Create a stub function, suitable for being passed to atexit,
155/// which passes the given address to the given destructor function.
156static llvm::Constant *createAtExitStub(CodeGenModule &CGM,
157 llvm::Constant *dtor,
158 llvm::Constant *addr) {
159 // Get the destructor function type, void(*)(void).
160 llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
161 llvm::Function *fn =
162 CreateGlobalInitOrDestructFunction(CGM, ty,
163 Twine("__dtor_", addr->getName()));
164
165 CodeGenFunction CGF(CGM);
166
Alexey Samsonov34b41f82012-10-25 10:18:50 +0000167 // Initialize debug info if needed.
168 CGF.maybeInitializeDebugInfo();
169
John McCall30fa3702012-04-06 18:21:06 +0000170 CGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, fn,
171 CGM.getTypes().arrangeNullaryFunction(),
172 FunctionArgList(), SourceLocation());
173
174 llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
175
176 // Make sure the call and the callee agree on calling convention.
177 if (llvm::Function *dtorFn =
178 dyn_cast<llvm::Function>(dtor->stripPointerCasts()))
179 call->setCallingConv(dtorFn->getCallingConv());
180
181 CGF.FinishFunction();
182
183 return fn;
184}
185
John McCall20bb1752012-05-01 06:13:13 +0000186/// Register a global destructor using the C atexit runtime function.
187void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtor,
188 llvm::Constant *addr) {
John McCall30fa3702012-04-06 18:21:06 +0000189 // Create a function which calls the destructor.
John McCall20bb1752012-05-01 06:13:13 +0000190 llvm::Constant *dtorStub = createAtExitStub(CGM, dtor, addr);
John McCall30fa3702012-04-06 18:21:06 +0000191
192 // extern "C" int atexit(void (*f)(void));
193 llvm::FunctionType *atexitTy =
John McCall20bb1752012-05-01 06:13:13 +0000194 llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
John McCall30fa3702012-04-06 18:21:06 +0000195
196 llvm::Constant *atexit =
John McCall20bb1752012-05-01 06:13:13 +0000197 CGM.CreateRuntimeFunction(atexitTy, "atexit");
John McCall30fa3702012-04-06 18:21:06 +0000198 if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit))
199 atexitFn->setDoesNotThrow();
200
John McCall20bb1752012-05-01 06:13:13 +0000201 Builder.CreateCall(atexit, dtorStub)->setDoesNotThrow();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000202}
203
John McCall3030eb82010-11-06 09:44:32 +0000204void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000205 llvm::GlobalVariable *DeclPtr,
Richard Smith7ca48502012-02-13 22:16:19 +0000206 bool PerformInit) {
John McCall32096692011-03-18 02:56:14 +0000207 // If we've been asked to forbid guard variables, emit an error now.
208 // This diagnostic is hard-coded for Darwin's use case; we can find
209 // better phrasing if someone else needs it.
210 if (CGM.getCodeGenOpts().ForbidGuardVariables)
211 CGM.Error(D.getLocation(),
212 "this initialization requires a guard variable, which "
213 "the kernel does not support");
214
Chandler Carruth0f30a122012-03-30 19:44:53 +0000215 CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +0000216}
217
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000218static llvm::Function *
219CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
Chris Lattner2acc6e32011-07-18 04:24:23 +0000220 llvm::FunctionType *FTy,
NAKAMURA Takumia0786c92012-03-28 16:24:29 +0000221 const Twine &Name) {
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000222 llvm::Function *Fn =
223 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
224 Name, &CGM.getModule());
Richard Smith7edf9e32012-11-01 22:30:59 +0000225 if (!CGM.getLangOpts().AppleKext) {
Fariborz Jahaniand6c9a0f2011-02-15 18:54:46 +0000226 // Set the section if needed.
227 if (const char *Section =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000228 CGM.getContext().getTargetInfo().getStaticInitSectionSpecifier())
Fariborz Jahaniand6c9a0f2011-02-15 18:54:46 +0000229 Fn->setSection(Section);
230 }
Anders Carlsson18af3682010-06-08 22:47:50 +0000231
David Blaikie4e4d0842012-03-11 07:00:24 +0000232 if (!CGM.getLangOpts().Exceptions)
John McCall044cc542010-07-06 04:38:10 +0000233 Fn->setDoesNotThrow();
234
Will Dietz4f45bc02013-01-18 11:30:38 +0000235 if (CGM.getSanOpts().Address)
Kostya Serebryany85aee962013-02-26 06:58:27 +0000236 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
237 if (CGM.getSanOpts().Thread)
238 Fn->addFnAttr(llvm::Attribute::SanitizeThread);
239 if (CGM.getSanOpts().Memory)
240 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
Kostya Serebryanyb9d2b3b2012-06-26 08:56:33 +0000241
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000242 return Fn;
243}
244
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000245void
John McCall3030eb82010-11-06 09:44:32 +0000246CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +0000247 llvm::GlobalVariable *Addr,
248 bool PerformInit) {
Chris Lattner8b418682012-02-07 00:39:47 +0000249 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000250
251 // Create a variable initialization function.
252 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000253 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
Eli Friedman6c6bda32010-01-08 00:50:11 +0000254
Richard Smith7ca48502012-02-13 22:16:19 +0000255 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
256 PerformInit);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000257
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000258 if (D->hasAttr<InitPriorityAttr>()) {
259 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
Chris Lattnerec2830d2010-06-27 06:32:58 +0000260 OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000261 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
John McCallbf40cb52010-07-15 23:40:35 +0000262 DelayedCXXInitPosition.erase(D);
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000263 } else {
John McCallbf40cb52010-07-15 23:40:35 +0000264 llvm::DenseMap<const Decl *, unsigned>::iterator I =
265 DelayedCXXInitPosition.find(D);
266 if (I == DelayedCXXInitPosition.end()) {
267 CXXGlobalInits.push_back(Fn);
268 } else {
269 assert(CXXGlobalInits[I->second] == 0);
270 CXXGlobalInits[I->second] = Fn;
271 DelayedCXXInitPosition.erase(I);
272 }
273 }
Eli Friedman6c6bda32010-01-08 00:50:11 +0000274}
275
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000276void
277CodeGenModule::EmitCXXGlobalInitFunc() {
John McCallbf40cb52010-07-15 23:40:35 +0000278 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
279 CXXGlobalInits.pop_back();
280
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000281 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000282 return;
283
Chris Lattner8b418682012-02-07 00:39:47 +0000284 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000285
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000286
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000287 // Create our global initialization function.
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000288 if (!PrioritizedCXXGlobalInits.empty()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000289 SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
Fariborz Jahanian027d7ed2010-06-21 19:49:38 +0000290 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000291 PrioritizedCXXGlobalInits.end());
292 // Iterate over "chunks" of ctors with same priority and emit each chunk
293 // into separate function. Note - everything is sorted first by priority,
294 // second - by lex order, so we emit ctor functions in proper order.
295 for (SmallVectorImpl<GlobalInitData >::iterator
296 I = PrioritizedCXXGlobalInits.begin(),
297 E = PrioritizedCXXGlobalInits.end(); I != E; ) {
298 SmallVectorImpl<GlobalInitData >::iterator
299 PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
300
301 LocalCXXGlobalInits.clear();
302 unsigned Priority = I->first.priority;
303 // Compute the function suffix from priority. Prepend with zeroes to make
304 // sure the function names are also ordered as priorities.
305 std::string PrioritySuffix = llvm::utostr(Priority);
306 // Priority is always <= 65535 (enforced by sema)..
307 PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
308 llvm::Function *Fn =
309 CreateGlobalInitOrDestructFunction(*this, FTy,
310 "_GLOBAL__I_" + PrioritySuffix);
311
312 for (; I < PrioE; ++I)
313 LocalCXXGlobalInits.push_back(I->second);
314
315 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000316 &LocalCXXGlobalInits[0],
317 LocalCXXGlobalInits.size());
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000318 AddGlobalCtor(Fn, Priority);
319 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000320 }
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000321
322 llvm::Function *Fn =
323 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
324
325 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
326 &CXXGlobalInits[0],
327 CXXGlobalInits.size());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000328 AddGlobalCtor(Fn);
Anton Korobeynikov4179ddd2012-11-06 22:44:45 +0000329
Axel Naumann54ec6c52011-05-06 15:24:04 +0000330 CXXGlobalInits.clear();
331 PrioritizedCXXGlobalInits.clear();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000332}
333
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000334void CodeGenModule::EmitCXXGlobalDtorFunc() {
335 if (CXXGlobalDtors.empty())
336 return;
337
Chris Lattner8b418682012-02-07 00:39:47 +0000338 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000339
340 // Create our global destructor function.
341 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000342 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000343
John McCall3f88f682012-04-06 18:21:03 +0000344 CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000345 AddGlobalDtor(Fn);
346}
347
John McCall3030eb82010-11-06 09:44:32 +0000348/// Emit the code necessary to initialize the given global variable.
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000349void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
John McCall3030eb82010-11-06 09:44:32 +0000350 const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +0000351 llvm::GlobalVariable *Addr,
352 bool PerformInit) {
Alexey Samsonova240df22012-10-16 07:22:28 +0000353 // Check if we need to emit debug info for variable initializer.
354 if (!D->hasAttr<NoDebugAttr>())
355 maybeInitializeDebugInfo();
Nick Lewycky78d1a102012-07-24 01:40:49 +0000356
357 StartFunction(GlobalDecl(D), getContext().VoidTy, Fn,
John McCallde5d3c72012-02-17 03:33:10 +0000358 getTypes().arrangeNullaryFunction(),
Nick Lewycky78d1a102012-07-24 01:40:49 +0000359 FunctionArgList(), D->getInit()->getExprLoc());
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000360
Douglas Gregore67d1512011-07-01 21:54:36 +0000361 // Use guarded initialization if the global variable is weak. This
362 // occurs for, e.g., instantiated static data members and
363 // definitions explicitly marked weak.
364 if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
365 Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
Richard Smith7ca48502012-02-13 22:16:19 +0000366 EmitCXXGuardedInit(*D, Addr, PerformInit);
John McCall3030eb82010-11-06 09:44:32 +0000367 } else {
Richard Smith7ca48502012-02-13 22:16:19 +0000368 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
Fariborz Jahanian92d835a2010-10-26 22:47:47 +0000369 }
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000370
371 FinishFunction();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000372}
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000373
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000374void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
375 llvm::Constant **Decls,
376 unsigned NumDecls) {
Alexey Samsonov34b41f82012-10-25 10:18:50 +0000377 // Initialize debug info if needed.
378 maybeInitializeDebugInfo();
379
John McCalld26bc762011-03-09 04:27:21 +0000380 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
John McCallde5d3c72012-02-17 03:33:10 +0000381 getTypes().arrangeNullaryFunction(),
John McCalld26bc762011-03-09 04:27:21 +0000382 FunctionArgList(), SourceLocation());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000383
John McCallf85e1932011-06-15 23:02:42 +0000384 RunCleanupsScope Scope(*this);
385
386 // When building in Objective-C++ ARC mode, create an autorelease pool
387 // around the global initializers.
David Blaikie4e4d0842012-03-11 07:00:24 +0000388 if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
John McCallf85e1932011-06-15 23:02:42 +0000389 llvm::Value *token = EmitObjCAutoreleasePoolPush();
390 EmitObjCAutoreleasePoolCleanup(token);
391 }
392
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000393 for (unsigned i = 0; i != NumDecls; ++i)
John McCallbf40cb52010-07-15 23:40:35 +0000394 if (Decls[i])
John McCallf85e1932011-06-15 23:02:42 +0000395 Builder.CreateCall(Decls[i]);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000396
John McCallf85e1932011-06-15 23:02:42 +0000397 Scope.ForceCleanup();
398
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000399 FinishFunction();
400}
401
John McCall3f88f682012-04-06 18:21:03 +0000402void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
Chris Lattner810112e2010-06-19 05:52:45 +0000403 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000404 &DtorsAndObjects) {
Alexey Samsonov34b41f82012-10-25 10:18:50 +0000405 // Initialize debug info if needed.
406 maybeInitializeDebugInfo();
407
John McCalld26bc762011-03-09 04:27:21 +0000408 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
John McCallde5d3c72012-02-17 03:33:10 +0000409 getTypes().arrangeNullaryFunction(),
John McCalld26bc762011-03-09 04:27:21 +0000410 FunctionArgList(), SourceLocation());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000411
412 // Emit the dtors, in reverse order from construction.
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000413 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
Chris Lattner810112e2010-06-19 05:52:45 +0000414 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000415 llvm::CallInst *CI = Builder.CreateCall(Callee,
416 DtorsAndObjects[e - i - 1].second);
417 // Make sure the call and the callee agree on calling convention.
418 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
419 CI->setCallingConv(F->getCallingConv());
420 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000421
422 FinishFunction();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000423}
424
John McCalla91f6662011-07-13 03:01:35 +0000425/// generateDestroyHelper - Generates a helper function which, when
426/// invoked, destroys the given object.
Anders Carlsson77291362010-06-08 22:17:27 +0000427llvm::Function *
John McCalla91f6662011-07-13 03:01:35 +0000428CodeGenFunction::generateDestroyHelper(llvm::Constant *addr,
429 QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +0000430 Destroyer *destroyer,
John McCalla91f6662011-07-13 03:01:35 +0000431 bool useEHCleanupForArray) {
John McCalld26bc762011-03-09 04:27:21 +0000432 FunctionArgList args;
433 ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy);
434 args.push_back(&dst);
Anders Carlsson77291362010-06-08 22:17:27 +0000435
Anders Carlsson77291362010-06-08 22:17:27 +0000436 const CGFunctionInfo &FI =
John McCallde5d3c72012-02-17 03:33:10 +0000437 CGM.getTypes().arrangeFunctionDeclaration(getContext().VoidTy, args,
438 FunctionType::ExtInfo(),
439 /*variadic*/ false);
440 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
John McCalla91f6662011-07-13 03:01:35 +0000441 llvm::Function *fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000442 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
Anders Carlsson77291362010-06-08 22:17:27 +0000443
Alexey Samsonov34b41f82012-10-25 10:18:50 +0000444 // Initialize debug info if needed.
445 maybeInitializeDebugInfo();
446
John McCalla91f6662011-07-13 03:01:35 +0000447 StartFunction(GlobalDecl(), getContext().VoidTy, fn, FI, args,
John McCalld26bc762011-03-09 04:27:21 +0000448 SourceLocation());
Anders Carlsson77291362010-06-08 22:17:27 +0000449
John McCalla91f6662011-07-13 03:01:35 +0000450 emitDestroy(addr, type, destroyer, useEHCleanupForArray);
Anders Carlsson77291362010-06-08 22:17:27 +0000451
452 FinishFunction();
453
John McCalla91f6662011-07-13 03:01:35 +0000454 return fn;
Anders Carlsson77291362010-06-08 22:17:27 +0000455}