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

Douglas Gregor dgregor at apple.com
Thu Feb 12 09:02:46 PST 2009


On Feb 12, 2009, at 8:18 AM, Howard Hinnant wrote:

> On Feb 12, 2009, at 11:10 AM, Douglas Gregor wrote:
>
>>
>> On Feb 12, 2009, at 8:02 AM, Douglas Gregor wrote:
>>> 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)
>>
>> Sorry, I got my attribute syntax mangled. That should be:
>>
>> float __attribute__((always_inline, overloadable)) __tg_sin(float f)
>> { return sinf(f); }
>> double __attribute__((always_inline, overloadable)) __tg_sin(double  
>> d)
>> { return sin(d); }
>> long double __attribute__((always_inline, overloadable)) __tg_sin 
>> (long
>> double ld) { return sin(ld); }
>> #define sin(x) __tg_sin(s)
>
> This sure illustrates how convenient (and concise) it is to have the  
> overloadable attribute on the definition.
>
> On Feb 11, 2009, at 6:02 PM, Douglas Gregor wrote:
>
>> 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));
>
> Is it practical to lift the "declaration-only" restriction?  Perhaps  
> a "first mention of function" rule?

Sorry, I was imprecise: the overloadable attribute can only be placed  
on functions. It can show up on either a function declaration or a  
function definition (but the syntax is slightly different, as you can  
see from my mistake above).

The main restriction is that the attribute must be on *all* of the  
functions with that name. Actually, now that I think of it, I think we  
might want to clamp this down further to say that every single  
declaration must *explicitly* contain the overloadable attribute,  
including redeclarations. We wouldn't want some crafty programmer to  
write:

   double sin(double) __attribute__((overloaded));
   #include <math.h>
   // Oh, goody! I just gave math.h's sin() function a mangled C++ name!

	- Doug



More information about the cfe-commits mailing list