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