[1b34b87] | 1 | \chapter{\CFA} \label{CFA} |
---|
| 2 | |
---|
| 3 | \section{Introduction} |
---|
| 4 | Similar to \CC, C is a popular programming language especially in systems |
---|
| 5 | programming. For example, the Windows NT and Linux kernel are written in C, and they are the foundation of many higher level |
---|
| 6 | and popular projects. Therefore, it is unlikely that the programming language C is |
---|
| 7 | going to disappear any time soon. |
---|
| 8 | |
---|
| 9 | However, C has inherent problems in syntax, semantics and many |
---|
| 10 | more \cite{Reference2}. Even though \CC is meant to fix these problems, \CC has many |
---|
| 11 | irreversible legacy design choices, and newer versions of \CC require significantly more effort to convert C-based projects into \CC. |
---|
| 12 | |
---|
| 13 | To solve this problem, the programming language \CFA is being created at the University of Waterloo. The goal |
---|
| 14 | of the language is to extend C with modern language features that many new |
---|
| 15 | languages have, such as Rust and Go. Hence, the \CFA extension provides a |
---|
| 16 | backward-compatible version of C, while fixing existing problems known in C and |
---|
| 17 | modernizing the language at the same time. |
---|
| 18 | |
---|
| 19 | \section{Overloading} |
---|
| 20 | Overloading is when a compiler permits a name to have multiple meanings. All |
---|
| 21 | programming languages allow simple overloading of operators on basic types such |
---|
| 22 | as the \verb|+| operator (add) on integer and floating-point types. Most programming languages extend |
---|
| 23 | overloading of operators to user-defined types and/or general function |
---|
| 24 | overloading. \CFA also supports overloading of variables and the literals \verb|0/1|. |
---|
| 25 | |
---|
| 26 | \subsection{Variable} |
---|
| 27 | Variables in the same block are allowed to have the same name but different |
---|
| 28 | types. An assignment to a new variable uses that variable's type to infer the |
---|
| 29 | required type, and that type is used to select a variable containing the appropriate type. |
---|
| 30 | |
---|
| 31 | \begin{lstlisting}[numbers=left, xleftmargin=3.0ex, style=C++nokeyword, caption={Overloading variables in \CFA}, label={CFA-overload-var}] |
---|
| 32 | short int MAX = SHRT_MAX; |
---|
| 33 | int MAX = INT_MAX; |
---|
| 34 | double MAX = DBL_MAX; |
---|
| 35 | |
---|
| 36 | // select variable MAX based on its left-hand type |
---|
| 37 | short int s = MAX; // s = SHRT_MAX |
---|
| 38 | int s = MAX; // s = INT_MAX |
---|
| 39 | double s = MAX; // s = DBL_MAX |
---|
| 40 | \end{lstlisting} |
---|
| 41 | |
---|
| 42 | The listing \ref{CFA-overload-var} shows that when variable overloading exists |
---|
| 43 | in the same scope, the variable is selected based on the left side of |
---|
| 44 | initialization/assignment and operands of the right side of the expression. For |
---|
| 45 | instance, the first assignment to variable \verb|s| at line 6, which is type short int, |
---|
| 46 | selects the MAX with the same type. |
---|
| 47 | |
---|
| 48 | \subsection{Function} |
---|
| 49 | Functions in the same block can be overloaded depending on the number and type of |
---|
| 50 | parameters and returns. |
---|
| 51 | |
---|
| 52 | \begin{lstlisting}[numbers=left, xleftmargin=3.0ex, style=C++nokeyword, caption={Overloading routines in \CFA}, |
---|
| 53 | label={CFA-overload-func}] |
---|
| 54 | void f(<@\textcolor{red}{void}@>); // (1) |
---|
| 55 | void f(<@\textcolor{red}{char}@>); // (2) |
---|
| 56 | <@\textcolor{red}{char}@> f(void); // (3) |
---|
| 57 | <@\textcolor{red}{[int,double]}@> f(); // (4) |
---|
| 58 | |
---|
| 59 | f(); // pick (1) |
---|
| 60 | f('a'); // pick (2) |
---|
| 61 | char s = f('a'); // pick (3) |
---|
| 62 | [int, double] s = f(); // pick (4) |
---|
| 63 | \end{lstlisting} |
---|
| 64 | |
---|
| 65 | The listing \ref{CFA-overload-func} shows that when many functions are overloaded in |
---|
| 66 | the same scope, a function is selected based on the combination of its return type and its |
---|
| 67 | arguments. For instance, from line 1-4, four different types of a function called |
---|
| 68 | \verb|f| are declared. For the call \verb|f('a')|, the function selected is the |
---|
| 69 | one on line 2, if the call voids the result. However, if the call assigns to a |
---|
| 70 | char, then the routine on line 3 is selected. This example can be seen on lines |
---|
| 71 | 7-8. |
---|
| 72 | |
---|
| 73 | \subsection{Operator} |
---|
| 74 | An operator name is denoted with \verb|?| for the operand and any standard C |
---|
| 75 | operator. Operator names within the same block can be overloaded depending on |
---|
| 76 | the number and type of parameters and returns. However, operators \verb|&&|, |
---|
| 77 | \verb-||-, \verb|?:| cannot be overloaded because short-circuit semantics |
---|
| 78 | cannot be preserved. |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | \begin{lstlisting}[style=C++nokeyword, caption={Overloading operators in \CFA}, |
---|
| 82 | label={CFA-overload-ops}] |
---|
| 83 | int <@\textcolor{red}{++?}@>(int op); // unary prefix increment |
---|
| 84 | int <@\textcolor{red}{?++}@>(int op); // unary postfix increment |
---|
| 85 | int <@\textcolor{red}{?+?}@>(int op1, int op2); // unary postfix increment |
---|
| 86 | |
---|
| 87 | struct S { double x, double y } |
---|
| 88 | |
---|
| 89 | // overload operator plus-assignment |
---|
| 90 | S <@\textcolor{red}{?+?}@>(S a, S b) { |
---|
| 91 | return (S) {a.x + b.x, a.y + b.y}; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | S a, b, c; |
---|
| 95 | a + b + c; |
---|
| 96 | \end{lstlisting} |
---|
| 97 | |
---|
| 98 | The listing \ref{CFA-overload-ops} shows that operator overloading is permitted |
---|
| 99 | similar to \CC. However, the difference is that the operator name is |
---|
| 100 | denoted with \verb|?| instead, and operator selection uses the return type. |
---|