RFC: Function Overloading in C

Hi all,

I've just finished the implementation of the "overloadable" function attribute that introduces function overloading into C. The intent is to make it easier to implement "type-generic" functions (such as those in tgmath.h and altivec.h). The feature is in Clang's trunk, and is documented here:

  http://clang.llvm.org/docs/LanguageExtensions.html#overloading-in-c

Comments welcome!

  - Doug

Douglas Gregor wrote:

Hi all,

I've just finished the implementation of the "overloadable" function
attribute that introduces function overloading into C. The intent is
to make it easier to implement "type-generic" functions (such as those
in tgmath.h and altivec.h). The feature is in Clang's trunk, and is
documented here:

  Clang Language Extensions — Clang 18.0.0git documentation

Comments welcome!
  

I wonder if it's possible (and a good idea) to use the C++ linkage
specification syntax for this, i.e.

extern "C++" double tgsin(double d) { return sin(d); }
extern "C++" float tgsin(float f) { return sinf(f); }
extern "C++" long double tgsin(long double d) { return sinl(d); }

As a C++ programmer, it looks more natural to me. Of course, I have no
idea how this would look to a pure C programmer. Also, this syntax would
imply that we will always mangle this like C++ names.

Sebastian

Great, Doug, thanks!

Makslane

I actually prefer an attribute, for two reasons. 1) this would require adding support for linkage specs to the "c parser" and "c sema" which is a bigger change, 2) to the user, the connection that this would enable overloading, but nothing else, seems strange.

As a user, I would expect to be able to write something like this as well:
extern "C++" template <typename t> void foo();

Attributes are "ugly", but they are very standard, and it makes it very clear that it enables "overloadability"

-Chris

Douglas Gregor wrote:

Hi all,

I've just finished the implementation of the "overloadable" function
attribute that introduces function overloading into C. The intent is
to make it easier to implement "type-generic" functions (such as those
in tgmath.h and altivec.h). The feature is in Clang's trunk, and is
documented here:

  Clang Language Extensions — Clang 18.0.0git documentation

Comments welcome!

I wonder if it's possible (and a good idea) to use the C++ linkage
specification syntax for this, i.e.

extern "C++" double tgsin(double d) { return sin(d); }
extern "C++" float tgsin(float f) { return sinf(f); }
extern "C++" long double tgsin(long double d) { return sinl(d); }

As a C++ programmer, it looks more natural to me.

Huh. I never even thought of that!

Of course, I have no
idea how this would look to a pure C programmer. Also, this syntax would
imply that we will always mangle this like C++ names.

I'm on the fence with this one. My main concern is that 'extern "C++"' is very open-ended, and users seeing this in code might be tempted to try all sorts of crazy C++ things following the 'extern "C++"'. We'll need to make sure we reject all those, otherwise they might work and users might start relying on them :slight_smile:

I like the "overloadable" attribute better in this regard, because it is (was) easy to lock it down and make it only work in very specific circumstances. Plus, it gives us a lot of wiggle room w.r.t. the mangling of these names.

  - Doug

Douglas Gregor wrote:

Hi all,

I've just finished the implementation of the "overloadable" function
attribute that introduces function overloading into C. The intent is
to make it easier to implement "type-generic" functions (such as
those
in tgmath.h and altivec.h). The feature is in Clang's trunk, and is
documented here:

  Clang Language Extensions — Clang 18.0.0git documentation

Comments welcome!

I wonder if it's possible (and a good idea) to use the C++ linkage
specification syntax for this, i.e.

extern "C++" double tgsin(double d) { return sin(d); }
extern "C++" float tgsin(float f) { return sinf(f); }
extern "C++" long double tgsin(long double d) { return sinl(d); }

As a C++ programmer, it looks more natural to me.

Huh. I never even thought of that!

Of course, I have no
idea how this would look to a pure C programmer. Also, this syntax
would
imply that we will always mangle this like C++ names.

I'm on the fence with this one. My main concern is that 'extern "C++"'
is very open-ended, and users seeing this in code might be tempted to
try all sorts of crazy C++ things following the 'extern "C++"'. We'll
need to make sure we reject all those, otherwise they might work and
users might start relying on them :slight_smile:

I like the "overloadable" attribute better in this regard, because it
is (was) easy to lock it down and make it only work in very specific
circumstances. Plus, it gives us a lot of wiggle room w.r.t. the
mangling of these names.

It looks weird to apply extern "C++' on an overloadable 'static' function.

- Fariborz

Good point! These functions will usually be inline static, e.g.,

   extern "C++" inline static tgsin(double d) { return sin(d); }

is rather confusing.

Now I definitely like __attribute__((overloadable)) best.

   - Doug

Douglas Gregor wrote:

It looks weird to apply extern "C++' on an overloadable 'static'
function.

Good point! These functions will usually be inline static, e.g.,

  extern "C++" inline static tgsin(double d) { return sin(d); }

is rather confusing.

Now I definitely like __attribute__((overloadable)) best.

Right, I'm convinced.

Sebastian