Skip to content

Commit 75cc1cf

Browse files
committed
[Demangle][Rust] Parse function signatures
Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D102581
1 parent e4fa6c9 commit 75cc1cf

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

llvm/include/llvm/Demangle/RustDemangle.h

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class Demangler {
8888
void demangleImplPath(InType InType);
8989
void demangleGenericArg();
9090
void demangleType();
91+
void demangleFnSig();
9192
void demangleConst();
9293
void demangleConstInt();
9394
void demangleConstBool();

llvm/lib/Demangle/RustDemangle.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,57 @@ void Demangler::demangleType() {
472472
print("*mut ");
473473
demangleType();
474474
break;
475+
case 'F':
476+
demangleFnSig();
477+
break;
475478
default:
476479
Position = Start;
477480
demanglePath(rust_demangle::InType::Yes);
478481
break;
479482
}
480483
}
481484

485+
// <fn-sig> := [<binder>] ["U"] ["K" <abi>] {<type>} "E" <type>
486+
// <abi> = "C"
487+
// | <undisambiguated-identifier>
488+
void Demangler::demangleFnSig() {
489+
// FIXME demangle binder.
490+
491+
if (consumeIf('U'))
492+
print("unsafe ");
493+
494+
if (consumeIf('K')) {
495+
print("extern \"");
496+
if (consumeIf('C')) {
497+
print("C");
498+
} else {
499+
Identifier Ident = parseIdentifier();
500+
for (char C : Ident.Name) {
501+
// When mangling ABI string, the "-" is replaced with "_".
502+
if (C == '_')
503+
C = '-';
504+
print(C);
505+
}
506+
}
507+
print("\" ");
508+
}
509+
510+
print("fn(");
511+
for (size_t I = 0; !Error && !consumeIf('E'); ++I) {
512+
if (I > 0)
513+
print(", ");
514+
demangleType();
515+
}
516+
print(")");
517+
518+
if (consumeIf('u')) {
519+
// Skip the unit type from the output.
520+
} else {
521+
print(" -> ");
522+
demangleType();
523+
}
524+
}
525+
482526
// <const> = <basic-type> <const-data>
483527
// | "p" // placeholder
484528
// | <backref>

llvm/test/Demangle/rust.test

+29
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,35 @@ CHECK: types::<*const _>
187187
CHECK: types::<*mut _>
188188
_RIC5typesOpE
189189

190+
; Function signatures
191+
192+
CHECK: function::<fn()>
193+
_RIC8functionFEuE
194+
195+
CHECK: function::<fn() -> _>
196+
_RIC8functionFEpE
197+
198+
CHECK: function::<fn(_)>
199+
_RIC8functionFpEuE
200+
201+
CHECK: function::<fn(_, _)>
202+
_RIC8functionFppEuE
203+
204+
CHECK: function::<fn(_, _, _)>
205+
_RIC8functionFpppEuE
206+
207+
CHECK: function::<unsafe fn()>
208+
_RIC8functionFUEuE
209+
210+
CHECK: function::<extern "C" fn()>
211+
_RIC8functionFKCEuE
212+
213+
CHECK: function::<extern "cdecl" fn()>
214+
_RIC8functionFK5cdeclEuE
215+
216+
CHECK: function::<unsafe extern "C-cmse-nonsecure-call" fn()>
217+
_RIC8functionFUK21C_cmse_nonsecure_callEuE
218+
190219
; Integer constants. Test value demangling.
191220

192221
CHECK: integer::<0>

0 commit comments

Comments
 (0)