Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 1 | //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// |
| 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 | |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 18 | #include "Mangle.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 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" |
Anders Carlsson | 6815e94 | 2009-09-27 18:58:34 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 25 | #include "clang/CodeGen/CodeGenOptions.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 30 | /// Determines whether the given function has a trivial body that does |
| 31 | /// not require any specific codegen. |
| 32 | static bool HasTrivialBody(const FunctionDecl *FD) { |
| 33 | Stmt *S = FD->getBody(); |
| 34 | if (!S) |
| 35 | return true; |
| 36 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 37 | return true; |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | /// Try to emit a base destructor as an alias to its primary |
| 42 | /// base-class destructor. |
| 43 | bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { |
| 44 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 45 | return true; |
| 46 | |
| 47 | // If the destructor doesn't have a trivial body, we have to emit it |
| 48 | // separately. |
| 49 | if (!HasTrivialBody(D)) |
| 50 | return true; |
| 51 | |
| 52 | const CXXRecordDecl *Class = D->getParent(); |
| 53 | |
| 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 | |
| 62 | // If any fields have a non-trivial destructor, we have to emit it |
| 63 | // separately. |
| 64 | for (CXXRecordDecl::field_iterator I = Class->field_begin(), |
| 65 | E = Class->field_end(); I != E; ++I) |
| 66 | if (const RecordType *RT = (*I)->getType()->getAs<RecordType>()) |
| 67 | if (!cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor()) |
| 68 | return true; |
| 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. |
| 99 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(getContext()); |
| 100 | if (!BaseD->isImplicit() && !BaseD->getBody()) |
| 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); |
| 105 | if (ClassLayout.getBaseClassOffset(UniqueBase) != 0) |
| 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. |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 168 | MangleBuffer MangledName; |
| 169 | getMangledName(MangledName, AliasDecl); |
| 170 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 171 | if (Entry) { |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 172 | assert(Entry->isDeclaration() && "definition already exists for alias"); |
| 173 | assert(Entry->getType() == AliasType && |
| 174 | "declaration exists with different type"); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 175 | Alias->takeName(Entry); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 176 | Entry->replaceAllUsesWith(Alias); |
| 177 | Entry->eraseFromParent(); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 178 | } else { |
| 179 | Alias->setName(MangledName.getString()); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 180 | } |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 181 | |
| 182 | // Finally, set up the alias with its proper name and attributes. |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 183 | SetCommonAttributes(AliasDecl.getDecl(), Alias); |
| 184 | |
| 185 | return false; |
| 186 | } |
Anders Carlsson | b9de2c5 | 2009-05-11 23:37:08 +0000 | [diff] [blame] | 187 | |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 188 | void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 189 | // The constructor used for constructing this as a complete class; |
| 190 | // constucts the virtual bases, then calls the base constructor. |
John McCall | 92ac9ff | 2010-02-17 03:52:49 +0000 | [diff] [blame] | 191 | EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 192 | |
| 193 | // The constructor used for constructing this as a base class; |
| 194 | // ignores virtual bases. |
John McCall | 8e51a1f | 2010-02-18 21:31:48 +0000 | [diff] [blame] | 195 | EmitGlobal(GlobalDecl(D, Ctor_Base)); |
Anders Carlsson | 95d4e5d | 2009-04-15 15:55:24 +0000 | [diff] [blame] | 196 | } |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 197 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 199 | CXXCtorType Type) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 200 | // The complete constructor is equivalent to the base constructor |
| 201 | // for classes with no virtual bases. Try to emit it as an alias. |
| 202 | if (Type == Ctor_Complete && |
| 203 | !D->getParent()->getNumVBases() && |
| 204 | !TryEmitDefinitionAsAlias(GlobalDecl(D, Ctor_Complete), |
| 205 | GlobalDecl(D, Ctor_Base))) |
| 206 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 208 | llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXConstructor(D, Type)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 210 | CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 212 | SetFunctionDefinitionAttributes(D, Fn); |
| 213 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 214 | } |
| 215 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 216 | llvm::GlobalValue * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 218 | CXXCtorType Type) { |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 219 | MangleBuffer Name; |
| 220 | getMangledCXXCtorName(Name, D, Type); |
| 221 | if (llvm::GlobalValue *V = GetGlobalValue(Name)) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 222 | return V; |
| 223 | |
Fariborz Jahanian | 30509a3 | 2009-11-06 18:47:57 +0000 | [diff] [blame] | 224 | const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>(); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 225 | const llvm::FunctionType *FTy = |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 226 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), |
Fariborz Jahanian | 30509a3 | 2009-11-06 18:47:57 +0000 | [diff] [blame] | 227 | FPT->isVariadic()); |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 228 | return cast<llvm::Function>( |
| 229 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 230 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 231 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 232 | void CodeGenModule::getMangledCXXCtorName(MangleBuffer &Name, |
| 233 | const CXXConstructorDecl *D, |
| 234 | CXXCtorType Type) { |
| 235 | getMangleContext().mangleCXXCtor(D, Type, Name.getBuffer()); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 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 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 255 | CXXDtorType Type) { |
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. |
| 258 | if (Type == Dtor_Complete && |
| 259 | !D->getParent()->getNumVBases() && |
| 260 | !TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Complete), |
| 261 | GlobalDecl(D, Dtor_Base))) |
| 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. |
| 268 | if (Type == Dtor_Base && !TryEmitBaseDestructorAsAlias(D)) |
| 269 | return; |
| 270 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 271 | llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXDestructor(D, Type)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 273 | CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 275 | SetFunctionDefinitionAttributes(D, Fn); |
| 276 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 277 | } |
| 278 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 279 | llvm::GlobalValue * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 281 | CXXDtorType Type) { |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 282 | MangleBuffer Name; |
| 283 | getMangledCXXDtorName(Name, D, Type); |
| 284 | if (llvm::GlobalValue *V = GetGlobalValue(Name)) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 285 | return V; |
| 286 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 287 | const llvm::FunctionType *FTy = |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 288 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Chris Lattner | b4880ba | 2009-05-12 21:21:08 +0000 | [diff] [blame] | 290 | return cast<llvm::Function>( |
| 291 | GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 292 | } |
| 293 | |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 294 | void CodeGenModule::getMangledCXXDtorName(MangleBuffer &Name, |
| 295 | const CXXDestructorDecl *D, |
| 296 | CXXDtorType Type) { |
| 297 | getMangleContext().mangleCXXDtor(D, Type, Name.getBuffer()); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 298 | } |
Fariborz Jahanian | e7d346b | 2009-07-20 23:18:55 +0000 | [diff] [blame] | 299 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 300 | llvm::Constant * |
Eli Friedman | 35c98cc | 2009-12-03 04:27:05 +0000 | [diff] [blame] | 301 | CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD, |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 302 | bool Extern, |
| 303 | const ThunkAdjustment &ThisAdjustment) { |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 304 | return GenerateCovariantThunk(Fn, GD, Extern, |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 305 | CovariantThunkAdjustment(ThisAdjustment, |
| 306 | ThunkAdjustment())); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 309 | llvm::Value * |
| 310 | CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, |
| 311 | const ThunkAdjustment &Adjustment) { |
| 312 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
| 313 | |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 314 | const llvm::Type *OrigTy = V->getType(); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 315 | if (Adjustment.NonVirtual) { |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 316 | // Do the non-virtual adjustment |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 317 | V = Builder.CreateBitCast(V, Int8PtrTy); |
| 318 | V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual); |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 319 | V = Builder.CreateBitCast(V, OrigTy); |
| 320 | } |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 321 | |
| 322 | if (!Adjustment.Virtual) |
| 323 | return V; |
| 324 | |
| 325 | assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 && |
| 326 | "vtable entry unaligned"); |
| 327 | |
| 328 | // Do the virtual this adjustment |
| 329 | const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); |
| 330 | const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo(); |
| 331 | |
| 332 | llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy); |
| 333 | V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo()); |
| 334 | V = Builder.CreateLoad(V, "vtable"); |
| 335 | |
| 336 | llvm::Value *VTablePtr = V; |
| 337 | uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8); |
| 338 | V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); |
| 339 | V = Builder.CreateLoad(V); |
| 340 | V = Builder.CreateGEP(ThisVal, V); |
| 341 | |
| 342 | return Builder.CreateBitCast(V, OrigTy); |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 345 | llvm::Constant * |
| 346 | CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn, |
Eli Friedman | 35c98cc | 2009-12-03 04:27:05 +0000 | [diff] [blame] | 347 | GlobalDecl GD, bool Extern, |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 348 | const CovariantThunkAdjustment &Adjustment) { |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 349 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 350 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 351 | QualType ResultType = FPT->getResultType(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 352 | |
| 353 | FunctionArgList Args; |
| 354 | ImplicitParamDecl *ThisDecl = |
| 355 | ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, |
| 356 | MD->getThisType(getContext())); |
| 357 | Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); |
| 358 | for (FunctionDecl::param_const_iterator i = MD->param_begin(), |
| 359 | e = MD->param_end(); |
| 360 | i != e; ++i) { |
| 361 | ParmVarDecl *D = *i; |
| 362 | Args.push_back(std::make_pair(D, D->getType())); |
| 363 | } |
| 364 | IdentifierInfo *II |
| 365 | = &CGM.getContext().Idents.get("__thunk_named_foo_"); |
| 366 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 367 | getContext().getTranslationUnitDecl(), |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 368 | SourceLocation(), II, ResultType, 0, |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 369 | Extern |
| 370 | ? FunctionDecl::Extern |
| 371 | : FunctionDecl::Static, |
| 372 | false, true); |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 373 | StartFunction(FD, ResultType, Fn, Args, SourceLocation()); |
| 374 | |
| 375 | // generate body |
Mike Stump | 736529e | 2009-11-03 02:12:59 +0000 | [diff] [blame] | 376 | const llvm::Type *Ty = |
| 377 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 378 | FPT->isVariadic()); |
Eli Friedman | 35c98cc | 2009-12-03 04:27:05 +0000 | [diff] [blame] | 379 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty); |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 380 | |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 381 | CallArgList CallArgs; |
| 382 | |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 383 | bool ShouldAdjustReturnPointer = true; |
Mike Stump | 736529e | 2009-11-03 02:12:59 +0000 | [diff] [blame] | 384 | QualType ArgType = MD->getThisType(getContext()); |
| 385 | llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this"); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 386 | if (!Adjustment.ThisAdjustment.isEmpty()) { |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 387 | // Do the this adjustment. |
Mike Stump | d0fe536 | 2009-11-04 00:53:51 +0000 | [diff] [blame] | 388 | const llvm::Type *OrigTy = Callee->getType(); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 389 | Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment); |
| 390 | |
| 391 | if (!Adjustment.ReturnAdjustment.isEmpty()) { |
| 392 | const CovariantThunkAdjustment &ReturnAdjustment = |
| 393 | CovariantThunkAdjustment(ThunkAdjustment(), |
| 394 | Adjustment.ReturnAdjustment); |
| 395 | |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 396 | Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 397 | |
Mike Stump | d0fe536 | 2009-11-04 00:53:51 +0000 | [diff] [blame] | 398 | Callee = Builder.CreateBitCast(Callee, OrigTy); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 399 | ShouldAdjustReturnPointer = false; |
Mike Stump | d0fe536 | 2009-11-04 00:53:51 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Mike Stump | 736529e | 2009-11-03 02:12:59 +0000 | [diff] [blame] | 403 | CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType)); |
| 404 | |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 405 | for (FunctionDecl::param_const_iterator i = MD->param_begin(), |
| 406 | e = MD->param_end(); |
| 407 | i != e; ++i) { |
| 408 | ParmVarDecl *D = *i; |
| 409 | QualType ArgType = D->getType(); |
| 410 | |
| 411 | // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst); |
Eli Friedman | 6804fa2 | 2009-12-03 04:49:52 +0000 | [diff] [blame] | 412 | Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(), |
| 413 | SourceLocation()); |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 414 | CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType)); |
| 415 | } |
| 416 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 417 | RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs, |
| 418 | FPT->getCallConv(), |
| 419 | FPT->getNoReturnAttr()), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 420 | Callee, ReturnValueSlot(), CallArgs, MD); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 421 | if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) { |
Mike Stump | 03e777e | 2009-11-05 06:32:02 +0000 | [diff] [blame] | 422 | bool CanBeZero = !(ResultType->isReferenceType() |
| 423 | // FIXME: attr nonnull can't be zero either |
| 424 | /* || ResultType->hasAttr<NonNullAttr>() */ ); |
Mike Stump | c902d22 | 2009-11-03 16:59:27 +0000 | [diff] [blame] | 425 | // Do the return result adjustment. |
Mike Stump | 03e777e | 2009-11-05 06:32:02 +0000 | [diff] [blame] | 426 | if (CanBeZero) { |
| 427 | llvm::BasicBlock *NonZeroBlock = createBasicBlock(); |
| 428 | llvm::BasicBlock *ZeroBlock = createBasicBlock(); |
| 429 | llvm::BasicBlock *ContBlock = createBasicBlock(); |
Mike Stump | 7c276b8 | 2009-11-05 06:12:26 +0000 | [diff] [blame] | 430 | |
Mike Stump | 03e777e | 2009-11-05 06:32:02 +0000 | [diff] [blame] | 431 | const llvm::Type *Ty = RV.getScalarVal()->getType(); |
| 432 | llvm::Value *Zero = llvm::Constant::getNullValue(Ty); |
| 433 | Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero), |
| 434 | NonZeroBlock, ZeroBlock); |
| 435 | EmitBlock(NonZeroBlock); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 436 | llvm::Value *NZ = |
| 437 | DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment); |
Mike Stump | 03e777e | 2009-11-05 06:32:02 +0000 | [diff] [blame] | 438 | EmitBranch(ContBlock); |
| 439 | EmitBlock(ZeroBlock); |
| 440 | llvm::Value *Z = RV.getScalarVal(); |
| 441 | EmitBlock(ContBlock); |
| 442 | llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty); |
| 443 | RVOrZero->reserveOperandSpace(2); |
| 444 | RVOrZero->addIncoming(NZ, NonZeroBlock); |
| 445 | RVOrZero->addIncoming(Z, ZeroBlock); |
| 446 | RV = RValue::get(RVOrZero); |
| 447 | } else |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 448 | RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), |
| 449 | Adjustment.ReturnAdjustment)); |
Mike Stump | c5dac4e | 2009-11-02 23:22:01 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Mike Stump | f49ed94 | 2009-11-02 23:47:45 +0000 | [diff] [blame] | 452 | if (!ResultType->isVoidType()) |
| 453 | EmitReturnOfRValue(RV, ResultType); |
| 454 | |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 455 | FinishFunction(); |
| 456 | return Fn; |
| 457 | } |
| 458 | |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 459 | llvm::Constant * |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 460 | CodeGenModule::GetAddrOfThunk(GlobalDecl GD, |
| 461 | const ThunkAdjustment &ThisAdjustment) { |
| 462 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 463 | |
| 464 | // Compute mangled name |
| 465 | llvm::SmallString<256> OutName; |
| 466 | if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) |
| 467 | getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), ThisAdjustment, |
| 468 | OutName); |
| 469 | else |
| 470 | getMangleContext().mangleThunk(MD, ThisAdjustment, OutName); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 471 | |
| 472 | // Get function for mangled name |
| 473 | const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 474 | return GetOrCreateLLVMFunction(OutName, Ty, GlobalDecl()); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | llvm::Constant * |
| 478 | CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD, |
| 479 | const CovariantThunkAdjustment &Adjustment) { |
| 480 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 481 | |
| 482 | // Compute mangled name |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 483 | llvm::SmallString<256> Name; |
| 484 | getMangleContext().mangleCovariantThunk(MD, Adjustment, Name); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 485 | |
| 486 | // Get function for mangled name |
| 487 | const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD); |
| 488 | return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl()); |
| 489 | } |
| 490 | |
| 491 | void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) { |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 492 | CGVtableInfo::AdjustmentVectorTy *AdjPtr = getVtableInfo().getAdjustments(GD); |
| 493 | if (!AdjPtr) |
| 494 | return; |
| 495 | CGVtableInfo::AdjustmentVectorTy &Adj = *AdjPtr; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 496 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 497 | for (unsigned i = 0; i < Adj.size(); i++) { |
| 498 | GlobalDecl OGD = Adj[i].first; |
| 499 | const CXXMethodDecl *OMD = cast<CXXMethodDecl>(OGD.getDecl()); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 500 | QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType(); |
| 501 | CanQualType oret = getContext().getCanonicalType(nc_oret); |
| 502 | QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType(); |
| 503 | CanQualType ret = getContext().getCanonicalType(nc_ret); |
| 504 | ThunkAdjustment ReturnAdjustment; |
| 505 | if (oret != ret) { |
| 506 | QualType qD = nc_ret->getPointeeType(); |
| 507 | QualType qB = nc_oret->getPointeeType(); |
| 508 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 509 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 510 | ReturnAdjustment = ComputeThunkAdjustment(D, B); |
| 511 | } |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 512 | ThunkAdjustment ThisAdjustment = Adj[i].second; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 513 | bool Extern = !cast<CXXRecordDecl>(OMD->getDeclContext())->isInAnonymousNamespace(); |
| 514 | if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) { |
| 515 | CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment); |
| 516 | llvm::Constant *FnConst; |
| 517 | if (!ReturnAdjustment.isEmpty()) |
| 518 | FnConst = GetAddrOfCovariantThunk(GD, CoAdj); |
| 519 | else |
| 520 | FnConst = GetAddrOfThunk(GD, ThisAdjustment); |
| 521 | if (!isa<llvm::Function>(FnConst)) { |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 522 | llvm::Constant *SubExpr = |
| 523 | cast<llvm::ConstantExpr>(FnConst)->getOperand(0); |
| 524 | llvm::Function *OldFn = cast<llvm::Function>(SubExpr); |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 525 | llvm::Constant *NewFnConst; |
| 526 | if (!ReturnAdjustment.isEmpty()) |
| 527 | NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj); |
| 528 | else |
| 529 | NewFnConst = GetAddrOfThunk(GD, ThisAdjustment); |
| 530 | llvm::Function *NewFn = cast<llvm::Function>(NewFnConst); |
| 531 | NewFn->takeName(OldFn); |
| 532 | llvm::Constant *NewPtrForOldDecl = |
| 533 | llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType()); |
| 534 | OldFn->replaceAllUsesWith(NewPtrForOldDecl); |
| 535 | OldFn->eraseFromParent(); |
| 536 | FnConst = NewFn; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 537 | } |
| 538 | llvm::Function *Fn = cast<llvm::Function>(FnConst); |
| 539 | if (Fn->isDeclaration()) { |
| 540 | llvm::GlobalVariable::LinkageTypes linktype; |
| 541 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 542 | if (!Extern) |
| 543 | linktype = llvm::GlobalValue::InternalLinkage; |
| 544 | Fn->setLinkage(linktype); |
| 545 | if (!Features.Exceptions && !Features.ObjCNonFragileABI) |
| 546 | Fn->addFnAttr(llvm::Attribute::NoUnwind); |
| 547 | Fn->setAlignment(2); |
| 548 | CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj); |
| 549 | } |
| 550 | } |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | llvm::Constant * |
Eli Friedman | 35c98cc | 2009-12-03 04:27:05 +0000 | [diff] [blame] | 555 | CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern, |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 556 | const ThunkAdjustment &ThisAdjustment) { |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 557 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 558 | llvm::SmallString<256> OutName; |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 559 | if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) { |
| 560 | getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment, |
| 561 | OutName); |
| 562 | } else |
| 563 | getMangleContext().mangleThunk(MD, ThisAdjustment, OutName); |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 564 | |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 565 | llvm::GlobalVariable::LinkageTypes linktype; |
| 566 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 567 | if (!Extern) |
| 568 | linktype = llvm::GlobalValue::InternalLinkage; |
| 569 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 570 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 571 | const llvm::FunctionType *FTy = |
| 572 | getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), |
| 573 | FPT->isVariadic()); |
| 574 | |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 575 | llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 576 | &getModule()); |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 577 | CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment); |
Mike Stump | ed032eb | 2009-09-04 18:27:16 +0000 | [diff] [blame] | 578 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 579 | return m; |
| 580 | } |
| 581 | |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 582 | llvm::Constant * |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 583 | CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern, |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 584 | const CovariantThunkAdjustment &Adjustment) { |
Mike Stump | 919d5e5 | 2009-12-03 03:47:56 +0000 | [diff] [blame] | 585 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 586 | llvm::SmallString<256> OutName; |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 587 | getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 588 | llvm::GlobalVariable::LinkageTypes linktype; |
| 589 | linktype = llvm::GlobalValue::WeakAnyLinkage; |
| 590 | if (!Extern) |
| 591 | linktype = llvm::GlobalValue::InternalLinkage; |
| 592 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 593 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 594 | const llvm::FunctionType *FTy = |
| 595 | getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), |
| 596 | FPT->isVariadic()); |
| 597 | |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 598 | llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 599 | &getModule()); |
Anders Carlsson | 7622cd3 | 2009-11-26 03:09:37 +0000 | [diff] [blame] | 600 | CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment); |
Mike Stump | 6e319f6 | 2009-09-11 23:25:56 +0000 | [diff] [blame] | 601 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 602 | return m; |
| 603 | } |
| 604 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 605 | static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex, |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 606 | llvm::Value *This, const llvm::Type *Ty) { |
| 607 | Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo(); |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 608 | |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 609 | llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty); |
| 610 | Vtable = CGF.Builder.CreateLoad(Vtable); |
| 611 | |
| 612 | llvm::Value *VFuncPtr = |
| 613 | CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn"); |
| 614 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 615 | } |
| 616 | |
| 617 | llvm::Value * |
| 618 | CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, |
| 619 | const llvm::Type *Ty) { |
| 620 | MD = MD->getCanonicalDecl(); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 621 | uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 622 | |
| 623 | return ::BuildVirtualCall(*this, VtableIndex, This, Ty); |
| 624 | } |
| 625 | |
| 626 | llvm::Value * |
| 627 | CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 628 | llvm::Value *&This, const llvm::Type *Ty) { |
| 629 | DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 630 | uint64_t VtableIndex = |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 631 | CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type)); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 632 | |
| 633 | return ::BuildVirtualCall(*this, VtableIndex, This, Ty); |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 634 | } |