Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 1 | //===--- 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 McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/CodeGenOptions.h" |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Intrinsics.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 21 | |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 25 | static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, |
| 26 | llvm::Constant *DeclPtr) { |
| 27 | assert(D.hasGlobalStorage() && "VarDecl must have global storage!"); |
| 28 | assert(!D.getType()->isReferenceType() && |
| 29 | "Should not call EmitDeclInit on a reference!"); |
| 30 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 31 | ASTContext &Context = CGF.getContext(); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 32 | |
Eli Friedman | 6da2c71 | 2011-12-03 04:14:32 +0000 | [diff] [blame] | 33 | CharUnits alignment = Context.getDeclAlign(&D); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 34 | QualType type = D.getType(); |
| 35 | LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment); |
| 36 | |
| 37 | const Expr *Init = D.getInit(); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 38 | switch (CGF.getEvaluationKind(type)) { |
| 39 | case TEK_Scalar: { |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 40 | CodeGenModule &CGM = CGF.CGM; |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 41 | if (lv.isObjCStrong()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 42 | CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init), |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 43 | DeclPtr, D.getTLSKind()); |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 44 | else if (lv.isObjCWeak()) |
| 45 | CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init), |
| 46 | DeclPtr); |
Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 47 | else |
John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 48 | CGF.EmitScalarInit(Init, &D, lv, false); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | case TEK_Complex: |
| 52 | CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true); |
| 53 | return; |
| 54 | case TEK_Aggregate: |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 55 | CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed, |
| 56 | AggValueSlot::DoesNotNeedGCBarriers, |
| 57 | AggValueSlot::IsNotAliased)); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 58 | return; |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 59 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 60 | llvm_unreachable("bad evaluation kind"); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 61 | } |
| 62 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 63 | /// Emit code to cause the destruction of the given variable with |
| 64 | /// static storage duration. |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 65 | static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 66 | llvm::Constant *addr) { |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 67 | CodeGenModule &CGM = CGF.CGM; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 68 | |
| 69 | // FIXME: __attribute__((cleanup)) ? |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 70 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 71 | QualType type = D.getType(); |
| 72 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 73 | |
| 74 | switch (dtorKind) { |
| 75 | case QualType::DK_none: |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 76 | return; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 77 | |
| 78 | case QualType::DK_cxx_destructor: |
| 79 | break; |
| 80 | |
| 81 | case QualType::DK_objc_strong_lifetime: |
| 82 | case QualType::DK_objc_weak_lifetime: |
| 83 | // We don't care about releasing objects during process teardown. |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 84 | assert(!D.getTLSKind() && "should have rejected this"); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 85 | return; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | llvm::Constant *function; |
| 89 | llvm::Constant *argument; |
| 90 | |
| 91 | // Special-case non-array C++ destructors, where there's a function |
| 92 | // with the right signature that we can just call. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 93 | const CXXRecordDecl *record = nullptr; |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 94 | if (dtorKind == QualType::DK_cxx_destructor && |
| 95 | (record = type->getAsCXXRecordDecl())) { |
| 96 | assert(!record->hasTrivialDestructor()); |
| 97 | CXXDestructorDecl *dtor = record->getDestructor(); |
| 98 | |
| 99 | function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete); |
Timur Iskhodzhanov | 9a3be4c | 2013-10-02 16:03:16 +0000 | [diff] [blame] | 100 | argument = llvm::ConstantExpr::getBitCast( |
| 101 | addr, CGF.getTypes().ConvertType(type)->getPointerTo()); |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 102 | |
| 103 | // Otherwise, the standard logic requires a helper function. |
| 104 | } else { |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 105 | function = CodeGenFunction(CGM) |
| 106 | .generateDestroyHelper(addr, type, CGF.getDestroyer(dtorKind), |
| 107 | CGF.needsEHCleanup(dtorKind), &D); |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 108 | argument = llvm::Constant::getNullValue(CGF.Int8PtrTy); |
| 109 | } |
| 110 | |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 111 | CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument); |
Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 114 | /// Emit code to cause the variable at the given address to be considered as |
| 115 | /// constant from this point onwards. |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 116 | static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, |
| 117 | llvm::Constant *Addr) { |
Richard Smith | 00a8c3f | 2012-02-17 20:12:52 +0000 | [diff] [blame] | 118 | // Don't emit the intrinsic if we're not optimizing. |
| 119 | if (!CGF.CGM.getCodeGenOpts().OptimizationLevel) |
| 120 | return; |
| 121 | |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 122 | // Grab the llvm.invariant.start intrinsic. |
| 123 | llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start; |
| 124 | llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID); |
| 125 | |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 126 | // Emit a call with the size in bytes of the object. |
| 127 | CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType()); |
| 128 | uint64_t Width = WidthChars.getQuantity(); |
| 129 | llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width), |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 130 | llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)}; |
| 131 | CGF.Builder.CreateCall(InvariantStart, Args); |
| 132 | } |
| 133 | |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 134 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 135 | llvm::Constant *DeclPtr, |
| 136 | bool PerformInit) { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 137 | |
| 138 | const Expr *Init = D.getInit(); |
| 139 | QualType T = D.getType(); |
| 140 | |
| 141 | if (!T->isReferenceType()) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 142 | if (PerformInit) |
| 143 | EmitDeclInit(*this, D, DeclPtr); |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 144 | if (CGM.isTypeConstant(D.getType(), true)) |
Nick Lewycky | ef78446 | 2012-02-21 00:26:58 +0000 | [diff] [blame] | 145 | EmitDeclInvariant(*this, D, DeclPtr); |
Richard Smith | abb9432 | 2012-02-17 07:31:37 +0000 | [diff] [blame] | 146 | else |
| 147 | EmitDeclDestroy(*this, D, DeclPtr); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 150 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 151 | assert(PerformInit && "cannot have constant initializer which needs " |
| 152 | "destruction for reference"); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 153 | unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); |
Richard Smith | d4ec562 | 2013-06-12 23:38:09 +0000 | [diff] [blame] | 154 | RValue RV = EmitReferenceBindingToExpr(Init); |
Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 155 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T); |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 156 | } |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 157 | |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 158 | static llvm::Function * |
| 159 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, |
| 160 | llvm::FunctionType *ty, |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 161 | const Twine &name, |
| 162 | bool TLS = false); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 163 | |
| 164 | /// Create a stub function, suitable for being passed to atexit, |
| 165 | /// which passes the given address to the given destructor function. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 166 | static llvm::Constant *createAtExitStub(CodeGenModule &CGM, const VarDecl &VD, |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 167 | llvm::Constant *dtor, |
| 168 | llvm::Constant *addr) { |
| 169 | // Get the destructor function type, void(*)(void). |
| 170 | llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false); |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 171 | SmallString<256> FnName; |
| 172 | { |
| 173 | llvm::raw_svector_ostream Out(FnName); |
| 174 | CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out); |
| 175 | } |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 176 | llvm::Function *fn = |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 177 | CreateGlobalInitOrDestructFunction(CGM, ty, FnName.str()); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 178 | |
| 179 | CodeGenFunction CGF(CGM); |
| 180 | |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 181 | CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 182 | CGM.getTypes().arrangeNullaryFunction(), FunctionArgList()); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 183 | |
| 184 | llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr); |
| 185 | |
| 186 | // Make sure the call and the callee agree on calling convention. |
| 187 | if (llvm::Function *dtorFn = |
| 188 | dyn_cast<llvm::Function>(dtor->stripPointerCasts())) |
| 189 | call->setCallingConv(dtorFn->getCallingConv()); |
| 190 | |
| 191 | CGF.FinishFunction(); |
| 192 | |
| 193 | return fn; |
| 194 | } |
| 195 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 196 | /// Register a global destructor using the C atexit runtime function. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 197 | void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD, |
| 198 | llvm::Constant *dtor, |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 199 | llvm::Constant *addr) { |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 200 | // Create a function which calls the destructor. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 201 | llvm::Constant *dtorStub = createAtExitStub(CGM, VD, dtor, addr); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 202 | |
| 203 | // extern "C" int atexit(void (*f)(void)); |
| 204 | llvm::FunctionType *atexitTy = |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 205 | llvm::FunctionType::get(IntTy, dtorStub->getType(), false); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 206 | |
| 207 | llvm::Constant *atexit = |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 208 | CGM.CreateRuntimeFunction(atexitTy, "atexit"); |
John McCall | 30fa370 | 2012-04-06 18:21:06 +0000 | [diff] [blame] | 209 | if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit)) |
| 210 | atexitFn->setDoesNotThrow(); |
| 211 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 212 | EmitNounwindRuntimeCall(atexit, dtorStub); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 213 | } |
| 214 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 215 | void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 216 | llvm::GlobalVariable *DeclPtr, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 217 | bool PerformInit) { |
John McCall | 3209669 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 218 | // If we've been asked to forbid guard variables, emit an error now. |
| 219 | // This diagnostic is hard-coded for Darwin's use case; we can find |
| 220 | // better phrasing if someone else needs it. |
| 221 | if (CGM.getCodeGenOpts().ForbidGuardVariables) |
| 222 | CGM.Error(D.getLocation(), |
| 223 | "this initialization requires a guard variable, which " |
| 224 | "the kernel does not support"); |
| 225 | |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 226 | CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 229 | static llvm::Function * |
| 230 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 231 | llvm::FunctionType *FTy, |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 232 | const Twine &Name, bool TLS) { |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 233 | llvm::Function *Fn = |
| 234 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 235 | Name, &CGM.getModule()); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 236 | if (!CGM.getLangOpts().AppleKext && !TLS) { |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 237 | // Set the section if needed. |
| 238 | if (const char *Section = |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 239 | CGM.getTarget().getStaticInitSectionSpecifier()) |
Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 240 | Fn->setSection(Section); |
| 241 | } |
Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 242 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 243 | Fn->setCallingConv(CGM.getRuntimeCC()); |
| 244 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 245 | if (!CGM.getLangOpts().Exceptions) |
John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 246 | Fn->setDoesNotThrow(); |
| 247 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 248 | if (!CGM.getSanitizerBlacklist().isIn(*Fn)) { |
| 249 | if (CGM.getLangOpts().Sanitize.Address) |
| 250 | Fn->addFnAttr(llvm::Attribute::SanitizeAddress); |
| 251 | if (CGM.getLangOpts().Sanitize.Thread) |
| 252 | Fn->addFnAttr(llvm::Attribute::SanitizeThread); |
| 253 | if (CGM.getLangOpts().Sanitize.Memory) |
| 254 | Fn->addFnAttr(llvm::Attribute::SanitizeMemory); |
| 255 | } |
Kostya Serebryany | b9d2b3b | 2012-06-26 08:56:33 +0000 | [diff] [blame] | 256 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 257 | return Fn; |
| 258 | } |
| 259 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 260 | void |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 261 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 262 | llvm::GlobalVariable *Addr, |
| 263 | bool PerformInit) { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 264 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Reid Kleckner | c5c6fa7 | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 265 | SmallString<256> FnName; |
| 266 | { |
| 267 | llvm::raw_svector_ostream Out(FnName); |
| 268 | getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out); |
| 269 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 270 | |
| 271 | // Create a variable initialization function. |
| 272 | llvm::Function *Fn = |
Reid Kleckner | c5c6fa7 | 2013-09-10 20:43:12 +0000 | [diff] [blame] | 273 | CreateGlobalInitOrDestructFunction(*this, FTy, FnName.str()); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 274 | |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 275 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr, |
| 276 | PerformInit); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 277 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 278 | if (D->getTLSKind()) { |
| 279 | // FIXME: Should we support init_priority for thread_local? |
| 280 | // FIXME: Ideally, initialization of instantiated thread_local static data |
| 281 | // members of class templates should not trigger initialization of other |
| 282 | // entities in the TU. |
| 283 | // FIXME: We only need to register one __cxa_thread_atexit function for the |
| 284 | // entire TU. |
| 285 | CXXThreadLocalInits.push_back(Fn); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 286 | } else if (const InitPriorityAttr *IPA = D->getAttr<InitPriorityAttr>()) { |
| 287 | OrderGlobalInits Key(IPA->getPriority(), PrioritizedCXXGlobalInits.size()); |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 288 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 289 | DelayedCXXInitPosition.erase(D); |
Reid Kleckner | c47063e | 2013-09-04 00:54:24 +0000 | [diff] [blame] | 290 | } else if (D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && |
| 291 | D->getTemplateSpecializationKind() != TSK_Undeclared) { |
Reid Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 292 | // C++ [basic.start.init]p2: |
Reid Kleckner | c47063e | 2013-09-04 00:54:24 +0000 | [diff] [blame] | 293 | // Definitions of explicitly specialized class template static data |
| 294 | // members have ordered initialization. Other class template static data |
| 295 | // members (i.e., implicitly or explicitly instantiated specializations) |
| 296 | // have unordered initialization. |
Reid Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 297 | // |
| 298 | // As a consequence, we can put them into their own llvm.global_ctors entry. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 299 | // |
| 300 | // In addition, put the initializer into a COMDAT group with the global |
| 301 | // being initialized. On most platforms, this is a minor startup time |
| 302 | // optimization. In the MS C++ ABI, there are no guard variables, so this |
| 303 | // COMDAT key is required for correctness. |
| 304 | AddGlobalCtor(Fn, 65535, Addr); |
Reid Kleckner | b969e84 | 2013-08-22 20:07:45 +0000 | [diff] [blame] | 305 | DelayedCXXInitPosition.erase(D); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 306 | } else { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 307 | llvm::DenseMap<const Decl *, unsigned>::iterator I = |
| 308 | DelayedCXXInitPosition.find(D); |
| 309 | if (I == DelayedCXXInitPosition.end()) { |
| 310 | CXXGlobalInits.push_back(Fn); |
| 311 | } else { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 312 | assert(CXXGlobalInits[I->second] == nullptr); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 313 | CXXGlobalInits[I->second] = Fn; |
| 314 | DelayedCXXInitPosition.erase(I); |
| 315 | } |
| 316 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 319 | void CodeGenModule::EmitCXXThreadLocalInitFunc() { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 320 | llvm::Function *InitFn = nullptr; |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 321 | if (!CXXThreadLocalInits.empty()) { |
| 322 | // Generate a guarded initialization function. |
| 323 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 324 | InitFn = CreateGlobalInitOrDestructFunction(*this, FTy, "__tls_init", |
| 325 | /*TLS*/ true); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 326 | llvm::GlobalVariable *Guard = new llvm::GlobalVariable( |
| 327 | getModule(), Int8Ty, false, llvm::GlobalVariable::InternalLinkage, |
| 328 | llvm::ConstantInt::get(Int8Ty, 0), "__tls_guard"); |
| 329 | Guard->setThreadLocal(true); |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 330 | CodeGenFunction(*this) |
| 331 | .GenerateCXXGlobalInitFunc(InitFn, CXXThreadLocalInits, Guard); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | getCXXABI().EmitThreadLocalInitFuncs(CXXThreadLocals, InitFn); |
| 335 | |
| 336 | CXXThreadLocalInits.clear(); |
| 337 | CXXThreadLocals.clear(); |
| 338 | } |
| 339 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 340 | void |
| 341 | CodeGenModule::EmitCXXGlobalInitFunc() { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 342 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
| 343 | CXXGlobalInits.pop_back(); |
| 344 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 345 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 346 | return; |
| 347 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 348 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 349 | |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 350 | |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 351 | // Create our global initialization function. |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 352 | if (!PrioritizedCXXGlobalInits.empty()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 353 | SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; |
Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 354 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 355 | PrioritizedCXXGlobalInits.end()); |
| 356 | // Iterate over "chunks" of ctors with same priority and emit each chunk |
| 357 | // into separate function. Note - everything is sorted first by priority, |
| 358 | // second - by lex order, so we emit ctor functions in proper order. |
| 359 | for (SmallVectorImpl<GlobalInitData >::iterator |
| 360 | I = PrioritizedCXXGlobalInits.begin(), |
| 361 | E = PrioritizedCXXGlobalInits.end(); I != E; ) { |
| 362 | SmallVectorImpl<GlobalInitData >::iterator |
| 363 | PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp()); |
| 364 | |
| 365 | LocalCXXGlobalInits.clear(); |
| 366 | unsigned Priority = I->first.priority; |
| 367 | // Compute the function suffix from priority. Prepend with zeroes to make |
| 368 | // sure the function names are also ordered as priorities. |
| 369 | std::string PrioritySuffix = llvm::utostr(Priority); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 370 | // Priority is always <= 65535 (enforced by sema). |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 371 | PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix; |
| 372 | llvm::Function *Fn = |
| 373 | CreateGlobalInitOrDestructFunction(*this, FTy, |
| 374 | "_GLOBAL__I_" + PrioritySuffix); |
| 375 | |
| 376 | for (; I < PrioE; ++I) |
| 377 | LocalCXXGlobalInits.push_back(I->second); |
| 378 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 379 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 380 | AddGlobalCtor(Fn, Priority); |
| 381 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 382 | } |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 383 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 384 | // Include the filename in the symbol name. Including "sub_" matches gcc and |
| 385 | // makes sure these symbols appear lexicographically behind the symbols with |
| 386 | // priority emitted above. |
| 387 | SourceManager &SM = Context.getSourceManager(); |
| 388 | SmallString<128> FileName(llvm::sys::path::filename( |
| 389 | SM.getFileEntryForID(SM.getMainFileID())->getName())); |
| 390 | for (size_t i = 0; i < FileName.size(); ++i) { |
| 391 | // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens |
| 392 | // to be the set of C preprocessing numbers. |
| 393 | if (!isPreprocessingNumberBody(FileName[i])) |
| 394 | FileName[i] = '_'; |
| 395 | } |
| 396 | llvm::Function *Fn = CreateGlobalInitOrDestructFunction( |
| 397 | *this, FTy, llvm::Twine("_GLOBAL__sub_I_", FileName)); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 398 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 399 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 400 | AddGlobalCtor(Fn); |
Anton Korobeynikov | 4179ddd | 2012-11-06 22:44:45 +0000 | [diff] [blame] | 401 | |
Axel Naumann | 54ec6c5 | 2011-05-06 15:24:04 +0000 | [diff] [blame] | 402 | CXXGlobalInits.clear(); |
| 403 | PrioritizedCXXGlobalInits.clear(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 406 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 407 | if (CXXGlobalDtors.empty()) |
| 408 | return; |
| 409 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 410 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 411 | |
| 412 | // Create our global destructor function. |
| 413 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 414 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 415 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 416 | CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 417 | AddGlobalDtor(Fn); |
| 418 | } |
| 419 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 420 | /// Emit the code necessary to initialize the given global variable. |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 421 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 422 | const VarDecl *D, |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 423 | llvm::GlobalVariable *Addr, |
| 424 | bool PerformInit) { |
Alexey Samsonov | a240df2 | 2012-10-16 07:22:28 +0000 | [diff] [blame] | 425 | // Check if we need to emit debug info for variable initializer. |
David Blaikie | c3030bc | 2013-08-26 20:33:21 +0000 | [diff] [blame] | 426 | if (D->hasAttr<NoDebugAttr>()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 427 | DebugInfo = nullptr; // disable debug info indefinitely for this function |
Nick Lewycky | 78d1a10 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 428 | |
| 429 | StartFunction(GlobalDecl(D), getContext().VoidTy, Fn, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 430 | getTypes().arrangeNullaryFunction(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 431 | FunctionArgList(), D->getLocation(), |
| 432 | D->getInit()->getExprLoc()); |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 433 | |
Douglas Gregor | e67d151 | 2011-07-01 21:54:36 +0000 | [diff] [blame] | 434 | // Use guarded initialization if the global variable is weak. This |
| 435 | // occurs for, e.g., instantiated static data members and |
| 436 | // definitions explicitly marked weak. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 437 | if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage()) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 438 | EmitCXXGuardedInit(*D, Addr, PerformInit); |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 439 | } else { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 440 | EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); |
Fariborz Jahanian | 92d835a | 2010-10-26 22:47:47 +0000 | [diff] [blame] | 441 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 442 | |
| 443 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 444 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 445 | |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 446 | void |
| 447 | CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 448 | ArrayRef<llvm::Constant *> Decls, |
| 449 | llvm::GlobalVariable *Guard) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 450 | { |
| 451 | ArtificialLocation AL(*this, Builder); |
| 452 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 453 | getTypes().arrangeNullaryFunction(), FunctionArgList()); |
| 454 | // Emit an artificial location for this function. |
| 455 | AL.Emit(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 456 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 457 | llvm::BasicBlock *ExitBlock = nullptr; |
| 458 | if (Guard) { |
| 459 | // If we have a guard variable, check whether we've already performed |
| 460 | // these initializations. This happens for TLS initialization functions. |
| 461 | llvm::Value *GuardVal = Builder.CreateLoad(Guard); |
| 462 | llvm::Value *Uninit = Builder.CreateIsNull(GuardVal, |
| 463 | "guard.uninitialized"); |
| 464 | // Mark as initialized before initializing anything else. If the |
| 465 | // initializers use previously-initialized thread_local vars, that's |
| 466 | // probably supposed to be OK, but the standard doesn't say. |
| 467 | Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard); |
| 468 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
| 469 | ExitBlock = createBasicBlock("exit"); |
| 470 | Builder.CreateCondBr(Uninit, InitBlock, ExitBlock); |
| 471 | EmitBlock(InitBlock); |
| 472 | } |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 473 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 474 | RunCleanupsScope Scope(*this); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 475 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 476 | // When building in Objective-C++ ARC mode, create an autorelease pool |
| 477 | // around the global initializers. |
| 478 | if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) { |
| 479 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); |
| 480 | EmitObjCAutoreleasePoolCleanup(token); |
| 481 | } |
Benjamin Kramer | c7b5f38 | 2013-04-26 21:32:52 +0000 | [diff] [blame] | 482 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 483 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) |
| 484 | if (Decls[i]) |
| 485 | EmitRuntimeCall(Decls[i]); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 486 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 487 | Scope.ForceCleanup(); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 488 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 489 | if (ExitBlock) { |
| 490 | Builder.CreateBr(ExitBlock); |
| 491 | EmitBlock(ExitBlock); |
| 492 | } |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 495 | FinishFunction(); |
| 496 | } |
| 497 | |
John McCall | 3f88f68 | 2012-04-06 18:21:03 +0000 | [diff] [blame] | 498 | void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 499 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 500 | &DtorsAndObjects) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 501 | { |
| 502 | ArtificialLocation AL(*this, Builder); |
| 503 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
| 504 | getTypes().arrangeNullaryFunction(), FunctionArgList()); |
| 505 | // Emit an artificial location for this function. |
| 506 | AL.Emit(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 507 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 508 | // Emit the dtors, in reverse order from construction. |
| 509 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
| 510 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; |
| 511 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 512 | DtorsAndObjects[e - i - 1].second); |
| 513 | // Make sure the call and the callee agree on calling convention. |
| 514 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 515 | CI->setCallingConv(F->getCallingConv()); |
| 516 | } |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 517 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 518 | |
| 519 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 520 | } |
| 521 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 522 | /// generateDestroyHelper - Generates a helper function which, when |
| 523 | /// invoked, destroys the given object. |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 524 | llvm::Function *CodeGenFunction::generateDestroyHelper( |
| 525 | llvm::Constant *addr, QualType type, Destroyer *destroyer, |
| 526 | bool useEHCleanupForArray, const VarDecl *VD) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 527 | FunctionArgList args; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 528 | ImplicitParamDecl dst(getContext(), nullptr, SourceLocation(), nullptr, |
| 529 | getContext().VoidPtrTy); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 530 | args.push_back(&dst); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 531 | |
| 532 | const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 533 | getContext().VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 534 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 535 | llvm::Function *fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 536 | CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 537 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 538 | StartFunction(VD, getContext().VoidTy, fn, FI, args); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 539 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 540 | emitDestroy(addr, type, destroyer, useEHCleanupForArray); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 541 | |
| 542 | FinishFunction(); |
| 543 | |
John McCall | a91f666 | 2011-07-13 03:01:35 +0000 | [diff] [blame] | 544 | return fn; |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 545 | } |