\Header{Foreign}{Foreign Function Interface}
\alias{.C}{Foreign}
\alias{.Call}{Foreign}
\alias{.External}{Foreign}
\alias{.Fortran}{Foreign}
\keyword{programming}{Foreign}
\begin{Description}\relax
Functions to make calls to compiled code that has been loaded into \R.\end{Description}
\begin{Usage}
\begin{verbatim}
       .C(name, ..., NAOK = FALSE, DUP = TRUE, PACKAGE)
 .Fortran(name, ..., NAOK = FALSE, DUP = TRUE, PACKAGE)
.External(name, ...)
    .Call(name, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{name}] a character string giving the name of a C function or
Fortran subroutine.
\item[\code{...}] arguments to be passed to the foreign function.
\item[\code{NAOK}] if \code{TRUE} then any \code{\Link{NA}} or
\code{\Link{NaN}} or \code{\Link{Inf}} values in the arguments are
passed on to the foreign function.  If \code{FALSE}, the presence of
\code{NA} or \code{NaN} or \code{Inf} values is regarded as an error.
\item[\code{DUP}] if \code{TRUE} then arguments are ``duplicated'' before
their address is passed to C or Fortran.
\item[\code{PACKAGE}] if supplied, confine the search for the \code{name} to
the DLL given by this argument (plus the conventional extension,
\code{.so}, \code{.sl}, \code{.dll}, \dots).  This is intended to
add safety for packages, which can ensure that no other package can
override their external symbols by using this argument.  Use
\code{PACKAGE="base"} for symbols linked in to \R.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The functions \code{.C} and \code{.Fortran} can be used to make calls
to C and Fortran code.

\code{.External} can be used to call compiled code that uses \R{}
objects in the same way as internal \R{} functions.

\code{.Call} can be used call compiled code which makes use of
internal \R{} objects.  The arguments are passed to the C code as a
sequence of \R{} objects.  It is included to provide compatibility with
S version 4.

For details about how to write code to use with \code{.Call} and
\code{.External}, see the chapter on ``System and foreign language
interfaces'' in ``Writing \R{} Extensions'' in the \file{doc/manual}
subdirectory of the \R{} source tree).\end{Details}
\begin{Value}
The functions \code{.C} and \code{.Fortran} return a list similar to
the \code{...} list of arguments passed in, but reflecting any
changes made by the C or Fortran code.

\code{.External} and \code{.Call} return an \R{} object.

These calls are typically made in conjunction with
\code{\Link{dyn.load}} which links DLLs to \R.\end{Value}
\begin{Section}{Argument types}
The mapping of the types of \R{} arguments to C or Fortran arguments
in \code{.C} or \code{.Fortran} is

\Tabular{lll}{
\R{} &     C &     Fortran\\
integer & int * & integer\\
numeric & double * & double precision\\
-- or -- & float * & real\\
complex & Rcomplex * & double complex\\
logical & int * & integer \\
character & char ** & [see below]\\
list & SEXP *& not allowed\\
other & SEXP& not allowed\\
}

Numeric vectors in \R{} will be passed as type \code{double *} to C (and
as \code{double precision} to Fortran) unless (i) \code{.C} or
\code{.Fortran} is used, (ii) \code{DUP} is false and (iii) the
argument has attribute \code{Csingle} set to \code{TRUE} (use
\code{\Link{as.single}} or \code{\Link{single}}).  This mechanism is
only intended to be use to facilitate the interfacing of existing C
and Fortran code.

The C type \code{Rcomplex} is defined in \file{Complex.h} as a
\code{typedef struct \{double r; double i;\}}.  Fortran type
\code{double complex} is an extension to the Fortran standard, and the
availability of a mapping of \code{complex} to Fortran may be compiler
dependent.

\emph{Note:} The C types corresponding to \code{integer} and
\code{logical} are \code{int}, not \code{long} as in S.

The first character string of a character vector is passed as a C
character array to Fortran: that string may be usable as
\code{character*255} if its true length is passed separately.  Only up
to 255 characters of the string are passed back.

Functions, expressions, environments and other language elements are
passed as the internal \R{} pointer type \code{SEXP}.  This type is
defined in \file{Rinternals.h} or the arguments can be declared as
generic pointers, \code{void *}.  Lists are passed as C arrays of
\code{SEXP} and can be declared as \code{void *} or \code{SEXP *}.

\R{} functions can be invoked using \code{call\_S} or \code{call\_R} and
can be passed lists or the simple types as arguments.\end{Section}
\begin{Section}{Header files for external code}
Writing code for use with \code{.External} and \code{.Call} will
use internal \R{} structures. If possible use just those defined in
\file{Rinternals.h} and/or the macros in \file{Rdefines.h},
as other header files are not installed and are even more
likely to be changed.\end{Section}
\begin{Note}\relax
\emph{\code{DUP=FALSE} is dangerous.}

There are three important dangers with \code{DUP=FALSE}. The first is that
garbage collection may move the object, resulting in the pointers
pointing nowhere useful and causing hard-to-reproduce bugs.

The second is that if you pass a formal parameter of the calling
function to \code{.C}/\code{.Fortran} with \code{DUP=FALSE}, it may not
necessarily be copied.  You may be able to change not only the local
variable but the variable one level up.  This will also be very hard to
trace.

The third is that lists are passed as a single \R{} \code{SEXP} with
\code{DUP=FALSE}, not as an array of \code{SEXP}. This means the
accessor macros in \file{Rinternals.h} are needed to get at the list
elements and the lists cannot be passed to \code{call\_S}/\code{call\_R}.

1.  If your C/Fortran routine calls back any \R{} function including
\code{S\_alloc}/\code{R\_alloc} then do not use \code{DUP=FALSE}.  Do not
even think about it.  Calling almost any \R{} function could trigger
garbage collection.

2.  If you don't trigger garbage collection it is safe and useful to set
\code{DUP=FALSE} if you don't change any of the variables that might be
affected, e.g.,

\code{.C("Cfunction", input=x, output=numeric(10))}.

In this case the output variable didn't exist before the call so it can't
cause trouble. If the input variable is not changed in \code{Cfunction} you are
safe.\end{Note}
\begin{SeeAlso}\relax
\code{\Link{dyn.load}}.\end{SeeAlso}

