source: doc/theses/lynn_tran_SE499/Chapters/CFA.tex @ d6ad99e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since d6ad99e was 1b34b87, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

Lynn's GDB essay

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