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 | |
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" |
Anders Carlsson | 283a062 | 2009-04-13 18:03:33 +0000 | [diff] [blame] | 19 | #include "Mangle.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Fariborz Jahanian | 742cd1b | 2009-07-25 21:12:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | e1b29ef | 2008-08-22 16:00:37 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Anders Carlsson | 774e7c6 | 2009-04-03 22:50:24 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | 86e9644 | 2008-08-23 19:42:54 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.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 | |
| 63 | // If any fields have a non-trivial destructor, we have to emit it |
| 64 | // separately. |
| 65 | for (CXXRecordDecl::field_iterator I = Class->field_begin(), |
| 66 | E = Class->field_end(); I != E; ++I) |
| 67 | if (const RecordType *RT = (*I)->getType()->getAs<RecordType>()) |
| 68 | if (!cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor()) |
| 69 | return true; |
| 70 | |
| 71 | // Try to find a unique base class with a non-trivial destructor. |
| 72 | const CXXRecordDecl *UniqueBase = 0; |
| 73 | for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), |
| 74 | E = Class->bases_end(); I != E; ++I) { |
| 75 | |
| 76 | // We're in the base destructor, so skip virtual bases. |
| 77 | if (I->isVirtual()) continue; |
| 78 | |
| 79 | // Skip base classes with trivial destructors. |
| 80 | const CXXRecordDecl *Base |
| 81 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 82 | if (Base->hasTrivialDestructor()) continue; |
| 83 | |
| 84 | // If we've already found a base class with a non-trivial |
| 85 | // destructor, give up. |
| 86 | if (UniqueBase) return true; |
| 87 | UniqueBase = Base; |
| 88 | } |
| 89 | |
| 90 | // If we didn't find any bases with a non-trivial destructor, then |
| 91 | // the base destructor is actually effectively trivial, which can |
| 92 | // happen if it was needlessly user-defined or if there are virtual |
| 93 | // bases with non-trivial destructors. |
| 94 | if (!UniqueBase) |
| 95 | return true; |
| 96 | |
John McCall | 9a70846 | 2010-03-03 03:40:11 +0000 | [diff] [blame] | 97 | /// If we don't have a definition for the destructor yet, don't |
| 98 | /// emit. We can't emit aliases to declarations; that's just not |
| 99 | /// how aliases work. |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 100 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 101 | if (!BaseD->isImplicit() && !BaseD->hasBody()) |
John McCall | 9a70846 | 2010-03-03 03:40:11 +0000 | [diff] [blame] | 102 | return true; |
| 103 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 104 | // If the base is at a non-zero offset, give up. |
| 105 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); |
| 106 | if (ClassLayout.getBaseClassOffset(UniqueBase) != 0) |
| 107 | return true; |
| 108 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 109 | return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), |
| 110 | GlobalDecl(BaseD, Dtor_Base)); |
| 111 | } |
| 112 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 113 | /// Try to emit a definition as a global alias for another definition. |
| 114 | bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, |
| 115 | GlobalDecl TargetDecl) { |
| 116 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 117 | return true; |
| 118 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 119 | // The alias will use the linkage of the referrent. If we can't |
| 120 | // support aliases with that linkage, fail. |
| 121 | llvm::GlobalValue::LinkageTypes Linkage |
| 122 | = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl())); |
| 123 | |
| 124 | switch (Linkage) { |
| 125 | // We can definitely emit aliases to definitions with external linkage. |
| 126 | case llvm::GlobalValue::ExternalLinkage: |
| 127 | case llvm::GlobalValue::ExternalWeakLinkage: |
| 128 | break; |
| 129 | |
| 130 | // Same with local linkage. |
| 131 | case llvm::GlobalValue::InternalLinkage: |
| 132 | case llvm::GlobalValue::PrivateLinkage: |
| 133 | case llvm::GlobalValue::LinkerPrivateLinkage: |
| 134 | break; |
| 135 | |
| 136 | // We should try to support linkonce linkages. |
| 137 | case llvm::GlobalValue::LinkOnceAnyLinkage: |
| 138 | case llvm::GlobalValue::LinkOnceODRLinkage: |
| 139 | return true; |
| 140 | |
| 141 | // Other linkages will probably never be supported. |
| 142 | default: |
| 143 | return true; |
| 144 | } |
| 145 | |
Rafael Espindola | bc6afd1 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 146 | llvm::GlobalValue::LinkageTypes TargetLinkage |
| 147 | = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl())); |
| 148 | |
Rafael Espindola | 3c15745 | 2010-03-06 07:35:18 +0000 | [diff] [blame] | 149 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) |
Rafael Espindola | bc6afd1 | 2010-03-05 01:21:10 +0000 | [diff] [blame] | 150 | return true; |
| 151 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 152 | // Derive the type for the alias. |
| 153 | const llvm::PointerType *AliasType |
| 154 | = getTypes().GetFunctionType(AliasDecl)->getPointerTo(); |
| 155 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 156 | // Find the referrent. Some aliases might require a bitcast, in |
| 157 | // which case the caller is responsible for ensuring the soundness |
| 158 | // of these semantics. |
| 159 | llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
| 160 | llvm::Constant *Aliasee = Ref; |
| 161 | if (Ref->getType() != AliasType) |
| 162 | Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); |
| 163 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 164 | // Create the alias with no name. |
| 165 | llvm::GlobalAlias *Alias = |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 166 | new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule()); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 167 | |
John McCall | 1962bee | 2010-02-24 20:32:01 +0000 | [diff] [blame] | 168 | // Switch any previous uses to the alias. |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 169 | llvm::StringRef MangledName = getMangledName(AliasDecl); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 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 { |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 179 | Alias->setName(MangledName); |
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)); |
John McCall | 8b24233 | 2010-05-25 04:30:21 +0000 | [diff] [blame] | 209 | setFunctionLinkage(D, Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 211 | CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 213 | SetFunctionDefinitionAttributes(D, Fn); |
| 214 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 215 | } |
| 216 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 217 | llvm::GlobalValue * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 219 | CXXCtorType Type) { |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 220 | GlobalDecl GD(D, Type); |
| 221 | |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 222 | llvm::StringRef Name = getMangledName(GD); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 223 | if (llvm::GlobalValue *V = GetGlobalValue(Name)) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 224 | return V; |
| 225 | |
Fariborz Jahanian | 30509a3 | 2009-11-06 18:47:57 +0000 | [diff] [blame] | 226 | const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>(); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 227 | const llvm::FunctionType *FTy = |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 228 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), |
Fariborz Jahanian | 30509a3 | 2009-11-06 18:47:57 +0000 | [diff] [blame] | 229 | FPT->isVariadic()); |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 230 | return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, GD)); |
Anders Carlsson | 363c184 | 2009-04-16 23:57:24 +0000 | [diff] [blame] | 231 | } |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 232 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 233 | void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 234 | // The destructor in a virtual table is always a 'deleting' |
| 235 | // destructor, which calls the complete destructor and then uses the |
| 236 | // appropriate operator delete. |
Eli Friedman | ea9a208 | 2009-11-14 04:19:37 +0000 | [diff] [blame] | 237 | if (D->isVirtual()) |
Eli Friedman | 624c7d7 | 2009-12-15 02:06:15 +0000 | [diff] [blame] | 238 | EmitGlobal(GlobalDecl(D, Dtor_Deleting)); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 239 | |
| 240 | // The destructor used for destructing this as a most-derived class; |
| 241 | // call the base destructor and then destructs any virtual bases. |
John McCall | 8e51a1f | 2010-02-18 21:31:48 +0000 | [diff] [blame] | 242 | EmitGlobal(GlobalDecl(D, Dtor_Complete)); |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 243 | |
| 244 | // The destructor used for destructing this as a base class; ignores |
| 245 | // virtual bases. |
John McCall | 8e51a1f | 2010-02-18 21:31:48 +0000 | [diff] [blame] | 246 | EmitGlobal(GlobalDecl(D, Dtor_Base)); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 250 | CXXDtorType Type) { |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 251 | // The complete destructor is equivalent to the base destructor for |
| 252 | // classes with no virtual bases, so try to emit it as an alias. |
| 253 | if (Type == Dtor_Complete && |
| 254 | !D->getParent()->getNumVBases() && |
| 255 | !TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Complete), |
| 256 | GlobalDecl(D, Dtor_Base))) |
| 257 | return; |
| 258 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 259 | // The base destructor is equivalent to the base destructor of its |
| 260 | // base class if there is exactly one non-virtual base class with a |
| 261 | // non-trivial destructor, there are no fields with a non-trivial |
| 262 | // destructor, and the body of the destructor is trivial. |
| 263 | if (Type == Dtor_Base && !TryEmitBaseDestructorAsAlias(D)) |
| 264 | return; |
| 265 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 266 | llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXDestructor(D, Type)); |
John McCall | 8b24233 | 2010-05-25 04:30:21 +0000 | [diff] [blame] | 267 | setFunctionLinkage(D, Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 269 | CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 271 | SetFunctionDefinitionAttributes(D, Fn); |
| 272 | SetLLVMFunctionAttributesForDefinition(D, Fn); |
| 273 | } |
| 274 | |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 275 | llvm::GlobalValue * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 277 | CXXDtorType Type) { |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 278 | GlobalDecl GD(D, Type); |
| 279 | |
Anders Carlsson | 9a20d55 | 2010-06-22 16:16:50 +0000 | [diff] [blame] | 280 | llvm::StringRef Name = getMangledName(GD); |
John McCall | f746aa6 | 2010-03-19 23:29:14 +0000 | [diff] [blame] | 281 | if (llvm::GlobalValue *V = GetGlobalValue(Name)) |
John McCall | d46f985 | 2010-02-19 01:32:20 +0000 | [diff] [blame] | 282 | return V; |
| 283 | |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 284 | const llvm::FunctionType *FTy = |
Anders Carlsson | f6c56e2 | 2009-11-25 03:15:49 +0000 | [diff] [blame] | 285 | getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Anders Carlsson | dc709a8 | 2010-06-09 02:30:12 +0000 | [diff] [blame] | 287 | return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, GD)); |
Anders Carlsson | 27ae536 | 2009-04-17 01:58:57 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 290 | static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex, |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 291 | llvm::Value *This, const llvm::Type *Ty) { |
| 292 | Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo(); |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 293 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 294 | llvm::Value *VTable = CGF.Builder.CreateBitCast(This, Ty); |
| 295 | VTable = CGF.Builder.CreateLoad(VTable); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 296 | |
| 297 | llvm::Value *VFuncPtr = |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 298 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 299 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 300 | } |
| 301 | |
| 302 | llvm::Value * |
| 303 | CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, |
| 304 | const llvm::Type *Ty) { |
| 305 | MD = MD->getCanonicalDecl(); |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 306 | uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 307 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 308 | return ::BuildVirtualCall(*this, VTableIndex, This, Ty); |
Anders Carlsson | 566abee | 2009-11-13 04:45:41 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | llvm::Value * |
| 312 | CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, |
| 313 | llvm::Value *&This, const llvm::Type *Ty) { |
| 314 | DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 315 | uint64_t VTableIndex = |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 316 | CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type)); |
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); |
Mike Stump | f0070db | 2009-08-26 20:46:33 +0000 | [diff] [blame] | 319 | } |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 320 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 321 | CGCXXABI::~CGCXXABI() {} |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 322 | |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 323 | static void ErrorUnsupportedABI(CodeGenFunction &CGF, |
| 324 | llvm::StringRef S) { |
| 325 | Diagnostic &Diags = CGF.CGM.getDiags(); |
| 326 | unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, |
| 327 | "cannot yet compile %s in this ABI"); |
| 328 | Diags.Report(CGF.getContext().getFullLoc(CGF.CurCodeDecl->getLocation()), |
| 329 | DiagID) |
| 330 | << S; |
| 331 | } |
| 332 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 333 | llvm::Value *CGCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 334 | llvm::Value *&This, |
| 335 | llvm::Value *MemPtr, |
| 336 | const MemberPointerType *MPT) { |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 337 | ErrorUnsupportedABI(CGF, "calls through member pointers"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 338 | |
| 339 | const FunctionProtoType *FPT = |
| 340 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 341 | const CXXRecordDecl *RD = |
| 342 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 343 | const llvm::FunctionType *FTy = |
| 344 | CGF.CGM.getTypes().GetFunctionType( |
| 345 | CGF.CGM.getTypes().getFunctionInfo(RD, FPT), |
| 346 | FPT->isVariadic()); |
| 347 | return llvm::Constant::getNullValue(FTy->getPointerTo()); |
| 348 | } |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 349 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 350 | void CGCXXABI::EmitMemberFunctionPointerConversion(CodeGenFunction &CGF, |
| 351 | const CastExpr *E, |
| 352 | llvm::Value *Src, |
| 353 | llvm::Value *Dest, |
| 354 | bool VolatileDest) { |
| 355 | ErrorUnsupportedABI(CGF, "member function pointer conversions"); |
| 356 | } |
| 357 | |
| 358 | void CGCXXABI::EmitNullMemberFunctionPointer(CodeGenFunction &CGF, |
| 359 | const MemberPointerType *MPT, |
| 360 | llvm::Value *Dest, |
| 361 | bool VolatileDest) { |
| 362 | ErrorUnsupportedABI(CGF, "null member function pointers"); |
| 363 | } |
| 364 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 365 | void CGCXXABI::EmitMemberFunctionPointer(CodeGenFunction &CGF, |
| 366 | const CXXMethodDecl *MD, |
| 367 | llvm::Value *DestPtr, |
| 368 | bool VolatileDest) { |
| 369 | ErrorUnsupportedABI(CGF, "member function pointers"); |
| 370 | } |
| 371 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame^] | 372 | llvm::Value * |
| 373 | CGCXXABI::EmitMemberFunctionPointerComparison(CodeGenFunction &CGF, |
| 374 | llvm::Value *L, |
| 375 | llvm::Value *R, |
| 376 | const MemberPointerType *MPT, |
| 377 | bool Inequality) { |
| 378 | ErrorUnsupportedABI(CGF, "member function pointer comparison"); |
| 379 | return CGF.Builder.getFalse(); |
| 380 | } |
| 381 | |
| 382 | llvm::Value * |
| 383 | CGCXXABI::EmitMemberFunctionPointerIsNotNull(CodeGenFunction &CGF, |
| 384 | llvm::Value *MemPtr, |
| 385 | const MemberPointerType *MPT) { |
| 386 | ErrorUnsupportedABI(CGF, "member function pointer null testing"); |
| 387 | return CGF.Builder.getFalse(); |
| 388 | } |
| 389 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 390 | llvm::Constant * |
| 391 | CGCXXABI::EmitMemberFunctionPointerConversion(llvm::Constant *C, |
| 392 | const CastExpr *E) { |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | llvm::Constant * |
| 397 | CGCXXABI::EmitNullMemberFunctionPointer(const MemberPointerType *MPT) { |
| 398 | return 0; |
| 399 | } |
| 400 | |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 401 | llvm::Constant *CGCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) { |
| 402 | return 0; |
| 403 | } |
| 404 | |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 405 | bool CGCXXABI::RequiresNonZeroInitializer(QualType T) { |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | bool CGCXXABI::RequiresNonZeroInitializer(const CXXRecordDecl *D) { |
| 410 | return RequiresNonZeroInitializer(QualType(D->getTypeForDecl(), 0)); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 411 | } |