Core¶
The scheme-agnostic half. A new mangling scheme is written against these and nothing else; Architecture has the layering rules.
The builder protocol¶
The contract between a parser and its output.
Every parser in this package is written against Builder and never constructs its own
strings or nodes. It recognises a grammar production and reports it – builder.pointer(
inner), builder.template(base, args) – leaving what that becomes to the builder.
The reason is that the two things callers want from a demangler have opposite cost profiles. Labelling every symbol in a binary wants a string and nothing else, several hundred thousand times. Inspecting one symbol's template arguments wants a tree. Writing the parser twice to serve both would guarantee the two copies disagree, and the ways they would disagree are exactly the subtle ones – declarator placement, substitution numbering – that are hardest to notice.
So: one parser, two builders (SpellingBuilder, AstBuilder), and a third is a class
rather than a fork.
Values passed between these methods are handles. A parser may hold one, pass it back
in, and ask the builder to spell it, but must never inspect it – a handle is a
Spelling under one builder and a Node under another. spell() exists for the few
places where a grammar genuinely requires the text of something already built (naming
the class in Foo::Foo, for instance), and is the only legal way to look inside.
Handle
module-attribute
¶
Handle = Any
An opaque builder product. Its concrete type is the builder's business.
Builder
¶
Bases: Protocol
What a parser may ask for. Implementations choose what to produce.
name
¶
name(text: str) -> Handle
A single identifier, already decoded from its length-prefixed form.
raw
¶
raw(text: str) -> Handle
Pre-rendered text with no further structure.
The escape hatch for productions whose spelling a scheme fixes outright, such
as std::nullptr_t. Reach for a specific method first: text passed here is
invisible to structured consumers.
literal
¶
A constant appearing in a type, such as a non-type template argument.
expression
¶
A constant expression: sizeof (T), a + b, x ? y : z.
parts interleaves the fixed text of the production with the handles of its
operands, in output order, so a builder that wants text concatenates and one
that wants a tree keeps the boundaries. form names the shape – binary,
call, sizeof, conditional – so a consumer can ask what an expression is
without matching on its spelling.
One method rather than one per operator, because the alternative is fifteen
methods that every future builder has to implement and that still would not
cover the next operator someone mangles. The parser already owns operator
spelling: it comes from the tables in tables.py, which exist to be checked
against the ABI. What the builder decides is structure.
Brackets are a part like any other. Whether an operand needs them is a question
about precedence that only the parser can answer, so it arrives here already
settled, as "(" and ")" around the operand it applies to.
qualified
¶
A scoped name: the parts of a::b::c, outermost first.
template
¶
A template specialisation, base<arguments...>.
qualify
¶
Apply cv-qualifiers, in the canonical order the caller has already imposed.
member_pointer
¶
inner Owner::* – a pointer to member.
array
¶
inner [dimension]. An empty dimension means an unbounded array.
function
¶
function(
returns: Handle | None,
parameters: Sequence[Handle],
suffix: str = "",
name: Handle | None = None,
) -> Handle
A function type or a whole function declaration.
returns is None where the scheme encodes no return type – constructors,
destructors, conversion operators, and plain non-template functions under the
Itanium ABI. suffix carries what trails the parameter list and binds to the
function rather than the return type: cv-qualifiers, a ref-qualifier,
noexcept. name is the declarator, absent for a bare function type.
parameter_pack
¶
A pack of concrete arguments.
Must stay a sequence rather than collapse to joined text: a declarator applied
to a pack applies to every member, so Dp O T_ over three arguments is three
rvalue references.
vendor_qualify
¶
A vendor extended qualifier, spelled after the type it applies to.
special
¶
A symbol that is about an entity: vtable for Foo, typeinfo for Bar.
decorated
¶
A symbol carrying a symbol-table decoration: an ELF version, a clone suffix.
Kept as structure rather than folded into text so a caller can ask which copy of a function a symbol names, or strip the version and compare.
spell
¶
spell(handle: Handle, declarator: str = '') -> str
Render a handle, placing declarator in the type's declarator position.
size
¶
size(handle: Handle) -> int
How many characters handle would render to, without rendering it.
Must be O(1). This is what lets a parser enforce an output bound while
building: the substitution scheme allows each component to be assembled from two
copies of an earlier one, so output can double every few input bytes. Checking
the bound by measuring spell() would mean materialising the very string the
bound exists to prevent – a few hundred bytes of input can reach gigabytes.
Spelling¶
The fast builder: C++ declaration syntax, built directly as text.
C++ does not write a type before the name, it writes it around the name. The type
"pointer to function taking char and returning int" is spelled int (*)(char), and
giving it a name puts that name in a hole in the middle: int (*f)(char). Arrays do
the same: int a[10], and a pointer to one is int (*)[10].
So a partially built type is a pair of strings, left and right, and rendering is
left + declarator + right. Every function here exists to keep that hole in the right
place as declarators nest. It is the part of a demangler most often subtly wrong, which
is why it is one small module with its own tests rather than logic spread through three
parsers.
Spellings are immutable. Nothing here mutates a handle it was given, so a builder product can be stored in a substitution table and reused without defensive copying – which matters, because that is exactly what the Itanium back-reference scheme does.
Spelling
¶
Spelling(left, right='')
One type or name, split around its declarator position.
Only the two halves are stored. What else a declarator needs to know is a class attribute here and is overridden by the subclasses below, which is what lets the commonest construction in a parse, a plain name, cost two stores rather than six.
spell
¶
spell(declarator='')
The type with declarator – a name – written where a declarator goes.
Both references space the name off the type unless the type has a right part
to put after it, which is what NonTypeTemplateParamDecl::printLeft asks
hasRHSComponent in LLVM's ItaniumDemangle.h: int* $N and int A::* $N
against int$N [3] and int (*$N) [3]. A function type's left half already
ends in a space, so int $N() comes out with exactly one either way. Verified
against both references over every declarator shape; int*$N, which is what a
rule about the last character gives, is what neither writes.
SpellingBuilder
¶
SpellingBuilder(
legacy_angle_spacing=False,
gnu_clone_suffix=False,
collapse_duplicate_qualifiers=False,
tight_after_star=False,
tight_member_array=True,
)
Bases: Builder
Builds C++ declaration text. The backend behind demangle().
Stateless, so one instance is shared by every parse rather than allocated per name.
qualify
¶
qualify(inner, qualifiers, cv=True)
Apply qualifiers to inner, on the right, the way C++ writes them.
cv says whether these are cv-qualifiers, which is what decides whether a
repeat collapses. _Complex and _Imaginary go through here too and are not
cv-qualifiers: [basic.type.qualifier] folds a duplicate const, and nothing
folds a duplicate _Imaginary. c++filt 2.42 writes signed char _Imaginary
_Imaginary for _Z1fGGa; collapsing it to one would lose a word of the name,
and only the gnu style collapses at all.
pack
¶
pack(inner)
An unexpanded pack expansion: the whole type, then ....
The reference's ParameterPackExpansion prints its child – both halves – and
appends the ellipsis, so a declarator type keeps its shape and the dots follow
it: void ()..., void (*)()..., int [3].... Putting them in the left half
alone would put them where a declarator's name goes, void (*...)() and
int... [3], which is not what any reference prints. A type with no right half
is unchanged.
vendor_qualify
¶
vendor_qualify(inner, qualifier)
A vendor qualifier goes after the whole type, declarator and all.
The reference prints the type and then the extension, so a function type comes
out void () block_pointer – not void block_pointer(), which is what putting
the word in the left half alone gives for anything that has a right half.
Distributes over a pack like every other declarator: an empty pack gives nothing
rather than a bare enable_if, a qualifier on nothing.
pack_of
¶
pack_of(members)
A parameter pack, spelled as its members and still addressable as a sequence.
Nested packs are spliced. A pack whose one member is an expansion of an empty pack
is empty, not a pack of one empty thing – otherwise AnalysisManager<T_, J Dp T0_ E>
with T0_ bound to nothing prints the separator for an argument that is not there.
The tree¶
The node classes a tree is made of, and the builder a scheme hands them to. Every class
declares __match_args__, so a caller can match on the shape of a subtree rather than
compare node.kind against a string:
import demangle
from demangle.core.ast import Builtin, Pointer
match demangle.parse("_Z1fPi").parameters[0]:
case Pointer(Builtin(spelling)):
print("pointer to", spelling)
A scheme's own node kinds are under its section on the schemes page, and
demangle.node_kinds() lists the kind strings a scheme can produce.
The structured builder: a tree a caller can walk.
demangle() answers "what does this name say". parse() answers "what are its parts" –
which namespace, which template arguments, how many parameters and of what types. Tools
that rename functions, group symbols by library, or match call signatures need the
second, and recovering it by pattern-matching the string is not workable: C++
declaration syntax nests, and nesting is what regular expressions cannot follow.
Every node re-emits itself into any builder through build(). That is what keeps this
module free of rendering logic: turning a tree back into text drives SpellingBuilder
over it, so there is exactly one implementation of declarator placement in the package
and a tree can never disagree with the fast path. It also means a new output format –
JSON, HTML, a token stream – is a builder, not a second traversal to keep in sync.
Node
¶
Base class for every parsed component.
__slots__ throughout: a single C++ symbol can be a few hundred nodes and a binary
a few hundred thousand symbols, so per-instance dictionaries are not affordable.
kind
class-attribute
¶
kind: str = 'node'
What the node is, as a string to switch on: "function", "pointer", "name".
demangle.node_kinds() lists every value a tree can hold.
children
¶
children() -> tuple[Any, ...]
Direct children, in source order. Leaves return an empty tuple.
to_dict
¶
to_dict() -> dict[str, Any]
The subtree as plain data – dicts, lists and strings – for json.dumps.
Every node becomes a dict with a kind and the fields that node kind has, so the
role of each child survives: a function has returns, parameters and
name, not three anonymous children. A field holding a node becomes a nested
dict and one holding a sequence of them becomes a list; anything else – a
spelling, a qualifier list, a flag – is carried as it is.
kind is the vocabulary to switch on, and demangle.node_kinds() is all of it.
A node reached more than once is written out once, with an id, and afterwards
as {"$ref": id}. This is not a size optimisation, it is the difference between
terminating and not: the structure is a graph, not a tree. Itanium's
substitutions make a component reachable from several places, and a Rust node
names its own children twice over – parts orders them and base and
arguments say what they are – so writing each occurrence out in full doubles
per level. A real symbol from the Rust toolchain costs seconds and hundreds
of megabytes that way, and there is no bound on how much worse it can get.
walk
¶
walk() -> Iterator[Any]
Yield this node and every descendant, depth first.
Iterative rather than recursive, and not for speed: a yield from per level
means one interpreter frame per level for the whole traversal, so a tree well
inside max_depth – which bounds the parse, in frames of a different size –
could still exhaust the stack here. An explicit stack cannot.
find
¶
find(kind: type[_N]) -> Iterator[_N]
find(kind: str) -> Iterator[Any]
find(kind: str | type[Node]) -> Iterator[Any]
Yield every descendant of the given kind, including this node.
kind is a kind string or a node class. A class also matches its subclasses,
and tells a type checker which fields the nodes have:
>>> import demangle
>>> from demangle.core.ast import Name
>>> [name.text for name in demangle.parse("_ZN3foo3barEv").find(Name)]
['foo', 'bar']
render
¶
render() -> str
The text of this subtree, under the default spelling policy.
spell() is the method to reach for – it takes the style the tree was parsed
under, which this cannot. A scheme whose spelling rules do not fit C++
declarator syntax carries its fragments as text and renders by concatenating
them, so render() is what its own nodes and its own tests use, and spell()
delegates to it.
It is defined on the base class so every node answers it: walking a tree and
asking each node for its text – the obvious thing to do with walk() – must
not fail partway through on a Decorated wrapper or an Itanium node. For a node
that carries pre-rendered text this is that text; for one spelled from structure
it is spell() with no declarator, which is the same string.
spell
¶
spell(
declarator: str = "", style: str | Style | None = None
) -> str
Render as declaration text.
style selects the spelling policy, and should be the style the tree was parsed
under: some choices – whether a std:: abbreviation is expanded, for instance –
are made during parsing and are already baked into the tree, so spelling a
gnu-parsed tree with LLVM's builder gives a mixture of the two.
Raw
¶
Raw(text: str)
Expression
¶
Expression(form: str, parts: Iterable[Node | str])
Bases: Node
A constant expression appearing in a type or a template argument.
parts interleaves fixed text with operand subtrees, in output order, exactly as
the parser reported them – so rendering concatenates and cannot spell the
expression differently from the text path. form says which shape it is: binary,
unary, call, conditional, sizeof, and so on.
Brackets, where the spelling needs them, are plain parts. Precedence is settled by the parser before it reports the production, because only the parser knows what the operand was; a consumer reading the tree sees the operands, not the punctuation.
Template
¶
Qualify
¶
Qualify(
inner: Node, qualifiers: Iterable[str], cv: bool = True
)
Bases: Node
Qualifiers applied to a type, written on the right.
cv says whether they are cv-qualifiers. _Complex and _Imaginary arrive here
too and are not, which matters only because a repeated cv-qualifier collapses under
the gnu style and a repeated _Imaginary does not. Carried on the node so that a
tree spells what the text path spelled.
MemberPointer
¶
Function
¶
Decorated
¶
Decorated(inner: Node, decoration: str)
Bases: Node
A symbol plus what the symbol table appended to it.
decoration keeps its separator: "@@GLIBCXX_3.4", ".cold". A tool that wants
the undecorated entity walks to inner; one that wants to know which clone or which
library version this is reads decoration.
AstBuilder
¶
AstBuilder(style=None)
Bases: Builder
Builds Node trees. The backend behind parse().
One instance serves every call. The only state is the leaf table below, which is a cache and does not make an answer depend on what was parsed before it.
Each method records on the node it builds an upper bound on that subtree's rendered
length, which is what makes size() constant time. Over-estimating is deliberate –
the number enforces max_output, where erring high is the safe direction – and it
is carried rather than measured because the tree is a graph with shared subtrees, and
walking it would be exponential in exactly the cases the bound exists to stop.
Leaves are interned; nothing else is. Measured over the 5,913 trees of the shipped
libstdc++: leaves are 55% of all nodes and repeat 49 times over on average – 15,654
builtin instances hold 32 distinct spellings, 8,807 raw instances hold 16.
Interning them costs nothing because a leaf is keyed by its text, so a lookup is
one string hash and no comparison of subtrees; it is 40% less memory over that corpus
and, because allocating fewer objects is less work than allocating more, about 6%
faster as well.
Composite nodes are not interned: a composite hashes by walking its children, so
building that table would cost several times the whole parse to save memory a caller
may not be short of.
Node.__eq__ still compares composites by value, so a caller wanting to deduplicate
a particular result can.
builder_for
¶
builder_for(style)
The tree builder for style.
A registered style is held by name, so one registered again under a name it already
had is picked up rather than remembered, and the common path – parse(name), whose
style is the string "llvm" – is a dictionary lookup.
A style object that is not the registered one under its name gets its own builder,
holding the object. Keyed by name it would be wrong in both directions: a composed
style like demangle.style("llvm", msvc={...}) keeps the name "llvm" and would
silently be served the registered style's builder, and a style whose name is not
registered at all would make parse() raise unknown style from inside the parser
while demangle() accepts the very same object. Building one per call matches the
bargain demangle() makes for a style object: it is not cached either.
setdefault rather than a lock: two threads arriving together each build one and
the loser's is discarded, which costs an allocation and cannot produce two builders
in use for one style.
Reading¶
A bounds-checked cursor over a mangled name.
Every parser in the package reads through this and never indexes the string directly.
That is not ceremony: the schemes are full of places where the next character decides
the production, and a bare text[pos] at the end of a truncated name raises
IndexError – which then has to be caught somewhere far away and translated into
something meaningful. Reading past the end here is simply "no character", and the
parser decides whether that is fatal.
Kept deliberately small and __slots__-ed: an instance exists per name demangled, and
its methods are the innermost loop in the package.
Reader
¶
Reader(text: str)
A cursor over text, with the lookahead and consumption parsers need.
length is the end of input and is not always len(text). A parser may shorten it
to bound a nested encoding it has found the end of by other means – the Itanium
scheme does this for ___Z<encoding>_block_invoke, where a regex says where the
encoding stops and the parser must not read the literal that follows. Every method
here answers against length rather than against the string, because a bound only
half the class honours is worse than none: if peek reported the end of input
while expect stepped past it, a truncated S would borrow the _ of
_block_invoke for its terminator and read as though it were whole.
peek
¶
peek() -> str
The character at the cursor, or "" past the end.
Returning a string rather than raising is what lets a parser write
if reader.peek() == "N" without first checking for the end of input.
A parser that has just had a non-empty character out of peek, with nothing
between the two, may consume it by advancing pos itself rather than calling
take: the bounds test take would repeat has already happened. Nothing weaker
licenses it. An intervening call may have moved the cursor, and an empty result
means there was no character to consume – either way pos += 1 would step past
the end, which is the one thing this class exists to prevent. The productions
that do it say so at the site.
It is called around fifty times per name demangled – more than any other method
in the package – and CPython charges for a default it then has to bind, so it
takes no argument: that is a fifth off the cost of the call. ahead is the
offset form.
ahead2
¶
ahead2(offset: int) -> str
The two characters offset past the cursor, bounded like peek2.
For the lookaheads that decide a branch without consuming anything – whether an
abbreviation is followed by a constructor, whether gs introduces an allocation.
Reaching into text directly is the end of input's one blind spot: a bound only
half the class honours is worse than none.
take_exactly
¶
take_exactly(count: int) -> str
Consume exactly count characters, or raise if they are not all there.
digits
¶
digits() -> str
Consume a run of decimal digits and return it, or raise if there are none.
Bounded: an unbounded run would reach int(), which CPython refuses above 4300
digits with a ValueError – not this package's error type, and so not something
a caller of parse() can be expected to catch.
length_prefixed
¶
length_prefixed() -> tuple[int, str]
A decimal length and the characters it counts, as one production.
<source-name> is the commonest production in these grammars – every component
of every qualified name is one – and reading it as int(digits()) followed by
take_exactly() costs three frames for what is one scan and one slice. Raises
the same way each of those does: no digits, a run past the bound, or a length
the name is too short for.
number
¶
number(allow_negative: bool = True) -> str
A <number>: an optional leading n meaning negative, then digits.
seq_id
¶
seq_id() -> int
A <seq-id> followed by _, returned as a zero-based index.
The encoding is offset by one so that the empty sequence can mean something:
S_ is entry 0 and S0_ is entry 1.
Plugins¶
A distribution can add a scheme of its own by calling register or by advertising a
demangle.languages entry point. Advertised plugins load only when a program calls
demangle.load_plugins() (the demangle command does); register always takes
effect. Adding a scheme is the walk-through.
The contract a mangling scheme implements.
A language is a LanguagePlugin. Nothing in core imports a language module at import
time, and no language imports another, so a scheme can be developed, tested, replaced or
shipped separately from everything else here.
Adding one means providing three things and registering them. It does not mean understanding the rest of the codebase, which is the point: the project should be approachable to a contributor who knows one ABI well and nothing else about it.
LanguagePlugin
dataclass
¶
LanguagePlugin(
name: str,
detect: Callable[[str], bool],
parse: Callable[..., Any],
parse_type: Callable[..., Any] | None = None,
node_kinds: tuple[str, ...] = (),
description: str = "",
aliases: Sequence[str] = tuple(),
options_type: type | None = None,
symbol_table_decorations: bool = False,
spells_what_it_reads: bool = False,
first_characters: str = "",
priority: int = 100,
)
One mangling scheme.
name
instance-attribute
¶
name: str
Stable identifier, used in the API and on the command line: itanium, msvc.
detect
instance-attribute
¶
detect: Callable[[str], bool]
Cheap test for "is this plausibly mine".
Called on every symbol a caller offers, including the great majority that are not mangled at all, so it must not allocate or parse. A prefix comparison is the expected shape. False negatives lose symbols; false positives only cost a failed parse, so err towards accepting.
The exception is a scheme whose names carry no marker, gnuv2, codewarrior and
ada among them. Its detect screens with a substring test and then parses the
whole name, and it must not err towards accepting: it is ordered after the
marked schemes, and what justifies it is a measured false-positive count of zero over
the other schemes' corpora and over real symbol tables.
parse
instance-attribute
¶
parse: Callable[..., Any]
parse(mangled, builder, limits=..., options=...) -> handle.
Raises NotMangledError or ParseError on failure; never returns a partial result.
parse_type
class-attribute
instance-attribute
¶
parse_type: Callable[..., Any] | None = None
parse_type(mangled, builder, limits=..., options=...) -> handle, or None.
A type encoding rather than a whole symbol – Pi for int*, an MSVC .PEAX, a
Swift type string. This is what typeinfo names, RTTI descriptors and vtable
entries carry, and it is a separate entry point because a type encoding carries no
marker saying which scheme it belongs to: i is a valid Itanium type and a valid
anything-else, so it can only be read when a caller says which scheme to read it as.
None for a scheme with no type grammar of its own, which is most of them: Go, Nim, Free Pascal and JNI names carry types only as text they already spell.
node_kinds
class-attribute
instance-attribute
¶
node_kinds: tuple[str, ...] = ()
Every kind string a tree from this scheme can hold.
The published vocabulary. parse() returns a walkable tree, and without this a
caller building on it has to read the source to learn what to switch on – which is
what swift-demangle's kind= dumps and libiberty's hundred DEMANGLE_COMPONENT_*
enumerators exist to avoid. demangle.node_kinds() is the whole of it, and
tests/test_api_surface.py::TestThePublishedVocabulary
checks it against every corpus so it cannot drift.
description
class-attribute
instance-attribute
¶
description: str = ''
One line, shown by demangle --list-languages.
aliases
class-attribute
instance-attribute
¶
aliases: Sequence[str] = field(default_factory=tuple)
Other names callers may use: gnu and gcc both mean itanium.
options_type
class-attribute
instance-attribute
¶
options_type: type | None = None
The dataclass this language accepts as options, if it takes any.
symbol_table_decorations
class-attribute
instance-attribute
¶
symbol_table_decorations: bool = False
Whether names in this scheme may carry symbol-table decorations.
True for schemes that appear in ELF and Mach-O symbol tables, where the linker
appends version suffixes and the compiler appends clone suffixes. It must stay False
for MSVC, whose decorated names use @ as their own scope separator and would be
truncated at the first one.
spells_what_it_reads
class-attribute
instance-attribute
¶
spells_what_it_reads: bool = False
Whether a control character can reach a spelling only by being in the name.
True lets parse() skip spelling a tree to look for one when the name holds none.
It must stay False for a scheme that decodes anything into text: Go's %1b, JNI's
_0001b, punycode. Itanium string literals are decoded too, but spelled escaped.
first_characters
class-attribute
instance-attribute
¶
first_characters: str = ''
The characters a name of this scheme may begin with, if they are not all of them.
An optimisation, and one the registry checks rather than trusts: a name whose first
character is not in here is never offered to detect. Leave it empty – the default
– for a scheme whose names have no fixed start, which costs nothing but a call. A
wide set still pays where it leaves out another scheme's opening: Nim and Free Pascal
declare the letters and _, so neither is asked about an MSVC ? name.
Getting it wrong loses symbols silently, so the rule is narrow: put a character here
only if detect returns False for every name that does not begin with one of
them. tests/test_core.py checks the built-in schemes against their own corpora.
priority
class-attribute
instance-attribute
¶
priority: int = 100
Detection order, lower first.
Matters only where two schemes share a prefix. Rust's legacy scheme is Itanium mangling, so Rust must be offered the name first or every Rust symbol demangles as a C++ one.
Discovery of mangling schemes.
Built-in languages are imported lazily, and not only when named: the registry knows
each one's detection order and first characters without importing it, so a scheme's
module is imported only once a name reaches its detect – a name starting ? never
costs the Swift parser. Third-party languages are found through the
demangle.languages entry-point group, so a separate distribution can add a language
without a patch here – but only once a caller asks, with load_plugins(). Nothing is
discovered on its own: an installed distribution cannot replace a built-in scheme
unnoticed, and two installs read a name alike.
The registry is process-global and populated once. Registration is idempotent, so importing a plugin module twice is harmless.
register
¶
register(plugin: LanguagePlugin) -> LanguagePlugin
Add a plugin to the registry, replacing any earlier one of the same name.
get
¶
get(name: str) -> LanguagePlugin
Look up a plugin by name or alias. Raises KeyError if unknown.
Symbol-table decorations¶
Decorations a symbol table adds around a mangled name.
A symbol in a real binary is often not just a mangled name. The linker and the compiler both append things to it, and none of those additions belong to any mangling scheme – they are conventions of the object format and the toolchain:
_ZNSt10moneypunctIcLb0EE2idE@@GLIBCXX_3.4 ELF symbol versioning
_ZN3Foo3barEv.cold a cold-path clone
_ZN3Foo3barEv.part.0 a partial inlining clone
_ZN3Foo3barEv.llvm.1234567890 an LLVM internalisation suffix
_ZN3Foo3barEv.constprop.0 a constant-propagation clone
Handling these in core rather than in each parser matters because they are orthogonal
to the grammar: a scheme should not have to know about ELF, and a new scheme gets this
for free. The reference demanglers pass them through verbatim, and so do we – the
suffix identifies which copy of a function a symbol is, which is information a caller
split_decorations
¶
split_decorations(name: str) -> tuple[str, str]
Split name into (mangled, version-suffix).
Only the ELF version suffix is split here, because only it can be recognised
before parsing: @ appears in no scheme this applies to, so the first one is
necessarily the start of the suffix.
Clone suffixes deliberately are not. A . is not reserved: GCC names a coroutine's
frame type _ZN6modern9coroutineEi.Frame and writes that as a length-prefixed
identifier, so the coroutine's actor is
_ZN6modern9coroutineEPZNS_9coroutineEiE28_ZN6modern9coroutineEi.Frame.actor –
where the first . is inside the identifier and only the second begins the clone
suffix. Cutting at the first . truncates the name mid-production. A clone suffix
can only be recognised as what is left over once the grammar has consumed
everything it can, which means the parser has to do it.
The suffix is returned with its separator intact, so joining is concatenation.
describe
¶
describe(
decoration: str, gnu_clone_suffix: bool = False
) -> str
A readable spelling of a decoration.
ELF version suffixes are passed through as written by both references. Clone
suffixes are not: llvm-cxxfilt writes Foo::bar() (.cold) where GNU c++filt writes
Foo::bar() [clone .cold]. Neither is more correct, so it is a style choice.
Styles¶
Output styles.
Some spelling questions have more than one correct answer, and the reference
implementations answer them differently: llvm-cxxfilt prints std::string and
Foo<Bar<int>>, GNU c++filt prints the full basic_string specialisation and
Foo<Bar<int> >. Both describe the same types.
A Style bundles those choices so a caller picks one name – "llvm", "gnu" –
instead of threading half a dozen flags through every call. New languages add their own
options to the mapping without changing this type, which is why the per-language policy
is an opaque dict rather than a fixed set of fields.
Not every choice is a disagreement between references, though, and the second kind is
what with_options is for: how much of a name to print. Every peer tool lets a caller
compose those at the call site – llvm-undname --no-calling-convention,
UnDecorateSymbolName's mask, c++filt -p – because "the same names, with less around
them" is what makes a symbol table greppable. A style is immutable, so composing one
returns a new style rather than changing the shared default, and a style object handed to
demangle() is deliberately not cached: it is one call's policy, not the process's.
Style
dataclass
¶
Style(
name: str,
spelling_builder: Builder,
language_options: Mapping[str, Any] = dict(),
)
A named set of output policy choices.
name
instance-attribute
¶
name: str
What the style is registered and looked up under – "llvm", "gnu".
spelling_builder
instance-attribute
¶
spelling_builder: Builder
The builder used when a caller asks for text. Must be a Builder.
language_options
class-attribute
instance-attribute
¶
language_options: Mapping[str, Any] = field(
default_factory=dict
)
Per-language option objects, keyed by language name.
options_for
¶
options_for(language: str) -> Any
The option object this style specifies for language, or None.
with_options
¶
with_options(**languages: Any) -> Style
Return a new style: this one with some language's options changed.
Each keyword is a language name and each value is either that language's whole options object or a mapping of the fields to change in the one already in force:
>>> import demangle
>>> from demangle.core.style import get_style
>>> narrow = get_style("llvm").with_options(msvc={"calling_convention": False})
>>> demangle.demangle("?f@@YAXH@Z", style=narrow)
'void f(int)'
demangle.style() is the same thing spelled from the package.
The result is a Style object rather than a registered name, which is what keeps
it a per-call policy: demangle() does not cache a call that passes one, so one
caller's narrower spelling cannot be served to another asking for the default.
register_style
¶
Add a style, so a downstream project can define its own house spelling.
get_style
¶
Look up a style by name. None gives the default, and a Style is returned as is.
Raises:
| Type | Description |
|---|---|
ValueError
|
|
available_styles
¶
available_styles() -> list[str]
The registered style names, sorted. demangle.styles() is this.
Caching¶
Bounded memoisation.
Symbol tables repeat themselves relentlessly: one C++ binary can name
std::allocator<char> thousands of times, and every one costs the same parse. Caching
is worth more here than almost any micro-optimisation.
It also cannot be unbounded. A tool that walks a corpus of binaries would otherwise accumulate an entry per distinct symbol ever seen, and there is no natural end to that.
CacheStats
¶
Bases: TypedDict
What demangle.cache_stats() returns.
BoundedCache
¶
BoundedCache(max_size=8192, max_weight=None, weigh=None)
A dict of at most max_size entries, kept as two generations.
New entries go into the young generation. When it holds half of max_size, it
becomes the old one – the previous old one is dropped whole – and a new, empty
young generation starts. A lookup tries the young generation, then the old; an entry
found in the old one moves back to the young, so whatever is still in use survives
every turnover while what went cold ages out within two.
A hit in the young generation costs one lookup, and a hit in the old one a second lookup and a move. True LRU would cost a linked-list update on every hit, the operation being optimised, and served a warm name 15% slower for a few points of hit rate.
Adding an entry – a miss, or a promotion from the old generation – takes a lock,
so under free threading the turnover and the weight are exact and the bound holds.
A hit in the young generation takes none, being the path this exists to make cheap.
The lock is re-entrant: a signal handler or finalizer that runs demangle() while
this thread is inside put proceeds rather than deadlocks, at worst leaving the
weight one entry off.
epoch counts clear() calls. A caller that computes a value from state whose
change clears the cache reads it before computing and passes it to put, which then
drops the value if a clear came in between, rather than keep an answer from before.
hits and misses count lookups: a hit in either generation is a hit. They are
not guarded, so under free threading concurrent lookups can lose an increment and
the figures are approximate.
weigh(key, value), with max_weight, also bounds what the entries hold.
Handoff
¶
Handoff()
The reading a scheme's detect just made, kept for the parse that follows it.
A scheme with no prefix of its own claims a name by reading all of it, and the
registry then asks parse for the same reading. Without this every such name is
read twice. One entry, taken by the first parse that asks for its key, so no
reading is handed to two callers; clear_memos empties it.
memo
¶
memo() -> dict
A dict a scheme keeps sub-results in across names, emptied by clear_memos.
cache_clear() promises that the next call starts cold, and a scheme that remembers
pieces of earlier names would quietly break that promise without this.