[cfe-commits] r64336 - in /cfe/trunk: include/clang/AST/Attr.h

Douglas Gregor dgregor at apple.com
Thu Feb 12 08:02:30 PST 2009


On Feb 12, 2009, at 12:25 AM, Sebastian Redl wrote:

>
> On Wed, 11 Feb 2009 23:02:50 -0000, Douglas Gregor <dgregor at apple.com>
> wrote:
>> Author: dgregor
>> Date: Wed Feb 11 17:02:49 2009
>> New Revision: 64336
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=64336&view=rev
>> Log:
>> Initial implementation of function overloading in C.
>>
>> This commit adds a new attribute, "overloadable", that enables C++
>> function overloading in C. The attribute can only be added to  
>> function
>> declarations, e.g.,
>>
>>  int *f(int) __attribute__((overloadable));
>>
>> If the "overloadable" attribute exists on a function with a given
>> name, *all* functions with that name (and in that scope) must have  
>> the
>> "overloadable" attribute.
>>
>>  2) All overloadable functions with the same name will have the same
>>  linkage name, which means we'll get a collision in the linker (if
>>  not sooner). We'll need to mangle the names of these functions.
>>
>
> This is a problem for the intended use case in tgmath.h. There will  
> be a
> non-overloaded version of the function in math.h (since we don't  
> provide
> that file), which will conflict with the overloaded versions in  
> tgmath.h.
> Also, even if we mark the version in math.h as overloaded, how do we  
> then
> link to the non-mangled version in libm?


With macros, of course! :)

float __tg_sin(float f) __attribute__((always_inline, overloadable))  
{ return sinf(f); }
double __tg_sin(double d) __attribute__((always_inline, overloadable))  
{ return sin(d); }
long double __tg_sin(long double ld) __attribute__((always_inline,  
overloadable)) { return sin(ld); }
#define sin(x) __tg_sin(s)

It's also important to keep the entry points in tgmath.h as macros  
because it still allows us to take the address of a function, e.g.,

   (&sin)

is the address of

   double sin(double)

while &__tg_sin would be the address of an overloaded function.

	- Doug



More information about the cfe-commits mailing list