Updated mangling specs

Daveed Vandevoorde daveed at edg.com
Thu Jan 27 00:11:44 UTC 2000


Attached are the slightly updated name mangling specifications.

	Daveed
-------------- next part --------------
Name mangling
=============

[ Notes:
   1) Most of the "special entities" spec is still missing.
   2) The truncation+hashing approach needs to be described if desired.
   3) A proof of nonambiguity is needed.
   4) Many things (e.g., the <prefix>) may need tweaking.
--end Notes ]

Entities with C linkage and file scope variables are not mangled.

General structure
-----------------

    <prefix><name><type>opt

<prefix> is one of:
   . "_D" for namespace scope variables and static data members
   . "_F" for functions and operators (also those generated from a template)
   . "_S" for special entities (constructors etc; see below)

<length> is the decimal representation of the length of <name>.

<name> is one of:
   <unqualified-name>
      <length><function name>
      <length><function name>I<template-args>E
      <operator-name>
      <operator-name>I<template-args>E
      <special-name>
         (see below: ctors, vtables, ...)   
   <qualified-name>
      N<qual-1>...<qual-J><unqualified-name>E

<type> is used to disambiguate overloaded functions, but also to
distinguish the various virtual tables associated with a given complete
class type.  For nontemplate functions, <type> lists the parameter types
only. For template functions, <type> lists the return type followed by
the parameter types.  <type> is omitted for variables and static data
members.


Operator encodings
------------------

Operators appear as function names, but also in nontype template argument
expressions.
   new           nw
   new[]         na
   delete        dl
   delete[]      da
   - (unary)     ng
   & (unary)     ad
   * (unary)     de
   ~             co
   +             pl
   -             mi
   *             ml
   /             dv
   %             rm
   &             an
   |             or
   ^             eo
   =             aS
   +=            pL
   -=            mI
   *=            mL
   /=            dV
   %=            rM
   &=            aN
   |=            oR
   ^=            eO
   <<            ls
   >>            rs
   <<=           lS
   >>=           rS
   ==            eq
   !=            ne
   <             lt
   >             gt
   <=            le
   >=            ge
   !             nt
   &&            aa
   ||            oo
   ++            pp
   --            mm
   ,             cm
   ->*           pm
   ->            pt
   ()            cl
   []            ix
   ?             qu
   (cast)        cv

Unlike Cfront, unary and binary operators using the same symbol have
different encodings.  All operators are encoded using exactly two letters,
the first of which is lowercase.


Other special functions and entities
------------------------------------

   TV            virtual table
   TI            typeinfo structure
   C1            complete object constructor
   C2            base object constructor
   D0            deleting destructor
   D1            complete object destructor
   D2            base object destructor


Type encodings
--------------
Types are encoded as follows:

builtin types: (one letter)
   void                     v
   wchar_t                  w
   bool                     b
   char                     c
   signed char              a
   unsigned char            h
   short                    s
   unsigned short           t
   int                      i
   unsigned int             j
   long                     l
   unsigned long            m
   long long                x
   unsigned long long       y
   float                    f
   double                   d
   long double              e
   ellipsis                 z

classes & enums:
   <decimal length of unqualified name><unqualified-name>
   Class names can optionally be followed by the encoding of a template
   argument list (see below).

template params (including nontype and template template parameters):
   T<param num>_
   TT<param num>_  // For template parameters

other dependent names: (see below)
   N<qual 1>...<qual N><unqual name>E

template argument list: (see below)
   I<arg1>...<argN>E

function types:
   F<return type><param type 1>...<param type N>E

array types:
   A<dimension>_<element type encoding>

pointers, references:
   P<encoding pointed-to type>
   R<encoding pointed-to type>

pointer-to-member:
   M<class type encoding><member type encoding>

cv-qualifiers:
   K const
   V volatile
   r restrict


Scope encoding
--------------
Scope is encoded as the qualifier of qualified names: it can be the top-level
name qualification or it can appear inside <type> to denote dependent types
or bind specific names as arguments.  Qualified names are encoded as:
   N<qual 1>...<qual N><unqual name>E
where each <qual K> is the encoding of a namespace name or a class name (with
the latter possibly including a template argument list).


Template argument encoding
--------------------------
Template-ids are encoded by following the unqualified name with
	I<arguments>E
This is used for the <specialization> segment in particular, but also in the
<type> and <scope> segments.

Type arguments appear using their regular encoding.  For example, the
template class "A<char, float>" is encoded as "1AIcfE".  A slightly more
involved example might be a dependent function parameter type "A<T2>::X"
(T2 is the second template parameter) which is encoded as "N1AIT2_E1XE",
where the "N...E" construct is used to describe a qualified name.

Nontype arguments can be:
   a) a literal, e.g. "A<42L>": these are encoded as "L<type><num>E";
      negative integer <num> are preceded with "n"; false is "Lb0E";
      true is "Lb1E".  For example, "A<-42L>" becomes "1AILln42EE".
   b) a reference to an entity with external linkage: encoded with
      "L<mangled name>E".  For example:
          void foo(char); // mangled as _F3fooc
          template<void (&)(char)> struct CB;
          // CB<foo> is encoded with "2CBIL_F3foocEE"
   c) an expression, e.g., "B<(J+1)/2>" is encoded with a prefix traversal
      of the operators involved, delimited by "X...E".  The operators are
      encoded using their two letter mangled names.  For example, "B<(J+1)/2>"
      becomes "1BI Xdv pl T1_ Li1E Li2E E E" (the blanks were inserted to
      visualize the decomposition).


Compression
-----------
The subsequence
   S<num>_
is used to repeat the num-th most recently encoded type (in right-to-left
order, starting at "1"), but only if "S<num>_" is strictly shorter that the
previous encoding.

(Work on numbering other entities like templates, function names and variable
names.)


Truncation
----------
If the mangled name exceeds 255 characters in length, it is reduced as
follows:
    (description of strong hash and truncation)



Examples
--------

1) "f": The C function or variable "f" or a file scope variable "f".

2) "_F1f": Ret? f();

3) "_F1fi": Ret? f(int);

4) "_F3foo3bar": Ret? foo(bar);

5) "_Frm1X1X": Ret? operator%(X, X);

6) "_FplR1XR1X": Ret? operator+(X&, X&);

7) "_FlsRK1XS1_": Ret? operator(X const&, X const&);
       (Note: strlen("S1_")<strlen("RK1X"))

8) "_F1fIiE": void f<int>();

9) "_F1fIiEvi": void f<int>(/*nondependent*/int);
       (Note: the return type is always explicitly encoded for template
              functions taking parameters.)

10) "_F5firstI3DuoEvS2_": void first<Duo>(/*nondependent*/Duo);
       (Note: "S1_" would refer to the "void" return type.)

11) "_F5firstI3DuoEvT1_": void first<Duo>(/*T1=*/Duo);

12) "_FN1N1fE": Ret? N::f();

13) "_FN6System5Sound4beepE: Ret? System::Sound::beep();

14) "_DN5Arena5levelE": Type? Arena::level;

15) "_DN5StackIiiE5levelE": Type? Stack<int, int>::level

16) "_F1fI1XEvPVN1AIT1_E1TE": void f<X>(A</*T1=*/X>::T volatile*);
                |         |
                |         `------> end dependent name encoding
                `----------------> start of dependent name A<T1>::T

17) "_FngIL42iEEvN1AIXplT1_L2iEE1TE": void operator-</*int J=*/42>(A<J+2>::T);

18) "_F4makeI7FactoryiETT1_IT2_E": T1<T2> make();
                                          // T1 == template template parameter



More information about the cxx-abi-dev mailing list