\chapter{\CFA Existing Features}

\CFA (C-for-all)~\cite{Cforall} is an open-source project extending ISO C with modern safety and productivity features, while still ensuring backwards compatibility with C and its programmers.
\CFA is designed to have an orthogonal feature-set based closely on the C programming paradigm (non-object-oriented) and these features can be added incrementally to an existing C code-base allowing programmers to learn \CFA on an as-needed basis.

\section{Overloading and extern}
Cforall has overloading, allowing multiple definitions of the same name to
be defined.~\cite{Moss18}

This also adds name mangling so that the assembly symbols are unique for
different overloads. For compatability with names in C there is also a
syntax to diable the name mangling. These unmangled names cannot be overloaded
but act as the interface between C and \CFA code.

The syntax for disabling mangling is:
\begin{cfa}
extern "C" {
    ...
}
\end{cfa}

To re-enable mangling once it is disabled the syntax is:
\begin{cfa}
extern "Cforall" {
    ...
}
\end{cfa}

Both should occur at the declaration level and effect all the declarations
in @...@. Neither care about the state of mangling when they begin
and will return to that state after the group is finished. So re-enabling
is only used to nest areas of mangled and unmangled declarations.

\section{References}
\CFA adds references to C. These are auto-dereferencing pointers and use the
same syntax as pointers except they use ampersand (@&@) instead of
the asterisk (@*@). They can also be constaint or mutable, if they
are mutable they may be assigned to by using the address-of operator
(@&@) which converts them into a pointer.

\section{Constructors and Destructors}

Both constructors and destructors are operators, which means they are just
functions with special names. The special names are used to define them and
may be used to call the functions expicately. The \CFA special names are
constructed by taking the tokens in the operators and putting @?@ where
the arguments would go. So multiplication is @?*?@ while dereference
is @*?@. This also make it easy to tell the difference between
pre-fix operations (such as @++?@) and post-fix operations
(@?++@).

The special name for contructors is @?{}@, which comes from the
initialization syntax in C. The special name for destructors is
@^{}@. % I don't like the \^{} symbol but $^\wedge$ isn't better.

Any time a type T goes out of scope the destructor matching
@void ^?{}(T &);@ is called. In theory this is also true of
primitive types such as @int@, but in practice those are no-ops and
are usually omitted for optimization.

\section{Polymorphism}
\CFA uses polymorphism to create functions and types that are defined over
different types. \CFA polymorphic declarations serve the same role as \CC
templates or Java generics.

Polymorphic declaractions start with a forall clause that goes before the
standard (monomorphic) declaration. These declarations have the same syntax
except that you may use the names introduced by the forall clause in them.

Forall clauses are written @forall( ... )@ where @...@ becomes
the list of polymorphic variables (local type names) and assertions, which
repersent required operations on those types.

\begin{cfa}
forall(dtype T | { void do_once(T &); })
void do_twice(T & value) {
    do_once(value);
    do_once(value);
}
\end{cfa}

A polymorphic function can be used in the same way normal functions are.
The polymorphics variables are filled in with concrete types and the
assertions are checked. An assertion checked by seeing if that name of that
type (with all the variables replaced with the concrete types) is defined at
the the call site.

As an example, even if no function named @do_once@ is not defined
near the definition of @do_twice@ the following code will work.
\begin{cfa}
int quadruple(int x) {
    void do_once(int & y) {
        y = y * 2;
    }
    do_twice(x);
    return x;
}
\end{cfa}
This is not the recommended way to implement a quadruple function but it
does work. The complier will deduce that @do_twice@'s T is an
integer from the argument. It will then look for a definition matching the
assertion which is the @do_once@ defined within the function. That
function will be passed in as a function pointer to @do_twice@ and
called within it.

To avoid typing out long lists of assertions again and again there are also
traits which collect assertions into convenent packages that can then be used
in assertion lists instead of all of their components.
\begin{cfa}
trait done_once(dtype T) {
    void do_once(T &);
}
\end{cfa}

