From: Douglas Gregor
Date: Fri, 13 Feb 2009 00:57:04 +0000 (+0000)
Subject: Add documentation for overloaded functions in C
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cb54d4362faf9f309f371219d4311a47bdcd5b59;p=clang
Add documentation for overloaded functions in C
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64423 91177308-0d34-0410-b5e6-96231b3b80d8
---
diff --git a/docs/LanguageExtensions.html b/docs/LanguageExtensions.html
index 1bccdbd2cc..e9f409ace9 100644
--- a/docs/LanguageExtensions.html
+++ b/docs/LanguageExtensions.html
@@ -21,6 +21,7 @@ td {
Introduction
Vectors and Extended Vectors
Blocks
+Function Overloading in C
Builtin Functions
- __builtin_overload
@@ -54,6 +55,80 @@ href="#__builtin_shufflevector">__builtin_shufflevector.
The idea, syntax, and semantics.
+
+Function Overloading in C
+
+
+Clang provides support for C++ function overloading in C. Function overloading in C is introduced using the overloadable attribute. For example, one might provide several overloaded versions of a tgsin function that invokes the appropriate standard function computing the sine of a value with float, double, or long double precision:
+
+
+
+#include <math.h>
+float __attribute__((overloadable)) tgsin(float x) { return sinf(x); }
+double __attribute__((overloadable)) tgsin(double x) { return sin(x); }
+long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); }
+
+
+
+Given these declarations, one can call tgsin with a
+float value to receive a float result, with a
+double to receive a double result, etc. Function
+overloading in C follows the rules of C++ function overloading to pick
+the best overload given the call arguments, with a few C-specific
+semantics:
+
+ - Conversion from float or double to long
+ double is ranked as a floating-point promotion (per C99) rather
+ than as a floating-point conversion (as in C++).
+
+ - A conversion from a pointer of type T* to a pointer of type
+ U* is considered a pointer conversion (with conversion
+ rank) if T and U are compatible types.
+
+ - A conversion from type T to a value of type U
+ is permitted if T and U are compatible types. This
+ conversion is given "conversion" rank.
+
+
+The declaration of overloadable functions is restricted to
+function declarations and definitions. Most importantly, if any
+function with a given name is given the overloadable
+attribute, then all function declarations and definitions with that
+name (and in that scope) must have the overloadable
+attribute. This rule even applies to redeclarations of functions whose original declaration had the overloadable attribute, e.g.,
+
+
+
+int f(int) __attribute__((overloadable));
+float f(float); // error: declaration of "f" must have the "overloadable" attribute
+
+int g(int) __attribute__((overloadable));
+int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute
+
+
+
+Functions declared with the overloadable attribute have
+their names mangled according to the same rules as C++ function
+names. For example, the three tgsin functions in our
+motivating example get the mangled names _Z5tgsinf,
+_Z5tgsind, and Z5tgsine, respectively. There are two
+caveats to this use of name mangling:
+
+
+
+ - Future versions of Clang may change the name mangling of
+ functions overloaded in C, so you should not depend on an specific
+ mangling. To be completely safe, we strongly urge the use of
+ static inline with overloadable functions.
+
+ - The overloadable attribute has almost no meaning when
+ used in C++, because names will already be mangled and functions are
+ already overloadable. However, when an overloadable
+ function occurs within an extern "C" linkage specification,
+ it's name will be mangled in the same way as it would in
+ C.
+
+
Builtin Functions
@@ -82,6 +157,7 @@ builtins that we need to implement.
functions in C. This builtin is used to implement the <tgmath.h>
header file, but is intended to be usable for a broad variety of other similar
situations, like the <altivec.h> header.
+In the future, we intend to eliminate __builtin_overload in favor of function overloading in C, which provides a better solution for type-generic "overloaded" functions.
Syntax: