Devang Patel | daaf1de | 2010-10-04 21:15:33 +0000 | [diff] [blame] | 1 | //===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===// |
Anders Carlsson | e1b29ef | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 14 | // We might split this into multiple files if it gets too unwieldy |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 15 | |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 23 | #include "clang/AST/Mangle.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtCXX.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
John McCall | c0bf462 | 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 | dc8e938 | 2013-11-13 23:20:45 +0000 | [diff] [blame] | 37 | // Producing an alias to a base class ctor/dtor can degrade debug quality |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 38 | // as the debugger cannot tell them apart. |
Rafael Espindola | dc8e938 | 2013-11-13 23:20:45 +0000 | [diff] [blame] | 39 | if (getCodeGenOpts().OptimizationLevel == 0) |
| 40 | return true; |
| 41 | |
John McCall | c0bf462 | 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 | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 44 | if (!D->hasTrivialBody()) |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 45 | return true; |
| 46 | |
| 47 | const CXXRecordDecl *Class = D->getParent(); |
| 48 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 49 | // We are going to instrument this destructor, so give up even if it is |
| 50 | // currently empty. |
| 51 | if (Class->mayInsertExtraPadding()) |
| 52 | return true; |
| 53 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 54 | // If we need to manipulate a VTT parameter, give up. |
| 55 | if (Class->getNumVBases()) { |
| 56 | // Extra Credit: passing extra parameters is perfectly safe |
| 57 | // in many calling conventions, so only bail out if the ctor's |
| 58 | // calling convention is nonstandard. |
| 59 | return true; |
| 60 | } |
| 61 | |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 62 | // If any field has a non-trivial destructor, we have to emit the |
| 63 | // destructor separately. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 64 | for (const auto *I : Class->fields()) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 65 | if (I->getType().isDestructedType()) |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 66 | return true; |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 67 | |
| 68 | // Try to find a unique base class with a non-trivial destructor. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 69 | const CXXRecordDecl *UniqueBase = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 70 | for (const auto &I : Class->bases()) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 71 | |
| 72 | // We're in the base destructor, so skip virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 73 | if (I.isVirtual()) continue; |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 74 | |
| 75 | // Skip base classes with trivial destructors. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 76 | const auto *Base = |
| 77 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 78 | if (Base->hasTrivialDestructor()) continue; |
| 79 | |
| 80 | // If we've already found a base class with a non-trivial |
| 81 | // destructor, give up. |
| 82 | if (UniqueBase) return true; |
| 83 | UniqueBase = Base; |
| 84 | } |
| 85 | |
| 86 | // If we didn't find any bases with a non-trivial destructor, then |
| 87 | // the base destructor is actually effectively trivial, which can |
| 88 | // happen if it was needlessly user-defined or if there are virtual |
| 89 | // bases with non-trivial destructors. |
| 90 | if (!UniqueBase) |
| 91 | return true; |
| 92 | |
| 93 | // If the base is at a non-zero offset, give up. |
| 94 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 95 | if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero()) |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 96 | return true; |
| 97 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 98 | // Give up if the calling conventions don't match. We could update the call, |
| 99 | // but it is probably not worth it. |
Rafael Espindola | 6e84e92 | 2013-11-09 01:57:21 +0000 | [diff] [blame] | 100 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 101 | if (BaseD->getType()->getAs<FunctionType>()->getCallConv() != |
| 102 | D->getType()->getAs<FunctionType>()->getCallConv()) |
| 103 | return true; |
| 104 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 105 | return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 106 | GlobalDecl(BaseD, Dtor_Base), |
| 107 | false); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 108 | } |
| 109 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 110 | /// Try to emit a definition as a global alias for another definition. |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 111 | /// If \p InEveryTU is true, we know that an equivalent alias can be produced |
| 112 | /// in every translation unit. |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 113 | bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 114 | GlobalDecl TargetDecl, |
| 115 | bool InEveryTU) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 116 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 117 | return true; |
| 118 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 119 | // The alias will use the linkage of the referent. If we can't |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 120 | // support aliases with that linkage, fail. |
Peter Collingbourne | 144a31f | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 121 | llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 122 | |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 123 | // We can't use an alias if the linkage is not valid for one. |
| 124 | if (!llvm::GlobalAlias::isValidLinkage(Linkage)) |
Rafael Espindola | bc6afd1 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 125 | return true; |
| 126 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 127 | // Don't create a weak alias for a dllexport'd symbol. |
| 128 | if (AliasDecl.getDecl()->hasAttr<DLLExportAttr>() && |
| 129 | llvm::GlobalValue::isWeakForLinker(Linkage)) |
| 130 | return true; |
| 131 | |
Rafael Espindola | 61a0a75 | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 132 | llvm::GlobalValue::LinkageTypes TargetLinkage = |
| 133 | getFunctionLinkage(TargetDecl); |
| 134 | |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 135 | // Check if we have it already. |
| 136 | StringRef MangledName = getMangledName(AliasDecl); |
| 137 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
| 138 | if (Entry && !Entry->isDeclaration()) |
| 139 | return false; |
Rafael Espindola | 61a0a75 | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 140 | if (Replacements.count(MangledName)) |
| 141 | return false; |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 142 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 143 | // Derive the type for the alias. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 144 | llvm::PointerType *AliasType |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 145 | = getTypes().GetFunctionType(AliasDecl)->getPointerTo(); |
| 146 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 147 | // Find the referent. Some aliases might require a bitcast, in |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 148 | // which case the caller is responsible for ensuring the soundness |
| 149 | // of these semantics. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 150 | auto *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 151 | llvm::Constant *Aliasee = Ref; |
| 152 | if (Ref->getType() != AliasType) |
| 153 | Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 154 | |
Rafael Espindola | 0171c6b | 2013-11-08 22:59:46 +0000 | [diff] [blame] | 155 | // Instead of creating as alias to a linkonce_odr, replace all of the uses |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 156 | // of the aliasee. |
Bill Wendling | cc05778 | 2013-11-25 05:40:33 +0000 | [diff] [blame] | 157 | if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) && |
| 158 | (TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage || |
| 159 | !TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 160 | // FIXME: An extern template instantiation will create functions with |
Bill Wendling | cc05778 | 2013-11-25 05:40:33 +0000 | [diff] [blame] | 161 | // linkage "AvailableExternally". In libc++, some classes also define |
| 162 | // members with attribute "AlwaysInline" and expect no reference to |
| 163 | // be generated. It is desirable to reenable this optimisation after |
| 164 | // corresponding LLVM changes. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 165 | Replacements[MangledName] = Aliasee; |
Rafael Espindola | 0171c6b | 2013-11-08 22:59:46 +0000 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
Rafael Espindola | 6e84e92 | 2013-11-09 01:57:21 +0000 | [diff] [blame] | 169 | if (!InEveryTU) { |
| 170 | /// If we don't have a definition for the destructor yet, don't |
| 171 | /// emit. We can't emit aliases to declarations; that's just not |
| 172 | /// how aliases work. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 173 | if (Ref->isDeclaration()) |
Rafael Espindola | 61a0a75 | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 174 | return true; |
Rafael Espindola | 61a0a75 | 2013-11-05 21:37:29 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Rafael Espindola | 610616c | 2013-11-12 22:06:46 +0000 | [diff] [blame] | 177 | // Don't create an alias to a linker weak symbol. This avoids producing |
| 178 | // different COMDATs in different TUs. Another option would be to |
| 179 | // output the alias both for weak_odr and linkonce_odr, but that |
| 180 | // requires explicit comdat support in the IL. |
| 181 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) |
| 182 | return true; |
| 183 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 184 | // Create the alias with no name. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 185 | auto *Alias = llvm::GlobalAlias::create(AliasType->getElementType(), 0, |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 186 | Linkage, "", Aliasee, &getModule()); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 187 | |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 188 | // Switch any previous uses to the alias. |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 189 | if (Entry) { |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 190 | assert(Entry->getType() == AliasType && |
| 191 | "declaration exists with different type"); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 192 | Alias->takeName(Entry); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 193 | Entry->replaceAllUsesWith(Alias); |
| 194 | Entry->eraseFromParent(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 195 | } else { |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 196 | Alias->setName(MangledName); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 197 | } |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 198 | |
| 199 | // Finally, set up the alias with its proper name and attributes. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 200 | setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 201 | |
| 202 | return false; |
| 203 | } |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 204 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 205 | llvm::Function *CodeGenModule::codegenCXXStructor(const CXXMethodDecl *MD, |
| 206 | StructorType Type) { |
| 207 | const CGFunctionInfo &FnInfo = |
| 208 | getTypes().arrangeCXXStructorDeclaration(MD, Type); |
| 209 | auto *Fn = cast<llvm::Function>( |
| 210 | getAddrOfCXXStructor(MD, Type, &FnInfo, nullptr, true)); |
| 211 | |
| 212 | GlobalDecl GD; |
| 213 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 214 | GD = GlobalDecl(DD, toCXXDtorType(Type)); |
| 215 | } else { |
| 216 | const auto *CD = cast<CXXConstructorDecl>(MD); |
| 217 | GD = GlobalDecl(CD, toCXXCtorType(Type)); |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 220 | setFunctionLinkage(GD, Fn); |
| 221 | CodeGenFunction(*this).GenerateCode(GD, Fn, FnInfo); |
| 222 | setFunctionDefinitionAttributes(MD, Fn); |
| 223 | SetLLVMFunctionAttributesForDefinition(MD, Fn); |
| 224 | return Fn; |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 227 | llvm::GlobalValue *CodeGenModule::getAddrOfCXXStructor( |
| 228 | const CXXMethodDecl *MD, StructorType Type, const CGFunctionInfo *FnInfo, |
| 229 | llvm::FunctionType *FnType, bool DontDefer) { |
| 230 | GlobalDecl GD; |
| 231 | if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { |
| 232 | GD = GlobalDecl(CD, toCXXCtorType(Type)); |
| 233 | } else { |
| 234 | auto *DD = dyn_cast<CXXDestructorDecl>(MD); |
| 235 | GD = GlobalDecl(DD, toCXXDtorType(Type)); |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 236 | } |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 237 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 238 | StringRef Name = getMangledName(GD); |
| 239 | if (llvm::GlobalValue *Existing = GetGlobalValue(Name)) |
| 240 | return Existing; |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 241 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 242 | if (!FnType) { |
| 243 | if (!FnInfo) |
| 244 | FnInfo = &getTypes().arrangeCXXStructorDeclaration(MD, Type); |
| 245 | FnType = getTypes().GetFunctionType(*FnInfo); |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 246 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 247 | |
| 248 | return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FnType, GD, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 249 | /*ForVTable=*/false, |
| 250 | DontDefer)); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 253 | static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF, |
| 254 | GlobalDecl GD, |
| 255 | llvm::Type *Ty, |
| 256 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 257 | assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && |
| 258 | "No kext in Microsoft ABI"); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 259 | GD = GD.getCanonicalDecl(); |
| 260 | CodeGenModule &CGM = CGF.CGM; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 261 | llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits()); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 262 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 263 | VTable = CGF.Builder.CreateBitCast(VTable, Ty); |
| 264 | assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 265 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 266 | uint64_t AddressPoint = |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 267 | CGM.getItaniumVTableContext().getVTableLayout(RD) |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 268 | .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); |
| 269 | VTableIndex += AddressPoint; |
| 270 | llvm::Value *VFuncPtr = |
| 271 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 272 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 273 | } |
| 274 | |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 275 | /// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 276 | /// indirect call to virtual functions. It makes the call through indexing |
| 277 | /// into the vtable. |
| 278 | llvm::Value * |
| 279 | CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 280 | NestedNameSpecifier *Qual, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 281 | llvm::Type *Ty) { |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 282 | assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && |
| 283 | "BuildAppleKextVirtualCall - bad Qual kind"); |
| 284 | |
| 285 | const Type *QTy = Qual->getAsType(); |
| 286 | QualType T = QualType(QTy, 0); |
| 287 | const RecordType *RT = T->getAs<RecordType>(); |
| 288 | assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 289 | const auto *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 290 | |
| 291 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 292 | return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 293 | |
| 294 | return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD); |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 297 | /// BuildVirtualCall - This routine makes indirect vtable call for |
| 298 | /// call to virtual destructors. It returns 0 if it could not do it. |
| 299 | llvm::Value * |
| 300 | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
| 301 | const CXXDestructorDecl *DD, |
| 302 | CXXDtorType Type, |
| 303 | const CXXRecordDecl *RD) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 304 | const auto *MD = cast<CXXMethodDecl>(DD); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 305 | // FIXME. Dtor_Base dtor is always direct!! |
| 306 | // It need be somehow inline expanded into the caller. |
| 307 | // -O does that. But need to support -O0 as well. |
| 308 | if (MD->isVirtual() && Type != Dtor_Base) { |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 309 | // Compute the function type we're calling. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 310 | const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration( |
| 311 | DD, StructorType::Complete); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 312 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 313 | return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 314 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 315 | return nullptr; |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 316 | } |