After this the forall list in the previous example could instead be written
with the trait instead of the assertion itself.
\begin{cfa}
forall(dtype T | done_once(T))
\end{cfa}

Traits can have arbitrary number of assertions in them and are usually used to
create short hands for, and give descriptive names to, commond groupings of
assertions.

Polymorphic structures and unions may also be defined by putting a forall
clause before the declaration. The type variables work the same way except
are now used in field declaractions instead of parameters and local variables.

\begin{cfa}
forall(dtype T)
struct node {
    node(T) * next;
    T * data;
}
\end{cfa}

The @node(T)@ is a use of a polymorphic structure. Polymorphic types
must be provided their polymorphic parameters.

There are many other features of polymorphism that have not given here but
these are the ones used by the exception system.

\section{Concurrency}

\CFA has a number of concurrency features, @thread@s,
@monitor@s and @mutex@ parameters, @coroutine@s and
@generator@s. The two features that interact with the exception system
are @thread@s and @coroutine@s; they and their supporting
constructs will be described here.

\subsection{Coroutines}
Coroutines are routines that do not have to finish execution to hand control
back to their caller, instead they may suspend their execution at any time
and resume it later.
Coroutines are not true concurrency but share some similarities and many of
the same underpinnings and so are included as part of the \CFA threading
library.

In \CFA coroutines are created using the @coroutine@ keyword which
works just like @struct@ except that the created structure will be
modified by the compiler to satify the @is_coroutine@ trait.

These structures act as the interface between callers and the coroutine,
the fields are used to pass information in and out. Here is a simple example
where the single field is used to pass the next number in a sequence out.
\begin{cfa}
coroutine CountUp {
    unsigned int next;
}
\end{cfa}

The routine part of the coroutine is a main function for the coroutine. It
takes a reference to a coroutine object and returns nothing. In this function,
and any functions called by this function, the suspend statement may be used
to return execution to the coroutine's caller. When control returns to the
function it continue from that same suspend statement instead of at the top
of the function.
\begin{cfa}
void main(CountUp & this) {
    unsigned int next = 0;
    while (true) {
        this.next = next;
        suspend;
        next = next + 1;
    }
}
\end{cfa}

Control is passed to the coroutine with the resume function. This includes the
first time when the coroutine is starting up. The resume function takes a
reference to the coroutine structure and returns the same reference. The
return value is for easy access to communication variables. For example the
next value from a count-up can be generated and collected in a single
expression: @resume(count).next@.

\subsection{Monitors and Mutex}

True concurrency does not garrenty ordering. To get some of that ordering back
\CFA uses monitors and mutex (mutual exclution) parameters. A monitor is
another special declaration that contains a lock and is compatable with mutex
parameters.

Function parameters can have the @mutex@ qualifiers on reference
arguments, for example @void example(a_monitor & mutex arg);@. When the
function is called it will acquire the lock on all of the mutex parameters.

This means that all functions that mutex on a type are part of a critical
section and only one will ever run at a time.

\subsection{Threads}
While coroutines allow new things to be done with a single execution path
threads actually introduce new paths of execution that continue independently.
Now for threads to work together their must be some communication between them
and that means the timing of certain operations does have to be known. There
or various means of syncronization and mutual exclution provided by \CFA but
for exceptions only the basic two -- fork and join -- are needed.

Threads are created like coroutines except the keyword is changed:
\begin{cfa}
thread StringWorker {
    const char * input;
    int result;
};

void main(StringWorker & this) {
    const char * localCopy = this.input;
    // ... do some work, perhaps hashing the string ...
    this.result = result;
}
\end{cfa}
The main function will start executing after the fork operation and continue
executing until it is finished. If another thread joins with this one it will
wait until main has completed execution. In other words everything the thread
does is between fork and join.

From the outside this is the creation and destruction of the thread object.
Fork happens after the constructor is run and join happens before the
destructor runs. Join also happens during the @join@ function which
can be used to join a thread earlier. If it is used the destructor does not
join as that has already been completed.
