\chapter{\CFA} \label{CFA}

\section{Introduction}
Similar to \CC, C is a popular programming language especially in systems
programming. For example, the Windows NT and Linux kernel are written in C, and they are the foundation of many higher level
and popular projects. Therefore, it is unlikely that the programming language C is
going to disappear any time soon.

However, C has inherent problems in syntax, semantics and many
more \cite{Reference2}. Even though \CC is meant to fix these problems, \CC has many
irreversible legacy design choices, and newer versions of \CC require significantly more effort to convert C-based projects into \CC.

To solve this problem, the programming language \CFA is being created at the University of Waterloo. The goal
of the language is to extend C with modern language features that many new
languages have, such as Rust and Go. Hence, the \CFA extension provides a
backward-compatible version of C, while fixing existing problems known in C and
modernizing the language at the same time.

\section{Overloading}
Overloading is when a compiler permits a name to have multiple meanings. All
programming languages allow simple overloading of operators on basic types such
as the \verb|+| operator (add) on integer and floating-point types. Most programming languages extend
overloading of operators to user-defined types and/or general function
overloading. \CFA also supports overloading of variables and the literals \verb|0/1|.

\subsection{Variable}
Variables in the same block are allowed to have the same name but different
types. An assignment to a new variable uses that variable's type to infer the
required type, and that type is used to select a variable containing the appropriate type.

\begin{lstlisting}[numbers=left, xleftmargin=3.0ex, style=C++nokeyword, caption={Overloading variables in \CFA}, label={CFA-overload-var}]
short int MAX = SHRT_MAX;
int MAX = INT_MAX;
double MAX = DBL_MAX;

// select variable MAX based on its left-hand type
short int s = MAX;      // s = SHRT_MAX
int s = MAX;            // s = INT_MAX
double s = MAX;         // s = DBL_MAX
\end{lstlisting}

The listing \ref{CFA-overload-var} shows that when variable overloading exists
in the same scope, the variable is selected based on the left side of
initialization/assignment and operands of the right side of the expression. For
instance, the first assignment to variable \verb|s| at line 6, which is type short int,
selects the MAX with the same type.

\subsection{Function}
Functions in the same block can be overloaded depending on the number and type of
parameters and returns.

\begin{lstlisting}[numbers=left, xleftmargin=3.0ex, style=C++nokeyword, caption={Overloading routines in \CFA},
label={CFA-overload-func}]
void f(<@\textcolor{red}{void}@>);              // (1)
void f(<@\textcolor{red}{char}@>);              // (2)
<@\textcolor{red}{char}@> f(void);              // (3)
<@\textcolor{red}{[int,double]}@> f();           // (4)

f();                      // pick (1)
f('a');                   // pick (2)
char s = f('a');          // pick (3)
[int, double] s = f();    // pick (4)
\end{lstlisting}

The listing \ref{CFA-overload-func} shows that when many functions are overloaded in
the same scope, a function is selected based on the combination of its return type and its
arguments. For instance, from line 1-4, four different types of a function called
\verb|f| are declared. For the call \verb|f('a')|, the function selected is the
one on line 2, if the call voids the result. However, if the call assigns to a
char, then the routine on line 3 is selected. This example can be seen on lines
7-8.

\subsection{Operator}
An operator name is denoted with \verb|?| for the operand and any standard C
operator. Operator names within the same block can be overloaded depending on
the number and type of parameters and returns. However, operators \verb|&&|,
\verb-||-, \verb|?:| cannot be overloaded because short-circuit semantics
cannot be preserved.


\begin{lstlisting}[style=C++nokeyword, caption={Overloading operators in \CFA},
label={CFA-overload-ops}]
int <@\textcolor{red}{++?}@>(int op);           // unary prefix increment
int <@\textcolor{red}{?++}@>(int op);           // unary postfix increment
int <@\textcolor{red}{?+?}@>(int op1, int op2); // unary postfix increment

struct S { double x, double y }

// overload operator plus-assignment
S <@\textcolor{red}{?+?}@>(S a, S b) {
    return (S) {a.x + b.x, a.y + b.y};
}

S a, b, c;
a + b + c;
\end{lstlisting}

The listing \ref{CFA-overload-ops} shows that operator overloading is permitted
similar to \CC. However, the difference is that the operator name is
denoted with \verb|?| instead, and operator selection uses the return type.
