| 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" | 
| Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 15 | #include "CGObjCRuntime.h" | 
| John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" | 
| Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/CodeGenOptions.h" | 
| Douglas Gregor | 86a3a03 | 2010-05-16 01:24:12 +0000 | [diff] [blame] | 18 | #include "llvm/Intrinsics.h" | 
|  | 19 |  | 
| Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 20 | using namespace clang; | 
|  | 21 | using namespace CodeGen; | 
|  | 22 |  | 
| Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 23 | static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, | 
|  | 24 | llvm::Constant *DeclPtr) { | 
|  | 25 | assert(D.hasGlobalStorage() && "VarDecl must have global storage!"); | 
|  | 26 | assert(!D.getType()->isReferenceType() && | 
|  | 27 | "Should not call EmitDeclInit on a reference!"); | 
|  | 28 |  | 
| Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 29 | ASTContext &Context = CGF.getContext(); | 
| Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 30 |  | 
| John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 31 | unsigned alignment = Context.getDeclAlign(&D).getQuantity(); | 
|  | 32 | QualType type = D.getType(); | 
|  | 33 | LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment); | 
|  | 34 |  | 
|  | 35 | const Expr *Init = D.getInit(); | 
|  | 36 | if (!CGF.hasAggregateLLVMType(type)) { | 
| Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 37 | CodeGenModule &CGM = CGF.CGM; | 
| John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 38 | if (lv.isObjCStrong()) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 39 | CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init), | 
|  | 40 | DeclPtr, D.isThreadSpecified()); | 
| John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 41 | else if (lv.isObjCWeak()) | 
|  | 42 | CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init), | 
|  | 43 | DeclPtr); | 
| Fariborz Jahanian | ec80512 | 2011-01-13 20:00:54 +0000 | [diff] [blame] | 44 | else | 
| John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 45 | CGF.EmitScalarInit(Init, &D, lv, false); | 
|  | 46 | } else if (type->isAnyComplexType()) { | 
|  | 47 | CGF.EmitComplexExprIntoAddr(Init, DeclPtr, lv.isVolatile()); | 
| Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 48 | } else { | 
| John McCall | a07398e | 2011-06-16 04:16:24 +0000 | [diff] [blame] | 49 | CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv, true)); | 
| Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 50 | } | 
|  | 51 | } | 
|  | 52 |  | 
| John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 53 | /// Emit code to cause the destruction of the given variable with | 
|  | 54 | /// static storage duration. | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 55 | static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, | 
|  | 56 | llvm::Constant *DeclPtr) { | 
|  | 57 | CodeGenModule &CGM = CGF.CGM; | 
|  | 58 | ASTContext &Context = CGF.getContext(); | 
|  | 59 |  | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 60 | QualType T = D.getType(); | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 61 |  | 
| John McCall | 85aca0f | 2010-07-30 04:56:58 +0000 | [diff] [blame] | 62 | // Drill down past array types. | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 63 | const ConstantArrayType *Array = Context.getAsConstantArrayType(T); | 
|  | 64 | if (Array) | 
|  | 65 | T = Context.getBaseElementType(Array); | 
|  | 66 |  | 
| John McCall | 85aca0f | 2010-07-30 04:56:58 +0000 | [diff] [blame] | 67 | /// If that's not a record, we're done. | 
|  | 68 | /// FIXME:  __attribute__((cleanup)) ? | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 69 | const RecordType *RT = T->getAs<RecordType>(); | 
|  | 70 | if (!RT) | 
|  | 71 | return; | 
|  | 72 |  | 
|  | 73 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 74 | if (RD->hasTrivialDestructor()) | 
|  | 75 | return; | 
|  | 76 |  | 
| Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 77 | CXXDestructorDecl *Dtor = RD->getDestructor(); | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 78 |  | 
|  | 79 | llvm::Constant *DtorFn; | 
|  | 80 | if (Array) { | 
|  | 81 | DtorFn = | 
| Anders Carlsson | 02e370a | 2010-06-08 22:14:59 +0000 | [diff] [blame] | 82 | CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(Dtor, Array, | 
|  | 83 | DeclPtr); | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 84 | const llvm::Type *Int8PtrTy = | 
| Anders Carlsson | 02e370a | 2010-06-08 22:14:59 +0000 | [diff] [blame] | 85 | llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 86 | DeclPtr = llvm::Constant::getNullValue(Int8PtrTy); | 
|  | 87 | } else | 
|  | 88 | DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); | 
|  | 89 |  | 
|  | 90 | CGF.EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr); | 
|  | 91 | } | 
|  | 92 |  | 
| Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 93 | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, | 
|  | 94 | llvm::Constant *DeclPtr) { | 
|  | 95 |  | 
|  | 96 | const Expr *Init = D.getInit(); | 
|  | 97 | QualType T = D.getType(); | 
|  | 98 |  | 
|  | 99 | if (!T->isReferenceType()) { | 
|  | 100 | EmitDeclInit(*this, D, DeclPtr); | 
| Douglas Gregor | cc6a44b | 2010-05-05 15:38:32 +0000 | [diff] [blame] | 101 | EmitDeclDestroy(*this, D, DeclPtr); | 
| Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 102 | return; | 
|  | 103 | } | 
| Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 104 |  | 
| Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 105 | unsigned Alignment = getContext().getDeclAlign(&D).getQuantity(); | 
| Anders Carlsson | 045a6d8 | 2010-06-27 17:52:15 +0000 | [diff] [blame] | 106 | RValue RV = EmitReferenceBindingToExpr(Init, &D); | 
| Daniel Dunbar | 91a16fa | 2010-08-21 02:24:36 +0000 | [diff] [blame] | 107 | EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T); | 
| Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 108 | } | 
| Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 109 |  | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 110 | void | 
|  | 111 | CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, | 
|  | 112 | llvm::Constant *DeclPtr) { | 
|  | 113 | // Generate a global destructor entry if not using __cxa_atexit. | 
|  | 114 | if (!CGM.getCodeGenOpts().CXAAtExit) { | 
|  | 115 | CGM.AddCXXDtorEntry(DtorFn, DeclPtr); | 
|  | 116 | return; | 
|  | 117 | } | 
|  | 118 |  | 
| Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 119 | // Get the destructor function type | 
|  | 120 | const llvm::Type *DtorFnTy = | 
| John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 121 | llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), | 
| Benjamin Kramer | 95d318c | 2011-05-28 14:26:31 +0000 | [diff] [blame] | 122 | Int8PtrTy, false); | 
| Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 123 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); | 
|  | 124 |  | 
| Benjamin Kramer | 95d318c | 2011-05-28 14:26:31 +0000 | [diff] [blame] | 125 | const llvm::Type *Params[] = { DtorFnTy, Int8PtrTy, Int8PtrTy }; | 
| Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 126 |  | 
|  | 127 | // Get the __cxa_atexit function type | 
|  | 128 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); | 
|  | 129 | const llvm::FunctionType *AtExitFnTy = | 
|  | 130 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); | 
|  | 131 |  | 
|  | 132 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, | 
|  | 133 | "__cxa_atexit"); | 
| Anders Carlsson | bd4a073 | 2011-03-20 20:52:32 +0000 | [diff] [blame] | 134 | if (llvm::Function *Fn = dyn_cast<llvm::Function>(AtExitFn)) | 
|  | 135 | Fn->setDoesNotThrow(); | 
| Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 136 |  | 
|  | 137 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, | 
|  | 138 | "__dso_handle"); | 
|  | 139 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), | 
|  | 140 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), | 
|  | 141 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; | 
|  | 142 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); | 
|  | 143 | } | 
|  | 144 |  | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 145 | void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, | 
|  | 146 | llvm::GlobalVariable *DeclPtr) { | 
| John McCall | 3209669 | 2011-03-18 02:56:14 +0000 | [diff] [blame] | 147 | // If we've been asked to forbid guard variables, emit an error now. | 
|  | 148 | // This diagnostic is hard-coded for Darwin's use case;  we can find | 
|  | 149 | // better phrasing if someone else needs it. | 
|  | 150 | if (CGM.getCodeGenOpts().ForbidGuardVariables) | 
|  | 151 | CGM.Error(D.getLocation(), | 
|  | 152 | "this initialization requires a guard variable, which " | 
|  | 153 | "the kernel does not support"); | 
|  | 154 |  | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 155 | CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr); | 
| John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 158 | static llvm::Function * | 
|  | 159 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, | 
|  | 160 | const llvm::FunctionType *FTy, | 
|  | 161 | llvm::StringRef Name) { | 
|  | 162 | llvm::Function *Fn = | 
|  | 163 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, | 
|  | 164 | Name, &CGM.getModule()); | 
| Fariborz Jahanian | d6c9a0f | 2011-02-15 18:54:46 +0000 | [diff] [blame] | 165 | if (!CGM.getContext().getLangOptions().AppleKext) { | 
|  | 166 | // Set the section if needed. | 
|  | 167 | if (const char *Section = | 
|  | 168 | CGM.getContext().Target.getStaticInitSectionSpecifier()) | 
|  | 169 | Fn->setSection(Section); | 
|  | 170 | } | 
| Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 171 |  | 
| Anders Carlsson | 7a17851 | 2011-02-28 00:33:03 +0000 | [diff] [blame] | 172 | if (!CGM.getLangOptions().Exceptions) | 
| John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 173 | Fn->setDoesNotThrow(); | 
|  | 174 |  | 
| Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 175 | return Fn; | 
|  | 176 | } | 
|  | 177 |  | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 178 | void | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 179 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, | 
|  | 180 | llvm::GlobalVariable *Addr) { | 
| Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 181 | const llvm::FunctionType *FTy | 
|  | 182 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), | 
|  | 183 | false); | 
|  | 184 |  | 
|  | 185 | // Create a variable initialization function. | 
|  | 186 | llvm::Function *Fn = | 
| Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 187 | CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init"); | 
| Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 188 |  | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 189 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr); | 
| Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 190 |  | 
| Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 191 | if (D->hasAttr<InitPriorityAttr>()) { | 
|  | 192 | unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority(); | 
| Chris Lattner | ec2830d | 2010-06-27 06:32:58 +0000 | [diff] [blame] | 193 | OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size()); | 
| Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 194 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); | 
| John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 195 | DelayedCXXInitPosition.erase(D); | 
| Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 196 | } | 
| John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 197 | else { | 
|  | 198 | llvm::DenseMap<const Decl *, unsigned>::iterator I = | 
|  | 199 | DelayedCXXInitPosition.find(D); | 
|  | 200 | if (I == DelayedCXXInitPosition.end()) { | 
|  | 201 | CXXGlobalInits.push_back(Fn); | 
|  | 202 | } else { | 
|  | 203 | assert(CXXGlobalInits[I->second] == 0); | 
|  | 204 | CXXGlobalInits[I->second] = Fn; | 
|  | 205 | DelayedCXXInitPosition.erase(I); | 
|  | 206 | } | 
|  | 207 | } | 
| Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 210 | void | 
|  | 211 | CodeGenModule::EmitCXXGlobalInitFunc() { | 
| John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 212 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) | 
|  | 213 | CXXGlobalInits.pop_back(); | 
|  | 214 |  | 
| Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 215 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) | 
| Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 216 | return; | 
|  | 217 |  | 
|  | 218 | const llvm::FunctionType *FTy | 
|  | 219 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), | 
|  | 220 | false); | 
|  | 221 |  | 
|  | 222 | // Create our global initialization function. | 
| Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 223 | llvm::Function *Fn = | 
|  | 224 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); | 
| Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 225 |  | 
| Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 226 | if (!PrioritizedCXXGlobalInits.empty()) { | 
| Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 227 | llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; | 
|  | 228 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), | 
| Fariborz Jahanian | f489688 | 2010-06-22 00:23:08 +0000 | [diff] [blame] | 229 | PrioritizedCXXGlobalInits.end()); | 
| Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 230 | for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) { | 
|  | 231 | llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second; | 
|  | 232 | LocalCXXGlobalInits.push_back(Fn); | 
|  | 233 | } | 
| John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 234 | LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end()); | 
| Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 235 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, | 
|  | 236 | &LocalCXXGlobalInits[0], | 
|  | 237 | LocalCXXGlobalInits.size()); | 
|  | 238 | } | 
|  | 239 | else | 
|  | 240 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, | 
|  | 241 | &CXXGlobalInits[0], | 
|  | 242 | CXXGlobalInits.size()); | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 243 | AddGlobalCtor(Fn); | 
| Axel Naumann | 54ec6c5 | 2011-05-06 15:24:04 +0000 | [diff] [blame] | 244 | CXXGlobalInits.clear(); | 
|  | 245 | PrioritizedCXXGlobalInits.clear(); | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 246 | } | 
|  | 247 |  | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 248 | void CodeGenModule::EmitCXXGlobalDtorFunc() { | 
|  | 249 | if (CXXGlobalDtors.empty()) | 
|  | 250 | return; | 
|  | 251 |  | 
|  | 252 | const llvm::FunctionType *FTy | 
|  | 253 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), | 
|  | 254 | false); | 
|  | 255 |  | 
|  | 256 | // Create our global destructor function. | 
|  | 257 | llvm::Function *Fn = | 
| Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 258 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 259 |  | 
|  | 260 | CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors); | 
|  | 261 | AddGlobalDtor(Fn); | 
|  | 262 | } | 
|  | 263 |  | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 264 | /// Emit the code necessary to initialize the given global variable. | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 265 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 266 | const VarDecl *D, | 
|  | 267 | llvm::GlobalVariable *Addr) { | 
| John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 268 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, | 
|  | 269 | getTypes().getNullaryFunctionInfo(), | 
|  | 270 | FunctionArgList(), SourceLocation()); | 
| Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 271 |  | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 272 | // Use guarded initialization if the global variable is weak due to | 
| John McCall | 99ace16 | 2011-04-12 01:46:54 +0000 | [diff] [blame] | 273 | // being a class template's static data member.  These will always | 
|  | 274 | // have weak_odr linkage. | 
|  | 275 | if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage && | 
|  | 276 | D->isStaticDataMember() && | 
|  | 277 | D->getInstantiatedFromStaticDataMember()) { | 
| John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 278 | EmitCXXGuardedInit(*D, Addr); | 
|  | 279 | } else { | 
|  | 280 | EmitCXXGlobalVarDeclInit(*D, Addr); | 
| Fariborz Jahanian | 92d835a | 2010-10-26 22:47:47 +0000 | [diff] [blame] | 281 | } | 
| Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 282 |  | 
|  | 283 | FinishFunction(); | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 284 | } | 
| Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 285 |  | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 286 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, | 
|  | 287 | llvm::Constant **Decls, | 
|  | 288 | unsigned NumDecls) { | 
| John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 289 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, | 
|  | 290 | getTypes().getNullaryFunctionInfo(), | 
|  | 291 | FunctionArgList(), SourceLocation()); | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 292 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 293 | RunCleanupsScope Scope(*this); | 
|  | 294 |  | 
|  | 295 | // When building in Objective-C++ ARC mode, create an autorelease pool | 
|  | 296 | // around the global initializers. | 
|  | 297 | if (getLangOptions().ObjCAutoRefCount && getLangOptions().CPlusPlus) { | 
|  | 298 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); | 
|  | 299 | EmitObjCAutoreleasePoolCleanup(token); | 
|  | 300 | } | 
|  | 301 |  | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 302 | for (unsigned i = 0; i != NumDecls; ++i) | 
| John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 303 | if (Decls[i]) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 304 | Builder.CreateCall(Decls[i]); | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 305 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 306 | Scope.ForceCleanup(); | 
|  | 307 |  | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 308 | FinishFunction(); | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn, | 
| Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 312 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 313 | &DtorsAndObjects) { | 
| John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 314 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, | 
|  | 315 | getTypes().getNullaryFunctionInfo(), | 
|  | 316 | FunctionArgList(), SourceLocation()); | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 317 |  | 
|  | 318 | // Emit the dtors, in reverse order from construction. | 
| Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 319 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { | 
| Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 320 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; | 
| Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 321 | llvm::CallInst *CI = Builder.CreateCall(Callee, | 
|  | 322 | DtorsAndObjects[e - i - 1].second); | 
|  | 323 | // Make sure the call and the callee agree on calling convention. | 
|  | 324 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) | 
|  | 325 | CI->setCallingConv(F->getCallingConv()); | 
|  | 326 | } | 
| Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 327 |  | 
|  | 328 | FinishFunction(); | 
| Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 329 | } | 
|  | 330 |  | 
| Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 331 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when | 
|  | 332 | /// invoked, calls the default destructor on array elements in reverse order of | 
|  | 333 | /// construction. | 
|  | 334 | llvm::Function * | 
|  | 335 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, | 
|  | 336 | const ArrayType *Array, | 
|  | 337 | llvm::Value *This) { | 
| John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 338 | FunctionArgList args; | 
|  | 339 | ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy); | 
|  | 340 | args.push_back(&dst); | 
| Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 341 |  | 
| Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 342 | const CGFunctionInfo &FI = | 
| John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 343 | CGM.getTypes().getFunctionInfo(getContext().VoidTy, args, | 
| Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 344 | FunctionType::ExtInfo()); | 
|  | 345 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); | 
| Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 346 | llvm::Function *Fn = | 
|  | 347 | CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); | 
| Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 348 |  | 
| John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 349 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FI, args, | 
|  | 350 | SourceLocation()); | 
| Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 351 |  | 
|  | 352 | QualType BaseElementTy = getContext().getBaseElementType(Array); | 
|  | 353 | const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo(); | 
|  | 354 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); | 
|  | 355 |  | 
|  | 356 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); | 
|  | 357 |  | 
|  | 358 | FinishFunction(); | 
|  | 359 |  | 
|  | 360 | return Fn; | 
|  | 361 | } |