Mangle functions

Brian Dessent
Mon Jan 28 18:12:00 GMT 2008

Ron Kreymborg wrote:

> My idea is a macro like:
> #define CLASS_ISR(theclassname, themethodname) \
>  ISR(theclassname) ISR_ALIASOF(gcc_mangle(theclassname, themethodname)); \
>  void theclassname::themethodname(void)

As written this is impossible, since the mangling includes the number
and types of arguments.

> Inserting the mangled name by hand in the macro above produces the vector
> alias that allows gcc to link the external C vector name while the C++ code
> remains unaware of its existence outside the owning class:

I can invision a way to automate this, assuming you have a full featured
build environment (i.e. unix toolset.) First define gcc_mangle a macro
that simply expands to a unique identifier name.  For example

#define gcc_mangle(class, method, arg) \

This works only for the case of one arg, if you want to support
arbitrary number of args you might have to get creative with the
macros.  It would also break if one of the args was a type like
std::string since that would be an invalid identifier.

Anyway the idea is that you then have an auto-generated header that will
fill in these definitions, e.g. for the above it would have generated:

#define MANGLED_NAME_CTimer0Interrupt___vector_16_void

This header is generated by the build system, via a script that greps
all the source files for occurances of gcc_mangle and adds an entry to
the header for each one.  To get the actual contents of the mangled
string, you can use gcc itself:

$ cat > <<EOF

echo "struct $1 { void $2 ( $3 ); }; void $1::$2 ( $3 ) { };" \
   | gcc -x c++ -S - -o - \
   | awk '/^.globl/ { print $2; exit }'

$ ./ CTimer0Interrupt __vector_16 void

So to sum up: your build system would need:

- a script that scans through a given set of source files for occurances
of gcc_mangle, and runs the above (or equivalent) with
the macro's args, and appends the results in the form of a #define to
the generated header.
- makefile rules to ensure that the header depends on all source files
that include it, as well as a rule to regenerate the header.
- developers that can remember to always include the autogenerated
header in any file that uses the gcc_mangle (or whatever you call it)

This is not exactly pretty, but it at least allows you to handle this


More information about the Gcc-help mailing list