/src/llvm-project/clang/lib/CodeGen/CGCXX.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This contains code dealing with C++ code generation. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | // We might split this into multiple files if it gets too unwieldy |
14 | | |
15 | | #include "CGCXXABI.h" |
16 | | #include "CodeGenFunction.h" |
17 | | #include "CodeGenModule.h" |
18 | | #include "clang/AST/ASTContext.h" |
19 | | #include "clang/AST/Attr.h" |
20 | | #include "clang/AST/Decl.h" |
21 | | #include "clang/AST/DeclCXX.h" |
22 | | #include "clang/AST/DeclObjC.h" |
23 | | #include "clang/AST/Mangle.h" |
24 | | #include "clang/AST/RecordLayout.h" |
25 | | #include "clang/AST/StmtCXX.h" |
26 | | #include "clang/Basic/CodeGenOptions.h" |
27 | | #include "llvm/ADT/StringExtras.h" |
28 | | using namespace clang; |
29 | | using namespace CodeGen; |
30 | | |
31 | | |
32 | | /// Try to emit a base destructor as an alias to its primary |
33 | | /// base-class destructor. |
34 | 0 | bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { |
35 | 0 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
36 | 0 | return true; |
37 | | |
38 | | // Producing an alias to a base class ctor/dtor can degrade debug quality |
39 | | // as the debugger cannot tell them apart. |
40 | 0 | if (getCodeGenOpts().OptimizationLevel == 0) |
41 | 0 | return true; |
42 | | |
43 | | // If sanitizing memory to check for use-after-dtor, do not emit as |
44 | | // an alias, unless this class owns no members. |
45 | 0 | if (getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
46 | 0 | !D->getParent()->field_empty()) |
47 | 0 | return true; |
48 | | |
49 | | // If the destructor doesn't have a trivial body, we have to emit it |
50 | | // separately. |
51 | 0 | if (!D->hasTrivialBody()) |
52 | 0 | return true; |
53 | | |
54 | 0 | const CXXRecordDecl *Class = D->getParent(); |
55 | | |
56 | | // We are going to instrument this destructor, so give up even if it is |
57 | | // currently empty. |
58 | 0 | if (Class->mayInsertExtraPadding()) |
59 | 0 | return true; |
60 | | |
61 | | // If we need to manipulate a VTT parameter, give up. |
62 | 0 | if (Class->getNumVBases()) { |
63 | | // Extra Credit: passing extra parameters is perfectly safe |
64 | | // in many calling conventions, so only bail out if the ctor's |
65 | | // calling convention is nonstandard. |
66 | 0 | return true; |
67 | 0 | } |
68 | | |
69 | | // If any field has a non-trivial destructor, we have to emit the |
70 | | // destructor separately. |
71 | 0 | for (const auto *I : Class->fields()) |
72 | 0 | if (I->getType().isDestructedType()) |
73 | 0 | return true; |
74 | | |
75 | | // Try to find a unique base class with a non-trivial destructor. |
76 | 0 | const CXXRecordDecl *UniqueBase = nullptr; |
77 | 0 | for (const auto &I : Class->bases()) { |
78 | | |
79 | | // We're in the base destructor, so skip virtual bases. |
80 | 0 | if (I.isVirtual()) continue; |
81 | | |
82 | | // Skip base classes with trivial destructors. |
83 | 0 | const auto *Base = |
84 | 0 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
85 | 0 | if (Base->hasTrivialDestructor()) continue; |
86 | | |
87 | | // If we've already found a base class with a non-trivial |
88 | | // destructor, give up. |
89 | 0 | if (UniqueBase) return true; |
90 | 0 | UniqueBase = Base; |
91 | 0 | } |
92 | | |
93 | | // If we didn't find any bases with a non-trivial destructor, then |
94 | | // the base destructor is actually effectively trivial, which can |
95 | | // happen if it was needlessly user-defined or if there are virtual |
96 | | // bases with non-trivial destructors. |
97 | 0 | if (!UniqueBase) |
98 | 0 | return true; |
99 | | |
100 | | // If the base is at a non-zero offset, give up. |
101 | 0 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); |
102 | 0 | if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero()) |
103 | 0 | return true; |
104 | | |
105 | | // Give up if the calling conventions don't match. We could update the call, |
106 | | // but it is probably not worth it. |
107 | 0 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
108 | 0 | if (BaseD->getType()->castAs<FunctionType>()->getCallConv() != |
109 | 0 | D->getType()->castAs<FunctionType>()->getCallConv()) |
110 | 0 | return true; |
111 | | |
112 | 0 | GlobalDecl AliasDecl(D, Dtor_Base); |
113 | 0 | GlobalDecl TargetDecl(BaseD, Dtor_Base); |
114 | | |
115 | | // The alias will use the linkage of the referent. If we can't |
116 | | // support aliases with that linkage, fail. |
117 | 0 | llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl); |
118 | | |
119 | | // We can't use an alias if the linkage is not valid for one. |
120 | 0 | if (!llvm::GlobalAlias::isValidLinkage(Linkage)) |
121 | 0 | return true; |
122 | | |
123 | 0 | llvm::GlobalValue::LinkageTypes TargetLinkage = |
124 | 0 | getFunctionLinkage(TargetDecl); |
125 | | |
126 | | // Check if we have it already. |
127 | 0 | StringRef MangledName = getMangledName(AliasDecl); |
128 | 0 | llvm::GlobalValue *Entry = GetGlobalValue(MangledName); |
129 | 0 | if (Entry && !Entry->isDeclaration()) |
130 | 0 | return false; |
131 | 0 | if (Replacements.count(MangledName)) |
132 | 0 | return false; |
133 | | |
134 | 0 | llvm::Type *AliasValueType = getTypes().GetFunctionType(AliasDecl); |
135 | | |
136 | | // Find the referent. |
137 | 0 | auto *Aliasee = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); |
138 | | |
139 | | // Instead of creating as alias to a linkonce_odr, replace all of the uses |
140 | | // of the aliasee. |
141 | 0 | if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) && |
142 | 0 | !(TargetLinkage == llvm::GlobalValue::AvailableExternallyLinkage && |
143 | 0 | TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) { |
144 | | // FIXME: An extern template instantiation will create functions with |
145 | | // linkage "AvailableExternally". In libc++, some classes also define |
146 | | // members with attribute "AlwaysInline" and expect no reference to |
147 | | // be generated. It is desirable to reenable this optimisation after |
148 | | // corresponding LLVM changes. |
149 | 0 | addReplacement(MangledName, Aliasee); |
150 | 0 | return false; |
151 | 0 | } |
152 | | |
153 | | // If we have a weak, non-discardable alias (weak, weak_odr), like an extern |
154 | | // template instantiation or a dllexported class, avoid forming it on COFF. |
155 | | // A COFF weak external alias cannot satisfy a normal undefined symbol |
156 | | // reference from another TU. The other TU must also mark the referenced |
157 | | // symbol as weak, which we cannot rely on. |
158 | 0 | if (llvm::GlobalValue::isWeakForLinker(Linkage) && |
159 | 0 | getTriple().isOSBinFormatCOFF()) { |
160 | 0 | return true; |
161 | 0 | } |
162 | | |
163 | | // If we don't have a definition for the destructor yet or the definition is |
164 | | // avaialable_externally, don't emit an alias. We can't emit aliases to |
165 | | // declarations; that's just not how aliases work. |
166 | 0 | if (Aliasee->isDeclarationForLinker()) |
167 | 0 | return true; |
168 | | |
169 | | // Don't create an alias to a linker weak symbol. This avoids producing |
170 | | // different COMDATs in different TUs. Another option would be to |
171 | | // output the alias both for weak_odr and linkonce_odr, but that |
172 | | // requires explicit comdat support in the IL. |
173 | 0 | if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) |
174 | 0 | return true; |
175 | | |
176 | | // Create the alias with no name. |
177 | 0 | auto *Alias = llvm::GlobalAlias::create(AliasValueType, 0, Linkage, "", |
178 | 0 | Aliasee, &getModule()); |
179 | | |
180 | | // Destructors are always unnamed_addr. |
181 | 0 | Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
182 | | |
183 | | // Switch any previous uses to the alias. |
184 | 0 | if (Entry) { |
185 | 0 | assert(Entry->getValueType() == AliasValueType && |
186 | 0 | Entry->getAddressSpace() == Alias->getAddressSpace() && |
187 | 0 | "declaration exists with different type"); |
188 | 0 | Alias->takeName(Entry); |
189 | 0 | Entry->replaceAllUsesWith(Alias); |
190 | 0 | Entry->eraseFromParent(); |
191 | 0 | } else { |
192 | 0 | Alias->setName(MangledName); |
193 | 0 | } |
194 | | |
195 | | // Finally, set up the alias with its proper name and attributes. |
196 | 0 | SetCommonAttributes(AliasDecl, Alias); |
197 | |
|
198 | 0 | return false; |
199 | 0 | } |
200 | | |
201 | 0 | llvm::Function *CodeGenModule::codegenCXXStructor(GlobalDecl GD) { |
202 | 0 | const CGFunctionInfo &FnInfo = getTypes().arrangeCXXStructorDeclaration(GD); |
203 | 0 | auto *Fn = cast<llvm::Function>( |
204 | 0 | getAddrOfCXXStructor(GD, &FnInfo, /*FnType=*/nullptr, |
205 | 0 | /*DontDefer=*/true, ForDefinition)); |
206 | |
|
207 | 0 | setFunctionLinkage(GD, Fn); |
208 | |
|
209 | 0 | CodeGenFunction(*this).GenerateCode(GD, Fn, FnInfo); |
210 | 0 | setNonAliasAttributes(GD, Fn); |
211 | 0 | SetLLVMFunctionAttributesForDefinition(cast<CXXMethodDecl>(GD.getDecl()), Fn); |
212 | 0 | return Fn; |
213 | 0 | } |
214 | | |
215 | | llvm::FunctionCallee CodeGenModule::getAddrAndTypeOfCXXStructor( |
216 | | GlobalDecl GD, const CGFunctionInfo *FnInfo, llvm::FunctionType *FnType, |
217 | 0 | bool DontDefer, ForDefinition_t IsForDefinition) { |
218 | 0 | auto *MD = cast<CXXMethodDecl>(GD.getDecl()); |
219 | |
|
220 | 0 | if (isa<CXXDestructorDecl>(MD)) { |
221 | | // Always alias equivalent complete destructors to base destructors in the |
222 | | // MS ABI. |
223 | 0 | if (getTarget().getCXXABI().isMicrosoft() && |
224 | 0 | GD.getDtorType() == Dtor_Complete && |
225 | 0 | MD->getParent()->getNumVBases() == 0) |
226 | 0 | GD = GD.getWithDtorType(Dtor_Base); |
227 | 0 | } |
228 | |
|
229 | 0 | if (!FnType) { |
230 | 0 | if (!FnInfo) |
231 | 0 | FnInfo = &getTypes().arrangeCXXStructorDeclaration(GD); |
232 | 0 | FnType = getTypes().GetFunctionType(*FnInfo); |
233 | 0 | } |
234 | |
|
235 | 0 | llvm::Constant *Ptr = GetOrCreateLLVMFunction( |
236 | 0 | getMangledName(GD), FnType, GD, /*ForVTable=*/false, DontDefer, |
237 | 0 | /*IsThunk=*/false, /*ExtraAttrs=*/llvm::AttributeList(), IsForDefinition); |
238 | 0 | return {FnType, Ptr}; |
239 | 0 | } |
240 | | |
241 | | static CGCallee BuildAppleKextVirtualCall(CodeGenFunction &CGF, |
242 | | GlobalDecl GD, |
243 | | llvm::Type *Ty, |
244 | 0 | const CXXRecordDecl *RD) { |
245 | 0 | assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && |
246 | 0 | "No kext in Microsoft ABI"); |
247 | 0 | CodeGenModule &CGM = CGF.CGM; |
248 | 0 | llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits()); |
249 | 0 | Ty = llvm::PointerType::getUnqual(CGM.getLLVMContext()); |
250 | 0 | assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); |
251 | 0 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
252 | 0 | const VTableLayout &VTLayout = CGM.getItaniumVTableContext().getVTableLayout(RD); |
253 | 0 | VTableLayout::AddressPointLocation AddressPoint = |
254 | 0 | VTLayout.getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); |
255 | 0 | VTableIndex += VTLayout.getVTableOffset(AddressPoint.VTableIndex) + |
256 | 0 | AddressPoint.AddressPointIndex; |
257 | 0 | llvm::Value *VFuncPtr = |
258 | 0 | CGF.Builder.CreateConstInBoundsGEP1_64(Ty, VTable, VTableIndex, "vfnkxt"); |
259 | 0 | llvm::Value *VFunc = CGF.Builder.CreateAlignedLoad( |
260 | 0 | Ty, VFuncPtr, llvm::Align(CGF.PointerAlignInBytes)); |
261 | 0 | CGCallee Callee(GD, VFunc); |
262 | 0 | return Callee; |
263 | 0 | } |
264 | | |
265 | | /// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making |
266 | | /// indirect call to virtual functions. It makes the call through indexing |
267 | | /// into the vtable. |
268 | | CGCallee |
269 | | CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
270 | | NestedNameSpecifier *Qual, |
271 | 0 | llvm::Type *Ty) { |
272 | 0 | assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && |
273 | 0 | "BuildAppleKextVirtualCall - bad Qual kind"); |
274 | | |
275 | 0 | const Type *QTy = Qual->getAsType(); |
276 | 0 | QualType T = QualType(QTy, 0); |
277 | 0 | const RecordType *RT = T->getAs<RecordType>(); |
278 | 0 | assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); |
279 | 0 | const auto *RD = cast<CXXRecordDecl>(RT->getDecl()); |
280 | |
|
281 | 0 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) |
282 | 0 | return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); |
283 | | |
284 | 0 | return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD); |
285 | 0 | } |
286 | | |
287 | | /// BuildVirtualCall - This routine makes indirect vtable call for |
288 | | /// call to virtual destructors. It returns 0 if it could not do it. |
289 | | CGCallee |
290 | | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
291 | | const CXXDestructorDecl *DD, |
292 | | CXXDtorType Type, |
293 | 0 | const CXXRecordDecl *RD) { |
294 | 0 | assert(DD->isVirtual() && Type != Dtor_Base); |
295 | | // Compute the function type we're calling. |
296 | 0 | const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration( |
297 | 0 | GlobalDecl(DD, Dtor_Complete)); |
298 | 0 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo); |
299 | 0 | return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD); |
300 | 0 | } |