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 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
| 18 | #include "CodeGenModule.h" |
| 19 | #include "clang/AST/ASTContext.h" |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 24 | #include "clang/AST/Mangle.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) |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 56 | if ((*I)->getType().isDestructedType()) |
| 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); |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 94 | if (ClassLayout.getBaseClassOffsetInBits(UniqueBase) != 0) |
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), |
| 98 | GlobalDecl(BaseD, Dtor_Base)); |
| 99 | } |
| 100 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 101 | /// Try to emit a definition as a global alias for another definition. |
| 102 | bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, |
| 103 | GlobalDecl TargetDecl) { |
| 104 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 105 | return true; |
| 106 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 107 | // The alias will use the linkage of the referrent. If we can't |
| 108 | // support aliases with that linkage, fail. |
| 109 | llvm::GlobalValue::LinkageTypes Linkage |
| 110 | = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl())); |
| 111 | |
| 112 | switch (Linkage) { |
| 113 | // We can definitely emit aliases to definitions with external linkage. |
| 114 | case llvm::GlobalValue::ExternalLinkage: |
| 115 | case llvm::GlobalValue::ExternalWeakLinkage: |
| 116 | break; |
| 117 | |
| 118 | // Same with local linkage. |
| 119 | case llvm::GlobalValue::InternalLinkage: |
| 120 | case llvm::GlobalValue::PrivateLinkage: |
| 121 | case llvm::GlobalValue::LinkerPrivateLinkage: |
| 122 | break; |
| 123 | |
| 124 | // We should try to support linkonce linkages. |
| 125 | case llvm::GlobalValue::LinkOnceAnyLinkage: |
| 126 | case llvm::GlobalValue::LinkOnceODRLinkage: |
| 127 | return true; |
| 128 | |
| 129 | // Other linkages will probably never be supported. |
| 130 | default: |
| 131 | return true; |
| 132 | } |
| 133 | |
Rafael Espindola | bc6afd1 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 134 | llvm::GlobalValue::LinkageTypes TargetLinkage |
| 135 | = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl())); |
| 136 | |
Rafael Espindola | 3c15745 | 2010-03-06 07:35:18 +0000 | [diff] [blame] | 137 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) |
Rafael Espindola | bc6afd1 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 138 | return true; |
| 139 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 140 | // Derive the type for the alias. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 141 | llvm::PointerType *AliasType |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 142 | = getTypes().GetFunctionType(AliasDecl)->getPointerTo(); |
| 143 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 144 | // Find the referrent. Some aliases might require a bitcast, in |
| 145 | // which case the caller is responsible for ensuring the soundness |
| 146 | // of these semantics. |
| 147 | llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
| 148 | llvm::Constant *Aliasee = Ref; |
| 149 | if (Ref->getType() != AliasType) |
| 150 | Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); |
| 151 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 152 | // Create the alias with no name. |
| 153 | llvm::GlobalAlias *Alias = |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 154 | new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule()); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 155 | |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 156 | // Switch any previous uses to the alias. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 157 | StringRef MangledName = getMangledName(AliasDecl); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 158 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 159 | if (Entry) { |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 160 | assert(Entry->isDeclaration() && "definition already exists for alias"); |
| 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 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 176 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 177 | // The constructor used for constructing this as a complete class; |
| 178 | // constucts the virtual bases, then calls the base constructor. |
John McCall | 492bafc | 2011-05-17 21:05:49 +0000 | [diff] [blame] | 179 | if (!D->getParent()->isAbstract()) { |
Anders Carlsson | 8e0397a | 2011-05-08 17:25:05 +0000 | [diff] [blame] | 180 | // We don't need to emit the complete ctor if the class is abstract. |
| 181 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 182 | } |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 183 | |
| 184 | // The constructor used for constructing this as a base class; |
| 185 | // ignores virtual bases. |
John McCall | 8e51a1f | 2010-02-18 21:31:48 +0000 | [diff] [blame] | 186 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 187 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 188 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 189 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor, |
| 190 | CXXCtorType ctorType) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 191 | // The complete constructor is equivalent to the base constructor |
| 192 | // for classes with no virtual bases. Try to emit it as an alias. |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 193 | if (ctorType == Ctor_Complete && |
| 194 | !ctor->getParent()->getNumVBases() && |
| 195 | !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete), |
| 196 | GlobalDecl(ctor, Ctor_Base))) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 197 | return; |
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 | const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(ctor, ctorType); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 200 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 201 | llvm::Function *fn = |
| 202 | cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo)); |
| 203 | setFunctionLinkage(ctor, fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 205 | CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 207 | SetFunctionDefinitionAttributes(ctor, fn); |
| 208 | SetLLVMFunctionAttributesForDefinition(ctor, fn); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 209 | } |
| 210 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 211 | llvm::GlobalValue * |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 212 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, |
| 213 | CXXCtorType ctorType, |
| 214 | const CGFunctionInfo *fnInfo) { |
| 215 | GlobalDecl GD(ctor, ctorType); |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 217 | StringRef name = getMangledName(GD); |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 218 | if (llvm::GlobalValue *existing = GetGlobalValue(name)) |
| 219 | return existing; |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 220 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 221 | if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(ctor, ctorType); |
| 222 | |
| 223 | const FunctionProtoType *proto = ctor->getType()->castAs<FunctionProtoType>(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 224 | llvm::FunctionType *fnType = |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 225 | getTypes().GetFunctionType(*fnInfo, proto->isVariadic()); |
| 226 | return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 227 | /*ForVTable=*/false)); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 228 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 229 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 230 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 231 | // The destructor in a virtual table is always a 'deleting' |
| 232 | // destructor, which calls the complete destructor and then uses the |
| 233 | // appropriate operator delete. |
Eli Friedman | ea9a208 | 2009-11-14 04:19:37 +0000 | [diff] [blame] | 234 | if (D->isVirtual()) |
Eli Friedman | 624c7d7 | 2009-12-15 02:06:15 +0000 | [diff] [blame] | 235 | EmitGlobal(GlobalDecl(D, Dtor_Deleting)); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 236 | |
| 237 | // The destructor used for destructing this as a most-derived class; |
| 238 | // call the base destructor and then destructs any virtual bases. |
Douglas Gregor | 3e23d68 | 2011-07-26 23:18:30 +0000 | [diff] [blame] | 239 | EmitGlobal(GlobalDecl(D, Dtor_Complete)); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 240 | |
| 241 | // The destructor used for destructing this as a base class; ignores |
| 242 | // virtual bases. |
John McCall | 8e51a1f | 2010-02-18 21:31:48 +0000 | [diff] [blame] | 243 | EmitGlobal(GlobalDecl(D, Dtor_Base)); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 244 | } |
| 245 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 246 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor, |
| 247 | CXXDtorType dtorType) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 248 | // The complete destructor is equivalent to the base destructor for |
| 249 | // classes with no virtual bases, so try to emit it as an alias. |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 250 | if (dtorType == Dtor_Complete && |
| 251 | !dtor->getParent()->getNumVBases() && |
| 252 | !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete), |
| 253 | GlobalDecl(dtor, Dtor_Base))) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 254 | return; |
| 255 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 256 | // The base destructor is equivalent to the base destructor of its |
| 257 | // base class if there is exactly one non-virtual base class with a |
| 258 | // non-trivial destructor, there are no fields with a non-trivial |
| 259 | // destructor, and the body of the destructor is trivial. |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 260 | if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor)) |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 261 | return; |
| 262 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 263 | const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(dtor, dtorType); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 264 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 265 | llvm::Function *fn = |
| 266 | cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo)); |
| 267 | setFunctionLinkage(dtor, fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 269 | CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 271 | SetFunctionDefinitionAttributes(dtor, fn); |
| 272 | SetLLVMFunctionAttributesForDefinition(dtor, fn); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 273 | } |
| 274 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 275 | llvm::GlobalValue * |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 276 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor, |
| 277 | CXXDtorType dtorType, |
| 278 | const CGFunctionInfo *fnInfo) { |
| 279 | GlobalDecl GD(dtor, dtorType); |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 281 | StringRef name = getMangledName(GD); |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 282 | if (llvm::GlobalValue *existing = GetGlobalValue(name)) |
| 283 | return existing; |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 284 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 285 | if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(dtor, dtorType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 287 | llvm::FunctionType *fnType = |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 288 | getTypes().GetFunctionType(*fnInfo, false); |
| 289 | |
| 290 | return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 291 | /*ForVTable=*/false)); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 294 | static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 295 | llvm::Value *This, llvm::Type *Ty) { |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 296 | Ty = Ty->getPointerTo()->getPointerTo(); |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 297 | |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 298 | llvm::Value *VTable = CGF.GetVTablePtr(This, Ty); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 299 | llvm::Value *VFuncPtr = |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 300 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 301 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 302 | } |
| 303 | |
| 304 | llvm::Value * |
| 305 | CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 306 | llvm::Type *Ty) { |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 307 | MD = MD->getCanonicalDecl(); |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 308 | uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(MD); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 309 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 310 | return ::BuildVirtualCall(*this, VTableIndex, This, Ty); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 313 | /// BuildVirtualCall - This routine is to support gcc's kext ABI making |
| 314 | /// indirect call to virtual functions. It makes the call through indexing |
| 315 | /// into the vtable. |
| 316 | llvm::Value * |
| 317 | CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 318 | NestedNameSpecifier *Qual, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 319 | llvm::Type *Ty) { |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 320 | llvm::Value *VTable = 0; |
| 321 | assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && |
| 322 | "BuildAppleKextVirtualCall - bad Qual kind"); |
| 323 | |
| 324 | const Type *QTy = Qual->getAsType(); |
| 325 | QualType T = QualType(QTy, 0); |
| 326 | const RecordType *RT = T->getAs<RecordType>(); |
| 327 | assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); |
| 328 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 329 | |
| 330 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) |
| 331 | return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); |
| 332 | |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 333 | VTable = CGM.getVTables().GetAddrOfVTable(RD); |
| 334 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 335 | VTable = Builder.CreateBitCast(VTable, Ty); |
| 336 | assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); |
| 337 | MD = MD->getCanonicalDecl(); |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 338 | uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(MD); |
Fariborz Jahanian | a50e33e | 2011-01-28 23:42:29 +0000 | [diff] [blame] | 339 | uint64_t AddressPoint = |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 340 | CGM.getVTableContext().getVTableLayout(RD) |
| 341 | .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); |
Fariborz Jahanian | a50e33e | 2011-01-28 23:42:29 +0000 | [diff] [blame] | 342 | VTableIndex += AddressPoint; |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 343 | llvm::Value *VFuncPtr = |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 344 | Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
| 345 | return Builder.CreateLoad(VFuncPtr); |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 348 | /// BuildVirtualCall - This routine makes indirect vtable call for |
| 349 | /// call to virtual destructors. It returns 0 if it could not do it. |
| 350 | llvm::Value * |
| 351 | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
| 352 | const CXXDestructorDecl *DD, |
| 353 | CXXDtorType Type, |
| 354 | const CXXRecordDecl *RD) { |
| 355 | llvm::Value * Callee = 0; |
| 356 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD); |
| 357 | // FIXME. Dtor_Base dtor is always direct!! |
| 358 | // It need be somehow inline expanded into the caller. |
| 359 | // -O does that. But need to support -O0 as well. |
| 360 | if (MD->isVirtual() && Type != Dtor_Base) { |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 361 | // Compute the function type we're calling. |
| 362 | const CGFunctionInfo *FInfo = |
| 363 | &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD), |
| 364 | Dtor_Complete); |
| 365 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 366 | llvm::Type *Ty |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 367 | = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic()); |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 368 | |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 369 | llvm::Value *VTable = CGM.getVTables().GetAddrOfVTable(RD); |
| 370 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 371 | VTable = Builder.CreateBitCast(VTable, Ty); |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 372 | DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 373 | uint64_t VTableIndex = |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 374 | CGM.getVTableContext().getMethodVTableIndex(GlobalDecl(DD, Type)); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 375 | uint64_t AddressPoint = |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 376 | CGM.getVTableContext().getVTableLayout(RD) |
| 377 | .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 378 | VTableIndex += AddressPoint; |
| 379 | llvm::Value *VFuncPtr = |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 380 | Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
| 381 | Callee = Builder.CreateLoad(VFuncPtr); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 382 | } |
| 383 | return Callee; |
| 384 | } |
| 385 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 386 | llvm::Value * |
| 387 | CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 388 | llvm::Value *This, llvm::Type *Ty) { |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 389 | DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 390 | uint64_t VTableIndex = |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 391 | CGM.getVTableContext().getMethodVTableIndex(GlobalDecl(DD, Type)); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 392 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 393 | return ::BuildVirtualCall(*this, VTableIndex, This, Ty); |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 394 | } |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 395 | |