# Clang Language Extensions

- Introduction
- Vectors and Extended Vectors
- Blocks
- Builtin Functions
  - __builtin_overload
  - __builtin_shufflevector

## 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.

## 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.

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.