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 | /// Determines whether the given function has a trivial body that does |
| 32 | /// not require any specific codegen. |
| 33 | static bool HasTrivialBody(const FunctionDecl *FD) { |
| 34 | Stmt *S = FD->getBody(); |
| 35 | if (!S) |
| 36 | return true; |
| 37 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 38 | return true; |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | /// Try to emit a base destructor as an alias to its primary |
| 43 | /// base-class destructor. |
| 44 | bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { |
| 45 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 46 | return true; |
| 47 | |
| 48 | // If the destructor doesn't have a trivial body, we have to emit it |
| 49 | // separately. |
| 50 | if (!HasTrivialBody(D)) |
| 51 | return true; |
| 52 | |
| 53 | const CXXRecordDecl *Class = D->getParent(); |
| 54 | |
| 55 | // If we need to manipulate a VTT parameter, give up. |
| 56 | if (Class->getNumVBases()) { |
| 57 | // Extra Credit: passing extra parameters is perfectly safe |
| 58 | // in many calling conventions, so only bail out if the ctor's |
| 59 | // calling convention is nonstandard. |
| 60 | return true; |
| 61 | } |
| 62 | |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 63 | // If any field has a non-trivial destructor, we have to emit the |
| 64 | // destructor separately. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 65 | for (CXXRecordDecl::field_iterator I = Class->field_begin(), |
| 66 | E = Class->field_end(); I != E; ++I) |
John McCall | 0d70d71 | 2011-02-13 00:46:43 +0000 | [diff] [blame] | 67 | if ((*I)->getType().isDestructedType()) |
| 68 | return true; |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 69 | |
| 70 | // Try to find a unique base class with a non-trivial destructor. |
| 71 | const CXXRecordDecl *UniqueBase = 0; |
| 72 | for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), |
| 73 | E = Class->bases_end(); I != E; ++I) { |
| 74 | |
| 75 | // We're in the base destructor, so skip virtual bases. |
| 76 | if (I->isVirtual()) continue; |
| 77 | |
| 78 | // Skip base classes with trivial destructors. |
| 79 | const CXXRecordDecl *Base |
| 80 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 81 | if (Base->hasTrivialDestructor()) continue; |
| 82 | |
| 83 | // If we've already found a base class with a non-trivial |
| 84 | // destructor, give up. |
| 85 | if (UniqueBase) return true; |
| 86 | UniqueBase = Base; |
| 87 | } |
| 88 | |
| 89 | // If we didn't find any bases with a non-trivial destructor, then |
| 90 | // the base destructor is actually effectively trivial, which can |
| 91 | // happen if it was needlessly user-defined or if there are virtual |
| 92 | // bases with non-trivial destructors. |
| 93 | if (!UniqueBase) |
| 94 | return true; |
| 95 | |
John McCall | 9a70846 | 2010-03-03 03:40:11 +0000 | [diff] [blame] | 96 | /// If we don't have a definition for the destructor yet, don't |
| 97 | /// emit. We can't emit aliases to declarations; that's just not |
| 98 | /// how aliases work. |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 99 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 100 | if (!BaseD->isImplicit() && !BaseD->hasBody()) |
John McCall | 9a70846 | 2010-03-03 03:40:11 +0000 | [diff] [blame] | 101 | return true; |
| 102 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 103 | // If the base is at a non-zero offset, give up. |
| 104 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 105 | if (ClassLayout.getBaseClassOffsetInBits(UniqueBase) != 0) |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 106 | return true; |
| 107 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 108 | return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), |
| 109 | GlobalDecl(BaseD, Dtor_Base)); |
| 110 | } |
| 111 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 112 | /// Try to emit a definition as a global alias for another definition. |
| 113 | bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, |
| 114 | GlobalDecl TargetDecl) { |
| 115 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 116 | return true; |
| 117 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 118 | // The alias will use the linkage of the referrent. If we can't |
| 119 | // support aliases with that linkage, fail. |
| 120 | llvm::GlobalValue::LinkageTypes Linkage |
| 121 | = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl())); |
| 122 | |
| 123 | switch (Linkage) { |
| 124 | // We can definitely emit aliases to definitions with external linkage. |
| 125 | case llvm::GlobalValue::ExternalLinkage: |
| 126 | case llvm::GlobalValue::ExternalWeakLinkage: |
| 127 | break; |
| 128 | |
| 129 | // Same with local linkage. |
| 130 | case llvm::GlobalValue::InternalLinkage: |
| 131 | case llvm::GlobalValue::PrivateLinkage: |
| 132 | case llvm::GlobalValue::LinkerPrivateLinkage: |
| 133 | break; |
| 134 | |
| 135 | // We should try to support linkonce linkages. |
| 136 | case llvm::GlobalValue::LinkOnceAnyLinkage: |
| 137 | case llvm::GlobalValue::LinkOnceODRLinkage: |
| 138 | return true; |
| 139 | |
| 140 | // Other linkages will probably never be supported. |
| 141 | default: |
| 142 | return true; |
| 143 | } |
| 144 | |
Rafael Espindola | bc6afd1 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 145 | llvm::GlobalValue::LinkageTypes TargetLinkage |
| 146 | = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl())); |
| 147 | |
Rafael Espindola | 3c15745 | 2010-03-06 07:35:18 +0000 | [diff] [blame] | 148 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) |
Rafael Espindola | bc6afd1 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 149 | return true; |
| 150 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 151 | // Derive the type for the alias. |
| 152 | const llvm::PointerType *AliasType |
| 153 | = getTypes().GetFunctionType(AliasDecl)->getPointerTo(); |
| 154 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 155 | // Find the referrent. Some aliases might require a bitcast, in |
| 156 | // which case the caller is responsible for ensuring the soundness |
| 157 | // of these semantics. |
| 158 | llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
| 159 | llvm::Constant *Aliasee = Ref; |
| 160 | if (Ref->getType() != AliasType) |
| 161 | Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); |
| 162 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 163 | // Create the alias with no name. |
| 164 | llvm::GlobalAlias *Alias = |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 165 | new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule()); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 166 | |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 167 | // Switch any previous uses to the alias. |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 168 | llvm::StringRef MangledName = getMangledName(AliasDecl); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 169 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 170 | if (Entry) { |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 171 | assert(Entry->isDeclaration() && "definition already exists for alias"); |
| 172 | assert(Entry->getType() == AliasType && |
| 173 | "declaration exists with different type"); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 174 | Alias->takeName(Entry); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 175 | Entry->replaceAllUsesWith(Alias); |
| 176 | Entry->eraseFromParent(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 177 | } else { |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 178 | Alias->setName(MangledName); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 179 | } |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 180 | |
| 181 | // Finally, set up the alias with its proper name and attributes. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 182 | SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 183 | |
| 184 | return false; |
| 185 | } |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 186 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 187 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 188 | // The constructor used for constructing this as a complete class; |
| 189 | // constucts the virtual bases, then calls the base constructor. |
John McCall | 92ac9ff | 2010-02-17 03:52:49 +0000 | [diff] [blame] | 190 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 191 | |
| 192 | // The constructor used for constructing this as a base class; |
| 193 | // ignores virtual bases. |
John McCall | 8e51a1f | 2010-02-18 21:31:48 +0000 | [diff] [blame] | 194 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 195 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 196 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 197 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor, |
| 198 | CXXCtorType ctorType) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 199 | // The complete constructor is equivalent to the base constructor |
| 200 | // 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] | 201 | if (ctorType == Ctor_Complete && |
| 202 | !ctor->getParent()->getNumVBases() && |
| 203 | !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete), |
| 204 | GlobalDecl(ctor, Ctor_Base))) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 205 | return; |
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 | const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(ctor, ctorType); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 208 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 209 | llvm::Function *fn = |
| 210 | cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo)); |
| 211 | setFunctionLinkage(ctor, fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 213 | CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 215 | SetFunctionDefinitionAttributes(ctor, fn); |
| 216 | SetLLVMFunctionAttributesForDefinition(ctor, fn); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 217 | } |
| 218 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 219 | llvm::GlobalValue * |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 220 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, |
| 221 | CXXCtorType ctorType, |
| 222 | const CGFunctionInfo *fnInfo) { |
| 223 | GlobalDecl GD(ctor, ctorType); |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 224 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 225 | llvm::StringRef name = getMangledName(GD); |
| 226 | if (llvm::GlobalValue *existing = GetGlobalValue(name)) |
| 227 | return existing; |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 228 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 229 | if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(ctor, ctorType); |
| 230 | |
| 231 | const FunctionProtoType *proto = ctor->getType()->castAs<FunctionProtoType>(); |
| 232 | const llvm::FunctionType *fnType = |
| 233 | getTypes().GetFunctionType(*fnInfo, proto->isVariadic()); |
| 234 | return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 235 | /*ForVTable=*/false)); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 236 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 237 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 238 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 239 | // The destructor in a virtual table is always a 'deleting' |
| 240 | // destructor, which calls the complete destructor and then uses the |
| 241 | // appropriate operator delete. |
Eli Friedman | ea9a208 | 2009-11-14 04:19:37 +0000 | [diff] [blame] | 242 | if (D->isVirtual()) |
Eli Friedman | 624c7d7 | 2009-12-15 02:06:15 +0000 | [diff] [blame] | 243 | EmitGlobal(GlobalDecl(D, Dtor_Deleting)); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 244 | |
| 245 | // The destructor used for destructing this as a most-derived class; |
| 246 | // call the base destructor and then destructs any virtual bases. |
John McCall | 8e51a1f | 2010-02-18 21:31:48 +0000 | [diff] [blame] | 247 | EmitGlobal(GlobalDecl(D, Dtor_Complete)); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 248 | |
| 249 | // The destructor used for destructing this as a base class; ignores |
| 250 | // virtual bases. |
John McCall | 8e51a1f | 2010-02-18 21:31:48 +0000 | [diff] [blame] | 251 | EmitGlobal(GlobalDecl(D, Dtor_Base)); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 252 | } |
| 253 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 254 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor, |
| 255 | CXXDtorType dtorType) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 256 | // The complete destructor is equivalent to the base destructor for |
| 257 | // 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] | 258 | if (dtorType == Dtor_Complete && |
| 259 | !dtor->getParent()->getNumVBases() && |
| 260 | !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete), |
| 261 | GlobalDecl(dtor, Dtor_Base))) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 262 | return; |
| 263 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 264 | // The base destructor is equivalent to the base destructor of its |
| 265 | // base class if there is exactly one non-virtual base class with a |
| 266 | // non-trivial destructor, there are no fields with a non-trivial |
| 267 | // destructor, and the body of the destructor is trivial. |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 268 | if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor)) |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 269 | return; |
| 270 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 271 | const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(dtor, dtorType); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 272 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 273 | llvm::Function *fn = |
| 274 | cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo)); |
| 275 | setFunctionLinkage(dtor, fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 277 | CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 279 | SetFunctionDefinitionAttributes(dtor, fn); |
| 280 | SetLLVMFunctionAttributesForDefinition(dtor, fn); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 281 | } |
| 282 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 283 | llvm::GlobalValue * |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 284 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor, |
| 285 | CXXDtorType dtorType, |
| 286 | const CGFunctionInfo *fnInfo) { |
| 287 | GlobalDecl GD(dtor, dtorType); |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 288 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 289 | llvm::StringRef name = getMangledName(GD); |
| 290 | if (llvm::GlobalValue *existing = GetGlobalValue(name)) |
| 291 | return existing; |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 292 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 293 | if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(dtor, dtorType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
John McCall | 1f6f961 | 2011-03-09 08:12:35 +0000 | [diff] [blame] | 295 | const llvm::FunctionType *fnType = |
| 296 | getTypes().GetFunctionType(*fnInfo, false); |
| 297 | |
| 298 | return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, |
Anders Carlsson | 1faa89f | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 299 | /*ForVTable=*/false)); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 302 | static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex, |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 303 | llvm::Value *This, const llvm::Type *Ty) { |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 304 | Ty = Ty->getPointerTo()->getPointerTo(); |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 305 | |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 306 | llvm::Value *VTable = CGF.GetVTablePtr(This, Ty); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 307 | llvm::Value *VFuncPtr = |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 308 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 309 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 310 | } |
| 311 | |
| 312 | llvm::Value * |
| 313 | CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, |
| 314 | const llvm::Type *Ty) { |
| 315 | MD = MD->getCanonicalDecl(); |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 316 | uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 317 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 318 | return ::BuildVirtualCall(*this, VTableIndex, This, Ty); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 321 | /// BuildVirtualCall - This routine is to support gcc's kext ABI making |
| 322 | /// indirect call to virtual functions. It makes the call through indexing |
| 323 | /// into the vtable. |
| 324 | llvm::Value * |
| 325 | CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 326 | NestedNameSpecifier *Qual, |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 327 | const llvm::Type *Ty) { |
| 328 | llvm::Value *VTable = 0; |
| 329 | assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && |
| 330 | "BuildAppleKextVirtualCall - bad Qual kind"); |
| 331 | |
| 332 | const Type *QTy = Qual->getAsType(); |
| 333 | QualType T = QualType(QTy, 0); |
| 334 | const RecordType *RT = T->getAs<RecordType>(); |
| 335 | assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); |
| 336 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 337 | |
| 338 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) |
| 339 | return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); |
| 340 | |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 341 | VTable = CGM.getVTables().GetAddrOfVTable(RD); |
| 342 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 343 | VTable = Builder.CreateBitCast(VTable, Ty); |
| 344 | assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); |
| 345 | MD = MD->getCanonicalDecl(); |
| 346 | uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD); |
Fariborz Jahanian | a50e33e | 2011-01-28 23:42:29 +0000 | [diff] [blame] | 347 | uint64_t AddressPoint = |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame^] | 348 | CGM.getVTables().getAddressPoint(BaseSubobject(RD, CharUnits::Zero()), RD); |
Fariborz Jahanian | a50e33e | 2011-01-28 23:42:29 +0000 | [diff] [blame] | 349 | VTableIndex += AddressPoint; |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 350 | llvm::Value *VFuncPtr = |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 351 | Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
| 352 | return Builder.CreateLoad(VFuncPtr); |
Fariborz Jahanian | 2726267 | 2011-01-20 17:19:02 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 355 | /// BuildVirtualCall - This routine makes indirect vtable call for |
| 356 | /// call to virtual destructors. It returns 0 if it could not do it. |
| 357 | llvm::Value * |
| 358 | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
| 359 | const CXXDestructorDecl *DD, |
| 360 | CXXDtorType Type, |
| 361 | const CXXRecordDecl *RD) { |
| 362 | llvm::Value * Callee = 0; |
| 363 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD); |
| 364 | // FIXME. Dtor_Base dtor is always direct!! |
| 365 | // It need be somehow inline expanded into the caller. |
| 366 | // -O does that. But need to support -O0 as well. |
| 367 | if (MD->isVirtual() && Type != Dtor_Base) { |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 368 | // Compute the function type we're calling. |
| 369 | const CGFunctionInfo *FInfo = |
| 370 | &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD), |
| 371 | Dtor_Complete); |
| 372 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 373 | const llvm::Type *Ty |
| 374 | = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic()); |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 375 | |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 376 | llvm::Value *VTable = CGM.getVTables().GetAddrOfVTable(RD); |
| 377 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 378 | VTable = Builder.CreateBitCast(VTable, Ty); |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 379 | DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 380 | uint64_t VTableIndex = |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 381 | CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type)); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 382 | uint64_t AddressPoint = |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame^] | 383 | CGM.getVTables().getAddressPoint(BaseSubobject(RD, CharUnits::Zero()), RD); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 384 | VTableIndex += AddressPoint; |
| 385 | llvm::Value *VFuncPtr = |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 386 | Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); |
| 387 | Callee = Builder.CreateLoad(VFuncPtr); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 388 | } |
| 389 | return Callee; |
| 390 | } |
| 391 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 392 | llvm::Value * |
| 393 | CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, |
Anders Carlsson | 83eedd9 | 2010-11-28 17:53:32 +0000 | [diff] [blame] | 394 | llvm::Value *This, const llvm::Type *Ty) { |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 395 | DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 396 | uint64_t VTableIndex = |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 397 | CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type)); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 398 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 399 | return ::BuildVirtualCall(*this, VTableIndex, This, Ty); |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 400 | } |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 401 | |