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; |
| 39 | if (CGF.getContext().getObjCGCAttrKind(T) == Qualifiers::Strong) |
| 40 | CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, V, DeclPtr, |
| 41 | D.isThreadSpecified()); |
| 42 | else if (CGF.getContext().getObjCGCAttrKind(T) == Qualifiers::Weak) |
| 43 | CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, V, DeclPtr); |
| 44 | else |
| 45 | CGF.EmitStoreOfScalar(V, DeclPtr, isVolatile, Alignment, T); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 46 | } else if (T->isAnyComplexType()) { |
Anders Carlsson | fcbfdc1 | 2009-12-10 00:57:45 +0000 | [diff] [blame] | 47 | CGF.EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile); |
Anders Carlsson | 5ec2e7c | 2009-12-10 00:16:00 +0000 | [diff] [blame] | 48 | } else { |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 49 | CGF.EmitAggExpr(Init, AggValueSlot::forAddr(DeclPtr, isVolatile, 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 | const llvm::Type *Int8PtrTy = |
| 120 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 121 | |
| 122 | std::vector<const llvm::Type *> Params; |
| 123 | Params.push_back(Int8PtrTy); |
| 124 | |
| 125 | // Get the destructor function type |
| 126 | const llvm::Type *DtorFnTy = |
| 127 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
| 128 | DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); |
| 129 | |
| 130 | Params.clear(); |
| 131 | Params.push_back(DtorFnTy); |
| 132 | Params.push_back(Int8PtrTy); |
| 133 | Params.push_back(Int8PtrTy); |
| 134 | |
| 135 | // Get the __cxa_atexit function type |
| 136 | // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); |
| 137 | const llvm::FunctionType *AtExitFnTy = |
| 138 | llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); |
| 139 | |
| 140 | llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, |
| 141 | "__cxa_atexit"); |
| 142 | |
| 143 | llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, |
| 144 | "__dso_handle"); |
| 145 | llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), |
| 146 | llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), |
| 147 | llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; |
| 148 | Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); |
| 149 | } |
| 150 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 151 | void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, |
| 152 | llvm::GlobalVariable *DeclPtr) { |
| 153 | CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 156 | static llvm::Function * |
| 157 | CreateGlobalInitOrDestructFunction(CodeGenModule &CGM, |
| 158 | const llvm::FunctionType *FTy, |
| 159 | llvm::StringRef Name) { |
| 160 | llvm::Function *Fn = |
| 161 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 162 | Name, &CGM.getModule()); |
| 163 | |
Anders Carlsson | 18af368 | 2010-06-08 22:47:50 +0000 | [diff] [blame] | 164 | // Set the section if needed. |
| 165 | if (const char *Section = |
| 166 | CGM.getContext().Target.getStaticInitSectionSpecifier()) |
| 167 | Fn->setSection(Section); |
| 168 | |
John McCall | 044cc54 | 2010-07-06 04:38:10 +0000 | [diff] [blame] | 169 | if (!CGM.getLangOptions().Exceptions) |
| 170 | Fn->setDoesNotThrow(); |
| 171 | |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 172 | return Fn; |
| 173 | } |
| 174 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 175 | void |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 176 | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, |
| 177 | llvm::GlobalVariable *Addr) { |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 178 | const llvm::FunctionType *FTy |
| 179 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 180 | false); |
| 181 | |
| 182 | // Create a variable initialization function. |
| 183 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 184 | CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init"); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 185 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 186 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr); |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 187 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 188 | if (D->hasAttr<InitPriorityAttr>()) { |
| 189 | unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority(); |
Chris Lattner | ec2830d | 2010-06-27 06:32:58 +0000 | [diff] [blame] | 190 | OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size()); |
Fariborz Jahanian | e0b691a | 2010-06-21 21:27:42 +0000 | [diff] [blame] | 191 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 192 | DelayedCXXInitPosition.erase(D); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 193 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 194 | else { |
| 195 | llvm::DenseMap<const Decl *, unsigned>::iterator I = |
| 196 | DelayedCXXInitPosition.find(D); |
| 197 | if (I == DelayedCXXInitPosition.end()) { |
| 198 | CXXGlobalInits.push_back(Fn); |
| 199 | } else { |
| 200 | assert(CXXGlobalInits[I->second] == 0); |
| 201 | CXXGlobalInits[I->second] = Fn; |
| 202 | DelayedCXXInitPosition.erase(I); |
| 203 | } |
| 204 | } |
Eli Friedman | 6c6bda3 | 2010-01-08 00:50:11 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 207 | void |
| 208 | CodeGenModule::EmitCXXGlobalInitFunc() { |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 209 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
| 210 | CXXGlobalInits.pop_back(); |
| 211 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 212 | if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty()) |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 213 | return; |
| 214 | |
| 215 | const llvm::FunctionType *FTy |
| 216 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 217 | false); |
| 218 | |
| 219 | // Create our global initialization function. |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 220 | llvm::Function *Fn = |
| 221 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a"); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 222 | |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 223 | if (!PrioritizedCXXGlobalInits.empty()) { |
Fariborz Jahanian | 027d7ed | 2010-06-21 19:49:38 +0000 | [diff] [blame] | 224 | llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits; |
| 225 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
Fariborz Jahanian | f489688 | 2010-06-22 00:23:08 +0000 | [diff] [blame] | 226 | PrioritizedCXXGlobalInits.end()); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 227 | for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) { |
| 228 | llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second; |
| 229 | LocalCXXGlobalInits.push_back(Fn); |
| 230 | } |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 231 | LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end()); |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 232 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 233 | &LocalCXXGlobalInits[0], |
| 234 | LocalCXXGlobalInits.size()); |
| 235 | } |
| 236 | else |
| 237 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, |
| 238 | &CXXGlobalInits[0], |
| 239 | CXXGlobalInits.size()); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 240 | AddGlobalCtor(Fn); |
| 241 | } |
| 242 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 243 | void CodeGenModule::EmitCXXGlobalDtorFunc() { |
| 244 | if (CXXGlobalDtors.empty()) |
| 245 | return; |
| 246 | |
| 247 | const llvm::FunctionType *FTy |
| 248 | = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 249 | false); |
| 250 | |
| 251 | // Create our global destructor function. |
| 252 | llvm::Function *Fn = |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 253 | CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a"); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 254 | |
| 255 | CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors); |
| 256 | AddGlobalDtor(Fn); |
| 257 | } |
| 258 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 259 | /// Emit the code necessary to initialize the given global variable. |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 260 | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 261 | const VarDecl *D, |
| 262 | llvm::GlobalVariable *Addr) { |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 263 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 264 | SourceLocation()); |
| 265 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 266 | // Use guarded initialization if the global variable is weak due to |
| 267 | // being a class template's static data member. |
| 268 | if (Addr->hasWeakLinkage() && D->getInstantiatedFromStaticDataMember()) { |
| 269 | EmitCXXGuardedInit(*D, Addr); |
| 270 | } else { |
| 271 | EmitCXXGlobalVarDeclInit(*D, Addr); |
Fariborz Jahanian | 92d835a | 2010-10-26 22:47:47 +0000 | [diff] [blame] | 272 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 273 | |
| 274 | FinishFunction(); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 275 | } |
Daniel Dunbar | 5c6846e | 2010-03-20 04:15:29 +0000 | [diff] [blame] | 276 | |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 277 | void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
| 278 | llvm::Constant **Decls, |
| 279 | unsigned NumDecls) { |
| 280 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 281 | SourceLocation()); |
| 282 | |
| 283 | for (unsigned i = 0; i != NumDecls; ++i) |
John McCall | bf40cb5 | 2010-07-15 23:40:35 +0000 | [diff] [blame] | 284 | if (Decls[i]) |
| 285 | Builder.CreateCall(Decls[i]); |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 286 | |
| 287 | FinishFunction(); |
| 288 | } |
| 289 | |
| 290 | void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn, |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 291 | const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> > |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 292 | &DtorsAndObjects) { |
| 293 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), |
| 294 | SourceLocation()); |
| 295 | |
| 296 | // Emit the dtors, in reverse order from construction. |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 297 | for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) { |
Chris Lattner | 810112e | 2010-06-19 05:52:45 +0000 | [diff] [blame] | 298 | llvm::Value *Callee = DtorsAndObjects[e - i - 1].first; |
Chris Lattner | c9a85f9 | 2010-04-26 20:35:54 +0000 | [diff] [blame] | 299 | llvm::CallInst *CI = Builder.CreateCall(Callee, |
| 300 | DtorsAndObjects[e - i - 1].second); |
| 301 | // Make sure the call and the callee agree on calling convention. |
| 302 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 303 | CI->setCallingConv(F->getCallingConv()); |
| 304 | } |
Daniel Dunbar | efb0fa9 | 2010-03-20 04:15:41 +0000 | [diff] [blame] | 305 | |
| 306 | FinishFunction(); |
Anders Carlsson | eb4072e | 2009-12-10 00:30:05 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 309 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when |
| 310 | /// invoked, calls the default destructor on array elements in reverse order of |
| 311 | /// construction. |
| 312 | llvm::Function * |
| 313 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 314 | const ArrayType *Array, |
| 315 | llvm::Value *This) { |
| 316 | FunctionArgList Args; |
| 317 | ImplicitParamDecl *Dst = |
| 318 | ImplicitParamDecl::Create(getContext(), 0, |
| 319 | SourceLocation(), 0, |
| 320 | getContext().getPointerType(getContext().VoidTy)); |
| 321 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 322 | |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 323 | const CGFunctionInfo &FI = |
| 324 | CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args, |
| 325 | FunctionType::ExtInfo()); |
| 326 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
Anders Carlsson | 9dc046e | 2010-06-08 22:40:05 +0000 | [diff] [blame] | 327 | llvm::Function *Fn = |
| 328 | CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor"); |
Anders Carlsson | 7729136 | 2010-06-08 22:17:27 +0000 | [diff] [blame] | 329 | |
| 330 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, Args, SourceLocation()); |
| 331 | |
| 332 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 333 | const llvm::Type *BasePtr = ConvertType(BaseElementTy)->getPointerTo(); |
| 334 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); |
| 335 | |
| 336 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); |
| 337 | |
| 338 | FinishFunction(); |
| 339 | |
| 340 | return Fn; |
| 341 | } |