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" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +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 | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 31 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 32 | /// Try to emit a base destructor as an alias to its primary |
| 33 | /// base-class destructor. |
| 34 | bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { |
| 35 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 36 | return true; |
| 37 | |
Rafael Espindola | d967bad | 2013-11-13 23:20:45 +0000 | [diff] [blame] | 38 | // 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] | 39 | // as the debugger cannot tell them apart. |
Rafael Espindola | d967bad | 2013-11-13 23:20:45 +0000 | [diff] [blame] | 40 | if (getCodeGenOpts().OptimizationLevel == 0) |
| 41 | return true; |
| 42 | |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 43 | // If sanitizing memory to check for use-after-dtor, do not emit as |
| 44 | // an alias, unless this class owns no members. |
| 45 | if (getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
| 46 | !D->getParent()->field_empty()) |
| 47 | return true; |
| 48 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 49 | // If the destructor doesn't have a trivial body, we have to emit it |
| 50 | // separately. |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 51 | if (!D->hasTrivialBody()) |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 52 | return true; |
| 53 | |
| 54 | const CXXRecordDecl *Class = D->getParent(); |
| 55 | |
Kostya Serebryany | 5f1b4e8 | 2014-10-31 19:01:02 +0000 | [diff] [blame] | 56 | // We are going to instrument this destructor, so give up even if it is |
| 57 | // currently empty. |
| 58 | if (Class->mayInsertExtraPadding()) |
| 59 | return true; |
| 60 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 61 | // If we need to manipulate a VTT parameter, give up. |
| 62 | if (Class->getNumVBases()) { |
| 63 | // Extra Credit: passing extra parameters is perfectly safe |
| 64 | // in many calling conventions, so only bail out if the ctor's |
| 65 | // calling convention is nonstandard. |
| 66 | return true; |
| 67 | } |
| 68 | |
John McCall | 2b3c553 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 69 | // If any field has a non-trivial destructor, we have to emit the |
| 70 | // destructor separately. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 71 | for (const auto *I : Class->fields()) |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 72 | if (I->getType().isDestructedType()) |
John McCall | 2b3c553 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 73 | return true; |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 74 | |
| 75 | // Try to find a unique base class with a non-trivial destructor. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 76 | const CXXRecordDecl *UniqueBase = nullptr; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 77 | for (const auto &I : Class->bases()) { |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 78 | |
| 79 | // We're in the base destructor, so skip virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 80 | if (I.isVirtual()) continue; |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 81 | |
| 82 | // Skip base classes with trivial destructors. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 83 | const auto *Base = |
| 84 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 85 | if (Base->hasTrivialDestructor()) continue; |
| 86 | |
| 87 | // If we've already found a base class with a non-trivial |
| 88 | // destructor, give up. |
| 89 | if (UniqueBase) return true; |
| 90 | UniqueBase = Base; |
| 91 | } |
| 92 | |
| 93 | // If we didn't find any bases with a non-trivial destructor, then |
| 94 | // the base destructor is actually effectively trivial, which can |
| 95 | // happen if it was needlessly user-defined or if there are virtual |
| 96 | // bases with non-trivial destructors. |
| 97 | if (!UniqueBase) |
| 98 | return true; |
| 99 | |
| 100 | // If the base is at a non-zero offset, give up. |
| 101 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 102 | if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero()) |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 103 | return true; |
| 104 | |
Rafael Espindola | 191b951 | 2014-03-05 21:04:41 +0000 | [diff] [blame] | 105 | // Give up if the calling conventions don't match. We could update the call, |
| 106 | // but it is probably not worth it. |
Rafael Espindola | 961ba21 | 2013-11-09 01:57:21 +0000 | [diff] [blame] | 107 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
Rafael Espindola | 191b951 | 2014-03-05 21:04:41 +0000 | [diff] [blame] | 108 | if (BaseD->getType()->getAs<FunctionType>()->getCallConv() != |
| 109 | D->getType()->getAs<FunctionType>()->getCallConv()) |
| 110 | return true; |
| 111 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 112 | return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 113 | GlobalDecl(BaseD, Dtor_Base), |
| 114 | false); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 115 | } |
| 116 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 117 | /// Try to emit a definition as a global alias for another definition. |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 118 | /// If \p InEveryTU is true, we know that an equivalent alias can be produced |
| 119 | /// in every translation unit. |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 120 | bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 121 | GlobalDecl TargetDecl, |
| 122 | bool InEveryTU) { |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 123 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 124 | return true; |
| 125 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 126 | // 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] | 127 | // support aliases with that linkage, fail. |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 128 | llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 129 | |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 130 | // We can't use an alias if the linkage is not valid for one. |
| 131 | if (!llvm::GlobalAlias::isValidLinkage(Linkage)) |
Rafael Espindola | 4c489c7 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 132 | return true; |
| 133 | |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 134 | llvm::GlobalValue::LinkageTypes TargetLinkage = |
| 135 | getFunctionLinkage(TargetDecl); |
| 136 | |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 137 | // Check if we have it already. |
| 138 | StringRef MangledName = getMangledName(AliasDecl); |
| 139 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 140 | if (Entry && !Entry->isDeclaration()) |
| 141 | return false; |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 142 | if (Replacements.count(MangledName)) |
| 143 | return false; |
Rafael Espindola | 3f643bd | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 144 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 145 | // Derive the type for the alias. |
David Blaikie | 2a791d7 | 2015-09-14 18:38:22 +0000 | [diff] [blame] | 146 | llvm::Type *AliasValueType = getTypes().GetFunctionType(AliasDecl); |
| 147 | llvm::PointerType *AliasType = AliasValueType->getPointerTo(); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 148 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 149 | // Find the referent. Some aliases might require a bitcast, in |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 150 | // which case the caller is responsible for ensuring the soundness |
| 151 | // of these semantics. |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 152 | auto *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 153 | llvm::Constant *Aliasee = Ref; |
| 154 | if (Ref->getType() != AliasType) |
| 155 | Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 156 | |
Rafael Espindola | e2ec6fa | 2013-11-08 22:59:46 +0000 | [diff] [blame] | 157 | // Instead of creating as alias to a linkonce_odr, replace all of the uses |
Rafael Espindola | b2633b9 | 2014-05-16 19:35:48 +0000 | [diff] [blame] | 158 | // of the aliasee. |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 159 | if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) && |
| 160 | (TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage || |
| 161 | !TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) { |
| 162 | // FIXME: An extern template instantiation will create functions with |
| 163 | // linkage "AvailableExternally". In libc++, some classes also define |
| 164 | // members with attribute "AlwaysInline" and expect no reference to |
| 165 | // be generated. It is desirable to reenable this optimisation after |
| 166 | // corresponding LLVM changes. |
Yaron Keren | f8adb38 | 2016-02-07 12:44:35 +0000 | [diff] [blame] | 167 | addReplacement(MangledName, Aliasee); |
Rafael Espindola | e2ec6fa | 2013-11-08 22:59:46 +0000 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
Reid Kleckner | 489cfe1 | 2015-11-10 22:23:58 +0000 | [diff] [blame] | 171 | // If we have a weak, non-discardable alias (weak, weak_odr), like an extern |
| 172 | // template instantiation or a dllexported class, avoid forming it on COFF. |
| 173 | // A COFF weak external alias cannot satisfy a normal undefined symbol |
| 174 | // reference from another TU. The other TU must also mark the referenced |
| 175 | // symbol as weak, which we cannot rely on. |
| 176 | if (llvm::GlobalValue::isWeakForLinker(Linkage) && |
| 177 | getTriple().isOSBinFormatCOFF()) { |
| 178 | return true; |
| 179 | } |
| 180 | |
Rafael Espindola | 961ba21 | 2013-11-09 01:57:21 +0000 | [diff] [blame] | 181 | if (!InEveryTU) { |
Nico Weber | f867c17 | 2015-01-12 21:22:27 +0000 | [diff] [blame] | 182 | // If we don't have a definition for the destructor yet, don't |
| 183 | // emit. We can't emit aliases to declarations; that's just not |
| 184 | // how aliases work. |
Rafael Espindola | 27c60b5 | 2014-06-03 02:42:01 +0000 | [diff] [blame] | 185 | if (Ref->isDeclaration()) |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 186 | return true; |
Rafael Espindola | 2e2995b | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Rafael Espindola | 129d313 | 2013-11-12 22:06:46 +0000 | [diff] [blame] | 189 | // Don't create an alias to a linker weak symbol. This avoids producing |
| 190 | // different COMDATs in different TUs. Another option would be to |
| 191 | // output the alias both for weak_odr and linkonce_odr, but that |
| 192 | // requires explicit comdat support in the IL. |
| 193 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) |
| 194 | return true; |
| 195 | |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 196 | // Create the alias with no name. |
David Blaikie | 2a791d7 | 2015-09-14 18:38:22 +0000 | [diff] [blame] | 197 | auto *Alias = llvm::GlobalAlias::create(AliasValueType, 0, Linkage, "", |
| 198 | Aliasee, &getModule()); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 199 | |
John McCall | aea181d | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 200 | // Switch any previous uses to the alias. |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 201 | if (Entry) { |
John McCall | aea181d | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 202 | assert(Entry->getType() == AliasType && |
| 203 | "declaration exists with different type"); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 204 | Alias->takeName(Entry); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 205 | Entry->replaceAllUsesWith(Alias); |
| 206 | Entry->eraseFromParent(); |
John McCall | 7ec5043 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 207 | } else { |
Anders Carlsson | ea836bc | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 208 | Alias->setName(MangledName); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 209 | } |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 210 | |
| 211 | // Finally, set up the alias with its proper name and attributes. |
Rafael Espindola | e04a17d | 2014-10-07 13:34:42 +0000 | [diff] [blame] | 212 | setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 213 | |
| 214 | return false; |
| 215 | } |
Anders Carlsson | bd7d11f | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 216 | |
Rafael Espindola | 5368618 | 2014-09-15 19:34:18 +0000 | [diff] [blame] | 217 | llvm::Function *CodeGenModule::codegenCXXStructor(const CXXMethodDecl *MD, |
| 218 | StructorType Type) { |
| 219 | const CGFunctionInfo &FnInfo = |
| 220 | getTypes().arrangeCXXStructorDeclaration(MD, Type); |
| 221 | auto *Fn = cast<llvm::Function>( |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 222 | getAddrOfCXXStructor(MD, Type, &FnInfo, /*FnType=*/nullptr, |
| 223 | /*DontDefer=*/true, /*IsForDefinition=*/true)); |
Rafael Espindola | 5368618 | 2014-09-15 19:34:18 +0000 | [diff] [blame] | 224 | |
| 225 | GlobalDecl GD; |
| 226 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 227 | GD = GlobalDecl(DD, toCXXDtorType(Type)); |
| 228 | } else { |
| 229 | const auto *CD = cast<CXXConstructorDecl>(MD); |
| 230 | GD = GlobalDecl(CD, toCXXCtorType(Type)); |
| 231 | } |
| 232 | |
| 233 | setFunctionLinkage(GD, Fn); |
Hans Wennborg | bb4f962 | 2015-05-28 17:44:56 +0000 | [diff] [blame] | 234 | setFunctionDLLStorageClass(GD, Fn); |
| 235 | |
Rafael Espindola | 5368618 | 2014-09-15 19:34:18 +0000 | [diff] [blame] | 236 | CodeGenFunction(*this).GenerateCode(GD, Fn, FnInfo); |
| 237 | setFunctionDefinitionAttributes(MD, Fn); |
| 238 | SetLLVMFunctionAttributesForDefinition(MD, Fn); |
| 239 | return Fn; |
| 240 | } |
| 241 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 242 | llvm::Constant *CodeGenModule::getAddrOfCXXStructor( |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 243 | const CXXMethodDecl *MD, StructorType Type, const CGFunctionInfo *FnInfo, |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 244 | llvm::FunctionType *FnType, bool DontDefer, bool IsForDefinition) { |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 245 | GlobalDecl GD; |
| 246 | if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { |
| 247 | GD = GlobalDecl(CD, toCXXCtorType(Type)); |
| 248 | } else { |
Duncan P. N. Exon Smith | d6616ac | 2015-05-06 22:18:39 +0000 | [diff] [blame] | 249 | GD = GlobalDecl(cast<CXXDestructorDecl>(MD), toCXXDtorType(Type)); |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 250 | } |
John McCall | d432414 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 251 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 252 | if (!FnType) { |
| 253 | if (!FnInfo) |
| 254 | FnInfo = &getTypes().arrangeCXXStructorDeclaration(MD, Type); |
| 255 | FnType = getTypes().GetFunctionType(*FnInfo); |
| 256 | } |
| 257 | |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 258 | return GetOrCreateLLVMFunction( |
| 259 | getMangledName(GD), FnType, GD, /*ForVTable=*/false, DontDefer, |
| 260 | /*isThunk=*/false, /*ExtraAttrs=*/llvm::AttributeSet(), IsForDefinition); |
Anders Carlsson | e8eeffd | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 261 | } |
Anders Carlsson | eaa28f7 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 262 | |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 263 | static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF, |
| 264 | GlobalDecl GD, |
| 265 | llvm::Type *Ty, |
| 266 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 267 | assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && |
| 268 | "No kext in Microsoft ABI"); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 269 | GD = GD.getCanonicalDecl(); |
| 270 | CodeGenModule &CGM = CGF.CGM; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 271 | llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits()); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 272 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 273 | VTable = CGF.Builder.CreateBitCast(VTable, Ty); |
| 274 | assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 275 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 276 | uint64_t AddressPoint = |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 277 | CGM.getItaniumVTableContext().getVTableLayout(RD) |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 278 | .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); |
| 279 | VTableIndex += AddressPoint; |
| 280 | llvm::Value *VFuncPtr = |
| 281 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 282 | return CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.PointerAlignInBytes); |
Anders Carlsson | e828c36 | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 285 | /// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making |
Fariborz Jahanian | 47609b0 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 286 | /// indirect call to virtual functions. It makes the call through indexing |
| 287 | /// into the vtable. |
| 288 | llvm::Value * |
| 289 | CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 290 | NestedNameSpecifier *Qual, |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 291 | llvm::Type *Ty) { |
Fariborz Jahanian | 47609b0 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 292 | assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && |
| 293 | "BuildAppleKextVirtualCall - bad Qual kind"); |
| 294 | |
| 295 | const Type *QTy = Qual->getAsType(); |
| 296 | QualType T = QualType(QTy, 0); |
| 297 | const RecordType *RT = T->getAs<RecordType>(); |
| 298 | assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 299 | const auto *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 300 | |
| 301 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 302 | return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 303 | |
| 304 | return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD); |
Fariborz Jahanian | 47609b0 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 307 | /// BuildVirtualCall - This routine makes indirect vtable call for |
| 308 | /// call to virtual destructors. It returns 0 if it could not do it. |
| 309 | llvm::Value * |
| 310 | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
| 311 | const CXXDestructorDecl *DD, |
| 312 | CXXDtorType Type, |
| 313 | const CXXRecordDecl *RD) { |
Rafael Espindola | 2ae250c | 2014-05-09 00:08:36 +0000 | [diff] [blame] | 314 | const auto *MD = cast<CXXMethodDecl>(DD); |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 315 | // FIXME. Dtor_Base dtor is always direct!! |
| 316 | // It need be somehow inline expanded into the caller. |
| 317 | // -O does that. But need to support -O0 as well. |
| 318 | if (MD->isVirtual() && Type != Dtor_Base) { |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 319 | // Compute the function type we're calling. |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 320 | const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration( |
| 321 | DD, StructorType::Complete); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 322 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo); |
Timur Iskhodzhanov | 03e8746 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 323 | return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD); |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 324 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 325 | return nullptr; |
Fariborz Jahanian | 265c325 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 326 | } |