From: ron on
I need to build a C++ mangled name from a full class method name. For
example "MyClass::MyFunction". The mangled name structure is known and
looks like:

_ZN <sizeof(MyClass)> MyClass <sizeof(MyFunction)> MyFunction Ev

so this example would be:

_ZN7MyClass10MyFunctionEv

The macro:

#define DO_MANGLE(theclass, L1, thename, L2) _ZN ## L1 ## theclass ##
L2 ## thename ## Ev

will do this but only if I pass L1 and L2 as numbers. Ie

DO_MANGLE(MyClass, 7, MyFunction, 10)

How can I get the compiler to calculate these two numbers for me?

Ronk
From: Francis Glassborow on
ron(a)jennaron.com.au wrote:
> I need to build a C++ mangled name from a full class method name. For
> example "MyClass::MyFunction". The mangled name structure is known and
> looks like:
>
> _ZN <sizeof(MyClass)> MyClass <sizeof(MyFunction)> MyFunction Ev
>
> so this example would be:
>
> _ZN7MyClass10MyFunctionEv
>
> The macro:
>
> #define DO_MANGLE(theclass, L1, thename, L2) _ZN ## L1 ## theclass ##
> L2 ## thename ## Ev
>
> will do this but only if I pass L1 and L2 as numbers. Ie
>
> DO_MANGLE(MyClass, 7, MyFunction, 10)
>
> How can I get the compiler to calculate these two numbers for me?
>

You can't :) The real crunch is that there is no way to find the size of
a function. Now perhaps you would share the problem you are trying to
solve because users should have no reason to want to know the mangle of
a name. I am struggling to imagine how you would use it once you had it.
From: ron on
On Jan 23, 9:40 pm, Francis Glassborow
<francis.glassbo...(a)btinternet.com> wrote:
> r...(a)jennaron.com.au wrote:
> > I need to build a C++ mangled name from a full class method name. For
> > example "MyClass::MyFunction". The mangled name structure is known and
> > looks like:
>
> >   _ZN <sizeof(MyClass)> MyClass <sizeof(MyFunction)> MyFunction Ev
>
> > so this example would be:
>
> >   _ZN7MyClass10MyFunctionEv
>
> > The macro:
>
> > #define DO_MANGLE(theclass, L1, thename, L2) _ZN ## L1 ## theclass ##
> > L2 ## thename ## Ev
>
> > will do this but only if I pass L1 and L2 as numbers. Ie
>
> >    DO_MANGLE(MyClass, 7, MyFunction, 10)
>
> > How can I get the compiler to calculate these two numbers for me?
>
> You can't :) The real crunch is that there is no way to find the size of
> a function. Now perhaps you would share the problem you are trying to
> solve because users should have no reason to want to know the mangle of
> a name. I am struggling to imagine how you would use it once you had it.- Hide quoted text -
>
> - Show quoted text -

I don't want the size of the function, just the number of characters
in the function name. Hence: sizeof(name), which does return the
correct number but cannot be used directly as a parameter to the
macro. It is simply substituted into the result:

_ZNsizeof(MyClass)MyClasssizeof(MyFunction)MyFunctionEv

A number of microprocessors have an interrupt vector table at
specified addresses in flash/rom. The compiler must match this jump
table to the respective interrupt handler at the name reconciliation
stage. Embedding interrupt handlers as private methods in the
peripheral managing class naturally mangles their name. However, gcc
provides an _alias function which allows matching the mangled name to
the global name. The DO_MANGLE macro performs part of this aliasing.
This all works, but requires manual counting of the characters in the
various names and while the compiler catches any errors here, I am
trying to make this automatic.

Ronk


From: Francis Glassborow on
ron(a)jennaron.com.au wrote:
> On Jan 23, 9:40 pm, Francis Glassborow
> <francis.glassbo...(a)btinternet.com> wrote:
>> r...(a)jennaron.com.au wrote:
>>> I need to build a C++ mangled name from a full class method name. For
>>> example "MyClass::MyFunction". The mangled name structure is known and
>>> looks like:
>>> _ZN <sizeof(MyClass)> MyClass <sizeof(MyFunction)> MyFunction Ev
>>> so this example would be:
>>> _ZN7MyClass10MyFunctionEv
>>> The macro:
>>> #define DO_MANGLE(theclass, L1, thename, L2) _ZN ## L1 ## theclass ##
>>> L2 ## thename ## Ev
>>> will do this but only if I pass L1 and L2 as numbers. Ie
>>> DO_MANGLE(MyClass, 7, MyFunction, 10)
>>> How can I get the compiler to calculate these two numbers for me?
>> You can't :) The real crunch is that there is no way to find the size of
>> a function. Now perhaps you would share the problem you are trying to
>> solve because users should have no reason to want to know the mangle of
>> a name. I am struggling to imagine how you would use it once you had it.- Hide quoted text -
>>
>> - Show quoted text -
>
> I don't want the size of the function, just the number of characters
> in the function name. Hence: sizeof(name), which does return the
> correct number but cannot be used directly as a parameter to the
> macro. It is simply substituted into the result:
>
> _ZNsizeof(MyClass)MyClasssizeof(MyFunction)MyFunctionEv
>
> A number of microprocessors have an interrupt vector table at
> specified addresses in flash/rom. The compiler must match this jump
> table to the respective interrupt handler at the name reconciliation
> stage. Embedding interrupt handlers as private methods in the
> peripheral managing class naturally mangles their name. However, gcc
> provides an _alias function which allows matching the mangled name to
> the global name. The DO_MANGLE macro performs part of this aliasing.
> This all works, but requires manual counting of the characters in the
> various names and while the compiler catches any errors here, I am
> trying to make this automatic.
>
How about:

