Devang Patel | cbc7bc9 | 2010-10-04 21:15:33 +0000 | [diff] [blame] | 1 | //===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===// |
Anders Carlsson | 87fc5a5 | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 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 C++ code generation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 14 | // We might split this into multiple files if it gets too unwieldy |
Anders Carlsson | 87fc5a5 | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 15 | |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Anders Carlsson | 87fc5a5 | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Anders Carlsson | 87fc5a5 | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | e5fd6f2 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 131be8b | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 23 | #include "clang/AST/Mangle.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | 52d78a5 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtCXX.h" |
Chandler Carruth | 8509824 | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | 87fc5a5 | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Anders Carlsson | 87fc5a5 | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 31 | /// Try to emit a base destructor as an alias to its primary |
| 32 | /// base-class destructor. |
| 33 | bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { |
| 34 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 35 | return true; |
| 36 | |
Rafael Espindola | d967bad | 2013-11-13 23:20:45 +0000 | [diff] [blame] | 37 | // Producing an alias to a base class ctor/dtor can degrade debug quality |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 38 | // as the debugger cannot tell them apart. |
Rafael Espindola | d967bad | 2013-11-13 23:20:45 +0000 | [diff] [blame] | 39 | if (getCodeGenOpts().OptimizationLevel == 0) |
| 40 | return true; |
| 41 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 42 | // If the destructor doesn't have a trivial body, we have to emit it |
| 43 | // separately. |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 44 | if (!D->hasTrivialBody()) |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 45 | return true; |
| 46 | |
| 47 | const CXXRecordDecl *Class = D->getParent(); |
| 48 | |
| 49 | // If we need to manipulate a VTT parameter, give up. |
| 50 | if (Class->getNumVBases()) { |
| 51 | // Extra Credit: passing extra parameters is perfectly safe |
| 52 | // in many calling conventions, so only bail out if the ctor's |
| 53 | // calling convention is nonstandard. |
| 54 | return true; |
| 55 | } |
| 56 | |
John McCall | 2b3c553 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 57 | // If any field has a non-trivial destructor, we have to emit the |
| 58 | // destructor separately. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame^] | 59 | for (const auto *I : Class->fields()) |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 60 | if (I->getType().isDestructedType()) |
John McCall | 2b3c553 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 61 | return true; |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 62 | |
| 63 | // Try to find a unique base class with a non-trivial destructor. |
| 64 | const CXXRecordDecl *UniqueBase = 0; |
| 65 | for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), |
| 66 | E = Class->bases_end(); I != E; ++I) { |
| 67 | |
| 68 | // We're in the base destructor, so skip virtual bases. |
| 69 | if (I->isVirtual()) continue; |
| 70 | |
| 71 | // Skip base classes with trivial destructors. |
| 72 | const CXXRecordDecl *Base |
| 73 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 74 | if (Base->hasTrivialDestructor()) continue; |
| 75 | |
| 76 | // If we've already found a base class with a non-trivial |
| 77 | // destructor, give up. |
| 78 | if (UniqueBase) return true; |
| 79 | UniqueBase = Base; |
| 80 | } |
| 81 | |
| 82 | // If we didn't find any bases with a non-trivial destructor, then |
| 83 | // the base destructor is actually effectively trivial, which can |
| 84 | // happen if it was needlessly user-defined or if there are virtual |
| 85 | // bases with non-trivial destructors. |
| 86 | if (!UniqueBase) |
| 87 | return true; |
| 88 | |
| 89 | // If the base is at a non-zero offset, give up. |
| 90 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 91 | if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero()) |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 92 | return true; |
| 93 | |
Rafael Espindola | 191b951 | 2014-03-05 21:04:41 +0000 | [diff] [blame] | 94 | // Give up if the calling conventions don't match. We could update the call, |
| 95 | // but it is probably not worth it. |
Rafael Espindola | 961ba21 | 2013-11-09 01:57:21 +0000 | [diff] [blame] | 96 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
Rafael Espindola | 191b951 | 2014-03-05 21:04:41 +0000 | [diff] [blame] | 97 | if (BaseD->getType()->getAs<FunctionType>()->getCallConv() != |
| 98 | D->getType()->getAs<FunctionType>()->getCallConv()) |
| 99 | return true; |
| 100 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 101 | return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 102 | GlobalDecl(BaseD, Dtor_Base), |
| 103 | false); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 104 | } |
| 105 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 106 | /// Try to emit a definition as a global alias for another definition. |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 107 | /// If \p InEveryTU is true, we know that an equivalent alias can be produced |
| 108 | /// in every translation unit. |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 109 | bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 110 | GlobalDecl TargetDecl, |
| 111 | bool InEveryTU) { |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 112 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 113 | return true; |
| 114 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 115 | // The alias will use the linkage of the referent. If we can't |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 116 | // support aliases with that linkage, fail. |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 117 | llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 118 | |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 119 | // We can't use an alias if the linkage is not valid for one. |
| 120 | if (!llvm::GlobalAlias::isValidLinkage(Linkage)) |
Rafael Espindola | 4c489c7 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 121 | return true; |
| 122 | |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 123 | llvm::GlobalValue::LinkageTypes TargetLinkage = |
| 124 | getFunctionLinkage(TargetDecl); |
| 125 | |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 126 | // Check if we have it already. |
| 127 | StringRef MangledName = getMangledName(AliasDecl); |
| 128 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 129 | if (Entry && !Entry->isDeclaration()) |
| 130 | return false; |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 131 | if (Replacements.count(MangledName)) |
| 132 | return false; |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 133 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 134 | // Derive the type for the alias. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 135 | llvm::PointerType *AliasType |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 136 | = getTypes().GetFunctionType(AliasDecl)->getPointerTo(); |
| 137 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 138 | // Find the referent. Some aliases might require a bitcast, in |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 139 | // which case the caller is responsible for ensuring the soundness |
| 140 | // of these semantics. |
| 141 | llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
| 142 | llvm::Constant *Aliasee = Ref; |
| 143 | if (Ref->getType() != AliasType) |
| 144 | Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); |
| 145 | |
Rafael Espindola | e2ec6fa | 2013-11-08 22:59:46 +0000 | [diff] [blame] | 146 | // Instead of creating as alias to a linkonce_odr, replace all of the uses |
| 147 | // of the aliassee. |
Joerg Sonnenberger | 374c2bb | 2013-11-22 21:34:35 +0000 | [diff] [blame] | 148 | if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) && |
| 149 | (TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage || |
| 150 | !TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) { |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 151 | // FIXME: An extern template instantiation will create functions with |
Joerg Sonnenberger | 374c2bb | 2013-11-22 21:34:35 +0000 | [diff] [blame] | 152 | // linkage "AvailableExternally". In libc++, some classes also define |
| 153 | // members with attribute "AlwaysInline" and expect no reference to |
| 154 | // be generated. It is desirable to reenable this optimisation after |
| 155 | // corresponding LLVM changes. |
Rafael Espindola | e2ec6fa | 2013-11-08 22:59:46 +0000 | [diff] [blame] | 156 | Replacements[MangledName] = Aliasee; |
| 157 | return false; |
| 158 | } |
| 159 | |
Rafael Espindola | 961ba21 | 2013-11-09 01:57:21 +0000 | [diff] [blame] | 160 | if (!InEveryTU) { |
| 161 | /// If we don't have a definition for the destructor yet, don't |
| 162 | /// emit. We can't emit aliases to declarations; that's just not |
| 163 | /// how aliases work. |
| 164 | if (Ref->isDeclaration()) |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 165 | return true; |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Rafael Espindola | 129d313 | 2013-11-12 22:06:46 +0000 | [diff] [blame] | 168 | // Don't create an alias to a linker weak symbol. This avoids producing |
| 169 | // different COMDATs in different TUs. Another option would be to |
| 170 | // output the alias both for weak_odr and linkonce_odr, but that |
| 171 | // requires explicit comdat support in the IL. |
| 172 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) |
| 173 | return true; |
| 174 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 175 | // Create the alias with no name. |
| 176 | llvm::GlobalAlias *Alias = |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 177 | new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule()); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 178 | |
John McCall | aea181d | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 179 | // Switch any previous uses to the alias. |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 180 | if (Entry) { |
John McCall | aea181d | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 181 | assert(Entry->getType() == AliasType && |
| 182 | "declaration exists with different type"); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 183 | Alias->takeName(Entry); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 184 | Entry->replaceAllUsesWith(Alias); |
| 185 | Entry->eraseFromParent(); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 186 | } else { |
Anders Carlsson | ea836bc | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 187 | Alias->setName(MangledName); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 188 | } |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 189 | |
| 190 | // Finally, set up the alias with its proper name and attributes. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 191 | SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 192 | |
| 193 | return false; |
| 194 | } |
Anders Carlsson | bd7d11f | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 195 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 196 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor, |
| 197 | CXXCtorType ctorType) { |
Reid Kleckner | 340ad86 | 2014-01-13 22:57:31 +0000 | [diff] [blame] | 198 | if (!getTarget().getCXXABI().hasConstructorVariants()) { |
| 199 | // If there are no constructor variants, always emit the complete destructor. |
| 200 | ctorType = Ctor_Complete; |
| 201 | } else if (!ctor->getParent()->getNumVBases() && |
| 202 | (ctorType == Ctor_Complete || ctorType == Ctor_Base)) { |
| 203 | // The complete constructor is equivalent to the base constructor |
| 204 | // for classes with no virtual bases. Try to emit it as an alias. |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 205 | bool ProducedAlias = |
| 206 | !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete), |
| 207 | GlobalDecl(ctor, Ctor_Base), true); |
| 208 | if (ctorType == Ctor_Complete && ProducedAlias) |
| 209 | return; |
| 210 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 212 | const CGFunctionInfo &fnInfo = |
| 213 | getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 214 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 215 | llvm::Function *fn = cast<llvm::Function>( |
| 216 | GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo, true)); |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 217 | setFunctionLinkage(GlobalDecl(ctor, ctorType), fn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 219 | CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 221 | SetFunctionDefinitionAttributes(ctor, fn); |
| 222 | SetLLVMFunctionAttributesForDefinition(ctor, fn); |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 223 | } |
| 224 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 225 | llvm::GlobalValue * |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 226 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, |
| 227 | CXXCtorType ctorType, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 228 | const CGFunctionInfo *fnInfo, |
| 229 | bool DontDefer) { |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 230 | GlobalDecl GD(ctor, ctorType); |
Anders Carlsson | 09b5fe6 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 232 | StringRef name = getMangledName(GD); |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 233 | if (llvm::GlobalValue *existing = GetGlobalValue(name)) |
| 234 | return existing; |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 235 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 236 | if (!fnInfo) |
| 237 | fnInfo = &getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType); |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 238 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 239 | llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo); |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 240 | return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 241 | /*ForVTable=*/false, |
| 242 | DontDefer)); |
Anders Carlsson | e8eeffd | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 243 | } |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 244 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 245 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor, |
| 246 | CXXDtorType dtorType) { |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 247 | // The complete destructor is equivalent to the base destructor for |
| 248 | // classes with no virtual bases, so try to emit it as an alias. |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 249 | if (!dtor->getParent()->getNumVBases() && |
| 250 | (dtorType == Dtor_Complete || dtorType == Dtor_Base)) { |
| 251 | bool ProducedAlias = |
| 252 | !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete), |
| 253 | GlobalDecl(dtor, Dtor_Base), true); |
| 254 | if (ProducedAlias) { |
| 255 | if (dtorType == Dtor_Complete) |
| 256 | return; |
| 257 | if (dtor->isVirtual()) |
| 258 | getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete)); |
| 259 | } |
| 260 | } |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 261 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 262 | // The base destructor is equivalent to the base destructor of its |
| 263 | // base class if there is exactly one non-virtual base class with a |
| 264 | // non-trivial destructor, there are no fields with a non-trivial |
| 265 | // destructor, and the body of the destructor is trivial. |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 266 | if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor)) |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 267 | return; |
| 268 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 269 | const CGFunctionInfo &fnInfo = |
| 270 | getTypes().arrangeCXXDestructor(dtor, dtorType); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 271 | |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 272 | llvm::Function *fn = cast<llvm::Function>( |
| 273 | GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo, 0, true)); |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 274 | setFunctionLinkage(GlobalDecl(dtor, dtorType), fn); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 276 | CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 278 | SetFunctionDefinitionAttributes(dtor, fn); |
| 279 | SetLLVMFunctionAttributesForDefinition(dtor, fn); |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 280 | } |
| 281 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 282 | llvm::GlobalValue * |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 283 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor, |
| 284 | CXXDtorType dtorType, |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 285 | const CGFunctionInfo *fnInfo, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 286 | llvm::FunctionType *fnType, |
| 287 | bool DontDefer) { |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 288 | GlobalDecl GD(dtor, dtorType); |
Anders Carlsson | 09b5fe6 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 290 | StringRef name = getMangledName(GD); |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 291 | if (llvm::GlobalValue *existing = GetGlobalValue(name)) |
| 292 | return existing; |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 293 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 294 | if (!fnType) { |
| 295 | if (!fnInfo) fnInfo = &getTypes().arrangeCXXDestructor(dtor, dtorType); |
| 296 | fnType = getTypes().GetFunctionType(*fnInfo); |
| 297 | } |
John McCall | 46288ef | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 298 | return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 299 | /*ForVTable=*/false, |
| 300 | DontDefer)); |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 303 | static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF, |
| 304 | GlobalDecl GD, |
| 305 | llvm::Type *Ty, |
| 306 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 307 | assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && |
| 308 | "No kext in Microsoft ABI"); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 309 | GD = GD.getCanonicalDecl(); |
| 310 | CodeGenModule &CGM = CGF.CGM; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 311 | llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits()); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 312 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 313 | VTable = CGF.Builder.CreateBitCast(VTable, Ty); |
| 314 | assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 315 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 316 | uint64_t AddressPoint = |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 317 | CGM.getItaniumVTableContext().getVTableLayout(RD) |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 318 | .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); |
| 319 | VTableIndex += AddressPoint; |
| 320 | llvm::Value *VFuncPtr = |
| 321 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
Anders Carlsson | e828c36 | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 322 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 323 | } |
| 324 | |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 325 | /// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making |
Fariborz Jahanian | 47609b0 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 326 | /// indirect call to virtual functions. It makes the call through indexing |
| 327 | /// into the vtable. |
| 328 | llvm::Value * |
| 329 | CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 330 | NestedNameSpecifier *Qual, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 331 | llvm::Type *Ty) { |
Fariborz Jahanian | 47609b0 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 332 | assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && |
| 333 | "BuildAppleKextVirtualCall - bad Qual kind"); |
| 334 | |
| 335 | const Type *QTy = Qual->getAsType(); |
| 336 | QualType T = QualType(QTy, 0); |
| 337 | const RecordType *RT = T->getAs<RecordType>(); |
| 338 | assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); |
| 339 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 340 | |
| 341 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) |
| 342 | return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 343 | |
| 344 | return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD); |
Fariborz Jahanian | 47609b0 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 347 | /// BuildVirtualCall - This routine makes indirect vtable call for |
| 348 | /// call to virtual destructors. It returns 0 if it could not do it. |
| 349 | llvm::Value * |
| 350 | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
| 351 | const CXXDestructorDecl *DD, |
| 352 | CXXDtorType Type, |
| 353 | const CXXRecordDecl *RD) { |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 354 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD); |
| 355 | // FIXME. Dtor_Base dtor is always direct!! |
| 356 | // It need be somehow inline expanded into the caller. |
| 357 | // -O does that. But need to support -O0 as well. |
| 358 | if (MD->isVirtual() && Type != Dtor_Base) { |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 359 | // Compute the function type we're calling. |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 360 | const CGFunctionInfo &FInfo = |
| 361 | CGM.getTypes().arrangeCXXDestructor(DD, Dtor_Complete); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 362 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 363 | return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD); |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 364 | } |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 365 | return 0; |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 366 | } |