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 | |
| 37 | // If the destructor doesn't have a trivial body, we have to emit it |
| 38 | // separately. |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 39 | if (!D->hasTrivialBody()) |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 40 | return true; |
| 41 | |
| 42 | const CXXRecordDecl *Class = D->getParent(); |
| 43 | |
| 44 | // If we need to manipulate a VTT parameter, give up. |
| 45 | if (Class->getNumVBases()) { |
| 46 | // Extra Credit: passing extra parameters is perfectly safe |
| 47 | // in many calling conventions, so only bail out if the ctor's |
| 48 | // calling convention is nonstandard. |
| 49 | return true; |
| 50 | } |
| 51 | |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 52 | // If any field has a non-trivial destructor, we have to emit the |
| 53 | // destructor separately. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 54 | for (CXXRecordDecl::field_iterator I = Class->field_begin(), |
| 55 | E = Class->field_end(); I != E; ++I) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 56 | if (I->getType().isDestructedType()) |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 57 | return true; |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 58 | |
| 59 | // Try to find a unique base class with a non-trivial destructor. |
| 60 | const CXXRecordDecl *UniqueBase = 0; |
| 61 | for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), |
| 62 | E = Class->bases_end(); I != E; ++I) { |
| 63 | |
| 64 | // We're in the base destructor, so skip virtual bases. |
| 65 | if (I->isVirtual()) continue; |
| 66 | |
| 67 | // Skip base classes with trivial destructors. |
| 68 | const CXXRecordDecl *Base |
| 69 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 70 | if (Base->hasTrivialDestructor()) continue; |
| 71 | |
| 72 | // If we've already found a base class with a non-trivial |
| 73 | // destructor, give up. |
| 74 | if (UniqueBase) return true; |
| 75 | UniqueBase = Base; |
| 76 | } |
| 77 | |
| 78 | // If we didn't find any bases with a non-trivial destructor, then |
| 79 | // the base destructor is actually effectively trivial, which can |
| 80 | // happen if it was needlessly user-defined or if there are virtual |
| 81 | // bases with non-trivial destructors. |
| 82 | if (!UniqueBase) |
| 83 | return true; |
| 84 | |
John McCall | 9a70846 | 2010-03-03 03:40:11 +0000 | [diff] [blame] | 85 | /// If we don't have a definition for the destructor yet, don't |
| 86 | /// emit. We can't emit aliases to declarations; that's just not |
| 87 | /// how aliases work. |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 88 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 89 | if (!BaseD->isImplicit() && !BaseD->hasBody()) |
John McCall | 9a70846 | 2010-03-03 03:40:11 +0000 | [diff] [blame] | 90 | return true; |
| 91 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 92 | // If the base is at a non-zero offset, give up. |
| 93 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 94 | if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero()) |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 95 | return true; |
| 96 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 97 | return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 98 | GlobalDecl(BaseD, Dtor_Base), |
| 99 | false); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 100 | } |
| 101 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 102 | /// Try to emit a definition as a global alias for another definition. |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 103 | /// If \p InEveryTU is true, we know that an equivalent alias can be produced |
| 104 | /// in every translation unit. |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 105 | bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 106 | GlobalDecl TargetDecl, |
| 107 | bool InEveryTU) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 108 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 109 | return true; |
| 110 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 111 | // The alias will use the linkage of the referrent. If we can't |
| 112 | // support aliases with that linkage, fail. |
Peter Collingbourne | 144a31f | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 113 | llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 114 | |
Rafael Espindola | 583f6de | 2013-11-05 05:29:54 +0000 | [diff] [blame^] | 115 | llvm::GlobalValue::LinkageTypes TargetLinkage |
| 116 | = getFunctionLinkage(TargetDecl); |
| 117 | |
| 118 | // Don't create an alias to a linker weak symbol unless we know we can do |
| 119 | // that in every TU. This avoids producing different COMDATs in different |
| 120 | // TUs. |
| 121 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) { |
| 122 | if (!InEveryTU) |
| 123 | return true; |
| 124 | |
| 125 | // In addition to making sure we produce it in every TU, we have to make |
| 126 | // sure llvm keeps it. |
| 127 | // FIXME: Instead of outputting an alias we could just replace every use of |
| 128 | // AliasDecl with TargetDecl. |
| 129 | assert(Linkage == TargetLinkage); |
| 130 | Linkage = llvm::GlobalValue::WeakODRLinkage; |
| 131 | } |
| 132 | |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 133 | // We can't use an alias if the linkage is not valid for one. |
| 134 | if (!llvm::GlobalAlias::isValidLinkage(Linkage)) |
Rafael Espindola | bc6afd1 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 135 | return true; |
| 136 | |
Rafael Espindola | 71fdc12 | 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; |
| 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 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 147 | // Find the referrent. Some aliases might require a bitcast, in |
| 148 | // which case the caller is responsible for ensuring the soundness |
| 149 | // of these semantics. |
| 150 | llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
| 151 | llvm::Constant *Aliasee = Ref; |
| 152 | if (Ref->getType() != AliasType) |
| 153 | Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); |
| 154 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 155 | // Create the alias with no name. |
| 156 | llvm::GlobalAlias *Alias = |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 157 | new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule()); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 158 | |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 159 | // Switch any previous uses to the alias. |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 160 | if (Entry) { |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 161 | assert(Entry->getType() == AliasType && |
| 162 | "declaration exists with different type"); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 163 | Alias->takeName(Entry); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 164 | Entry->replaceAllUsesWith(Alias); |
| 165 | Entry->eraseFromParent(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 166 | } else { |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 167 | Alias->setName(MangledName); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 168 | } |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 169 | |
| 170 | // Finally, set up the alias with its proper name and attributes. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 171 | SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 172 | |
| 173 | return false; |
| 174 | } |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 175 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 176 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor, |
| 177 | CXXCtorType ctorType) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 178 | // The complete constructor is equivalent to the base constructor |
| 179 | // for classes with no virtual bases. Try to emit it as an alias. |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 180 | if (getTarget().getCXXABI().hasConstructorVariants() && |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 181 | !ctor->getParent()->getNumVBases() && |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 182 | (ctorType == Ctor_Complete || ctorType == Ctor_Base)) { |
| 183 | bool ProducedAlias = |
| 184 | !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete), |
| 185 | GlobalDecl(ctor, Ctor_Base), true); |
| 186 | if (ctorType == Ctor_Complete && ProducedAlias) |
| 187 | return; |
| 188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 190 | const CGFunctionInfo &fnInfo = |
| 191 | getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 192 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 193 | llvm::Function *fn = |
| 194 | cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo)); |
Peter Collingbourne | 144a31f | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 195 | setFunctionLinkage(GlobalDecl(ctor, ctorType), fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 197 | CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 199 | SetFunctionDefinitionAttributes(ctor, fn); |
| 200 | SetLLVMFunctionAttributesForDefinition(ctor, fn); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 201 | } |
| 202 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 203 | llvm::GlobalValue * |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 204 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, |
| 205 | CXXCtorType ctorType, |
| 206 | const CGFunctionInfo *fnInfo) { |
| 207 | GlobalDecl GD(ctor, ctorType); |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 209 | StringRef name = getMangledName(GD); |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 210 | if (llvm::GlobalValue *existing = GetGlobalValue(name)) |
| 211 | return existing; |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 212 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 213 | if (!fnInfo) |
| 214 | fnInfo = &getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType); |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 215 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 216 | llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo); |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 217 | return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 218 | /*ForVTable=*/false)); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 219 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 220 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 221 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor, |
| 222 | CXXDtorType dtorType) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 223 | // The complete destructor is equivalent to the base destructor for |
| 224 | // classes with no virtual bases, so try to emit it as an alias. |
Rafael Espindola | 71fdc12 | 2013-11-04 18:38:59 +0000 | [diff] [blame] | 225 | if (!dtor->getParent()->getNumVBases() && |
| 226 | (dtorType == Dtor_Complete || dtorType == Dtor_Base)) { |
| 227 | bool ProducedAlias = |
| 228 | !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete), |
| 229 | GlobalDecl(dtor, Dtor_Base), true); |
| 230 | if (ProducedAlias) { |
| 231 | if (dtorType == Dtor_Complete) |
| 232 | return; |
| 233 | if (dtor->isVirtual()) |
| 234 | getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete)); |
| 235 | } |
| 236 | } |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 237 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 238 | // The base destructor is equivalent to the base destructor of its |
| 239 | // base class if there is exactly one non-virtual base class with a |
| 240 | // non-trivial destructor, there are no fields with a non-trivial |
| 241 | // destructor, and the body of the destructor is trivial. |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 242 | if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor)) |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 243 | return; |
| 244 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 245 | const CGFunctionInfo &fnInfo = |
| 246 | getTypes().arrangeCXXDestructor(dtor, dtorType); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 247 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 248 | llvm::Function *fn = |
| 249 | cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo)); |
Peter Collingbourne | 144a31f | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 250 | setFunctionLinkage(GlobalDecl(dtor, dtorType), fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 252 | CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 254 | SetFunctionDefinitionAttributes(dtor, fn); |
| 255 | SetLLVMFunctionAttributesForDefinition(dtor, fn); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 256 | } |
| 257 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 258 | llvm::GlobalValue * |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 259 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor, |
| 260 | CXXDtorType dtorType, |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 261 | const CGFunctionInfo *fnInfo, |
| 262 | llvm::FunctionType *fnType) { |
| 263 | // If the class has no virtual bases, then the complete and base destructors |
| 264 | // are equivalent, for all C++ ABIs supported by clang. We can save on code |
| 265 | // size by calling the base dtor directly, especially if we'd have to emit a |
| 266 | // thunk otherwise. |
| 267 | // FIXME: We should do this for Itanium, after verifying that nothing breaks. |
| 268 | if (dtorType == Dtor_Complete && dtor->getParent()->getNumVBases() == 0 && |
| 269 | getCXXABI().useThunkForDtorVariant(dtor, Dtor_Complete)) |
| 270 | dtorType = Dtor_Base; |
| 271 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 272 | GlobalDecl GD(dtor, dtorType); |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 274 | StringRef name = getMangledName(GD); |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 275 | if (llvm::GlobalValue *existing = GetGlobalValue(name)) |
| 276 | return existing; |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 277 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 278 | if (!fnType) { |
| 279 | if (!fnInfo) fnInfo = &getTypes().arrangeCXXDestructor(dtor, dtorType); |
| 280 | fnType = getTypes().GetFunctionType(*fnInfo); |
| 281 | } |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 282 | return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 283 | /*ForVTable=*/false)); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 286 | static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF, |
| 287 | GlobalDecl GD, |
| 288 | llvm::Type *Ty, |
| 289 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 290 | assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && |
| 291 | "No kext in Microsoft ABI"); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 292 | GD = GD.getCanonicalDecl(); |
| 293 | CodeGenModule &CGM = CGF.CGM; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 294 | llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits()); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 295 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 296 | VTable = CGF.Builder.CreateBitCast(VTable, Ty); |
| 297 | assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); |
| 298 | uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(GD); |
| 299 | uint64_t AddressPoint = |
| 300 | CGM.getVTableContext().getVTableLayout(RD) |
| 301 | .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); |
| 302 | VTableIndex += AddressPoint; |
| 303 | llvm::Value *VFuncPtr = |
| 304 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 305 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 306 | } |
| 307 | |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 308 | /// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 309 | /// indirect call to virtual functions. It makes the call through indexing |
| 310 | /// into the vtable. |
| 311 | llvm::Value * |
| 312 | CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 313 | NestedNameSpecifier *Qual, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 314 | llvm::Type *Ty) { |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 315 | assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && |
| 316 | "BuildAppleKextVirtualCall - bad Qual kind"); |
| 317 | |
| 318 | const Type *QTy = Qual->getAsType(); |
| 319 | QualType T = QualType(QTy, 0); |
| 320 | const RecordType *RT = T->getAs<RecordType>(); |
| 321 | assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); |
| 322 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 323 | |
| 324 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) |
| 325 | return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 326 | |
| 327 | return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD); |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 330 | /// BuildVirtualCall - This routine makes indirect vtable call for |
| 331 | /// call to virtual destructors. It returns 0 if it could not do it. |
| 332 | llvm::Value * |
| 333 | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
| 334 | const CXXDestructorDecl *DD, |
| 335 | CXXDtorType Type, |
| 336 | const CXXRecordDecl *RD) { |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 337 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD); |
| 338 | // FIXME. Dtor_Base dtor is always direct!! |
| 339 | // It need be somehow inline expanded into the caller. |
| 340 | // -O does that. But need to support -O0 as well. |
| 341 | if (MD->isVirtual() && Type != Dtor_Base) { |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 342 | // Compute the function type we're calling. |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 343 | const CGFunctionInfo &FInfo = |
| 344 | CGM.getTypes().arrangeCXXDestructor(DD, Dtor_Complete); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 345 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo); |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 346 | return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 347 | } |
Timur Iskhodzhanov | 6e007f9 | 2013-07-19 08:14:45 +0000 | [diff] [blame] | 348 | return 0; |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 349 | } |