Demangling compiled C++ names with the demangle class library

The demangle class library contains a small class hierarchy that client programs can use to demangle names and examine the resulting parts of the name. It also provides a C-language interface for use in C programs. Although it is a C++ library, it uses no external C++ features, so you can link it directly to C programs. The demangle library is included as part of libC.a, and is automatically linked, when required, if libC.a is linked.

The header file declares a base class, Name, and a member function, Demangle, that takes a mangled name as a parameter, and returns the corresponding demangled name. The header file declares four additional subclasses, which each contain member functions that allow you to get additional information about the name. These classes are:
ClassName
Can be used to query names of independent or nested classes.
FunctionName
Can be used to query names of functions.
MemberVarName
Can be used to query names of member variables.
MemberFunctionName
Can be used to query names of member functions.
For each of these classes, functions are defined to provide you with information about the name. For example, for function names, a set of functions are defined that return the following information:
Kind
Returns the type of the name being queried (that is, class, function, member variable, or member function).
Text
Returns the fully qualified original text of the function.
Rootname
Returns the unqualified original name of the function.
Arguments
Returns the original text of the parameter list.
Scope
Returns the original text of the function's qualifiers.
IsConst/IsVolatile/IsStatic
Returns true/false for these type qualifiers or storage class specifiers.

To demangle a name (represented as a character array), create a dynamic instance of the Name class, providing the character string to the class's constructor. For example, if the compiler mangled X::f(int) to the mangled name f__1XFi, in order to demangle the name, use the following code:

char *rest;
Name *name = Demangle(“f__1XFi”, rest) ;

If the supplied character string is not a name that requires demangling, because the original name was not mangled, the Demangle function returns NULL.

Once your program has constructed an instance of class Name, the program could query the instance to find out what kind of name it is, using the Kind method. Using the example of the mangled name f__1XFi, the following code:

name->Kind() 

returns MemberFunction.

Based on the kind of name returned, the program might ask for the text of the different parts of the name, or the text of the entire name. The following table shows examples, still assuming the mangled name f__1XFi.
To return... ...use this code: Result
The name of the function's qualifier ((MemberFunctionName *)name)->Scope()->Text() X
The unqualified name of the function ((MemberFunctionName *)name)->RootName() f
The fully qualified name of the function ((MemberFunctionName *)name)->Text() X::f(int)

For further details about the demangle library and the C++ interface, see the comments in the library's header file, /usr/vacpp/include/demangle.h.