Changes in doc/user/user.tex [0642216:3dafd83]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r0642216 r3dafd83 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Wed May 17 22:42:11201714 %% Update Count : 1 68513 %% Last Modified On : Mon May 15 18:29:58 2017 14 %% Update Count : 1598 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 94 94 \author{ 95 95 \huge \CFA Team \medskip \\ 96 \Large Richard Bilson, Peter A. Buhr, Thierry Delisle, \smallskip \\96 \Large Peter A. Buhr, Richard Bilson, Thierry Delisle, \smallskip \\ 97 97 \Large Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Rob Schluntz 98 98 }% author … … 217 217 218 218 As 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; 220 220 however, it largely extended the language, and did not address many existing problems.\footnote{% 221 221 Two 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.} … … 514 514 The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type. 515 515 In 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, \iemove 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.516 The \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. 517 517 \begin{quote2} 518 518 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} … … 659 659 660 660 661 \section{Pointer /Reference}661 \section{Pointer / Reference} 662 662 663 663 C provides a \newterm{pointer type}; 664 664 \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: 665 Both types contain an \newterm{address}, which is normally a location in memory. 666 Special addresses are used to denote certain states or access co-processor memory. 667 By 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. 668 Often dereferencing a special state causes a \Index{memory fault}, so checking is necessary during execution. 669 If the programming language assigns addresses, a program's execution is \Index{sound}, i.e., all addresses are to valid memory locations. 670 C allows programmers to assign addresses, so there is the potential for incorrect addresses, both inside and outside of the computer address-space. 671 672 Program variables are implicit pointers to memory locations generated by the compiler and automatically dereferenced, as in: 675 673 \begin{quote2} 676 \begin{tabular}{@{}ll @{\hspace{2em}}l@{}}674 \begin{tabular}{@{}lll@{}} 677 675 \begin{cfa} 678 676 int x; … … 693 691 \end{quote2} 694 692 where 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: 693 Since a variable name only points to one location during its lifetime, it is an \Index{immutable} \Index{pointer}; 694 hence, variables ©x© and ©y© are constant pointers in the compiler interpretation. 695 In general, variable addresses are stored in instructions instead of loaded independently, so an instruction fetch implicitly loads a variable's address. 699 696 \begin{quote2} 700 697 \begin{tabular}{@{}l|l@{}} 701 \multicolumn{1}{c|}{explicit variable address} & \multicolumn{1}{c}{implicit variable address} \\702 \hline703 698 \begin{cfa} 704 699 lda r1,100 // load address of x 705 ld r2,(r1)// load value of x700 ld r2,(r1) // load value of x 706 701 lda r3,104 // load address of y 707 st r2,(r3)// store x into y702 st r2,(r3) // store x into y 708 703 \end{cfa} 709 704 & … … 716 711 \end{tabular} 717 712 \end{quote2} 718 Finally, the immutable nature of a variable's address and the fact that there is no storage for the variable pointermeans pointer assignment\index{pointer!assignment}\index{assignment!pointer} is impossible.719 Therefore, the expression ©x = y© has only one meaning, ©*x = *y©, \iemanipulate 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, \iea 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.)713 Finally, 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. 714 Therefore, 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 716 A \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.) 723 718 Hence, a pointer occupies memory to store its current address, and the pointer's value is loaded by dereferencing, \eg: 724 719 \begin{quote2} 725 \begin{tabular}{@{}l @{\hspace{2em}}l@{}}720 \begin{tabular}{@{}ll@{}} 726 721 \begin{cfa} 727 722 int x, y, ®*® p1, ®*® p2, ®**® p3; … … 732 727 \end{cfa} 733 728 & 734 \raisebox{-0. 5\totalheight}{\input{pointer2.pstex_t}}729 \raisebox{-0.45\totalheight}{\input{pointer2.pstex_t}} 735 730 \end{tabular} 736 731 \end{quote2} 737 732 738 733 Notice, 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 734 In many cases, a compiler might be able to infer the meaning: 741 735 \begin{cfa} 742 736 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§ 743 737 \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: 738 because 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. 740 However, in C, the following cases are ambiguous, especially with pointer arithmetic: 741 \begin{cfa} 742 p1 = p2; §\C{// p1 = p2\ \ or\ \ *p1 = *p2}§ 743 p1 = p1 + 1; §\C{// p1 = p1 + 1\ \ or\ \ *p1 = *p1 + 1}§ 744 \end{cfa} 745 746 Most languages pick one meaning as the default and the programmer explicitly indicates the other meaning to resolve the address-duality ambiguity\index{address! ambiguity}. 747 In 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 ©*©. 754 748 \begin{cfa} 755 749 p1 = p2; §\C{// pointer address assignment}§ 756 ®*®p2 = ®*®p1 + x;§\C{// pointed-to value assignment / operation}§757 \end{cfa} 758 The C semanticsworks 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} 752 which works well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©). 759 753 760 754 However, in most other situations, the pointed-to value is requested more often than the pointer address. … … 768 762 \end{cfa} 769 763 770 To s upport 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.764 To switch the default meaning for an address requires a new kind of pointer, called a \newterm{reference} denoted by ©&©. 771 765 \begin{cfa} 772 766 int x, y, ®&® r1, ®&® r2, ®&&® r3; … … 779 773 Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example. 780 774 Hence, 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: 775 The 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} 777 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); 778 \end{cfa} 779 is rewritten as: 782 780 \begin{cfa} 783 781 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15); … … 795 793 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address of r2, (\&(\&*)*) cancel giving variable r3}§ 796 794 \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. 795 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth, and pointer and reference values are interchangeable because both contain addresses. 800 796 \begin{cfa} 801 797 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, … … 809 805 &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§ 810 806 \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: 807 Finally, implicit dereferencing and cancellation are a static (compilation) phenomenon not a dynamic one. 808 That 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. 809 A 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; 810 otherwise, everything else is equal. 811 812 Interestingly, \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 815 As for a pointer, a reference may have qualifiers: 815 816 \begin{cfa} 816 817 const int cx = 5; §\C{// cannot change cx;}§ … … 818 819 ®&®cr = &cx; §\C{// can change cr}§ 819 820 cr = 7; §\C{// error, cannot change cx}§ 820 int & const rc = x; §\C{// must be initialized }§821 int & const rc = x; §\C{// must be initialized, \CC reference}§ 821 822 ®&®rc = &x; §\C{// error, cannot change rc}§ 822 const int & const crc = cx; §\C{// must be initialized }§823 const int & const crc = cx; §\C{// must be initialized, \CC reference}§ 823 824 crc = 7; §\C{// error, cannot change cx}§ 824 825 ®&®crc = &cx; §\C{// error, cannot change crc}§ 825 826 \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 827 Hence, 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} 829 int & const r = *0; §\C{// where 0 is the int * zero}§ 830 \end{cfa} 831 Otherwise, 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. 837 832 Finally, the position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers. 838 833 The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations; … … 854 849 where the \CFA declaration is read left-to-right (see \VRef{s:Declarations}). 855 850 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 859 851 \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. 860 852 There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, return/temporary binding. 861 853 For reference initialization (like pointer), the initializing value must be an address (\Index{lvalue}) not a value (\Index{rvalue}). 862 854 \begin{cfa} 863 int * p = &x; §\C{// both \&x and x are possible interpretations in C}§855 int * p = &x; §\C{// both \&x and x are possible interpretations}§ 864 856 int & r = x; §\C{// x unlikely interpretation, because of auto-dereferencing}§ 865 857 \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. 858 Hence, the compiler implicitly inserts a reference operator, ©&©, before the initialization expression. 859 Similarly, when a reference is used for a parameter/return type, the call-site argument does not require a reference operator. 869 860 \begin{cfa} 870 861 int & f( int & rp ); §\C{// reference parameter and return}§ … … 872 863 \end{cfa} 873 864 Within 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.865 Since ©?+?© 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. 875 866 876 867 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions. … … 882 873 \end{cfa} 883 874 Here, 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 -typeparameter 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.) 885 876 \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. 886 877 \begin{cfa} … … 894 885 The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call. 895 886 896 While \CFA attempts to handle pointers and references in a uniform, symmetric manner, C handles routine objects in an inconsistent way: a routine objectis both a pointer and a reference (particle and wave).887 While \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). 897 888 \begin{cfa} 898 889 void f( int p ) {...} … … 902 893 fp(3); §\C{// reference invocation}§ 903 894 \end{cfa} 904 A routine objectis best described by a ©const© reference:895 A routine variable is best described by a ©const© reference: 905 896 \begin{cfa} 906 897 const void (&fp)( int ) = f; … … 909 900 &fp = ...; §\C{// changing routine reference}§ 910 901 \end{cfa} 911 because the value of the routine object is a routine literal, \iethe routine code is normally immutable during execution.\footnote{902 because the value of the routine variable is a routine literal, i.e., the routine code is normally immutable during execution.\footnote{ 912 903 Dynamic 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. 1086 905 1087 906 … … 1534 1353 because 1535 1354 1536 Currently, there are no \Index{lambda} expressions, \ieunnamed routines because routine names are very important to properly select the correct routine.1537 1538 1539 \section{ Tuples}1355 Currently, 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} 1540 1359 1541 1360 In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call. … … 1554 1373 [ v+w, x*y, 3.14159, f() ] 1555 1374 \end{cfa} 1556 Tuples are permitted to contain sub-tuples ( \ienesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple.1375 Tuples 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. 1557 1376 Note, a tuple is not a record (structure); 1558 1377 a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1). … … 1610 1429 tuple does not have structure like a record; a tuple is simply converted into a list of components. 1611 1430 \begin{rationale} 1612 The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; \iea statement such as ©g( f() )© is not supported.1431 The 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. 1613 1432 Using a temporary variable to store the results of the inner routine and then passing this variable to the outer routine works, however. 1614 1433 \end{rationale} … … 1623 1442 This requirement is the same as for comma expressions in argument lists. 1624 1443 1625 Type qualifiers, \ieconst 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)], \iethe qualifier is distributed across all of the types in the tuple, \eg:1444 Type qualifiers, i.e., const and volatile, may modify a tuple type. 1445 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)], i.e., the qualifier is distributed across all of the types in the tuple, \eg: 1627 1446 \begin{cfa} 1628 1447 const volatile [ int, float, const int ] x; … … 1662 1481 ©w© is implicitly opened to yield a tuple of four values, which are then assigned individually. 1663 1482 1664 A \newterm{flattening coercion} coerces a nested tuple, \iea 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:1483 A \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: 1665 1484 \begin{cfa} 1666 1485 [ a, b, c, d ] = [ 1, [ 2, 3 ], 4 ]; … … 1697 1516 \end{cfa} 1698 1517 \index{lvalue} 1699 The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, \ieany data object that can appear on the left-hand side of a conventional assignment statement.1518 The 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. 1700 1519 ©$\emph{expr}$© is any standard arithmetic expression. 1701 1520 Clearly, the types of the entities being assigned must be type compatible with the value of the expression. … … 1785 1604 [ x1, y1 ] = z = 0; 1786 1605 \end{cfa} 1787 As in C, the rightmost assignment is performed first, \ieassignment parses right to left.1606 As in C, the rightmost assignment is performed first, i.e., assignment parses right to left. 1788 1607 1789 1608 … … 1850 1669 1851 1670 1852 \section{Labelled Continue /Break}1671 \section{Labelled Continue / Break} 1853 1672 1854 1673 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure. … … 1947 1766 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. 1948 1767 Finally, 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©, \iethe closest enclosing loop or ©switch©, change as certain constructs are added or removed.1768 The implicit targets of the current ©continue© and ©break©, i.e., the closest enclosing loop or ©switch©, change as certain constructs are added or removed. 1950 1769 1951 1770 … … 2084 1903 Furthermore, 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. 2085 1904 As 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, \iethere are multiple entry points into its statement body.1905 The key observation is that the ©switch© statement branches into control structure, i.e., there are multiple entry points into its statement body. 2087 1906 \end{enumerate} 2088 1907 … … 2092 1911 the number of ©switch© statements is small, 2093 1912 \item 2094 most ©switch© statements are well formed ( \ieno \Index*{Duff's device}),1913 most ©switch© statements are well formed (i.e., no \Index*{Duff's device}), 2095 1914 \item 2096 1915 the ©default© clause is usually written as the last case-clause, … … 2102 1921 \item 2103 1922 Eliminating 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, \iea list of ©case© clauses executing common code, \eg:1923 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, i.e., a list of ©case© clauses executing common code, \eg: 2105 1924 \begin{cfa} 2106 1925 case 1: case 2: case 3: ... … … 2354 2173 2355 2174 The 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, \iethe seperator is adjusted only with respect to the next printed item.2175 Manipulators \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. 2357 2176 \begin{cfa}[mathescape=off,belowskip=0pt] 2358 2177 sout | sepOn | 1 | 2 | 3 | sepOn | endl; §\C{// separator at start of line}§ … … 2367 2186 12 3 2368 2187 \end{cfa} 2369 Manipulators \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} \emph{globally} toggle printing the separator, \iethe seperator is adjusted with respect to all subsequent printed items, unless locally adjusted.2188 Manipulators \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. 2370 2189 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2371 2190 sout | sepDisable | 1 | 2 | 3 | endl; §\C{// globally turn off implicit separation}§ … … 2642 2461 \caption{Constructors and Destructors} 2643 2462 \end{figure} 2463 2464 2465 \begin{comment} 2466 \section{References} 2467 2468 2469 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. 2470 In structures, a reference can replace a pointer to an object that should always have a valid value. 2471 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. 2472 2473 The syntax for using references in \CFA is the same as \CC with the exception of reference initialization. 2474 Use ©&© to specify a reference, and access references just like regular objects, not like pointers (use dot notation to access fields). 2475 When initializing a reference, \CFA uses a different syntax which differentiates reference initialization from assignment to a reference. 2476 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. 2477 \end{comment} 2644 2478 2645 2479 … … 4849 4683 \section{Incompatible} 4850 4684 4851 The following incompatibles exist between \CFA and C, and are similar to Annex C for \CC~\cite{ C++14}.4685 The following incompatibles exist between \CFA and C, and are similar to Annex C for \CC~\cite{ANSI14:C++}. 4852 4686 4853 4687 \begin{enumerate} … … 4947 4781 Personß.ßFace pretty; §\C{// type defined inside}§ 4948 4782 \end{cfa} 4949 In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \iethe nested types are hoisted to the scope of the outer-most type, which is not useful and confusing.4783 In 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. 4950 4784 \CFA is C \emph{incompatible} on this issue, and provides semantics similar to \Index*[C++]{\CC}. 4951 4785 Nested types are not hoisted and can be referenced using the field selection operator ``©.©'', unlike the \CC scope-resolution operator ``©::©''. … … 5044 4878 \end{tabular} 5045 4879 \end{quote2} 5046 For the prescribed head-files, \CFA uses header interposition to wraps theseincludes in an ©extern "C"©;4880 For the prescribed head-files, \CFA implicitly wraps their includes in an ©extern "C"©; 5047 4881 hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}). 5048 4882 All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling. … … 5052 4886 \label{s:StandardLibrary} 5053 4887 5054 The \CFA standard-library wraps many existing explicitly-polymorphic C general-routines into implicitly-polymorphic versions.4888 The goal of the \CFA standard-library is to wrap many of the existing C library-routines that are explicitly polymorphic into implicitly polymorphic versions. 5055 4889 5056 4890 … … 5176 5010 \label{s:Math Library} 5177 5011 5178 The \CFA math-library wraps many existing explicitly-polymorphic C math-routines into implicitly-polymorphic versions.5012 The 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. 5179 5013 5180 5014
Note: See TracChangeset
for help on using the changeset viewer.