Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    r0642216 r3dafd83  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Wed May 17 22:42:11 2017
    14 %% Update Count     : 1685
     13%% Last Modified On : Mon May 15 18:29:58 2017
     14%% Update Count     : 1598
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    9494\author{
    9595\huge \CFA Team \medskip \\
    96 \Large Richard Bilson, Peter A. Buhr, Thierry Delisle, \smallskip \\
     96\Large Peter A. Buhr, Richard Bilson, Thierry Delisle, \smallskip \\
    9797\Large Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Rob Schluntz
    9898}% author
     
    217217
    218218As stated, the goal of the \CFA project is to engineer modern language features into C in an evolutionary rather than revolutionary way.
    219 \CC~\cite{C++14,C++} is an example of a similar project;
     219\CC~\cite{c++,ANSI14:C++} is an example of a similar project;
    220220however, it largely extended the language, and did not address many existing problems.\footnote{%
    221221Two important existing problems addressed were changing the type of character literals from ©int© to ©char© and enumerator from ©int© to the type of its enumerators.}
     
    514514The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type.
    515515In the following example, \R{red} is for the base type and \B{blue} is for the qualifiers.
    516 The \CFA declarations move the qualifiers to the left of the base type, \ie move the blue to the left of the red, while the qualifiers have the same meaning but are ordered left to right to specify a variable's type.
     516The \CFA declarations move the qualifiers to the left of the base type, i.e., move the blue to the left of the red, while the qualifiers have the same meaning but are ordered left to right to specify a variable's type.
    517517\begin{quote2}
    518518\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
     
    659659
    660660
    661 \section{Pointer/Reference}
     661\section{Pointer / Reference}
    662662
    663663C provides a \newterm{pointer type};
    664664\CFA adds a \newterm{reference type}.
    665 These types may be derived from a object or routine type, called the \newterm{referenced type}.
    666 Objects of these types contain an \newterm{address}, which is normally a location in memory, but may also address memory-mapped registers in hardware devices.
    667 An integer constant expression with the value 0, or such an expression cast to type ©void *©, is called a \newterm{null-pointer constant}.\footnote{
    668 One way to conceptualize the null pointer is that no variable is placed at this address, so the null-pointer address can be used to denote an uninitialized pointer/reference object;
    669 \ie the null pointer is guaranteed to compare unequal to a pointer to any object or routine.}
    670 An address is \newterm{sound}, if it points to a valid memory location in scope, \ie has not been freed.
    671 Dereferencing an \newterm{unsound} address, including the null pointer, is \Index{undefined}, often resulting in a \Index{memory fault}.
    672 
    673 A program \newterm{object} is a region of data storage in the execution environment, the contents of which can represent values.
    674 In most cases, objects are located in memory at an address, and the variable name for an object is an implicit address to the object generated by the compiler and automatically dereferenced, as in:
     665Both types contain an \newterm{address}, which is normally a location in memory.
     666Special addresses are used to denote certain states or access co-processor memory.
     667By convention, no variable is placed at address 0, so addresses like 0, 1, 2, 3 are often used to denote no-value or other special states.
     668Often dereferencing a special state causes a \Index{memory fault}, so checking is necessary during execution.
     669If the programming language assigns addresses, a program's execution is \Index{sound}, i.e., all addresses are to valid memory locations.
     670C allows programmers to assign addresses, so there is the potential for incorrect addresses, both inside and outside of the computer address-space.
     671
     672Program variables are implicit pointers to memory locations generated by the compiler and automatically dereferenced, as in:
    675673\begin{quote2}
    676 \begin{tabular}{@{}ll@{\hspace{2em}}l@{}}
     674\begin{tabular}{@{}lll@{}}
    677675\begin{cfa}
    678676int x;
     
    693691\end{quote2}
    694692where the right example is how the compiler logically interprets the variables in the left example.
    695 Since a variable name only points to one address during its lifetime, it is an \Index{immutable} \Index{pointer};
    696 hence, the implicit type of pointer variables ©x© and ©y© are constant pointers in the compiler interpretation.
    697 In general, variable addresses are stored in instructions instead of loaded from memory, and hence may not occupy storage.
    698 These approaches are contrasted in the following:
     693Since a variable name only points to one location during its lifetime, it is an \Index{immutable} \Index{pointer};
     694hence, variables ©x© and ©y© are constant pointers in the compiler interpretation.
     695In general, variable addresses are stored in instructions instead of loaded independently, so an instruction fetch implicitly loads a variable's address.
    699696\begin{quote2}
    700697\begin{tabular}{@{}l|l@{}}
    701 \multicolumn{1}{c|}{explicit variable address} & \multicolumn{1}{c}{implicit variable address} \\
    702 \hline
    703698\begin{cfa}
    704699lda             r1,100                  // load address of x
    705 ld               r2,(r1)                  // load value of x
     700ld              r2,(r1)                   // load value of x
    706701lda             r3,104                  // load address of y
    707 st               r2,(r3)                  // store x into y
     702st              r2,(r3)                   // store x into y
    708703\end{cfa}
    709704&
     
    716711\end{tabular}
    717712\end{quote2}
    718 Finally, the immutable nature of a variable's address and the fact that there is no storage for the variable pointer means pointer assignment\index{pointer!assignment}\index{assignment!pointer} is impossible.
    719 Therefore, the expression ©x = y© has only one meaning, ©*x = *y©, \ie manipulate values, which is why explicitly writing the dereferences is unnecessary even though it occurs implicitly as part of instruction decoding.
    720 
    721 A \Index{pointer}/\Index{reference} object is a generalization of an object variable-name, \ie a mutable address that can point to more than one memory location during its lifetime.
    722 (Similarly, an integer variable can contain multiple integer literals during its lifetime versus an integer constant representing a single literal during its lifetime and, like a variable name, may not occupy storage as the literal is embedded directly into instructions.)
     713Finally, the immutable nature of a variable's address and the fact that there is no storage for a variable address means pointer assignment\index{pointer!assignment}\index{assignment!pointer} is impossible.
     714Therefore, the expression ©x = y© only has one meaning, ©*x = *y©, i.e., manipulate values, which is why explicitly writing the dereferences is unnecessary even though it occurs implicitly as part of instruction decoding.
     715
     716A \Index{pointer}/\Index{reference} is a generalization of a variable name, i.e., a mutable address that can point to more than one memory location during its lifetime.
     717(Similarly, an integer variable can contain multiple integer literals during its lifetime versus an integer constant representing a single literal during its lifetime and may not occupy storage as the literal is embedded directly into instructions.)
    723718Hence, a pointer occupies memory to store its current address, and the pointer's value is loaded by dereferencing, \eg:
    724719\begin{quote2}
    725 \begin{tabular}{@{}l@{\hspace{2em}}l@{}}
     720\begin{tabular}{@{}ll@{}}
    726721\begin{cfa}
    727722int x, y, ®*® p1, ®*® p2, ®**® p3;
     
    732727\end{cfa}
    733728&
    734 \raisebox{-0.5\totalheight}{\input{pointer2.pstex_t}}
     729\raisebox{-0.45\totalheight}{\input{pointer2.pstex_t}}
    735730\end{tabular}
    736731\end{quote2}
    737732
    738733Notice, an address has a duality\index{address!duality}: a location in memory or the value at that location.
    739 In many cases, a compiler might be able to infer the best meaning for these two cases.
    740 For example, \Index*{Algol68}~\cite{Algol68} inferences pointer dereferencing to select the best meaning for each pointer usage
     734In many cases, a compiler might be able to infer the meaning:
    741735\begin{cfa}
    742736p2 = p1 + x;                                    §\C{// compiler infers *p2 = *p1 + x;}§
    743737\end{cfa}
    744 Algol68 infers the following deferencing ©*p2 = *p1 + x©, because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation.
    745 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices.
    746 
    747 Rather than dereference inferencing, most programming languages pick one implicit dereferencing semantics, and the programmer explicitly indicates the other to resolve address-duality.
    748 In C, objects of pointer type always manipulate the pointer object's address:
    749 \begin{cfa}
    750 p1 = p2;                                                §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§
    751 p2 = p1 + x;                                    §\C{// p2 = p1 + x\ \ rather than\ \ *p1 = *p1 + x}§
    752 \end{cfa}
    753 even though the assignment to ©p2© is likely incorrect, and the programmer probably meant:
     738because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation.
     739\Index*{Algol68}~\cite{Algol68} inferences pointer dereferencing to select the best meaning for each pointer usage.
     740However, in C, the following cases are ambiguous, especially with pointer arithmetic:
     741\begin{cfa}
     742p1 = p2;                                                §\C{// p1 = p2\ \ or\ \ *p1 = *p2}§
     743p1 = p1 + 1;                                    §\C{// p1 = p1 + 1\ \ or\ \ *p1 = *p1 + 1}§
     744\end{cfa}
     745
     746Most languages pick one meaning as the default and the programmer explicitly indicates the other meaning to resolve the address-duality ambiguity\index{address! ambiguity}.
     747In C, the default meaning for pointers is to manipulate the pointer's address and the pointed-to value is explicitly accessed by the dereference operator ©*©.
    754748\begin{cfa}
    755749p1 = p2;                                                §\C{// pointer address assignment}§
    756 ®*®p2 = ®*®p1 + x;                              §\C{// pointed-to value assignment / operation}§
    757 \end{cfa}
    758 The C semantics works well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©).
     750*p1 = *p1 + 1;                                  §\C{// pointed-to value assignment / operation}§
     751\end{cfa}
     752which works well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©).
    759753
    760754However, in most other situations, the pointed-to value is requested more often than the pointer address.
     
    768762\end{cfa}
    769763
    770 To support this common case, a reference type is introduced in \CFA, denoted by ©&©, which is the opposite dereference semantics to a pointer type, making the value at the pointed-to location the implicit semantics for dereferencing.
     764To switch the default meaning for an address requires a new kind of pointer, called a \newterm{reference} denoted by ©&©.
    771765\begin{cfa}
    772766int x, y, ®&® r1, ®&® r2, ®&&® r3;
     
    779773Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example.
    780774Hence, a reference behaves like the variable name for the current variable it is pointing-to.
    781 One way to conceptualize a reference is via a rewrite rule, where the compiler inserts a dereference operator before the reference variable for each reference qualifier in a declaration, so the previous example becomes:
     775The simplest way to understand a reference is to imagine the compiler inserting a dereference operator before the reference variable for each reference qualifier in a declaration, \eg:
     776\begin{cfa}
     777r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15);
     778\end{cfa}
     779is rewritten as:
    782780\begin{cfa}
    783781®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15);
     
    795793(&(&®*®)®*®)r3 = &(&®*®)r2;             §\C{// (\&*) cancel giving address of r2, (\&(\&*)*) cancel giving variable r3}§
    796794\end{cfa}
    797 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth.
    798 
    799 Fundamentally, pointer and reference objects are functionally interchangeable because both contain addresses.
     795Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth, and pointer and reference values are interchangeable because both contain addresses.
    800796\begin{cfa}
    801797int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
     
    809805&&&r3 = p3;                                             §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§
    810806\end{cfa}
    811 Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types.
    812 Therefore, the choice between them is based solely on whether the address is dereferenced frequently or infrequently, which dictates the amount of dereferencing aid from the compiler.
    813 
    814 As for a pointer type, a reference type may have qualifiers:
     807Finally, implicit dereferencing and cancellation are a static (compilation) phenomenon not a dynamic one.
     808That is, all implicit dereferencing and any cancellation is carried out prior to the start of the program, so reference performance is equivalent to pointer performance.
     809A programmer selects a pointer or reference type solely on whether the address is dereferenced frequently or infrequently, which dictates the amount of direct aid from the compiler;
     810otherwise, everything else is equal.
     811
     812Interestingly, \Index*[C++]{\CC} deals with the address duality by making the pointed-to value the default, and prevent\-ing changes to the reference address, which eliminates half of the duality.
     813\Index*{Java} deals with the address duality by making address assignment the default and requiring field assignment (direct or indirect via methods), i.e., there is no builtin bit-wise or method-wise assignment, which eliminates half of the duality.
     814
     815As for a pointer, a reference may have qualifiers:
    815816\begin{cfa}
    816817const int cx = 5;                               §\C{// cannot change cx;}§
     
    818819®&®cr = &cx;                                    §\C{// can change cr}§
    819820cr = 7;                                                 §\C{// error, cannot change cx}§
    820 int & const rc = x;                             §\C{// must be initialized
     821int & const rc = x;                             §\C{// must be initialized, \CC reference
    821822®&®rc = &x;                                             §\C{// error, cannot change rc}§
    822 const int & const crc = cx;             §\C{// must be initialized
     823const int & const crc = cx;             §\C{// must be initialized, \CC reference
    823824crc = 7;                                                §\C{// error, cannot change cx}§
    824825®&®crc = &cx;                                   §\C{// error, cannot change crc}§
    825826\end{cfa}
    826 Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be the null pointer unless an arbitrary pointer is coerced into the reference}:
    827 \begin{cfa}
    828 int & const cr = *0;                    §\C{// where 0 is the int * zero}§
    829 \end{cfa}
    830 Note, constant reference types do not prevent addressing errors because of explicit storage-management:
    831 \begin{cfa}
    832 int & const cr = *malloc();
    833 delete &cr;
    834 cr = 7;                                                 §\C{// unsound pointer dereference}§
    835 \end{cfa}
    836 
     827Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be ©0© unless an arbitrary pointer is assigned to the reference}, \eg:
     828\begin{cfa}
     829int & const r = *0;                             §\C{// where 0 is the int * zero}§
     830\end{cfa}
     831Otherwise, the compiler is managing the addresses for type ©& const© not the programmer, and by a programming discipline of only using references with references, address errors can be prevented.
    837832Finally, the position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers.
    838833The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations;
     
    854849where the \CFA declaration is read left-to-right (see \VRef{s:Declarations}).
    855850
    856 In contract to \CFA reference types, \Index*[C++]{\CC{}}'s reference types are all ©const© references, preventing changes to the reference address, so only value assignment is possible, which eliminates half of the \Index{address duality}.
    857 \Index*{Java}'s reference types to objects (because all Java objects are on the heap) are like C pointers, which always manipulate the address and there is no (bit-wise) object assignment, so objects are explicitly cloned by shallow or deep copying, which eliminates half of the address duality.
    858 
    859851\Index{Initialization} is different than \Index{assignment} because initialization occurs on the empty (uninitialized) storage on an object, while assignment occurs on possibly initialized storage of an object.
    860852There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, return/temporary binding.
    861853For reference initialization (like pointer), the initializing value must be an address (\Index{lvalue}) not a value (\Index{rvalue}).
    862854\begin{cfa}
    863 int * p = &x;                                   §\C{// both \&x and x are possible interpretations in C
     855int * p = &x;                                   §\C{// both \&x and x are possible interpretations
    864856int & r = x;                                    §\C{// x unlikely interpretation, because of auto-dereferencing}§
    865857\end{cfa}
    866 C allows ©p© to be assigned with ©&x© or ©x© (many compilers warn about the latter assignment).
    867 \CFA allows ©r© to be assigned ©x© only because it inferences a dereference for ©x©, by implicitly inserting a address-of operator, ©&©, before the initialization expression because a reference behaves like the variable name it is pointing-to.
    868 Similarly, when a reference is used for a parameter/return type, the call-site argument does not require a reference operator for the same reason.
     858Hence, the compiler implicitly inserts a reference operator, ©&©, before the initialization expression.
     859Similarly, when a reference is used for a parameter/return type, the call-site argument does not require a reference operator.
    869860\begin{cfa}
    870861int & f( int & rp );                    §\C{// reference parameter and return}§
     
    872863\end{cfa}
    873864Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©rp© can be locally reassigned within ©f©.
    874 Since operator routine ©?+?© takes its arguments by value, the references returned from ©f© are used to initialize compiler generated temporaries with value semantics that copy from the references.
     865Since ©?+?© takes its arguments by value, the references returned from ©f© are used to initialize compiler generated temporaries with value semantics that copy from the references.
    875866
    876867When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions.
     
    882873\end{cfa}
    883874Here, the compiler passes the address to the literal 3 or the temporary for the expression ©x + y©, knowing the argument cannot be changed through the parameter.
    884 (The ©&© is necessary for the pointer-type parameter to make the types match, and is a common requirement for a C programmer.)
     875(The ©&© is necessary for the pointer parameter to make the types match, and is a common requirement for a C programmer.)
    885876\CFA \emph{extends} this semantics to a mutable pointer/reference parameter, and the compiler implicitly creates the necessary temporary (copying the argument), which is subsequently pointed-to by the reference parameter and can be changed.
    886877\begin{cfa}
     
    894885The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call.
    895886
    896 While \CFA attempts to handle pointers and references in a uniform, symmetric manner, C handles routine objects in an inconsistent way: a routine object is both a pointer and a reference (particle and wave).
     887While \CFA attempts to handle pointers and references in a uniform, symmetric manner, C handles routine variables in an inconsistent way: a routine variable is both a pointer and a reference (particle and wave).
    897888\begin{cfa}
    898889void f( int p ) {...}
     
    902893fp(3);                                                  §\C{// reference invocation}§
    903894\end{cfa}
    904 A routine object is best described by a ©const© reference:
     895A routine variable is best described by a ©const© reference:
    905896\begin{cfa}
    906897const void (&fp)( int ) = f;
     
    909900&fp = ...;                                              §\C{// changing routine reference}§
    910901\end{cfa}
    911 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{
     902because the value of the routine variable is a routine literal, i.e., the routine code is normally immutable during execution.\footnote{
    912903Dynamic code rewriting is possible but only in special circumstances.}
    913 \CFA allows this additional use of references for routine objects in an attempt to give a more consistent meaning for them.
    914 
    915 
    916 \begin{comment}
    917 \section{References}
    918 
    919 By introducing references in parameter types, users are given an easy way to pass a value by reference, without the need for NULL pointer checks.
    920 In structures, a reference can replace a pointer to an object that should always have a valid value.
    921 When a structure contains a reference, all of its constructors must initialize the reference and all instances of this structure must initialize it upon definition.
    922 
    923 The syntax for using references in \CFA is the same as \CC with the exception of reference initialization.
    924 Use ©&© to specify a reference, and access references just like regular objects, not like pointers (use dot notation to access fields).
    925 When initializing a reference, \CFA uses a different syntax which differentiates reference initialization from assignment to a reference.
    926 The ©&© is used on both sides of the expression to clarify that the address of the reference is being set to the address of the variable to which it refers.
    927 
    928 
    929 From: Richard Bilson <rcbilson@gmail.com>
    930 Date: Wed, 13 Jul 2016 01:58:58 +0000
    931 Subject: Re: pointers / references
    932 To: "Peter A. Buhr" <pabuhr@plg2.cs.uwaterloo.ca>
    933 
    934 As a general comment I would say that I found the section confusing, as you move back and forth
    935 between various real and imagined programming languages. If it were me I would rewrite into two
    936 subsections, one that specifies precisely the syntax and semantics of reference variables and
    937 another that provides the rationale.
    938 
    939 I don't see any obvious problems with the syntax or semantics so far as I understand them. It's not
    940 obvious that the description you're giving is complete, but I'm sure you'll find the special cases
    941 as you do the implementation.
    942 
    943 My big gripes are mostly that you're not being as precise as you need to be in your terminology, and
    944 that you say a few things that aren't actually true even though I generally know what you mean.
    945 
    946 20 C provides a pointer type; CFA adds a reference type. Both types contain an address, which is normally a
    947 21 location in memory.
    948 
    949 An address is not a location in memory; an address refers to a location in memory. Furthermore it
    950 seems weird to me to say that a type "contains" an address; rather, objects of that type do.
    951 
    952 21 Special addresses are used to denote certain states or access co-processor memory. By
    953 22 convention, no variable is placed at address 0, so addresses like 0, 1, 2, 3 are often used to denote no-value
    954 23 or other special states.
    955 
    956 This isn't standard C at all. There has to be one null pointer representation, but it doesn't have
    957 to be a literal zero representation and there doesn't have to be more than one such representation.
    958 
    959 23 Often dereferencing a special state causes a memory fault, so checking is necessary
    960 24 during execution.
    961 
    962 I don't see the connection between the two clauses here. I feel like if a bad pointer will not cause
    963 a memory fault then I need to do more checking, not less.
    964 
    965 24 If the programming language assigns addresses, a program's execution is sound, \ie all
    966 25 addresses are to valid memory locations.
    967 
    968 You haven't said what it means to "assign" an address, but if I use my intuitive understanding of
    969 the term I don't see how this can be true unless you're assuming automatic storage management.
    970 
    971 1 Program variables are implicit pointers to memory locations generated by the compiler and automatically
    972 2 dereferenced, as in:
    973 
    974 There is no reason why a variable needs to have a location in memory, and indeed in a typical
    975 program many variables will not. In standard terminology an object identifier refers to data in the
    976 execution environment, but not necessarily in memory.
    977 
    978 13 A pointer/reference is a generalization of a variable name, \ie a mutable address that can point to more
    979 14 than one memory location during its lifetime.
    980 
    981 I feel like you're off the reservation here. In my world there are objects of pointer type, which
    982 seem to be what you're describing here, but also pointer values, which can be stored in an object of
    983 pointer type but don't necessarily have to be. For example, how would you describe the value denoted
    984 by "&main" in a C program? I would call it a (function) pointer, but that doesn't satisfy your
    985 definition.
    986 
    987 16 not occupy storage as the literal is embedded directly into instructions.) Hence, a pointer occupies memory
    988 17 to store its current address, and the pointer's value is loaded by dereferencing, e.g.:
    989 
    990 As with my general objection regarding your definition of variables, there is no reason why a
    991 pointer variable (object of pointer type) needs to occupy memory.
    992 
    993 21 p2 = p1 + x; // compiler infers *p2 = *p1 + x;
    994 
    995 What language are we in now?
    996 
    997 24 pointer usage. However, in C, the following cases are ambiguous, especially with pointer arithmetic:
    998 25 p1 = p2; // p1 = p2 or *p1 = *p2
    999 
    1000 This isn't ambiguous. it's defined to be the first option.
    1001 
    1002 26 p1 = p1 + 1; // p1 = p1 + 1 or *p1 = *p1 + 1
    1003 
    1004 Again, this statement is not ambiguous.
    1005 
    1006 13 example. Hence, a reference behaves like the variable name for the current variable it is pointing-to. The
    1007 14 simplest way to understand a reference is to imagine the compiler inserting a dereference operator before
    1008 15 the reference variable for each reference qualifier in a declaration, e.g.:
    1009 
    1010 It's hard for me to understand who the audience for this part is. I think a practical programmer is
    1011 likely to be satisfied with "a reference behaves like the variable name for the current variable it
    1012 is pointing-to," maybe with some examples. Your "simplest way" doesn't strike me as simpler than
    1013 that. It feels like you're trying to provide a more precise definition for the semantics of
    1014 references, but it isn't actually precise enough to be a formal specification. If you want to
    1015 express the semantics of references using rewrite rules that's a great way to do it, but lay the
    1016 rules out clearly, and when you're showing an example of rewriting keep your
    1017 references/pointers/values separate (right now, you use \eg "r3" to mean a reference, a pointer,
    1018 and a value).
    1019 
    1020 24 Cancellation works to arbitrary depth, and pointer and reference values are interchangeable because both
    1021 25 contain addresses.
    1022 
    1023 Except they're not interchangeable, because they have different and incompatible types.
    1024 
    1025 40 Interestingly, C++ deals with the address duality by making the pointed-to value the default, and prevent-
    1026 41 ing changes to the reference address, which eliminates half of the duality. Java deals with the address duality
    1027 42 by making address assignment the default and requiring field assignment (direct or indirect via methods),
    1028 43 \ie there is no builtin bit-wise or method-wise assignment, which eliminates half of the duality.
    1029 
    1030 I can follow this but I think that's mostly because I already understand what you're trying to
    1031 say. I don't think I've ever heard the term "method-wise assignment" and I don't see you defining
    1032 it. Furthermore Java does have value assignment of basic (non-class) types, so your summary here
    1033 feels incomplete. (If it were me I'd drop this paragraph rather than try to save it.)
    1034 
    1035 11 Hence, for type & const, there is no pointer assignment, so &rc = &x is disallowed, and the address value
    1036 12 cannot be 0 unless an arbitrary pointer is assigned to the reference.
    1037 
    1038 Given the pains you've taken to motivate every little bit of the semantics up until now, this last
    1039 clause ("the address value cannot be 0") comes out of the blue. It seems like you could have
    1040 perfectly reasonable semantics that allowed the initialization of null references.
    1041 
    1042 12 In effect, the compiler is managing the
    1043 13 addresses for type & const not the programmer, and by a programming discipline of only using references
    1044 14 with references, address errors can be prevented.
    1045 
    1046 Again, is this assuming automatic storage management?
    1047 
    1048 18 rary binding. For reference initialization (like pointer), the initializing value must be an address (lvalue) not
    1049 19 a value (rvalue).
    1050 
    1051 This sentence appears to suggest that an address and an lvalue are the same thing.
    1052 
    1053 20 int * p = &x; // both &x and x are possible interpretations
    1054 
    1055 Are you saying that we should be considering "x" as a possible interpretation of the initializer
    1056 "&x"? It seems to me that this expression has only one legitimate interpretation in context.
    1057 
    1058 21 int & r = x; // x unlikely interpretation, because of auto-dereferencing
    1059 
    1060 You mean, we can initialize a reference using an integer value? Surely we would need some sort of
    1061 cast to induce that interpretation, no?
    1062 
    1063 22 Hence, the compiler implicitly inserts a reference operator, &, before the initialization expression.
    1064 
    1065 But then the expression would have pointer type, which wouldn't be compatible with the type of r.
    1066 
    1067 22 Similarly,
    1068 23 when a reference is used for a parameter/return type, the call-site argument does not require a reference
    1069 24 operator.
    1070 
    1071 Furthermore, it would not be correct to use a reference operator.
    1072 
    1073 45 The implicit conversion allows
    1074 1 seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call.
    1075 2 While C' attempts to handle pointers and references in a uniform, symmetric manner, C handles routine
    1076 3 variables in an inconsistent way: a routine variable is both a pointer and a reference (particle and wave).
    1077 
    1078 After all this talk of how expressions can have both pointer and value interpretations, you're
    1079 disparaging C because it has expressions that have both pointer and value interpretations?
    1080 
    1081 On Sat, Jul 9, 2016 at 4:18 PM Peter A. Buhr <pabuhr@plg.uwaterloo.ca> wrote:
    1082 > Aaron discovered a few places where "&"s are missing and where there are too many "&", which are
    1083 > corrected in the attached updated. None of the text has changed, if you have started reading
    1084 > already.
    1085 \end{comment}
     904\CFA allows this additional use of references for routine variables in an attempt to give a more consistent meaning for them.
    1086905
    1087906
     
    15341353because
    15351354
    1536 Currently, there are no \Index{lambda} expressions, \ie unnamed routines because routine names are very important to properly select the correct routine.
    1537 
    1538 
    1539 \section{Tuples}
     1355Currently, there are no \Index{lambda} expressions, i.e., unnamed routines because routine names are very important to properly select the correct routine.
     1356
     1357
     1358\section{Lexical List}
    15401359
    15411360In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call.
     
    15541373[ v+w, x*y, 3.14159, f() ]
    15551374\end{cfa}
    1556 Tuples are permitted to contain sub-tuples (\ie nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple.
     1375Tuples are permitted to contain sub-tuples (i.e., nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple.
    15571376Note, a tuple is not a record (structure);
    15581377a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1).
     
    16101429tuple does not have structure like a record; a tuple is simply converted into a list of components.
    16111430\begin{rationale}
    1612 The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; \ie a statement such as ©g( f() )© is not supported.
     1431The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; i.e., a statement such as ©g( f() )© is not supported.
    16131432Using a temporary variable to store the  results of the inner routine and then passing this variable to the outer routine works, however.
    16141433\end{rationale}
     
    16231442This requirement is the same as for comma expressions in argument lists.
    16241443
    1625 Type qualifiers, \ie const and volatile, may modify a tuple type.
    1626 The meaning is the same as for a type qualifier modifying an aggregate type [Int99, x 6.5.2.3(7),x 6.7.3(11)], \ie the qualifier is distributed across all of the types in the tuple, \eg:
     1444Type qualifiers, i.e., const and volatile, may modify a tuple type.
     1445The meaning is the same as for a type qualifier modifying an aggregate type [Int99, x 6.5.2.3(7),x 6.7.3(11)], i.e., the qualifier is distributed across all of the types in the tuple, \eg:
    16271446\begin{cfa}
    16281447const volatile [ int, float, const int ] x;
     
    16621481©w© is implicitly opened to yield a tuple of four values, which are then assigned individually.
    16631482
    1664 A \newterm{flattening coercion} coerces a nested tuple, \ie a tuple with one or more components, which are themselves tuples, into a flattened tuple, which is a tuple whose components are not tuples, as in:
     1483A \newterm{flattening coercion} coerces a nested tuple, i.e., a tuple with one or more components, which are themselves tuples, into a flattened tuple, which is a tuple whose components are not tuples, as in:
    16651484\begin{cfa}
    16661485[ a, b, c, d ] = [ 1, [ 2, 3 ], 4 ];
     
    16971516\end{cfa}
    16981517\index{lvalue}
    1699 The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement.
     1518The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, i.e., any data object that can appear on the left-hand side of a conventional assignment statement.
    17001519©$\emph{expr}$© is any standard arithmetic expression.
    17011520Clearly, the types of the entities being assigned must be type compatible with the value of the expression.
     
    17851604[ x1, y1 ] = z = 0;
    17861605\end{cfa}
    1787 As in C, the rightmost assignment is performed first, \ie assignment parses right to left.
     1606As in C, the rightmost assignment is performed first, i.e., assignment parses right to left.
    17881607
    17891608
     
    18501669
    18511670
    1852 \section{Labelled Continue/Break}
     1671\section{Labelled Continue / Break}
    18531672
    18541673While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure.
     
    19471766With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader.
    19481767Finally, using an explicit target for the transfer instead of an implicit target allows new constructs to be added or removed without affecting existing constructs.
    1949 The implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed.
     1768The implicit targets of the current ©continue© and ©break©, i.e., the closest enclosing loop or ©switch©, change as certain constructs are added or removed.
    19501769
    19511770
     
    20841903Furthermore, any statements before the first ©case© clause can only be executed if labelled and transferred to using a ©goto©, either from outside or inside of the ©switch©, both of which are problematic.
    20851904As well, the declaration of ©z© cannot occur after the ©case© because a label can only be attached to a statement, and without a fall through to case 3, ©z© is uninitialized.
    2086 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body.
     1905The key observation is that the ©switch© statement branches into control structure, i.e., there are multiple entry points into its statement body.
    20871906\end{enumerate}
    20881907
     
    20921911the number of ©switch© statements is small,
    20931912\item
    2094 most ©switch© statements are well formed (\ie no \Index*{Duff's device}),
     1913most ©switch© statements are well formed (i.e., no \Index*{Duff's device}),
    20951914\item
    20961915the ©default© clause is usually written as the last case-clause,
     
    21021921\item
    21031922Eliminating default fall-through has the greatest potential for affecting existing code.
    2104 However, even if fall-through is removed, most ©switch© statements would continue to work because of the explicit transfers already present at the end of each ©case© clause, the common placement of the ©default© clause at the end of the case list, and the most common use of fall-through, \ie a list of ©case© clauses executing common code, \eg:
     1923However, even if fall-through is removed, most ©switch© statements would continue to work because of the explicit transfers already present at the end of each ©case© clause, the common placement of the ©default© clause at the end of the case list, and the most common use of fall-through, i.e., a list of ©case© clauses executing common code, \eg:
    21051924\begin{cfa}
    21061925case 1:  case 2:  case 3: ...
     
    23542173
    23552174The following \CC-style \Index{manipulator}s allow control over implicit seperation.
    2356 Manipulators \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} \emph{locally} toggle printing the separator, \ie the seperator is adjusted only with respect to the next printed item.
     2175Manipulators \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} \emph{locally} toggle printing the separator, i.e., the seperator is adjusted only with respect to the next printed item.
    23572176\begin{cfa}[mathescape=off,belowskip=0pt]
    23582177sout | sepOn | 1 | 2 | 3 | sepOn | endl;        §\C{// separator at start of line}§
     
    2367218612 3
    23682187\end{cfa}
    2369 Manipulators \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} \emph{globally} toggle printing the separator, \ie the seperator is adjusted with respect to all subsequent printed items, unless locally adjusted.
     2188Manipulators \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} \emph{globally} toggle printing the separator, i.e., the seperator is adjusted with respect to all subsequent printed items, unless locally adjusted.
    23702189\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
    23712190sout | sepDisable | 1 | 2 | 3 | endl;           §\C{// globally turn off implicit separation}§
     
    26422461\caption{Constructors and Destructors}
    26432462\end{figure}
     2463
     2464
     2465\begin{comment}
     2466\section{References}
     2467
     2468
     2469By introducing references in parameter types, users are given an easy way to pass a value by reference, without the need for NULL pointer checks.
     2470In structures, a reference can replace a pointer to an object that should always have a valid value.
     2471When a structure contains a reference, all of its constructors must initialize the reference and all instances of this structure must initialize it upon definition.
     2472
     2473The syntax for using references in \CFA is the same as \CC with the exception of reference initialization.
     2474Use ©&© to specify a reference, and access references just like regular objects, not like pointers (use dot notation to access fields).
     2475When initializing a reference, \CFA uses a different syntax which differentiates reference initialization from assignment to a reference.
     2476The ©&© is used on both sides of the expression to clarify that the address of the reference is being set to the address of the variable to which it refers.
     2477\end{comment}
    26442478
    26452479
     
    48494683\section{Incompatible}
    48504684
    4851 The following incompatibles exist between \CFA and C, and are similar to Annex C for \CC~\cite{C++14}.
     4685The following incompatibles exist between \CFA and C, and are similar to Annex C for \CC~\cite{ANSI14:C++}.
    48524686
    48534687\begin{enumerate}
     
    49474781Personß.ßFace pretty;                   §\C{// type defined inside}§
    49484782\end{cfa}
    4949 In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \ie the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing.
     4783In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, i.e., the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing.
    49504784\CFA is C \emph{incompatible} on this issue, and provides semantics similar to \Index*[C++]{\CC}.
    49514785Nested types are not hoisted and can be referenced using the field selection operator ``©.©'', unlike the \CC scope-resolution operator ``©::©''.
     
    50444878\end{tabular}
    50454879\end{quote2}
    5046 For the prescribed head-files, \CFA uses header interposition to wraps these includes in an ©extern "C"©;
     4880For the prescribed head-files, \CFA implicitly wraps their includes in an ©extern "C"©;
    50474881hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}).
    50484882All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling.
     
    50524886\label{s:StandardLibrary}
    50534887
    5054 The \CFA standard-library wraps many existing explicitly-polymorphic C general-routines into implicitly-polymorphic versions.
     4888The goal of the \CFA standard-library is to wrap many of the existing C library-routines that are explicitly polymorphic into implicitly polymorphic versions.
    50554889
    50564890
     
    51765010\label{s:Math Library}
    51775011
    5178 The \CFA math-library wraps many existing explicitly-polymorphic C math-routines into implicitly-polymorphic versions.
     5012The goal of the \CFA math-library is to wrap many of the existing C math library-routines that are explicitly polymorphic into implicitly polymorphic versions.
    51795013
    51805014
Note: See TracChangeset for help on using the changeset viewer.