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