Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 1 | //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===// |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +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 of virtual tables. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 14 | #include "CodeGenFunction.h" |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 19 | #include "clang/CodeGen/CGFunctionInfo.h" |
John McCall | 5513fce9 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseSet.h" |
Anders Carlsson | d46ed89 | 2010-02-27 16:18:19 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | 94eab4a | 2010-02-13 10:38:52 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | 5d40c6f | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Format.h" |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Cloning.h" |
Anders Carlsson | 5644614 | 2010-03-17 20:06:32 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Zhongxing Xu | 1721ef7 | 2009-11-13 05:46:16 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
| 31 | |
Peter Collingbourne | a834166 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 32 | CodeGenVTables::CodeGenVTables(CodeGenModule &CGM) |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 33 | : CGM(CGM), ItaniumVTContext(CGM.getContext()) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 34 | if (CGM.getTarget().getCXXABI().isMicrosoft()) { |
| 35 | // FIXME: Eventually, we should only have one of V*TContexts available. |
| 36 | // Today we use both in the Microsoft ABI as MicrosoftVFTableContext |
| 37 | // is not completely supported in CodeGen yet. |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 38 | MicrosoftVTContext.reset(new MicrosoftVTableContext(CGM.getContext())); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
Peter Collingbourne | a834166 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 41 | |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 42 | llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, |
Anders Carlsson | fe8a993 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 43 | const ThunkInfo &Thunk) { |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 44 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 45 | |
| 46 | // Compute the mangled name. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 47 | SmallString<256> Name; |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 48 | llvm::raw_svector_ostream Out(Name); |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 49 | if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 50 | getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 51 | Thunk.This, Out); |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 52 | else |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 53 | getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out); |
| 54 | Out.flush(); |
| 55 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 56 | llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD); |
Rafael Espindola | 94abb8f | 2013-12-09 04:29:47 +0000 | [diff] [blame] | 57 | return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true, |
| 58 | /*DontDefer*/ true); |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 59 | } |
| 60 | |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 61 | static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, |
| 62 | const ThunkInfo &Thunk, llvm::Function *Fn) { |
Anders Carlsson | c6a4789 | 2011-01-29 19:39:23 +0000 | [diff] [blame] | 63 | CGM.setGlobalVisibility(Fn, MD); |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 64 | |
John McCall | b3732bb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 65 | if (!CGM.getCodeGenOpts().HiddenWeakVTables) |
| 66 | return; |
| 67 | |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 68 | // If the thunk has weak/linkonce linkage, but the function must be |
| 69 | // emitted in every translation unit that references it, then we can |
| 70 | // emit its thunks with hidden visibility, since its thunks must be |
| 71 | // emitted when the function is. |
| 72 | |
John McCall | 5513fce9 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 73 | // This follows CodeGenModule::setTypeVisibility; see the comments |
| 74 | // there for explanation. |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 75 | |
| 76 | if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage && |
| 77 | Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) || |
| 78 | Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility) |
| 79 | return; |
| 80 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 81 | if (MD->getExplicitVisibility(ValueDecl::VisibilityForValue)) |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 82 | return; |
| 83 | |
| 84 | switch (MD->getTemplateSpecializationKind()) { |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 85 | case TSK_ExplicitInstantiationDefinition: |
| 86 | case TSK_ExplicitInstantiationDeclaration: |
| 87 | return; |
| 88 | |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 89 | case TSK_Undeclared: |
| 90 | break; |
| 91 | |
John McCall | 5513fce9 | 2010-08-05 20:39:18 +0000 | [diff] [blame] | 92 | case TSK_ExplicitSpecialization: |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 93 | case TSK_ImplicitInstantiation: |
Douglas Gregor | 47c0896 | 2012-10-24 14:11:55 +0000 | [diff] [blame] | 94 | return; |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | |
| 98 | // If there's an explicit definition, and that definition is |
| 99 | // out-of-line, then we can't assume that all users will have a |
| 100 | // definition to emit. |
| 101 | const FunctionDecl *Def = 0; |
| 102 | if (MD->hasBody(Def) && Def->isOutOfLine()) |
| 103 | return; |
| 104 | |
| 105 | Fn->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 106 | } |
| 107 | |
John McCall | 5fe0096 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 108 | #ifndef NDEBUG |
| 109 | static bool similar(const ABIArgInfo &infoL, CanQualType typeL, |
| 110 | const ABIArgInfo &infoR, CanQualType typeR) { |
| 111 | return (infoL.getKind() == infoR.getKind() && |
| 112 | (typeL == typeR || |
| 113 | (isa<PointerType>(typeL) && isa<PointerType>(typeR)) || |
| 114 | (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR)))); |
| 115 | } |
| 116 | #endif |
| 117 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 118 | static RValue PerformReturnAdjustment(CodeGenFunction &CGF, |
| 119 | QualType ResultType, RValue RV, |
| 120 | const ThunkInfo &Thunk) { |
| 121 | // Emit the return adjustment. |
| 122 | bool NullCheckValue = !ResultType->isReferenceType(); |
| 123 | |
| 124 | llvm::BasicBlock *AdjustNull = 0; |
| 125 | llvm::BasicBlock *AdjustNotNull = 0; |
| 126 | llvm::BasicBlock *AdjustEnd = 0; |
| 127 | |
| 128 | llvm::Value *ReturnValue = RV.getScalarVal(); |
| 129 | |
| 130 | if (NullCheckValue) { |
| 131 | AdjustNull = CGF.createBasicBlock("adjust.null"); |
| 132 | AdjustNotNull = CGF.createBasicBlock("adjust.notnull"); |
| 133 | AdjustEnd = CGF.createBasicBlock("adjust.end"); |
| 134 | |
| 135 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue); |
| 136 | CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull); |
| 137 | CGF.EmitBlock(AdjustNotNull); |
| 138 | } |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 139 | |
| 140 | ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF, ReturnValue, |
| 141 | Thunk.Return); |
| 142 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 143 | if (NullCheckValue) { |
| 144 | CGF.Builder.CreateBr(AdjustEnd); |
| 145 | CGF.EmitBlock(AdjustNull); |
| 146 | CGF.Builder.CreateBr(AdjustEnd); |
| 147 | CGF.EmitBlock(AdjustEnd); |
| 148 | |
| 149 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2); |
| 150 | PHI->addIncoming(ReturnValue, AdjustNotNull); |
| 151 | PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), |
| 152 | AdjustNull); |
| 153 | ReturnValue = PHI; |
| 154 | } |
| 155 | |
| 156 | return RValue::get(ReturnValue); |
| 157 | } |
| 158 | |
| 159 | // This function does roughly the same thing as GenerateThunk, but in a |
| 160 | // very different way, so that va_start and va_end work correctly. |
| 161 | // FIXME: This function assumes "this" is the first non-sret LLVM argument of |
| 162 | // a function, and that there is an alloca built in the entry block |
| 163 | // for all accesses to "this". |
| 164 | // FIXME: This function assumes there is only one "ret" statement per function. |
| 165 | // FIXME: Cloning isn't correct in the presence of indirect goto! |
| 166 | // FIXME: This implementation of thunks bloats codesize by duplicating the |
| 167 | // function definition. There are alternatives: |
| 168 | // 1. Add some sort of stub support to LLVM for cases where we can |
| 169 | // do a this adjustment, then a sibcall. |
| 170 | // 2. We could transform the definition to take a va_list instead of an |
| 171 | // actual variable argument list, then have the thunks (including a |
| 172 | // no-op thunk for the regular definition) call va_start/va_end. |
| 173 | // There's a bit of per-call overhead for this solution, but it's |
| 174 | // better for codesize if the definition is long. |
| 175 | void CodeGenFunction::GenerateVarArgsThunk( |
| 176 | llvm::Function *Fn, |
| 177 | const CGFunctionInfo &FnInfo, |
| 178 | GlobalDecl GD, const ThunkInfo &Thunk) { |
| 179 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 180 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 181 | QualType ResultType = FPT->getResultType(); |
| 182 | |
| 183 | // Get the original function |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 184 | assert(FnInfo.isVariadic()); |
| 185 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 186 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
| 187 | llvm::Function *BaseFn = cast<llvm::Function>(Callee); |
| 188 | |
| 189 | // Clone to thunk. |
Benjamin Kramer | 6ca4210 | 2012-09-19 13:13:52 +0000 | [diff] [blame] | 190 | llvm::ValueToValueMapTy VMap; |
| 191 | llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap, |
| 192 | /*ModuleLevelChanges=*/false); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 193 | CGM.getModule().getFunctionList().push_back(NewFn); |
| 194 | Fn->replaceAllUsesWith(NewFn); |
| 195 | NewFn->takeName(Fn); |
| 196 | Fn->eraseFromParent(); |
| 197 | Fn = NewFn; |
| 198 | |
| 199 | // "Initialize" CGF (minimally). |
| 200 | CurFn = Fn; |
| 201 | |
| 202 | // Get the "this" value |
| 203 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 204 | if (CGM.ReturnTypeUsesSRet(FnInfo)) |
| 205 | ++AI; |
| 206 | |
| 207 | // Find the first store of "this", which will be to the alloca associated |
| 208 | // with "this". |
| 209 | llvm::Value *ThisPtr = &*AI; |
| 210 | llvm::BasicBlock *EntryBB = Fn->begin(); |
| 211 | llvm::Instruction *ThisStore = 0; |
| 212 | for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end(); |
| 213 | I != E; I++) { |
| 214 | if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) { |
| 215 | ThisStore = cast<llvm::StoreInst>(I); |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | assert(ThisStore && "Store of this should be in entry block?"); |
| 220 | // Adjust "this", if necessary. |
| 221 | Builder.SetInsertPoint(ThisStore); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 222 | llvm::Value *AdjustedThisPtr = |
| 223 | CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 224 | ThisStore->setOperand(0, AdjustedThisPtr); |
| 225 | |
| 226 | if (!Thunk.Return.isEmpty()) { |
| 227 | // Fix up the returned value, if necessary. |
| 228 | for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) { |
| 229 | llvm::Instruction *T = I->getTerminator(); |
| 230 | if (isa<llvm::ReturnInst>(T)) { |
| 231 | RValue RV = RValue::get(T->getOperand(0)); |
| 232 | T->eraseFromParent(); |
| 233 | Builder.SetInsertPoint(&*I); |
| 234 | RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk); |
| 235 | Builder.CreateRet(RV.getScalarVal()); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 242 | void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD, |
| 243 | const CGFunctionInfo &FnInfo) { |
| 244 | assert(!CurGD.getDecl() && "CurGD was already set!"); |
| 245 | CurGD = GD; |
| 246 | |
| 247 | // Build FunctionArgs. |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 248 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 249 | QualType ThisType = MD->getThisType(getContext()); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 250 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 251 | QualType ResultType = |
| 252 | CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getResultType(); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 253 | FunctionArgList FunctionArgs; |
| 254 | |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 255 | // Create the implicit 'this' parameter declaration. |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame^] | 256 | CGM.getCXXABI().buildThisParam(*this, FunctionArgs); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 257 | |
| 258 | // Add the rest of the parameters. |
| 259 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 260 | E = MD->param_end(); |
| 261 | I != E; ++I) |
| 262 | FunctionArgs.push_back(*I); |
Alexey Samsonov | 9b502e5 | 2012-10-25 10:18:50 +0000 | [diff] [blame] | 263 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame^] | 264 | if (isa<CXXDestructorDecl>(MD)) |
| 265 | CGM.getCXXABI().addImplicitStructorParams(*this, ResultType, FunctionArgs); |
| 266 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 267 | // Start defining the function. |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 268 | StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs, |
| 269 | SourceLocation()); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 270 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 271 | // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves. |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 272 | CGM.getCXXABI().EmitInstanceFunctionProlog(*this); |
Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 273 | CXXThisValue = CXXABIThisValue; |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 274 | } |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 275 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 276 | void CodeGenFunction::EmitCallAndReturnForThunk(GlobalDecl GD, |
| 277 | llvm::Value *Callee, |
| 278 | const ThunkInfo *Thunk) { |
| 279 | assert(isa<CXXMethodDecl>(CurGD.getDecl()) && |
| 280 | "Please use a new CGF for this thunk"); |
| 281 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 282 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 283 | // Adjust the 'this' pointer if necessary |
| 284 | llvm::Value *AdjustedThisPtr = Thunk ? CGM.getCXXABI().performThisAdjustment( |
| 285 | *this, LoadCXXThis(), Thunk->This) |
| 286 | : LoadCXXThis(); |
| 287 | |
| 288 | // Start building CallArgs. |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 289 | CallArgList CallArgs; |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 290 | QualType ThisType = MD->getThisType(getContext()); |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 291 | CallArgs.add(RValue::get(AdjustedThisPtr), ThisType); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 292 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 293 | if (isa<CXXDestructorDecl>(MD)) |
| 294 | CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, GD, CallArgs); |
| 295 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 296 | // Add the rest of the arguments. |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 297 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 298 | E = MD->param_end(); I != E; ++I) |
| 299 | EmitDelegateCallArg(CallArgs, *I, (*I)->getLocStart()); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 300 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 301 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 302 | |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 303 | #ifndef NDEBUG |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 304 | const CGFunctionInfo &CallFnInfo = |
| 305 | CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT, |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 306 | RequiredArgs::forPrototypePlus(FPT, 1)); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 307 | assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() && |
| 308 | CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() && |
| 309 | CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention()); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 310 | assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types |
| 311 | similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(), |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 312 | CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType())); |
| 313 | assert(CallFnInfo.arg_size() == CurFnInfo->arg_size()); |
| 314 | for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i) |
John McCall | 5fe0096 | 2011-03-09 07:12:35 +0000 | [diff] [blame] | 315 | assert(similar(CallFnInfo.arg_begin()[i].info, |
| 316 | CallFnInfo.arg_begin()[i].type, |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 317 | CurFnInfo->arg_begin()[i].info, |
| 318 | CurFnInfo->arg_begin()[i].type)); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 319 | #endif |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 320 | |
Douglas Gregor | aa2ac80 | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 321 | // Determine whether we have a return value slot to use. |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 322 | QualType ResultType = |
| 323 | CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getResultType(); |
Douglas Gregor | aa2ac80 | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 324 | ReturnValueSlot Slot; |
| 325 | if (!ResultType->isVoidType() && |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 326 | CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 327 | !hasScalarEvaluationKind(CurFnInfo->getReturnType())) |
Douglas Gregor | aa2ac80 | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 328 | Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified()); |
| 329 | |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 330 | // Now emit our call. |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 331 | RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 332 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 333 | // Consider return adjustment if we have ThunkInfo. |
| 334 | if (Thunk && !Thunk->Return.isEmpty()) |
| 335 | RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 336 | |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 337 | // Emit return. |
Douglas Gregor | aa2ac80 | 2010-05-20 05:54:35 +0000 | [diff] [blame] | 338 | if (!ResultType->isVoidType() && Slot.isNull()) |
John McCall | ad7c5c1 | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 339 | CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 340 | |
John McCall | ff755cd | 2012-07-31 00:33:55 +0000 | [diff] [blame] | 341 | // Disable the final ARC autorelease. |
| 342 | AutoreleaseResult = false; |
| 343 | |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 344 | FinishFunction(); |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void CodeGenFunction::GenerateThunk(llvm::Function *Fn, |
| 348 | const CGFunctionInfo &FnInfo, |
| 349 | GlobalDecl GD, const ThunkInfo &Thunk) { |
| 350 | StartThunk(Fn, GD, FnInfo); |
| 351 | |
| 352 | // Get our callee. |
| 353 | llvm::Type *Ty = |
| 354 | CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD)); |
| 355 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
| 356 | |
| 357 | // Make the call and return the result. |
| 358 | EmitCallAndReturnForThunk(GD, Callee, &Thunk); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 359 | |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 360 | // Set the right linkage. |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 361 | CGM.setFunctionLinkage(GD, Fn); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 362 | |
| 363 | // Set the right visibility. |
Hans Wennborg | 88497d6 | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 364 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
John McCall | c8bd9c2 | 2010-08-04 23:46:35 +0000 | [diff] [blame] | 365 | setThunkVisibility(CGM, MD, Thunk, Fn); |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 368 | void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, |
| 369 | bool ForVTable) { |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 370 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD); |
John McCall | a738c25 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 371 | |
| 372 | // FIXME: re-use FnInfo in this computation. |
Anders Carlsson | fe8a993 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 373 | llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | cd836f0 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 374 | |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 375 | // Strip off a bitcast if we got one back. |
Anders Carlsson | 4a3cdf5 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 376 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 377 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
Anders Carlsson | 4a3cdf5 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 378 | Entry = CE->getOperand(0); |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 381 | // There's already a declaration with the same name, check if it has the same |
| 382 | // type or if we need to replace it. |
Anders Carlsson | 4a3cdf5 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 383 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 384 | CGM.getTypes().GetFunctionTypeForVTable(GD)) { |
Anders Carlsson | 4a3cdf5 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 385 | llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry); |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 386 | |
| 387 | // If the types mismatch then we have to rewrite the definition. |
| 388 | assert(OldThunkFn->isDeclaration() && |
| 389 | "Shouldn't replace non-declaration"); |
| 390 | |
| 391 | // Remove the name from the old thunk function and get a new thunk. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 392 | OldThunkFn->setName(StringRef()); |
Anders Carlsson | fe8a993 | 2011-02-06 17:15:43 +0000 | [diff] [blame] | 393 | Entry = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 394 | |
| 395 | // If needed, replace the old thunk with a bitcast. |
| 396 | if (!OldThunkFn->use_empty()) { |
| 397 | llvm::Constant *NewPtrForOldDecl = |
Anders Carlsson | 4a3cdf5 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 398 | llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType()); |
Anders Carlsson | 55e89f8 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 399 | OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); |
| 400 | } |
| 401 | |
| 402 | // Remove the old thunk. |
| 403 | OldThunkFn->eraseFromParent(); |
| 404 | } |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 405 | |
Anders Carlsson | bad991d | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 406 | llvm::Function *ThunkFn = cast<llvm::Function>(Entry); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 407 | bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions(); |
| 408 | bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions; |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 409 | |
| 410 | if (!ThunkFn->isDeclaration()) { |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 411 | if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) { |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 412 | // There is already a thunk emitted for this function, do nothing. |
| 413 | return; |
| 414 | } |
| 415 | |
Anders Carlsson | e866d44 | 2011-02-06 20:09:44 +0000 | [diff] [blame] | 416 | // Change the linkage. |
Peter Collingbourne | 4d90dba | 2013-06-05 17:49:37 +0000 | [diff] [blame] | 417 | CGM.setFunctionLinkage(GD, ThunkFn); |
Anders Carlsson | e866d44 | 2011-02-06 20:09:44 +0000 | [diff] [blame] | 418 | return; |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Rafael Espindola | 8679243 | 2012-09-21 20:39:32 +0000 | [diff] [blame] | 421 | CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn); |
| 422 | |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 423 | if (ThunkFn->isVarArg()) { |
| 424 | // Varargs thunks are special; we can't just generate a call because |
| 425 | // we can't copy the varargs. Our implementation is rather |
| 426 | // expensive/sucky at the moment, so don't generate the thunk unless |
| 427 | // we have to. |
| 428 | // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly. |
Benjamin Kramer | 065c61b | 2013-12-07 16:12:52 +0000 | [diff] [blame] | 429 | if (!UseAvailableExternallyLinkage) { |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 430 | CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk); |
Benjamin Kramer | 065c61b | 2013-12-07 16:12:52 +0000 | [diff] [blame] | 431 | CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable); |
| 432 | } |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 433 | } else { |
| 434 | // Normal thunk body generation. |
| 435 | CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk); |
Benjamin Kramer | 065c61b | 2013-12-07 16:12:52 +0000 | [diff] [blame] | 436 | CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable); |
Eli Friedman | 49a94b1 | 2011-05-06 17:27:27 +0000 | [diff] [blame] | 437 | } |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 440 | void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD, |
| 441 | const ThunkInfo &Thunk) { |
| 442 | // If the ABI has key functions, only the TU with the key function should emit |
| 443 | // the thunk. However, we can allow inlining of thunks if we emit them with |
| 444 | // available_externally linkage together with vtables when optimizations are |
| 445 | // enabled. |
| 446 | if (CGM.getTarget().getCXXABI().hasKeyFunctions() && |
| 447 | !CGM.getCodeGenOpts().OptimizationLevel) |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 448 | return; |
| 449 | |
| 450 | // We can't emit thunks for member functions with incomplete types. |
| 451 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Chris Lattner | 8806e32 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 452 | if (!CGM.getTypes().isFuncTypeConvertible( |
Reid Kleckner | fe56be5 | 2013-10-11 20:46:27 +0000 | [diff] [blame] | 453 | MD->getType()->castAs<FunctionType>())) |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 454 | return; |
| 455 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 456 | emitThunk(GD, Thunk, /*ForVTable=*/true); |
Anders Carlsson | 5c5abad | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Anders Carlsson | 917229c | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 459 | void CodeGenVTables::EmitThunks(GlobalDecl GD) |
| 460 | { |
Anders Carlsson | 5c5abad | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 461 | const CXXMethodDecl *MD = |
| 462 | cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl(); |
| 463 | |
| 464 | // We don't need to generate thunks for the base destructor. |
| 465 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 466 | return; |
| 467 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 468 | const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector; |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 469 | if (MicrosoftVTContext.isValid()) { |
| 470 | ThunkInfoVector = MicrosoftVTContext->getThunkInfo(GD); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 471 | } else { |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 472 | ThunkInfoVector = ItaniumVTContext.getThunkInfo(GD); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Peter Collingbourne | 5ee9ee4 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 475 | if (!ThunkInfoVector) |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 476 | return; |
Anders Carlsson | e90954d | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 477 | |
Peter Collingbourne | 5ee9ee4 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 478 | for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I) |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 479 | emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false); |
Anders Carlsson | 917229c | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 482 | llvm::Constant * |
| 483 | CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD, |
Peter Collingbourne | affe111 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 484 | const VTableComponent *Components, |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 485 | unsigned NumComponents, |
Peter Collingbourne | affe111 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 486 | const VTableLayout::VTableThunkTy *VTableThunks, |
| 487 | unsigned NumVTableThunks) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 488 | SmallVector<llvm::Constant *, 64> Inits; |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 489 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 490 | llvm::Type *Int8PtrTy = CGM.Int8PtrTy; |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 492 | llvm::Type *PtrDiffTy = |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 493 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 494 | |
| 495 | QualType ClassType = CGM.getContext().getTagDeclType(RD); |
| 496 | llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType); |
| 497 | |
| 498 | unsigned NextVTableThunkIndex = 0; |
| 499 | |
David Blaikie | eb7d598 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 500 | llvm::Constant *PureVirtualFn = 0, *DeletedVirtualFn = 0; |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 501 | |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 502 | for (unsigned I = 0; I != NumComponents; ++I) { |
Peter Collingbourne | affe111 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 503 | VTableComponent Component = Components[I]; |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 504 | |
| 505 | llvm::Constant *Init = 0; |
| 506 | |
| 507 | switch (Component.getKind()) { |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 508 | case VTableComponent::CK_VCallOffset: |
Ken Dyck | 872d74a | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 509 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 510 | Component.getVCallOffset().getQuantity()); |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 511 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 512 | break; |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 513 | case VTableComponent::CK_VBaseOffset: |
Ken Dyck | 872d74a | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 514 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 515 | Component.getVBaseOffset().getQuantity()); |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 516 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 517 | break; |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 518 | case VTableComponent::CK_OffsetToTop: |
Ken Dyck | 872d74a | 2011-04-02 01:14:48 +0000 | [diff] [blame] | 519 | Init = llvm::ConstantInt::get(PtrDiffTy, |
| 520 | Component.getOffsetToTop().getQuantity()); |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 521 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 522 | break; |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 523 | case VTableComponent::CK_RTTI: |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 524 | Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy); |
| 525 | break; |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 526 | case VTableComponent::CK_FunctionPointer: |
| 527 | case VTableComponent::CK_CompleteDtorPointer: |
| 528 | case VTableComponent::CK_DeletingDtorPointer: { |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 529 | GlobalDecl GD; |
| 530 | |
| 531 | // Get the right global decl. |
| 532 | switch (Component.getKind()) { |
| 533 | default: |
| 534 | llvm_unreachable("Unexpected vtable component kind"); |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 535 | case VTableComponent::CK_FunctionPointer: |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 536 | GD = Component.getFunctionDecl(); |
| 537 | break; |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 538 | case VTableComponent::CK_CompleteDtorPointer: |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 539 | GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete); |
| 540 | break; |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 541 | case VTableComponent::CK_DeletingDtorPointer: |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 542 | GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting); |
| 543 | break; |
| 544 | } |
| 545 | |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 546 | if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) { |
| 547 | // We have a pure virtual member function. |
Joao Matos | 718a883 | 2012-07-17 19:17:58 +0000 | [diff] [blame] | 548 | if (!PureVirtualFn) { |
Eli Friedman | 48a3291 | 2012-09-14 01:19:01 +0000 | [diff] [blame] | 549 | llvm::FunctionType *Ty = |
| 550 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
| 551 | StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName(); |
| 552 | PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName); |
| 553 | PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn, |
Joao Matos | 718a883 | 2012-07-17 19:17:58 +0000 | [diff] [blame] | 554 | CGM.Int8PtrTy); |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 555 | } |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 556 | Init = PureVirtualFn; |
David Blaikie | eb7d598 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 557 | } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) { |
| 558 | if (!DeletedVirtualFn) { |
| 559 | llvm::FunctionType *Ty = |
| 560 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
| 561 | StringRef DeletedCallName = |
| 562 | CGM.getCXXABI().GetDeletedVirtualCallName(); |
| 563 | DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName); |
| 564 | DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn, |
| 565 | CGM.Int8PtrTy); |
| 566 | } |
| 567 | Init = DeletedVirtualFn; |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 568 | } else { |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 569 | // Check if we should use a thunk. |
Peter Collingbourne | affe111 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 570 | if (NextVTableThunkIndex < NumVTableThunks && |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 571 | VTableThunks[NextVTableThunkIndex].first == I) { |
| 572 | const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second; |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 573 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 574 | maybeEmitThunkForVTable(GD, Thunk); |
Benjamin Kramer | e6b4a16 | 2012-03-20 20:18:13 +0000 | [diff] [blame] | 575 | Init = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 576 | |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 577 | NextVTableThunkIndex++; |
| 578 | } else { |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 579 | llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD); |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 580 | |
Anders Carlsson | 3c23948 | 2011-02-05 04:35:53 +0000 | [diff] [blame] | 581 | Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true); |
Anders Carlsson | cb6207f | 2010-03-29 05:40:50 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy); |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 585 | } |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 586 | break; |
| 587 | } |
| 588 | |
Anders Carlsson | be1b9cb | 2010-04-10 19:13:06 +0000 | [diff] [blame] | 589 | case VTableComponent::CK_UnusedFunctionPointer: |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 590 | Init = llvm::ConstantExpr::getNullValue(Int8PtrTy); |
| 591 | break; |
| 592 | }; |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 593 | |
| 594 | Inits.push_back(Init); |
| 595 | } |
| 596 | |
| 597 | llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents); |
Jay Foad | 83be361 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 598 | return llvm::ConstantArray::get(ArrayType, Inits); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Anders Carlsson | 0534b02 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 601 | llvm::GlobalVariable * |
| 602 | CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 603 | const BaseSubobject &Base, |
| 604 | bool BaseIsVirtual, |
John McCall | 358d056 | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 605 | llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 606 | VTableAddressPointsMapTy& AddressPoints) { |
David Blaikie | d89b99d | 2013-08-22 15:23:05 +0000 | [diff] [blame] | 607 | if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) |
| 608 | DI->completeClassData(Base.getBase()); |
| 609 | |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 610 | OwningPtr<VTableLayout> VTLayout( |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 611 | ItaniumVTContext.createConstructionVTableLayout( |
| 612 | Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD)); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 613 | |
Anders Carlsson | a5736bd | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 614 | // Add the address points. |
Peter Collingbourne | 1c593c6 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 615 | AddressPoints = VTLayout->getAddressPoints(); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 616 | |
| 617 | // Get the mangled construction vtable name. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 618 | SmallString<256> OutName; |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 619 | llvm::raw_svector_ostream Out(OutName); |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 620 | cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext()) |
| 621 | .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), |
| 622 | Base.getBase(), Out); |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 623 | Out.flush(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 624 | StringRef Name = OutName.str(); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 625 | |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 626 | llvm::ArrayType *ArrayType = |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 627 | llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents()); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 628 | |
Richard Smith | 65fd2a4 | 2013-02-16 00:51:21 +0000 | [diff] [blame] | 629 | // Construction vtable symbols are not part of the Itanium ABI, so we cannot |
| 630 | // guarantee that they actually will be available externally. Instead, when |
| 631 | // emitting an available_externally VTT, we provide references to an internal |
| 632 | // linkage construction vtable. The ABI only requires complete-object vtables |
| 633 | // to be the same for all instances of a type, not construction vtables. |
| 634 | if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage) |
| 635 | Linkage = llvm::GlobalVariable::InternalLinkage; |
| 636 | |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 637 | // Create the variable that will hold the construction vtable. |
| 638 | llvm::GlobalVariable *VTable = |
John McCall | 358d056 | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 639 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage); |
| 640 | CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable); |
| 641 | |
| 642 | // V-tables are always unnamed_addr. |
| 643 | VTable->setUnnamedAddr(true); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 644 | |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 645 | // Create and set the initializer. |
| 646 | llvm::Constant *Init = |
| 647 | CreateVTableInitializer(Base.getBase(), |
Peter Collingbourne | 1c593c6 | 2011-09-26 01:57:04 +0000 | [diff] [blame] | 648 | VTLayout->vtable_component_begin(), |
| 649 | VTLayout->getNumVTableComponents(), |
| 650 | VTLayout->vtable_thunk_begin(), |
| 651 | VTLayout->getNumVTableThunks()); |
Anders Carlsson | a414714 | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 652 | VTable->setInitializer(Init); |
| 653 | |
Anders Carlsson | 0534b02 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 654 | return VTable; |
| 655 | } |
| 656 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 657 | /// Compute the required linkage of the v-table for the given class. |
| 658 | /// |
| 659 | /// Note that we only call this at the end of the translation unit. |
| 660 | llvm::GlobalVariable::LinkageTypes |
| 661 | CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 662 | if (!RD->isExternallyVisible()) |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 663 | return llvm::GlobalVariable::InternalLinkage; |
| 664 | |
| 665 | // We're at the end of the translation unit, so the current key |
| 666 | // function is fully correct. |
| 667 | if (const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD)) { |
| 668 | // If this class has a key function, use that to determine the |
| 669 | // linkage of the vtable. |
| 670 | const FunctionDecl *def = 0; |
| 671 | if (keyFunction->hasBody(def)) |
| 672 | keyFunction = cast<CXXMethodDecl>(def); |
| 673 | |
| 674 | switch (keyFunction->getTemplateSpecializationKind()) { |
| 675 | case TSK_Undeclared: |
| 676 | case TSK_ExplicitSpecialization: |
Rafael Espindola | ee6aa0c | 2013-09-03 21:05:13 +0000 | [diff] [blame] | 677 | assert(def && "Should not have been asked to emit this"); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 678 | if (keyFunction->isInlined()) |
| 679 | return !Context.getLangOpts().AppleKext ? |
| 680 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 681 | llvm::Function::InternalLinkage; |
| 682 | |
| 683 | return llvm::GlobalVariable::ExternalLinkage; |
| 684 | |
| 685 | case TSK_ImplicitInstantiation: |
| 686 | return !Context.getLangOpts().AppleKext ? |
| 687 | llvm::GlobalVariable::LinkOnceODRLinkage : |
| 688 | llvm::Function::InternalLinkage; |
| 689 | |
| 690 | case TSK_ExplicitInstantiationDefinition: |
| 691 | return !Context.getLangOpts().AppleKext ? |
| 692 | llvm::GlobalVariable::WeakODRLinkage : |
| 693 | llvm::Function::InternalLinkage; |
| 694 | |
| 695 | case TSK_ExplicitInstantiationDeclaration: |
Rafael Espindola | ee6aa0c | 2013-09-03 21:05:13 +0000 | [diff] [blame] | 696 | llvm_unreachable("Should not have been asked to emit this"); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | // -fapple-kext mode does not support weak linkage, so we must use |
| 701 | // internal linkage. |
| 702 | if (Context.getLangOpts().AppleKext) |
| 703 | return llvm::Function::InternalLinkage; |
| 704 | |
| 705 | switch (RD->getTemplateSpecializationKind()) { |
| 706 | case TSK_Undeclared: |
| 707 | case TSK_ExplicitSpecialization: |
| 708 | case TSK_ImplicitInstantiation: |
| 709 | return llvm::GlobalVariable::LinkOnceODRLinkage; |
| 710 | |
| 711 | case TSK_ExplicitInstantiationDeclaration: |
Rafael Espindola | ee6aa0c | 2013-09-03 21:05:13 +0000 | [diff] [blame] | 712 | llvm_unreachable("Should not have been asked to emit this"); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 713 | |
| 714 | case TSK_ExplicitInstantiationDefinition: |
| 715 | return llvm::GlobalVariable::WeakODRLinkage; |
| 716 | } |
| 717 | |
| 718 | llvm_unreachable("Invalid TemplateSpecializationKind!"); |
| 719 | } |
| 720 | |
| 721 | /// This is a callback from Sema to tell us that it believes that a |
| 722 | /// particular v-table is required to be emitted in this translation |
| 723 | /// unit. |
| 724 | /// |
| 725 | /// The reason we don't simply trust this callback is because Sema |
| 726 | /// will happily report that something is used even when it's used |
| 727 | /// only in code that we don't actually have to emit. |
| 728 | /// |
| 729 | /// \param isRequired - if true, the v-table is mandatory, e.g. |
| 730 | /// because the translation unit defines the key function |
| 731 | void CodeGenModule::EmitVTable(CXXRecordDecl *theClass, bool isRequired) { |
| 732 | if (!isRequired) return; |
| 733 | |
| 734 | VTables.GenerateClassData(theClass); |
| 735 | } |
| 736 | |
Anders Carlsson | a627ac7e | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 737 | void |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 738 | CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) { |
David Blaikie | d89b99d | 2013-08-22 15:23:05 +0000 | [diff] [blame] | 739 | if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) |
| 740 | DI->completeClassData(RD); |
| 741 | |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 742 | if (RD->getNumVBases()) |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 743 | CGM.getCXXABI().emitVirtualInheritanceTables(RD); |
Douglas Gregor | eadd3ca | 2010-04-08 15:52:03 +0000 | [diff] [blame] | 744 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 745 | CGM.getCXXABI().emitVTableDefinitions(*this, RD); |
Anders Carlsson | a627ac7e | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 746 | } |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 747 | |
| 748 | /// At this point in the translation unit, does it appear that can we |
| 749 | /// rely on the vtable being defined elsewhere in the program? |
| 750 | /// |
| 751 | /// The response is really only definitive when called at the end of |
| 752 | /// the translation unit. |
| 753 | /// |
| 754 | /// The only semantic restriction here is that the object file should |
| 755 | /// not contain a v-table definition when that v-table is defined |
| 756 | /// strongly elsewhere. Otherwise, we'd just like to avoid emitting |
| 757 | /// v-tables when unnecessary. |
| 758 | bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) { |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 759 | assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable."); |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 760 | |
| 761 | // If we have an explicit instantiation declaration (and not a |
| 762 | // definition), the v-table is defined elsewhere. |
| 763 | TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); |
| 764 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 765 | return true; |
| 766 | |
| 767 | // Otherwise, if the class is an instantiated template, the |
| 768 | // v-table must be defined here. |
| 769 | if (TSK == TSK_ImplicitInstantiation || |
| 770 | TSK == TSK_ExplicitInstantiationDefinition) |
| 771 | return false; |
| 772 | |
| 773 | // Otherwise, if the class doesn't have a key function (possibly |
| 774 | // anymore), the v-table must be defined here. |
| 775 | const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD); |
| 776 | if (!keyFunction) |
| 777 | return false; |
| 778 | |
| 779 | // Otherwise, if we don't have a definition of the key function, the |
| 780 | // v-table must be defined somewhere else. |
| 781 | return !keyFunction->hasBody(); |
| 782 | } |
| 783 | |
| 784 | /// Given that we're currently at the end of the translation unit, and |
| 785 | /// we've emitted a reference to the v-table for this class, should |
| 786 | /// we define that v-table? |
| 787 | static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM, |
| 788 | const CXXRecordDecl *RD) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 789 | return !CGM.getVTables().isVTableExternal(RD); |
| 790 | } |
| 791 | |
| 792 | /// Given that at some point we emitted a reference to one or more |
| 793 | /// v-tables, and that we are now at the end of the translation unit, |
| 794 | /// decide whether we should emit them. |
| 795 | void CodeGenModule::EmitDeferredVTables() { |
| 796 | #ifndef NDEBUG |
| 797 | // Remember the size of DeferredVTables, because we're going to assume |
| 798 | // that this entire operation doesn't modify it. |
| 799 | size_t savedSize = DeferredVTables.size(); |
| 800 | #endif |
| 801 | |
| 802 | typedef std::vector<const CXXRecordDecl *>::const_iterator const_iterator; |
| 803 | for (const_iterator i = DeferredVTables.begin(), |
| 804 | e = DeferredVTables.end(); i != e; ++i) { |
| 805 | const CXXRecordDecl *RD = *i; |
| 806 | if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD)) |
| 807 | VTables.GenerateClassData(RD); |
| 808 | } |
| 809 | |
| 810 | assert(savedSize == DeferredVTables.size() && |
| 811 | "deferred extra v-tables during v-table emission?"); |
| 812 | DeferredVTables.clear(); |
| 813 | } |