std::string mangle(char const* classname, char const* functionname){
std::ostringstream item;
item << "_ZD" << strlen(classname) << classname
<< strlen(functionname) << functionname << "Ev";
return return item.str();
}

You can extract an array of char from the return value (but make sure
that you capture the return value into a variable:
std::string const mangled_name(mangle(MyClass, MyFunction));
and use mangled_name.cstr() to get an array of char.
From: ron on
On Jan 24, 9:31 pm, Francis Glassborow
<francis.glassbo...(a)btinternet.com> wrote:
> r...(a)jennaron.com.au wrote:
> > On Jan 23, 9:40 pm, Francis Glassborow
> > <francis.glassbo...(a)btinternet.com> wrote:
> >> r...(a)jennaron.com.au wrote:
> >>> I need to build a C++ mangled name from a full class method name. For
> >>> example "MyClass::MyFunction". The mangled name structure is known and
> >>> looks like:
> >>>   _ZN <sizeof(MyClass)> MyClass <sizeof(MyFunction)> MyFunction Ev
> >>> so this example would be:
> >>>   _ZN7MyClass10MyFunctionEv
> >>> The macro:
> >>> #define DO_MANGLE(theclass, L1, thename, L2) _ZN ## L1 ## theclass ##
> >>> L2 ## thename ## Ev
> >>> will do this but only if I pass L1 and L2 as numbers. Ie
> >>>    DO_MANGLE(MyClass, 7, MyFunction, 10)
> >>> How can I get the compiler to calculate these two numbers for me?
> >> You can't :) The real crunch is that there is no way to find the size of
> >> a function. Now perhaps you would share the problem you are trying to
> >> solve because users should have no reason to want to know the mangle of
> >> a name. I am struggling to imagine how you would use it once you had it..- Hide quoted text -
>
> >> - Show quoted text -
>
> > I don't want the size of the function, just the number of characters
> > in the function name. Hence: sizeof(name), which does return the
> > correct number but cannot be used directly as a parameter to the
> > macro. It is simply substituted into the result:
>
> > _ZNsizeof(MyClass)MyClasssizeof(MyFunction)MyFunctionEv
>
> > A number of microprocessors have an interrupt vector table at
> > specified addresses in flash/rom. The compiler must match this jump
> > table to the respective interrupt handler at the name reconciliation
> > stage. Embedding interrupt handlers as private methods in the
> > peripheral managing class naturally mangles their name. However, gcc
> > provides an _alias function which allows matching the mangled name to
> > the global name. The DO_MANGLE macro performs part of this aliasing.
> > This all works, but requires manual counting of the characters in the
> > various names and while the compiler catches any errors here, I am
> > trying to make this automatic.
>
> How about:
>
> std::string mangle(char const* classname, char const* functionname){
>     std::ostringstream item;
>     item << "_ZD" << strlen(classname) << classname
>                       << strlen(functionname) << functionname << "Ev";
>     return return item.str();
>
> }
>
> You can extract an array of char from the return value (but make sure
> that you capture the return value into a variable:
>     std::string const mangled_name(mangle(MyClass, MyFunction));
> and use mangled_name.cstr() to get an array of char.- Hide quoted text -
>
> - Show quoted text -

I think this would work at runtime and produce the correct name. But
in my case it must be executable by the preprocessor, as the code
passed to the compiler must have all macros expanded. Here is a
meaningless but working sample program using the previous example that
can be used as a test:

#define DO_MANGLE(theclass, L1, thename, L2) \
_ZN ## L1 ## theclass ## L2 ## thename ## Ev

#define TEST_MACRO(theclass, L1, thename, L2) \ // see below!!!
DO_MANGLE(theclass, L1, thename, L2)()

void _ZN7MyClass10MyFunctionEv(void)
{
int m = 10;
}

int main(void)
{
TEST_MACRO(MyClass, 7, MyFunction, 10);
return 0;
}

However, what I want is the macro:

#define TEST_MACRO(theclass, thename) \
<a little bit of magic to assign numerical values to L1 and L2> \
DO_MANGLE(theclass, L1, thename, L2)()

Regards, Ronk