blob: 609f6aa7c6e6ee92b75b1bca63c6453185aaec70 [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"
John McCall5d865c322010-08-31 07:33:07 +000015#include "CGCXXABI.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "CGObjCRuntime.h"
Chandler Carruth85098242010-06-15 23:19:56 +000017#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikovabed7492012-11-06 22:44:45 +000018#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000019#include "llvm/IR/Intrinsics.h"
Douglas Gregor51150ab2010-05-16 01:24:12 +000020
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000021using namespace clang;
22using namespace CodeGen;
23
Anders Carlsson364051c2009-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 Carlsson364051c2009-12-10 00:57:45 +000030 ASTContext &Context = CGF.getContext();
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000031
Eli Friedmana0544d62011-12-03 04:14:32 +000032 CharUnits alignment = Context.getDeclAlign(&D);
John McCall1553b192011-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();
John McCall47fb9502013-03-07 21:37:08 +000037 switch (CGF.getEvaluationKind(type)) {
38 case TEK_Scalar: {
Fariborz Jahaniand1339792011-01-13 20:00:54 +000039 CodeGenModule &CGM = CGF.CGM;
John McCall1553b192011-06-16 04:16:24 +000040 if (lv.isObjCStrong())
John McCall31168b02011-06-15 23:02:42 +000041 CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
Richard Smithfd3834f2013-04-13 02:43:54 +000042 DeclPtr, D.getTLSKind());
John McCall1553b192011-06-16 04:16:24 +000043 else if (lv.isObjCWeak())
44 CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
45 DeclPtr);
Fariborz Jahaniand1339792011-01-13 20:00:54 +000046 else
John McCall1553b192011-06-16 04:16:24 +000047 CGF.EmitScalarInit(Init, &D, lv, false);
John McCall47fb9502013-03-07 21:37:08 +000048 return;
49 }
50 case TEK_Complex:
51 CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
52 return;
53 case TEK_Aggregate:
Chad Rosier615ed1a2012-03-29 17:37:10 +000054 CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
55 AggValueSlot::DoesNotNeedGCBarriers,
56 AggValueSlot::IsNotAliased));
John McCall47fb9502013-03-07 21:37:08 +000057 return;
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000058 }
John McCall47fb9502013-03-07 21:37:08 +000059 llvm_unreachable("bad evaluation kind");
Anders Carlssonbc49cfe2009-12-10 00:16:00 +000060}
61
John McCall68ff0372010-09-08 01:44:27 +000062/// Emit code to cause the destruction of the given variable with
63/// static storage duration.
Douglas Gregor370eadf2010-05-05 15:38:32 +000064static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
John McCall98de3d72011-07-13 03:01:35 +000065 llvm::Constant *addr) {
Douglas Gregor370eadf2010-05-05 15:38:32 +000066 CodeGenModule &CGM = CGF.CGM;
John McCall98de3d72011-07-13 03:01:35 +000067
68 // FIXME: __attribute__((cleanup)) ?
Douglas Gregor370eadf2010-05-05 15:38:32 +000069
John McCall98de3d72011-07-13 03:01:35 +000070 QualType type = D.getType();
71 QualType::DestructionKind dtorKind = type.isDestructedType();
72
73 switch (dtorKind) {
74 case QualType::DK_none:
Douglas Gregor370eadf2010-05-05 15:38:32 +000075 return;
John McCall98de3d72011-07-13 03:01:35 +000076
77 case QualType::DK_cxx_destructor:
78 break;
79
80 case QualType::DK_objc_strong_lifetime:
81 case QualType::DK_objc_weak_lifetime:
82 // We don't care about releasing objects during process teardown.
Douglas Gregor370eadf2010-05-05 15:38:32 +000083 return;
John McCall98de3d72011-07-13 03:01:35 +000084 }
85
86 llvm::Constant *function;
87 llvm::Constant *argument;
88
89 // Special-case non-array C++ destructors, where there's a function
90 // with the right signature that we can just call.
91 const CXXRecordDecl *record = 0;
92 if (dtorKind == QualType::DK_cxx_destructor &&
93 (record = type->getAsCXXRecordDecl())) {
94 assert(!record->hasTrivialDestructor());
95 CXXDestructorDecl *dtor = record->getDestructor();
96
97 function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete);
98 argument = addr;
99
100 // Otherwise, the standard logic requires a helper function.
101 } else {
102 function = CodeGenFunction(CGM).generateDestroyHelper(addr, type,
103 CGF.getDestroyer(dtorKind),
104 CGF.needsEHCleanup(dtorKind));
105 argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
106 }
107
John McCallc84ed6a2012-05-01 06:13:13 +0000108 CGM.getCXXABI().registerGlobalDtor(CGF, function, argument);
Douglas Gregor370eadf2010-05-05 15:38:32 +0000109}
110
Richard Smith08a51442012-02-17 07:31:37 +0000111/// Emit code to cause the variable at the given address to be considered as
112/// constant from this point onwards.
Nick Lewycky45062042012-02-21 00:26:58 +0000113static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
114 llvm::Constant *Addr) {
Richard Smith132bea92012-02-17 20:12:52 +0000115 // Don't emit the intrinsic if we're not optimizing.
116 if (!CGF.CGM.getCodeGenOpts().OptimizationLevel)
117 return;
118
Richard Smith08a51442012-02-17 07:31:37 +0000119 // Grab the llvm.invariant.start intrinsic.
120 llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
121 llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
122
Nick Lewycky45062042012-02-21 00:26:58 +0000123 // Emit a call with the size in bytes of the object.
124 CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType());
125 uint64_t Width = WidthChars.getQuantity();
126 llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width),
Richard Smith08a51442012-02-17 07:31:37 +0000127 llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
128 CGF.Builder.CreateCall(InvariantStart, Args);
129}
130
Anders Carlsson364051c2009-12-10 00:57:45 +0000131void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Richard Smith6331c402012-02-13 22:16:19 +0000132 llvm::Constant *DeclPtr,
133 bool PerformInit) {
Anders Carlsson364051c2009-12-10 00:57:45 +0000134
135 const Expr *Init = D.getInit();
136 QualType T = D.getType();
137
138 if (!T->isReferenceType()) {
Richard Smith6331c402012-02-13 22:16:19 +0000139 if (PerformInit)
140 EmitDeclInit(*this, D, DeclPtr);
Richard Smith08a51442012-02-17 07:31:37 +0000141 if (CGM.isTypeConstant(D.getType(), true))
Nick Lewycky45062042012-02-21 00:26:58 +0000142 EmitDeclInvariant(*this, D, DeclPtr);
Richard Smith08a51442012-02-17 07:31:37 +0000143 else
144 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlsson364051c2009-12-10 00:57:45 +0000145 return;
146 }
Anders Carlsson3f48c602010-06-27 17:52:15 +0000147
Richard Smith6331c402012-02-13 22:16:19 +0000148 assert(PerformInit && "cannot have constant initializer which needs "
149 "destruction for reference");
Daniel Dunbar03816342010-08-21 02:24:36 +0000150 unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
Anders Carlsson3f48c602010-06-27 17:52:15 +0000151 RValue RV = EmitReferenceBindingToExpr(Init, &D);
Daniel Dunbar03816342010-08-21 02:24:36 +0000152 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
Anders Carlsson364051c2009-12-10 00:57:45 +0000153}
Anders Carlsson633c6f62009-12-10 00:30:05 +0000154
John McCall76cc43a2012-04-06 18:21:06 +0000155static llvm::Function *
156CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
157 llvm::FunctionType *ty,
158 const Twine &name);
159
160/// Create a stub function, suitable for being passed to atexit,
161/// which passes the given address to the given destructor function.
162static llvm::Constant *createAtExitStub(CodeGenModule &CGM,
163 llvm::Constant *dtor,
164 llvm::Constant *addr) {
165 // Get the destructor function type, void(*)(void).
166 llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
167 llvm::Function *fn =
168 CreateGlobalInitOrDestructFunction(CGM, ty,
169 Twine("__dtor_", addr->getName()));
170
171 CodeGenFunction CGF(CGM);
172
Alexey Samsonov9b502e52012-10-25 10:18:50 +0000173 // Initialize debug info if needed.
174 CGF.maybeInitializeDebugInfo();
175
John McCall76cc43a2012-04-06 18:21:06 +0000176 CGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, fn,
177 CGM.getTypes().arrangeNullaryFunction(),
178 FunctionArgList(), SourceLocation());
179
180 llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
181
182 // Make sure the call and the callee agree on calling convention.
183 if (llvm::Function *dtorFn =
184 dyn_cast<llvm::Function>(dtor->stripPointerCasts()))
185 call->setCallingConv(dtorFn->getCallingConv());
186
187 CGF.FinishFunction();
188
189 return fn;
190}
191
John McCallc84ed6a2012-05-01 06:13:13 +0000192/// Register a global destructor using the C atexit runtime function.
193void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtor,
194 llvm::Constant *addr) {
John McCall76cc43a2012-04-06 18:21:06 +0000195 // Create a function which calls the destructor.
John McCallc84ed6a2012-05-01 06:13:13 +0000196 llvm::Constant *dtorStub = createAtExitStub(CGM, dtor, addr);
John McCall76cc43a2012-04-06 18:21:06 +0000197
198 // extern "C" int atexit(void (*f)(void));
199 llvm::FunctionType *atexitTy =
John McCallc84ed6a2012-05-01 06:13:13 +0000200 llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
John McCall76cc43a2012-04-06 18:21:06 +0000201
202 llvm::Constant *atexit =
John McCallc84ed6a2012-05-01 06:13:13 +0000203 CGM.CreateRuntimeFunction(atexitTy, "atexit");
John McCall76cc43a2012-04-06 18:21:06 +0000204 if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit))
205 atexitFn->setDoesNotThrow();
206
John McCall882987f2013-02-28 19:01:20 +0000207 EmitNounwindRuntimeCall(atexit, dtorStub);
Anders Carlsson633c6f62009-12-10 00:30:05 +0000208}
209
John McCallcdf7ef52010-11-06 09:44:32 +0000210void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
Chandler Carruth84537952012-03-30 19:44:53 +0000211 llvm::GlobalVariable *DeclPtr,
Richard Smith6331c402012-02-13 22:16:19 +0000212 bool PerformInit) {
John McCall7ef5cb32011-03-18 02:56:14 +0000213 // If we've been asked to forbid guard variables, emit an error now.
214 // This diagnostic is hard-coded for Darwin's use case; we can find
215 // better phrasing if someone else needs it.
216 if (CGM.getCodeGenOpts().ForbidGuardVariables)
217 CGM.Error(D.getLocation(),
218 "this initialization requires a guard variable, which "
219 "the kernel does not support");
220
Richard Smithfd3834f2013-04-13 02:43:54 +0000221 if (D.getTLSKind())
222 CGM.ErrorUnsupported(D.getInit(), "dynamic TLS initialization");
223
Chandler Carruth84537952012-03-30 19:44:53 +0000224 CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
John McCall68ff0372010-09-08 01:44:27 +0000225}
226
Anders Carlssonb2839e42010-06-08 22:40:05 +0000227static llvm::Function *
228CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
Chris Lattner2192fe52011-07-18 04:24:23 +0000229 llvm::FunctionType *FTy,
NAKAMURA Takumi9c7f1242012-03-28 16:24:29 +0000230 const Twine &Name) {
Anders Carlssonb2839e42010-06-08 22:40:05 +0000231 llvm::Function *Fn =
232 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
233 Name, &CGM.getModule());
Richard Smith9c6890a2012-11-01 22:30:59 +0000234 if (!CGM.getLangOpts().AppleKext) {
Fariborz Jahanian09948f12011-02-15 18:54:46 +0000235 // Set the section if needed.
236 if (const char *Section =
Douglas Gregore8bbc122011-09-02 00:18:52 +0000237 CGM.getContext().getTargetInfo().getStaticInitSectionSpecifier())
Fariborz Jahanian09948f12011-02-15 18:54:46 +0000238 Fn->setSection(Section);
239 }
Anders Carlsson851318a2010-06-08 22:47:50 +0000240
John McCall882987f2013-02-28 19:01:20 +0000241 Fn->setCallingConv(CGM.getRuntimeCC());
242
David Blaikiebbafb8a2012-03-11 07:00:24 +0000243 if (!CGM.getLangOpts().Exceptions)
John McCall466e2212010-07-06 04:38:10 +0000244 Fn->setDoesNotThrow();
245
Will Dietzf54319c2013-01-18 11:30:38 +0000246 if (CGM.getSanOpts().Address)
Kostya Serebryany4c0fc992013-02-26 06:58:27 +0000247 Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
248 if (CGM.getSanOpts().Thread)
249 Fn->addFnAttr(llvm::Attribute::SanitizeThread);
250 if (CGM.getSanOpts().Memory)
251 Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
Kostya Serebryanybf84b8f2012-06-26 08:56:33 +0000252
Anders Carlssonb2839e42010-06-08 22:40:05 +0000253 return Fn;
254}
255
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000256void
John McCallcdf7ef52010-11-06 09:44:32 +0000257CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith6331c402012-02-13 22:16:19 +0000258 llvm::GlobalVariable *Addr,
259 bool PerformInit) {
Richard Smithfd3834f2013-04-13 02:43:54 +0000260 if (D->getTLSKind())
261 ErrorUnsupported(D->getInit(), "dynamic TLS initialization");
262
Chris Lattnerece04092012-02-07 00:39:47 +0000263 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Eli Friedman5866fe32010-01-08 00:50:11 +0000264
265 // Create a variable initialization function.
266 llvm::Function *Fn =
Anders Carlssonb2839e42010-06-08 22:40:05 +0000267 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
Eli Friedman5866fe32010-01-08 00:50:11 +0000268
Richard Smith6331c402012-02-13 22:16:19 +0000269 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
270 PerformInit);
Eli Friedman5866fe32010-01-08 00:50:11 +0000271
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000272 if (D->hasAttr<InitPriorityAttr>()) {
273 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
Chris Lattnere0009072010-06-27 06:32:58 +0000274 OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
Fariborz Jahanian89bdd142010-06-21 21:27:42 +0000275 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
John McCall70013b62010-07-15 23:40:35 +0000276 DelayedCXXInitPosition.erase(D);
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000277 } else {
John McCall70013b62010-07-15 23:40:35 +0000278 llvm::DenseMap<const Decl *, unsigned>::iterator I =
279 DelayedCXXInitPosition.find(D);
280 if (I == DelayedCXXInitPosition.end()) {
281 CXXGlobalInits.push_back(Fn);
282 } else {
283 assert(CXXGlobalInits[I->second] == 0);
284 CXXGlobalInits[I->second] = Fn;
285 DelayedCXXInitPosition.erase(I);
286 }
287 }
Eli Friedman5866fe32010-01-08 00:50:11 +0000288}
289
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000290void
291CodeGenModule::EmitCXXGlobalInitFunc() {
John McCall70013b62010-07-15 23:40:35 +0000292 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
293 CXXGlobalInits.pop_back();
294
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000295 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
Anders Carlsson633c6f62009-12-10 00:30:05 +0000296 return;
297
Chris Lattnerece04092012-02-07 00:39:47 +0000298 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Anders Carlsson633c6f62009-12-10 00:30:05 +0000299
Anders Carlsson633c6f62009-12-10 00:30:05 +0000300
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000301 // Create our global initialization function.
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000302 if (!PrioritizedCXXGlobalInits.empty()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000303 SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
Fariborz Jahanian090e4e52010-06-21 19:49:38 +0000304 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000305 PrioritizedCXXGlobalInits.end());
306 // Iterate over "chunks" of ctors with same priority and emit each chunk
307 // into separate function. Note - everything is sorted first by priority,
308 // second - by lex order, so we emit ctor functions in proper order.
309 for (SmallVectorImpl<GlobalInitData >::iterator
310 I = PrioritizedCXXGlobalInits.begin(),
311 E = PrioritizedCXXGlobalInits.end(); I != E; ) {
312 SmallVectorImpl<GlobalInitData >::iterator
313 PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
314
315 LocalCXXGlobalInits.clear();
316 unsigned Priority = I->first.priority;
317 // Compute the function suffix from priority. Prepend with zeroes to make
318 // sure the function names are also ordered as priorities.
319 std::string PrioritySuffix = llvm::utostr(Priority);
320 // Priority is always <= 65535 (enforced by sema)..
321 PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
322 llvm::Function *Fn =
323 CreateGlobalInitOrDestructFunction(*this, FTy,
324 "_GLOBAL__I_" + PrioritySuffix);
325
326 for (; I < PrioE; ++I)
327 LocalCXXGlobalInits.push_back(I->second);
328
329 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000330 &LocalCXXGlobalInits[0],
331 LocalCXXGlobalInits.size());
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000332 AddGlobalCtor(Fn, Priority);
333 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +0000334 }
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000335
336 llvm::Function *Fn =
337 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
338
339 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
340 &CXXGlobalInits[0],
341 CXXGlobalInits.size());
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000342 AddGlobalCtor(Fn);
Anton Korobeynikovabed7492012-11-06 22:44:45 +0000343
Axel Naumannbd26a582011-05-06 15:24:04 +0000344 CXXGlobalInits.clear();
345 PrioritizedCXXGlobalInits.clear();
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000346}
347
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000348void CodeGenModule::EmitCXXGlobalDtorFunc() {
349 if (CXXGlobalDtors.empty())
350 return;
351
Chris Lattnerece04092012-02-07 00:39:47 +0000352 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000353
354 // Create our global destructor function.
355 llvm::Function *Fn =
Anders Carlssonb2839e42010-06-08 22:40:05 +0000356 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000357
John McCallee08c532012-04-06 18:21:03 +0000358 CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000359 AddGlobalDtor(Fn);
360}
361
John McCallcdf7ef52010-11-06 09:44:32 +0000362/// Emit the code necessary to initialize the given global variable.
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000363void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
John McCallcdf7ef52010-11-06 09:44:32 +0000364 const VarDecl *D,
Richard Smith6331c402012-02-13 22:16:19 +0000365 llvm::GlobalVariable *Addr,
366 bool PerformInit) {
Alexey Samsonov38e24962012-10-16 07:22:28 +0000367 // Check if we need to emit debug info for variable initializer.
368 if (!D->hasAttr<NoDebugAttr>())
369 maybeInitializeDebugInfo();
Nick Lewycky08597072012-07-24 01:40:49 +0000370
371 StartFunction(GlobalDecl(D), getContext().VoidTy, Fn,
John McCalla729c622012-02-17 03:33:10 +0000372 getTypes().arrangeNullaryFunction(),
Nick Lewycky08597072012-07-24 01:40:49 +0000373 FunctionArgList(), D->getInit()->getExprLoc());
Daniel Dunbar75722842010-03-20 04:15:29 +0000374
Douglas Gregorfa918f62011-07-01 21:54:36 +0000375 // Use guarded initialization if the global variable is weak. This
376 // occurs for, e.g., instantiated static data members and
377 // definitions explicitly marked weak.
378 if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
379 Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
Richard Smith6331c402012-02-13 22:16:19 +0000380 EmitCXXGuardedInit(*D, Addr, PerformInit);
John McCallcdf7ef52010-11-06 09:44:32 +0000381 } else {
Richard Smith6331c402012-02-13 22:16:19 +0000382 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
Fariborz Jahanian67ca8c42010-10-26 22:47:47 +0000383 }
Daniel Dunbar75722842010-03-20 04:15:29 +0000384
385 FinishFunction();
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000386}
Daniel Dunbar75722842010-03-20 04:15:29 +0000387
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000388void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
389 llvm::Constant **Decls,
390 unsigned NumDecls) {
Alexey Samsonov9b502e52012-10-25 10:18:50 +0000391 // Initialize debug info if needed.
392 maybeInitializeDebugInfo();
393
John McCalla738c252011-03-09 04:27:21 +0000394 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
John McCalla729c622012-02-17 03:33:10 +0000395 getTypes().arrangeNullaryFunction(),
John McCalla738c252011-03-09 04:27:21 +0000396 FunctionArgList(), SourceLocation());
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000397
John McCall31168b02011-06-15 23:02:42 +0000398 RunCleanupsScope Scope(*this);
399
400 // When building in Objective-C++ ARC mode, create an autorelease pool
401 // around the global initializers.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000402 if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
John McCall31168b02011-06-15 23:02:42 +0000403 llvm::Value *token = EmitObjCAutoreleasePoolPush();
404 EmitObjCAutoreleasePoolCleanup(token);
405 }
406
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000407 for (unsigned i = 0; i != NumDecls; ++i)
John McCall70013b62010-07-15 23:40:35 +0000408 if (Decls[i])
John McCall882987f2013-02-28 19:01:20 +0000409 EmitRuntimeCall(Decls[i]);
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000410
John McCall31168b02011-06-15 23:02:42 +0000411 Scope.ForceCleanup();
412
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000413 FinishFunction();
414}
415
John McCallee08c532012-04-06 18:21:03 +0000416void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
Chris Lattner87233f72010-06-19 05:52:45 +0000417 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000418 &DtorsAndObjects) {
Alexey Samsonov9b502e52012-10-25 10:18:50 +0000419 // Initialize debug info if needed.
420 maybeInitializeDebugInfo();
421
John McCalla738c252011-03-09 04:27:21 +0000422 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
John McCalla729c622012-02-17 03:33:10 +0000423 getTypes().arrangeNullaryFunction(),
John McCalla738c252011-03-09 04:27:21 +0000424 FunctionArgList(), SourceLocation());
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000425
426 // Emit the dtors, in reverse order from construction.
Chris Lattner5e8416a2010-04-26 20:35:54 +0000427 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
Chris Lattner87233f72010-06-19 05:52:45 +0000428 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
Chris Lattner5e8416a2010-04-26 20:35:54 +0000429 llvm::CallInst *CI = Builder.CreateCall(Callee,
430 DtorsAndObjects[e - i - 1].second);
431 // Make sure the call and the callee agree on calling convention.
432 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
433 CI->setCallingConv(F->getCallingConv());
434 }
Daniel Dunbarfe06df42010-03-20 04:15:41 +0000435
436 FinishFunction();
Anders Carlsson633c6f62009-12-10 00:30:05 +0000437}
438
John McCall98de3d72011-07-13 03:01:35 +0000439/// generateDestroyHelper - Generates a helper function which, when
440/// invoked, destroys the given object.
Anders Carlsson282bc102010-06-08 22:17:27 +0000441llvm::Function *
John McCall98de3d72011-07-13 03:01:35 +0000442CodeGenFunction::generateDestroyHelper(llvm::Constant *addr,
443 QualType type,
Peter Collingbourne1425b452012-01-26 03:33:36 +0000444 Destroyer *destroyer,
John McCall98de3d72011-07-13 03:01:35 +0000445 bool useEHCleanupForArray) {
John McCalla738c252011-03-09 04:27:21 +0000446 FunctionArgList args;
447 ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy);
448 args.push_back(&dst);
Anders Carlsson282bc102010-06-08 22:17:27 +0000449
Anders Carlsson282bc102010-06-08 22:17:27 +0000450 const CGFunctionInfo &FI =
John McCalla729c622012-02-17 03:33:10 +0000451 CGM.getTypes().arrangeFunctionDeclaration(getContext().VoidTy, args,
452 FunctionType::ExtInfo(),
453 /*variadic*/ false);
454 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
John McCall98de3d72011-07-13 03:01:35 +0000455 llvm::Function *fn =
Anders Carlssonb2839e42010-06-08 22:40:05 +0000456 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
Anders Carlsson282bc102010-06-08 22:17:27 +0000457
Alexey Samsonov9b502e52012-10-25 10:18:50 +0000458 // Initialize debug info if needed.
459 maybeInitializeDebugInfo();
460
John McCall98de3d72011-07-13 03:01:35 +0000461 StartFunction(GlobalDecl(), getContext().VoidTy, fn, FI, args,
John McCalla738c252011-03-09 04:27:21 +0000462 SourceLocation());
Anders Carlsson282bc102010-06-08 22:17:27 +0000463
John McCall98de3d72011-07-13 03:01:35 +0000464 emitDestroy(addr, type, destroyer, useEHCleanupForArray);
Anders Carlsson282bc102010-06-08 22:17:27 +0000465
466 FinishFunction();
467
John McCall98de3d72011-07-13 03:01:35 +0000468 return fn;
Anders Carlsson282bc102010-06-08 22:17:27 +0000469}