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