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