Clang Language Extensions
Introduction
This document describes the language extensions provided by Clang. In addition to the langauge extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.
Vectors and Extended Vectors
Supports the GCC vector extensions, plus some stuff like V[1]. ext_vector with V.xyzw syntax and other tidbits. See also __builtin_shufflevector.
Blocks
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
Clang supports a number of builtin library functions with the same syntax as GCC, including things like __builtin_nan, __builtin_constant_p, __builtin_choose_expr, __builtin_types_compatible_p, __sync_fetch_and_add, etc. In addition to the GCC builtins, Clang supports a number of builtins that GCC does not, which are listed here.
Please note that Clang does not and will not support all of the GCC builtins for vector operations. Instead of using builtins, you should use the functions defined in target-specific header files like <xmmintrin.h>, which define portable wrappers for these. Many of the Clang versions of these functions are implemented directly in terms of extended vector support instead of builtins, in order to reduce the number of builtins that we need to implement.
__builtin_overload
__builtin_overload is used to implement type-generic "overloaded" 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:
__builtin_overload(FnNameStr, PromotionRuleStr, NumArgs, arg1, arg2, ... overloadcandidate1, overloadcandidate2, ...)
Examples:
#define sin(x) \ (__builtin_overload("sin", "tgmath", 1, x, sinf, sin, sinl, csinf, csin, csinl)(x)) #define fma(x,y,z) \ (__builtin_overload("fma", "tgmath", 3, x, y, z, fmaf, fma, fmal)(x,y,z)) #define ldexp(x, y) \ (__builtin_overload("ldexp", "tgmath1", 2, x, 0, ldexpf, ldexp, ldexpl)(x,y))
Description:
The first argument to __builtin_overload is a string that is the name of the "function" being implemented. This is used to produce diagnostics that make sense to the user. For example, if you accidentally pass a pointer argument to "sin" in GCC, it emits 6 errors about incompatible types. This name allows Clang to diagnose the error in a way the user can understand.
The second argument is a string that indicates a set of promotion rules to apply to the arguments before prototype matching occurs. The currently supported rules are:
- tgmath
- Follow the rules of C99 7.22 to determine a single common type, and use it for every argument.
- tgmath1
- Follow the rules of C99 7.22 to determine a single common type of just the first argument (e.g. treat ints as doubles).
The third argument is an integer that specifies the arity of the function being overloaded. After this are N expression arguments which are promoted according to the rules specified by the promotion rule string.
The final arguments are functions or function pointers with different signatures. __builtin_overload will match and evaluate to the first function pointer whose signature is compatible and does not cause value truncation of any arguments to the function.
__builtin_shufflevector
todo describe me.