/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/Mangle.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===// |
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 | | // Implements generic name mangling support for blocks and Objective-C. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | #include "clang/AST/Attr.h" |
13 | | #include "clang/AST/ASTContext.h" |
14 | | #include "clang/AST/Decl.h" |
15 | | #include "clang/AST/DeclCXX.h" |
16 | | #include "clang/AST/DeclObjC.h" |
17 | | #include "clang/AST/DeclTemplate.h" |
18 | | #include "clang/AST/ExprCXX.h" |
19 | | #include "clang/AST/Mangle.h" |
20 | | #include "clang/AST/VTableBuilder.h" |
21 | | #include "clang/Basic/ABI.h" |
22 | | #include "clang/Basic/SourceManager.h" |
23 | | #include "clang/Basic/TargetInfo.h" |
24 | | #include "llvm/ADT/StringExtras.h" |
25 | | #include "llvm/IR/DataLayout.h" |
26 | | #include "llvm/IR/Mangler.h" |
27 | | #include "llvm/Support/ErrorHandling.h" |
28 | | #include "llvm/Support/Format.h" |
29 | | #include "llvm/Support/raw_ostream.h" |
30 | | |
31 | | using namespace clang; |
32 | | |
33 | | // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves |
34 | | // much to be desired. Come up with a better mangling scheme. |
35 | | |
36 | | static void mangleFunctionBlock(MangleContext &Context, |
37 | | StringRef Outer, |
38 | | const BlockDecl *BD, |
39 | 948 | raw_ostream &Out) { |
40 | 948 | unsigned discriminator = Context.getBlockId(BD, true); |
41 | 948 | if (discriminator == 0) |
42 | 763 | Out << "__" << Outer << "_block_invoke"; |
43 | 185 | else |
44 | 185 | Out << "__" << Outer << "_block_invoke_" << discriminator+1; |
45 | 948 | } |
46 | | |
47 | 0 | void MangleContext::anchor() { } |
48 | | |
49 | | enum CCMangling { |
50 | | CCM_Other, |
51 | | CCM_Fast, |
52 | | CCM_RegCall, |
53 | | CCM_Vector, |
54 | | CCM_Std, |
55 | | CCM_WasmMainArgcArgv |
56 | | }; |
57 | | |
58 | 83.6k | static bool isExternC(const NamedDecl *ND) { |
59 | 83.6k | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
60 | 78.2k | return FD->isExternC(); |
61 | 5.36k | if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) |
62 | 5.35k | return VD->isExternC(); |
63 | 9 | return false; |
64 | 9 | } |
65 | | |
66 | | static CCMangling getCallingConvMangling(const ASTContext &Context, |
67 | 5.12M | const NamedDecl *ND) { |
68 | 5.12M | const TargetInfo &TI = Context.getTargetInfo(); |
69 | 5.12M | const llvm::Triple &Triple = TI.getTriple(); |
70 | | |
71 | | // On wasm, the argc/argv form of "main" is renamed so that the startup code |
72 | | // can call it with the correct function signature. |
73 | | // On Emscripten, users may be exporting "main" and expecting to call it |
74 | | // themselves, so we can't mangle it. |
75 | 5.12M | if (Triple.isWasm() && !Triple.isOSEmscripten()1.26k ) |
76 | 1.24k | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
77 | 1.15k | if (FD->isMain() && FD->hasPrototype()5 && FD->param_size() == 25 ) |
78 | 4 | return CCM_WasmMainArgcArgv; |
79 | | |
80 | 5.12M | if (!Triple.isOSWindows() || !Triple.isX86()136k ) |
81 | 4.98M | return CCM_Other; |
82 | | |
83 | 134k | if (Context.getLangOpts().CPlusPlus && !isExternC(ND)83.6k && |
84 | 83.4k | TI.getCXXABI() == TargetCXXABI::Microsoft) |
85 | 73.6k | return CCM_Other; |
86 | | |
87 | 61.0k | const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
88 | 61.0k | if (!FD) |
89 | 992 | return CCM_Other; |
90 | 60.0k | QualType T = FD->getType(); |
91 | | |
92 | 60.0k | const FunctionType *FT = T->castAs<FunctionType>(); |
93 | | |
94 | 60.0k | CallingConv CC = FT->getCallConv(); |
95 | 60.0k | switch (CC) { |
96 | 59.7k | default: |
97 | 59.7k | return CCM_Other; |
98 | 87 | case CC_X86FastCall: |
99 | 87 | return CCM_Fast; |
100 | 51 | case CC_X86StdCall: |
101 | 51 | return CCM_Std; |
102 | 187 | case CC_X86VectorCall: |
103 | 187 | return CCM_Vector; |
104 | 60.0k | } |
105 | 60.0k | } |
106 | | |
107 | 3.91M | bool MangleContext::shouldMangleDeclName(const NamedDecl *D) { |
108 | 3.91M | const ASTContext &ASTContext = getASTContext(); |
109 | | |
110 | 3.91M | CCMangling CC = getCallingConvMangling(ASTContext, D); |
111 | 3.91M | if (CC != CCM_Other) |
112 | 178 | return true; |
113 | | |
114 | | // If the declaration has an owning module for linkage purposes that needs to |
115 | | // be mangled, we must mangle its name. |
116 | 3.91M | if (!D->hasExternalFormalLinkage() && D->getOwningModuleForLinkage()2.00M ) |
117 | 54 | return true; |
118 | | |
119 | | // In C, functions with no attributes never need to be mangled. Fastpath them. |
120 | 3.91M | if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs()1.85M ) |
121 | 113k | return false; |
122 | | |
123 | | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
124 | | // over all other naming in the .o file. |
125 | 3.80M | if (D->hasAttr<AsmLabelAttr>()) |
126 | 8.98k | return true; |
127 | | |
128 | | // Declarations that don't have identifier names always need to be mangled. |
129 | 3.79M | if (isa<MSGuidDecl>(D)) |
130 | 32 | return true; |
131 | | |
132 | 3.79M | return shouldMangleCXXName(D); |
133 | 3.79M | } |
134 | | |
135 | 1.21M | void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) { |
136 | 1.21M | const NamedDecl *D = cast<NamedDecl>(GD.getDecl()); |
137 | | // Any decl can be declared with __asm("foo") on it, and this takes precedence |
138 | | // over all other naming in the .o file. |
139 | 1.21M | if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { |
140 | | // If we have an asm name, then we use it as the mangling. |
141 | | |
142 | | // If the label isn't literal, or if this is an alias for an LLVM intrinsic, |
143 | | // do not add a "\01" prefix. |
144 | 9.01k | if (!ALA->getIsLiteralLabel() || ALA->getLabel().startswith("llvm.")8.70k ) { |
145 | 319 | Out << ALA->getLabel(); |
146 | 319 | return; |
147 | 319 | } |
148 | | |
149 | | // Adding the prefix can cause problems when one file has a "foo" and |
150 | | // another has a "\01foo". That is known to happen on ELF with the |
151 | | // tricks normally used for producing aliases (PR9177). Fortunately the |
152 | | // llvm mangler on ELF is a nop, so we can just avoid adding the \01 |
153 | | // marker. |
154 | 8.69k | char GlobalPrefix = |
155 | 8.69k | getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix(); |
156 | 8.69k | if (GlobalPrefix) |
157 | 8.66k | Out << '\01'; // LLVM IR Marker for __asm("foo") |
158 | | |
159 | 8.69k | Out << ALA->getLabel(); |
160 | 8.69k | return; |
161 | 8.69k | } |
162 | | |
163 | 1.20M | if (auto *GD = dyn_cast<MSGuidDecl>(D)) |
164 | 32 | return mangleMSGuidDecl(GD, Out); |
165 | | |
166 | 1.20M | const ASTContext &ASTContext = getASTContext(); |
167 | 1.20M | CCMangling CC = getCallingConvMangling(ASTContext, D); |
168 | | |
169 | 1.20M | if (CC == CCM_WasmMainArgcArgv) { |
170 | 2 | Out << "__main_argc_argv"; |
171 | 2 | return; |
172 | 2 | } |
173 | | |
174 | 1.20M | bool MCXX = shouldMangleCXXName(D); |
175 | 1.20M | const TargetInfo &TI = Context.getTargetInfo(); |
176 | 1.20M | if (CC == CCM_Other || (149 MCXX149 && TI.getCXXABI() == TargetCXXABI::Microsoft27 )) { |
177 | 1.20M | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) |
178 | 0 | mangleObjCMethodNameAsSourceName(OMD, Out); |
179 | 1.20M | else |
180 | 1.20M | mangleCXXName(GD, Out); |
181 | 1.20M | return; |
182 | 1.20M | } |
183 | | |
184 | 148 | Out << '\01'; |
185 | 148 | if (CC == CCM_Std) |
186 | 20 | Out << '_'; |
187 | 128 | else if (CC == CCM_Fast) |
188 | 38 | Out << '@'; |
189 | 90 | else if (CC == CCM_RegCall) |
190 | 0 | Out << "__regcall3__"; |
191 | | |
192 | 148 | if (!MCXX) |
193 | 122 | Out << D->getIdentifier()->getName(); |
194 | 26 | else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) |
195 | 0 | mangleObjCMethodNameAsSourceName(OMD, Out); |
196 | 26 | else |
197 | 26 | mangleCXXName(GD, Out); |
198 | | |
199 | 148 | const FunctionDecl *FD = cast<FunctionDecl>(D); |
200 | 148 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
201 | 148 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT); |
202 | 148 | if (CC == CCM_Vector) |
203 | 90 | Out << '@'; |
204 | 148 | Out << '@'; |
205 | 148 | if (!Proto) { |
206 | 5 | Out << '0'; |
207 | 5 | return; |
208 | 5 | } |
209 | 143 | assert(!Proto->isVariadic()); |
210 | 143 | unsigned ArgWords = 0; |
211 | 143 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
212 | 4 | if (!MD->isStatic()) |
213 | 3 | ++ArgWords; |
214 | 143 | for (const auto &AT : Proto->param_types()) |
215 | | // Size should be aligned to pointer size. |
216 | 250 | ArgWords += |
217 | 250 | llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) / |
218 | 250 | TI.getPointerWidth(0); |
219 | 143 | Out << ((TI.getPointerWidth(0) / 8) * ArgWords); |
220 | 143 | } |
221 | | |
222 | 64 | void MangleContext::mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &Out) { |
223 | | // For now, follow the MSVC naming convention for GUID objects on all |
224 | | // targets. |
225 | 64 | MSGuidDecl::Parts P = GD->getParts(); |
226 | 64 | Out << llvm::format("_GUID_%08" PRIx32 "_%04" PRIx32 "_%04" PRIx32 "_", |
227 | 64 | P.Part1, P.Part2, P.Part3); |
228 | 64 | unsigned I = 0; |
229 | 512 | for (uint8_t C : P.Part4And5) { |
230 | 512 | Out << llvm::format("%02" PRIx8, C); |
231 | 512 | if (++I == 2) |
232 | 64 | Out << "_"; |
233 | 512 | } |
234 | 64 | } |
235 | | |
236 | | void MangleContext::mangleGlobalBlock(const BlockDecl *BD, |
237 | | const NamedDecl *ID, |
238 | 107 | raw_ostream &Out) { |
239 | 107 | unsigned discriminator = getBlockId(BD, false); |
240 | 107 | if (ID) { |
241 | 77 | if (shouldMangleDeclName(ID)) |
242 | 12 | mangleName(ID, Out); |
243 | 65 | else { |
244 | 65 | Out << ID->getIdentifier()->getName(); |
245 | 65 | } |
246 | 77 | } |
247 | 107 | if (discriminator == 0) |
248 | 74 | Out << "_block_invoke"; |
249 | 33 | else |
250 | 33 | Out << "_block_invoke_" << discriminator+1; |
251 | 107 | } |
252 | | |
253 | | void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD, |
254 | | CXXCtorType CT, const BlockDecl *BD, |
255 | 32 | raw_ostream &ResStream) { |
256 | 32 | SmallString<64> Buffer; |
257 | 32 | llvm::raw_svector_ostream Out(Buffer); |
258 | 32 | mangleName(GlobalDecl(CD, CT), Out); |
259 | 32 | mangleFunctionBlock(*this, Buffer, BD, ResStream); |
260 | 32 | } |
261 | | |
262 | | void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD, |
263 | | CXXDtorType DT, const BlockDecl *BD, |
264 | 10 | raw_ostream &ResStream) { |
265 | 10 | SmallString<64> Buffer; |
266 | 10 | llvm::raw_svector_ostream Out(Buffer); |
267 | 10 | mangleName(GlobalDecl(DD, DT), Out); |
268 | 10 | mangleFunctionBlock(*this, Buffer, BD, ResStream); |
269 | 10 | } |
270 | | |
271 | | void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD, |
272 | 906 | raw_ostream &Out) { |
273 | 906 | assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC)); |
274 | | |
275 | 906 | SmallString<64> Buffer; |
276 | 906 | llvm::raw_svector_ostream Stream(Buffer); |
277 | 906 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) { |
278 | 38 | mangleObjCMethodNameAsSourceName(Method, Stream); |
279 | 868 | } else { |
280 | 868 | assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) && |
281 | 868 | "expected a NamedDecl or BlockDecl"); |
282 | 868 | if (isa<BlockDecl>(DC)) |
283 | 0 | for (; DC && isa<BlockDecl>(DC); DC = DC->getParent()) |
284 | 0 | (void) getBlockId(cast<BlockDecl>(DC), true); |
285 | 868 | assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) && |
286 | 868 | "expected a TranslationUnitDecl or a NamedDecl"); |
287 | 868 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC)) |
288 | 0 | mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out); |
289 | 868 | else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC)) |
290 | 0 | mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out); |
291 | 868 | else if (auto ND = dyn_cast<NamedDecl>(DC)) { |
292 | 868 | if (!shouldMangleDeclName(ND) && ND->getIdentifier()659 ) |
293 | 659 | Stream << ND->getIdentifier()->getName(); |
294 | 209 | else { |
295 | | // FIXME: We were doing a mangleUnqualifiedName() before, but that's |
296 | | // a private member of a class that will soon itself be private to the |
297 | | // Itanium C++ ABI object. What should we do now? Right now, I'm just |
298 | | // calling the mangleName() method on the MangleContext; is there a |
299 | | // better way? |
300 | 209 | mangleName(ND, Stream); |
301 | 209 | } |
302 | 868 | } |
303 | 868 | } |
304 | 906 | mangleFunctionBlock(*this, Buffer, BD, Out); |
305 | 906 | } |
306 | | |
307 | | void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD, |
308 | | raw_ostream &OS, |
309 | | bool includePrefixByte, |
310 | 3.14k | bool includeCategoryNamespace) { |
311 | 3.14k | if (getASTContext().getLangOpts().ObjCRuntime.isGNUFamily()) { |
312 | | // This is the mangling we've always used on the GNU runtimes, but it |
313 | | // has obvious collisions in the face of underscores within class |
314 | | // names, category names, and selectors; maybe we should improve it. |
315 | | |
316 | 76 | OS << (MD->isClassMethod() ? "_c_"8 : "_i_") |
317 | 84 | << MD->getClassInterface()->getName() << '_'; |
318 | | |
319 | 84 | if (includeCategoryNamespace) { |
320 | 84 | if (auto category = MD->getCategory()) |
321 | 2 | OS << category->getName(); |
322 | 84 | } |
323 | 84 | OS << '_'; |
324 | | |
325 | 84 | auto selector = MD->getSelector(); |
326 | 84 | for (unsigned slotIndex = 0, |
327 | 84 | numArgs = selector.getNumArgs(), |
328 | 84 | slotEnd = std::max(numArgs, 1U); |
329 | 170 | slotIndex != slotEnd; ++slotIndex86 ) { |
330 | 86 | if (auto name = selector.getIdentifierInfoForSlot(slotIndex)) |
331 | 86 | OS << name->getName(); |
332 | | |
333 | | // Replace all the positions that would've been ':' with '_'. |
334 | | // That's after each slot except that a unary selector doesn't |
335 | | // end in ':'. |
336 | 86 | if (numArgs) |
337 | 34 | OS << '_'; |
338 | 86 | } |
339 | | |
340 | 84 | return; |
341 | 84 | } |
342 | | |
343 | | // \01+[ContainerName(CategoryName) SelectorName] |
344 | 3.06k | if (includePrefixByte) { |
345 | 2.56k | OS << '\01'; |
346 | 2.56k | } |
347 | 2.78k | OS << (MD->isInstanceMethod() ? '-' : '+'278 ) << '['; |
348 | 3.06k | if (const auto *CID = MD->getCategory()) { |
349 | 422 | OS << CID->getClassInterface()->getName(); |
350 | 422 | if (includeCategoryNamespace) { |
351 | 417 | OS << '(' << *CID << ')'; |
352 | 417 | } |
353 | 2.64k | } else if (const auto *CD = |
354 | 2.64k | dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) { |
355 | 2.64k | OS << CD->getName(); |
356 | 0 | } else { |
357 | 0 | llvm_unreachable("Unexpected ObjC method decl context"); |
358 | 0 | } |
359 | 3.06k | OS << ' '; |
360 | 3.06k | MD->getSelector().print(OS); |
361 | 3.06k | OS << ']'; |
362 | 3.06k | } |
363 | | |
364 | | void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD, |
365 | 302 | raw_ostream &Out) { |
366 | 302 | SmallString<64> Name; |
367 | 302 | llvm::raw_svector_ostream OS(Name); |
368 | | |
369 | 302 | mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false, |
370 | 302 | /*includeCategoryNamespace=*/true); |
371 | 302 | Out << OS.str().size() << OS.str(); |
372 | 302 | } |
373 | | |
374 | | class ASTNameGenerator::Implementation { |
375 | | std::unique_ptr<MangleContext> MC; |
376 | | llvm::DataLayout DL; |
377 | | |
378 | | public: |
379 | | explicit Implementation(ASTContext &Ctx) |
380 | 393 | : MC(Ctx.createMangleContext()), DL(Ctx.getTargetInfo().getDataLayout()) { |
381 | 393 | } |
382 | | |
383 | 2.69k | bool writeName(const Decl *D, raw_ostream &OS) { |
384 | | // First apply frontend mangling. |
385 | 2.69k | SmallString<128> FrontendBuf; |
386 | 2.69k | llvm::raw_svector_ostream FrontendBufOS(FrontendBuf); |
387 | 2.69k | if (auto *FD = dyn_cast<FunctionDecl>(D)) { |
388 | 610 | if (FD->isDependentContext()) |
389 | 113 | return true; |
390 | 497 | if (writeFuncOrVarName(FD, FrontendBufOS)) |
391 | 0 | return true; |
392 | 2.08k | } else if (auto *VD = dyn_cast<VarDecl>(D)) { |
393 | 421 | if (writeFuncOrVarName(VD, FrontendBufOS)) |
394 | 0 | return true; |
395 | 1.66k | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
396 | 199 | MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false, |
397 | 199 | /*includeCategoryNamespace=*/true); |
398 | 199 | return false; |
399 | 1.46k | } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
400 | 117 | writeObjCClassName(ID, FrontendBufOS); |
401 | 1.35k | } else { |
402 | 1.35k | return true; |
403 | 1.35k | } |
404 | | |
405 | | // Now apply backend mangling. |
406 | 1.03k | llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL); |
407 | 1.03k | return false; |
408 | 1.03k | } |
409 | | |
410 | 1.33k | std::string getName(const Decl *D) { |
411 | 1.33k | std::string Name; |
412 | 1.33k | { |
413 | 1.33k | llvm::raw_string_ostream OS(Name); |
414 | 1.33k | writeName(D, OS); |
415 | 1.33k | } |
416 | 1.33k | return Name; |
417 | 1.33k | } |
418 | | |
419 | | enum ObjCKind { |
420 | | ObjCClass, |
421 | | ObjCMetaclass, |
422 | | }; |
423 | | |
424 | | static StringRef getClassSymbolPrefix(ObjCKind Kind, |
425 | 125 | const ASTContext &Context) { |
426 | 125 | if (Context.getLangOpts().ObjCRuntime.isGNUFamily()) |
427 | 4 | return Kind == ObjCMetaclass ? "_OBJC_METACLASS_"2 : "_OBJC_CLASS_"2 ; |
428 | 121 | return Kind == ObjCMetaclass ? "OBJC_METACLASS_$_"2 : "OBJC_CLASS_$_"119 ; |
429 | 121 | } |
430 | | |
431 | 4 | std::vector<std::string> getAllManglings(const ObjCContainerDecl *OCD) { |
432 | 4 | StringRef ClassName; |
433 | 4 | if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) |
434 | 2 | ClassName = OID->getObjCRuntimeNameAsString(); |
435 | 2 | else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD)) |
436 | 2 | ClassName = OID->getObjCRuntimeNameAsString(); |
437 | | |
438 | 4 | if (ClassName.empty()) |
439 | 0 | return {}; |
440 | | |
441 | 8 | auto Mangle = [&](ObjCKind Kind, StringRef ClassName) -> std::string 4 { |
442 | 8 | SmallString<40> Mangled; |
443 | 8 | auto Prefix = getClassSymbolPrefix(Kind, OCD->getASTContext()); |
444 | 8 | llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL); |
445 | 8 | return std::string(Mangled.str()); |
446 | 8 | }; |
447 | | |
448 | 4 | return { |
449 | 4 | Mangle(ObjCClass, ClassName), |
450 | 4 | Mangle(ObjCMetaclass, ClassName), |
451 | 4 | }; |
452 | 4 | } |
453 | | |
454 | 193 | std::vector<std::string> getAllManglings(const Decl *D) { |
455 | 193 | if (const auto *OCD = dyn_cast<ObjCContainerDecl>(D)) |
456 | 4 | return getAllManglings(OCD); |
457 | | |
458 | 189 | if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)168 )) |
459 | 105 | return {}; |
460 | | |
461 | 84 | const NamedDecl *ND = cast<NamedDecl>(D); |
462 | | |
463 | 84 | ASTContext &Ctx = ND->getASTContext(); |
464 | 84 | std::unique_ptr<MangleContext> M(Ctx.createMangleContext()); |
465 | | |
466 | 84 | std::vector<std::string> Manglings; |
467 | | |
468 | 1 | auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) { |
469 | 1 | auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false, |
470 | 1 | /*IsCXXMethod=*/true); |
471 | 1 | auto CC = MD->getType()->castAs<FunctionProtoType>()->getCallConv(); |
472 | 1 | return CC == DefaultCC; |
473 | 1 | }; |
474 | | |
475 | 84 | if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) { |
476 | 22 | Manglings.emplace_back(getMangledStructor(CD, Ctor_Base)); |
477 | | |
478 | 22 | if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) |
479 | 18 | if (!CD->getParent()->isAbstract()) |
480 | 16 | Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete)); |
481 | | |
482 | 22 | if (Ctx.getTargetInfo().getCXXABI().isMicrosoft()) |
483 | 4 | if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor()1 ) |
484 | 1 | if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0)) |
485 | 1 | Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure)); |
486 | 62 | } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) { |
487 | 15 | Manglings.emplace_back(getMangledStructor(DD, Dtor_Base)); |
488 | 15 | if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) { |
489 | 12 | Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete)); |
490 | 12 | if (DD->isVirtual()) |
491 | 10 | Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting)); |
492 | 12 | } |
493 | 47 | } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) { |
494 | 26 | Manglings.emplace_back(getName(ND)); |
495 | 26 | if (MD->isVirtual()) |
496 | 20 | if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD)) |
497 | 3 | for (const auto &T : *TIV) |
498 | 3 | Manglings.emplace_back(getMangledThunk(MD, T)); |
499 | 26 | } |
500 | | |
501 | 84 | return Manglings; |
502 | 84 | } |
503 | | |
504 | | private: |
505 | 918 | bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) { |
506 | 918 | if (MC->shouldMangleDeclName(D)) { |
507 | 504 | GlobalDecl GD; |
508 | 504 | if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D)) |
509 | 44 | GD = GlobalDecl(CtorD, Ctor_Complete); |
510 | 460 | else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D)) |
511 | 26 | GD = GlobalDecl(DtorD, Dtor_Complete); |
512 | 434 | else if (D->hasAttr<CUDAGlobalAttr>()) |
513 | 0 | GD = GlobalDecl(cast<FunctionDecl>(D)); |
514 | 434 | else |
515 | 434 | GD = GlobalDecl(D); |
516 | 504 | MC->mangleName(GD, OS); |
517 | 504 | return false; |
518 | 414 | } else { |
519 | 414 | IdentifierInfo *II = D->getIdentifier(); |
520 | 414 | if (!II) |
521 | 0 | return true; |
522 | 414 | OS << II->getName(); |
523 | 414 | return false; |
524 | 414 | } |
525 | 918 | } |
526 | | |
527 | 117 | void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) { |
528 | 117 | OS << getClassSymbolPrefix(ObjCClass, D->getASTContext()); |
529 | 117 | OS << D->getObjCRuntimeNameAsString(); |
530 | 117 | } |
531 | | |
532 | 76 | std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) { |
533 | 76 | std::string FrontendBuf; |
534 | 76 | llvm::raw_string_ostream FOS(FrontendBuf); |
535 | | |
536 | 76 | GlobalDecl GD; |
537 | 76 | if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) |
538 | 39 | GD = GlobalDecl(CD, static_cast<CXXCtorType>(StructorType)); |
539 | 37 | else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) |
540 | 37 | GD = GlobalDecl(DD, static_cast<CXXDtorType>(StructorType)); |
541 | 76 | MC->mangleName(GD, FOS); |
542 | | |
543 | 76 | std::string BackendBuf; |
544 | 76 | llvm::raw_string_ostream BOS(BackendBuf); |
545 | | |
546 | 76 | llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL); |
547 | | |
548 | 76 | return BOS.str(); |
549 | 76 | } |
550 | | |
551 | 3 | std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) { |
552 | 3 | std::string FrontendBuf; |
553 | 3 | llvm::raw_string_ostream FOS(FrontendBuf); |
554 | | |
555 | 3 | MC->mangleThunk(MD, T, FOS); |
556 | | |
557 | 3 | std::string BackendBuf; |
558 | 3 | llvm::raw_string_ostream BOS(BackendBuf); |
559 | | |
560 | 3 | llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL); |
561 | | |
562 | 3 | return BOS.str(); |
563 | 3 | } |
564 | | }; |
565 | | |
566 | | ASTNameGenerator::ASTNameGenerator(ASTContext &Ctx) |
567 | 393 | : Impl(std::make_unique<Implementation>(Ctx)) {} |
568 | | |
569 | 393 | ASTNameGenerator::~ASTNameGenerator() {} |
570 | | |
571 | 1.36k | bool ASTNameGenerator::writeName(const Decl *D, raw_ostream &OS) { |
572 | 1.36k | return Impl->writeName(D, OS); |
573 | 1.36k | } |
574 | | |
575 | 1.30k | std::string ASTNameGenerator::getName(const Decl *D) { |
576 | 1.30k | return Impl->getName(D); |
577 | 1.30k | } |
578 | | |
579 | 193 | std::vector<std::string> ASTNameGenerator::getAllManglings(const Decl *D) { |
580 | 193 | return Impl->getAllManglings(D); |
581 | 193 | } |