This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFC] Parametrized macro names




        Basically, what I'd like the preprocessor to do is to allow
	this kind of macro:

            #define macro ## param1 ## param2 ## ... ## paramN macrobody

	param1-N are macros themselves, and the above line defines a
	macro whose name is composed of all the other macros'
	contents, making it parametrized at all effects.

	So, say I have the above #define in an include file, I could do this:

	    #define param1 ...
	    #define param2 ...
	    ...
	    #define paramN ...

	    #include "macro.h"

	And automatically get a new macro named after the parameter's contents.

        [....]

        One more thing: for the above line to be really useful, it
        would be also needed that the preprocessor handled the case in
        which '##', in a macro body, is followed by a macro name, and
        not just by a parameter's name. I don't think it would cause
        any problems.

        Right now, this code doesn't work as one would expect:

            cpp << EOF
            #define macro1 foo
            #define macro2 baz ## macro1

            macro2
	    EOF

	produces

	    bazmacro1

	rather than

	    bazfoo

	as one might expect.


(Regarding the "one more thing" -- it could easily break existing
code.  One might expect differently, but then one would be wrong. :-)

Putting that all together, you seem to want users of your macros 
to be able use some combination of the strings:

	macro param1 param2 ... paramN

in some CPP construct to produce a string which is parameterized
by strings

	bodyparam1 bodyparam2 ... bodyparamN

where each `bodyparamK' is uniquely determined by a subset of the
`paramN' values.

There is a systematic way to accomplish what you want to do without
extending CPP.  (This should be sufficient reason to not extend CPP
this way.)

First, define a macro for each bodyparam as in this example:

	#define bodyparamK_param1val_param2val   ...someexpansion...

Then define `macro' with parameters:

	#define macro(param1, param2, ..., paramN) ...expansion...

In `expansion', wherever you want to refer to bodyparamK _outside_ of
the context of a ## expression, instead use something like:

	bodyparamK_ ## param1 ## _ ## param2

Now what if you want to use a bodyparamK _inside_ a  ## expression?
No problem: you can define

	#define bodyparamK_val(p1, p2)  bodyparamK_ ## p1 ## p2

and use that instead.

The upshot of all of this is that instead of a mechanism to get CPP to
define a new macro_foo_bar for you, you can get macro(foo,bar) to
expand in the way you like without too much fuss.

I've written a bunch of macros along these lines.  I fear I might have
not been clear or made some stupid mistake in my explanation but: if
you have a particular practical CPP problem to solve, contact me off
list and I can spend a _little_ time on it at least.

-t


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]