Changes in / [370f6ef:23a1eb7b]
- Location:
- doc
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/bibliography/pl.bib
r370f6ef r23a1eb7b 5870 5870 @mastersthesis{Schluntz17, 5871 5871 author = {Robert Schluntz}, 5872 title = {Resource Management and Tuples in C$\mathbf{\forall}$},5872 title = {Resource Management and Tuples in {C}$\mathbf{\forall}$}, 5873 5873 school = {School of Computer Science, University of Waterloo}, 5874 5874 year = 2017, 5875 5875 address = {Waterloo, Ontario, Canada, N2L 3G1}, 5876 note = { [[unpublished]]}5876 note = {\href{https://uwspace.uwaterloo.ca/handle/10012/11830}{https://\-uwspace.uwaterloo.ca/\-handle/\-10012/\-11830}}, 5877 5877 } 5878 5878 -
doc/papers/general/Paper.tex
r370f6ef r23a1eb7b 105 105 106 106 \newenvironment{cquote}{% 107 \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep= 4pt\parsep=0pt\leftmargin=\parindentlnth\rightmargin\leftmargin}%107 \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=3pt\parsep=0pt\leftmargin=\parindentlnth\rightmargin\leftmargin}% 108 108 \item\relax 109 109 }{% … … 1078 1078 1079 1079 Both labelled @continue@ and @break@ are a @goto@ restricted in the following ways: 1080 \begin{itemize} 1080 \begin{itemize}[topsep=3pt,itemsep=2pt,parsep=0pt] 1081 1081 \item 1082 1082 They cannot create a loop, which means only the looping constructs cause looping. … … 1159 1159 } 1160 1160 \end{cfa} 1161 \CFA allows the declaration of local variables, \eg @y@, at the start of the @switch@ with scope across the entire @switch@ body, \ie all @case@ clauses , but no statements.1161 \CFA allows the declaration of local variables, \eg @y@, at the start of the @switch@ with scope across the entire @switch@ body, \ie all @case@ clauses. 1162 1162 \CFA disallows the declaration of local variable, \eg @z@, directly within the @switch@ body, because a declaration cannot occur immediately after a @case@ since a label can only be attached to a statement, and the use of @z@ is undefined in @case 1@ as neither storage allocation nor initialization may have occurred. 1163 1163 … … 1255 1255 int i; 1256 1256 double d; 1257 int f() { $\C{// implicit "this" parameter}$1257 int f() { $\C{// implicit ``this'' aggregate}$ 1258 1258 `this->`c; `this->`i; `this->`d; $\C{// access containing fields}$ 1259 1259 } … … 1320 1320 \begin{cfa} 1321 1321 void ?{}( S & s, int i ) with ( s ) { $\C{// constructor}$ 1322 `s.i = i;` j = 3; m = 5.5;1322 `s.i = i;` j = 3; m = 5.5; $\C{// initialize fields}$ 1323 1323 } 1324 1324 \end{cfa} … … 1364 1364 \section{Declarations} 1365 1365 1366 It is important that \CFA subjectively ``feel like'' C to userprogrammers.1366 It is important that \CFA subjectively ``feel like'' C to programmers. 1367 1367 An important part of this subjective feel is maintaining C's procedural paradigm, as opposed to the object-oriented paradigm of other systems languages such as \CC and Rust. 1368 1368 Maintaining this procedural paradigm means that C coding-patterns remain not only functional but idiomatic in \CFA, reducing the mental burden of retraining C programmers and switching between C and \CFA development. … … 1398 1398 1399 1399 \CFA provides its own type, variable and routine declarations, using a different syntax. 1400 The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type.1400 The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right. 1401 1401 The qualifiers have the same meaning but are ordered left to right to specify a variable's type. 1402 1402 \begin{cquote} 1403 1403 \lstDeleteShortInline@% 1404 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{ }}1404 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}} 1405 1405 \multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1406 1406 \begin{cfa} … … 1414 1414 int `(*`x2`)[5]`; 1415 1415 `int (*`f( int p )`)[5]`; 1416 \end{cfa} 1417 & 1418 \begin{cfa} 1419 // array of 5 pointers to int 1420 // pointer to an array of 5 int 1421 // function returning pointer to an array of 5 int and taking an int 1416 1422 \end{cfa} 1417 1423 \end{tabular} … … 1428 1434 \begin{cfa} 1429 1435 `*` int x, y; 1436 int y; 1430 1437 \end{cfa} 1431 1438 & 1432 1439 \begin{cfa} 1433 1440 int `*`x, `*`y; 1441 1434 1442 \end{cfa} 1435 1443 \end{tabular} 1436 1444 \lstMakeShortInline@% 1437 1445 \end{cquote} 1438 The downside of this semantics is the need to separate regular and pointer declarations: 1439 \begin{cquote} 1440 \lstDeleteShortInline@% 1441 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}} 1442 \multicolumn{1}{c@{\hspace{\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1443 \begin{cfa} 1444 `*` int x; 1445 int y; 1446 \end{cfa} 1447 & 1448 \begin{cfa} 1449 int `*`x, y; 1450 1451 \end{cfa} 1452 \end{tabular} 1453 \lstMakeShortInline@% 1454 \end{cquote} 1455 which is prescribing a safety benefit. 1446 The downside of the \CFA semantics is the need to separate regular and pointer declarations. 1456 1447 1457 1448 \begin{comment} … … 1561 1552 & 1562 1553 \begin{cfa} 1563 double foo1( ), foo2( int ), foo3( double );1554 double foo1( void ), foo2( int ), foo3( double ); 1564 1555 \end{cfa} 1565 1556 \end{tabular} … … 1593 1584 a value that does not have a corresponding address is called a \newterm{rvalue} (for ``right-hand value''), while a value that does have an address is called a \newterm{lvalue} (for ``left-hand value''). 1594 1585 For example, in @int x; x = 42;@ the variable expression @x@ on the left-hand-side of the assignment is a lvalue, while the constant expression @42@ on the right-hand-side of the assignment is a rvalue. 1595 Despite the nomenclature of ``left-hand'' and ``right-hand'', an expression's classification as lvalue or rvalue is entirely dependent on whether it has an address or not; in imperative programming, the address of a value is used for both reading and writing (mutating) a value, and as such lvalues can be converted to rvalues and read from, but rvalues cannot be mutated because they lack a location to store the updated value.1586 Despite the nomenclature of ``left-hand'' and ``right-hand'', an expression's classification as lvalue or rvalue is entirely dependent on whether it has an address or not; in imperative programming, the address of a value is used for both reading and writing (mutating) a value, and as such, lvalues can be converted to rvalues and read from, but rvalues cannot be mutated because they lack a location to store the updated value. 1596 1587 1597 1588 Within a lexical scope, lvalue expressions have an \newterm{address interpretation} for writing a value or a \newterm{value interpretation} to read a value. 1598 1589 For example, in @x = y@, @x@ has an address interpretation, while @y@ has a value interpretation. 1599 Thoughthis duality of interpretation is useful, C lacks a direct mechanism to pass lvalues between contexts, instead relying on \newterm{pointer types} to serve a similar purpose.1590 While this duality of interpretation is useful, C lacks a direct mechanism to pass lvalues between contexts, instead relying on \newterm{pointer types} to serve a similar purpose. 1600 1591 In C, for any type @T@ there is a pointer type @T *@, the value of which is the address of a value of type @T@. 1601 1592 A pointer rvalue can be explicitly \newterm{dereferenced} to the pointed-to lvalue with the dereference operator @*?@, while the rvalue representing the address of a lvalue can be obtained with the address-of operator @&?@. … … 1609 1600 \end{cfa} 1610 1601 1611 Unfortunately, the dereference and address-of operators introduce a great deal of syntactic noise when dealing with pointed-to values rather than pointers, as well as the potential for subtle bugs. 1612 For both brevity and clarity, it would be desirable to have the compiler figure out how to elide the dereference operators in a complex expression such as the assignment to @*p2@ above. 1613 However, since C defines a number of forms of \newterm{pointer arithmetic}, two similar expressions involving pointers to arithmetic types (\eg @*p1 + x@ and @p1 + x@) may each have well-defined but distinct semantics, introducing the possibility that a user programmer may write one when they mean the other, and precluding any simple algorithm for elision of dereference operators. 1614 To solve these problems, \CFA introduces reference types @T&@; a @T&@ has exactly the same value as a @T*@, but where the @T*@ takes the address interpretation by default, a @T&@ takes the value interpretation by default, as below: 1602 Unfortunately, the dereference and address-of operators introduce a great deal of syntactic noise when dealing with pointed-to values rather than pointers, as well as the potential for subtle bugs because of pointer arithmetic. 1603 For both brevity and clarity, it is desirable for the compiler to figure out how to elide the dereference operators in a complex expression such as the assignment to @*p2@ above. 1604 However, since C defines a number of forms of \newterm{pointer arithmetic}, two similar expressions involving pointers to arithmetic types (\eg @*p1 + x@ and @p1 + x@) may each have well-defined but distinct semantics, introducing the possibility that a programmer may write one when they mean the other, and precluding any simple algorithm for elision of dereference operators. 1605 To solve these problems, \CFA introduces reference types @T &@; 1606 a @T &@ has exactly the same value as a @T *@, but where the @T *@ takes the address interpretation by default, a @T &@ takes the value interpretation by default, as below: 1615 1607 1616 1608 \begin{cfa} … … 1619 1611 &r2 = &y; $\C{// r2 points to y}$ 1620 1612 &&r3 = &&r1; $\C{// r3 points to r2}$ 1621 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); 1613 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); $\C{// implicit dereferencing}$ 1622 1614 \end{cfa} 1623 1615 1624 1616 Except for auto-dereferencing by the compiler, this reference example is exactly the same as the previous pointer example. 1625 Hence, a reference behaves like a variable name -- an lvalue expression which is interpreted as a value ,but also has the type system track the address of that value.1617 Hence, a reference behaves like a variable name -- an lvalue expression which is interpreted as a value -- but also has the type system track the address of that value. 1626 1618 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 the reference variable declaration, so the previous example implicitly acts like: 1627 1619 … … 1630 1622 \end{cfa} 1631 1623 1632 References in \CFA are similar to those in \CC, but with a couple important improvements, both ofwhich can be seen in the example above.1633 Firstly, \CFA does not forbid references to references , unlike \CC.1624 References in \CFA are similar to those in \CC, with important improvements, which can be seen in the example above. 1625 Firstly, \CFA does not forbid references to references. 1634 1626 This provides a much more orthogonal design for library implementors, obviating the need for workarounds such as @std::reference_wrapper@. 1635 1636 Secondly, unlike the references in \CC which always point to a fixed address, \CFA references are rebindable. 1637 This allows \CFA references to be default-initialized (\eg to a null pointer), and also to point to different addresses throughout their lifetime. 1638 This rebinding is accomplished without adding any new syntax to \CFA, but simply by extending the existing semantics of the address-of operator in C. 1627 Secondly, \CFA references are rebindable, whereas \CC references have a fixed address. 1628 \newsavebox{\LstBox} 1629 \begin{lrbox}{\LstBox} 1630 \lstset{basicstyle=\footnotesize\linespread{0.9}\sf} 1631 \begin{cfa} 1632 int & r = *new( int ); 1633 ... 1634 delete &r; 1635 r += 1; // undefined reference 1636 \end{cfa} 1637 \end{lrbox} 1638 Rebinding allows \CFA references to be default-initialized (\eg to a null pointer\footnote{ 1639 While effort has been put into non-null reference checking in \CC, the exercise seems moot for any non-managed languages, given that it only handles one of many different error situations: 1640 \begin{cquote} 1641 \usebox{\LstBox} 1642 \end{cquote} 1643 }% 1644 ) and point to different addresses throughout their lifetime, like pointers. 1645 Rebinding is accomplished by extending the existing syntax and semantics of the address-of operator in C. 1639 1646 1640 1647 In C, the address of a lvalue is always a rvalue, as in general that address is not stored anywhere in memory, and does not itself have an address. 1641 In \CFA, the address of a @T &@ is a lvalue @T*@, as the address of the underlying @T@ is stored in the reference, and can thus be mutated there.1648 In \CFA, the address of a @T &@ is a lvalue @T *@, as the address of the underlying @T@ is stored in the reference, and can thus be mutated there. 1642 1649 The result of this rule is that any reference can be rebound using the existing pointer assignment semantics by assigning a compatible pointer into the address of the reference, \eg @&r1 = &x;@ above. 1643 This rebinding can occur to an arbitrary depth of reference nesting; loosely speaking, nested address-of operators will produce an lvalue nested pointer up to as deep as the reference they're applied to. 1650 This rebinding occurs to an arbitrary depth of reference nesting; 1651 loosely speaking, nested address-of operators produce a nested lvalue pointer up to the depth of the reference. 1644 1652 These explicit address-of operators can be thought of as ``cancelling out'' the implicit dereference operators, \eg @(&`*`)r1 = &x@ or @(&(&`*`)`*`)r3 = &(&`*`)r1@ or even @(&`*`)r2 = (&`*`)`*`r3@ for @&r2 = &r3@. 1645 1653 More precisely: 1646 \begin{itemize} 1647 1648 1654 \begin{itemize}[topsep=3pt,itemsep=2pt,parsep=0pt] 1655 \item 1656 if @R@ is an rvalue of type {@T &@$_1 \cdots$@ &@$_r$} where $r \ge 1$ references (@&@ symbols) than @&R@ has type {@T `*`&@$_{\color{red}2} \cdots$@ &@$_{\color{red}r}$}, \\ \ie @T@ pointer with $r-1$ references (@&@ symbols). 1649 1657 1650 1651 1658 \item 1659 if @L@ is an lvalue of type {@T &@$_1 \cdots$@ &@$_l$} where $l \ge 0$ references (@&@ symbols) then @&L@ has type {@T `*`&@$_{\color{red}1} \cdots$@ &@$_{\color{red}l}$}, \\ \ie @T@ pointer with $l$ references (@&@ symbols). 1652 1660 \end{itemize} 1653 Since pointers and references share the same internal representation, code using either is equally performant; in fact the \CFA compiler converts references to pointers internally, and the choice between them i n user code can be made based solely on convenience.1661 Since pointers and references share the same internal representation, code using either is equally performant; in fact the \CFA compiler converts references to pointers internally, and the choice between them is made solely on convenience, \eg many pointer or value accesses. 1654 1662 1655 1663 By analogy to pointers, \CFA references also allow cv-qualifiers such as @const@: 1656 1657 \begin{cfa} 1658 const int cx = 5; $\C{// cannot change cx}$ 1659 const int & cr = cx; $\C{// cannot change cr's referred value}$ 1660 &cr = &cx; $\C{// rebinding cr allowed}$ 1661 cr = 7; $\C{// ERROR, cannot change cr}$ 1662 int & const rc = x; $\C{// must be initialized, like in \CC}$ 1663 &rc = &x; $\C{// ERROR, cannot rebind rc}$ 1664 rc = 7; $\C{// x now equal to 7}$ 1665 \end{cfa} 1666 1664 \begin{cfa} 1665 const int cx = 5; $\C{// cannot change cx}$ 1666 const int & cr = cx; $\C{// cannot change cr's referred value}$ 1667 &cr = &cx; $\C{// rebinding cr allowed}$ 1668 cr = 7; $\C{// ERROR, cannot change cr}$ 1669 int & const rc = x; $\C{// must be initialized, like in \CC}$ 1670 &rc = &x; $\C{// ERROR, cannot rebind rc}$ 1671 rc = 7; $\C{// x now equal to 7}$ 1672 \end{cfa} 1667 1673 Given that a reference is meant to represent a lvalue, \CFA provides some syntactic shortcuts when initializing references. 1668 1674 There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, and return/temporary binding. 1669 In each of these contexts, the address-of operator on the target lvalue may (in fact, must) be elided. 1670 The syntactic motivation for this is clearest when considering overloaded operator-assignment, \eg @int ?+=?(int &, int)@; given @int x, y@, the expected call syntax is @x += y@, not @&x += y@. 1671 1672 More generally, this initialization of references from lvalues rather than pointers is an instance of a ``lvalue-to-reference'' conversion rather than an elision of the address-of operator; this conversion can actually be used in any context in \CFA an implicit conversion would be allowed. 1675 In each of these contexts, the address-of operator on the target lvalue is elided. 1676 The syntactic motivation is clearest when considering overloaded operator-assignment, \eg @int ?+=?(int &, int)@; given @int x, y@, the expected call syntax is @x += y@, not @&x += y@. 1677 1678 More generally, this initialization of references from lvalues rather than pointers is an instance of a ``lvalue-to-reference'' conversion rather than an elision of the address-of operator; 1679 this conversion is used in any context in \CFA where an implicit conversion is allowed. 1673 1680 Similarly, use of a the value pointed to by a reference in an rvalue context can be thought of as a ``reference-to-rvalue'' conversion, and \CFA also includes a qualifier-adding ``reference-to-reference'' conversion, analogous to the @T *@ to @const T *@ conversion in standard C. 1674 1681 The final reference conversion included in \CFA is ``rvalue-to-reference'' conversion, implemented by means of an implicit temporary. 1675 1682 When an rvalue is used to initialize a reference, it is instead used to initialize a hidden temporary value with the same lexical scope as the reference, and the reference is initialized to the address of this temporary. 1683 \begin{cfa} 1684 struct S { double x, y; }; 1685 int i, j; 1686 void f( int & i, int & j, S & s, int v[] ); 1687 f( 3, i + j, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); $\C{// pass rvalue to lvalue \(\Rightarrow\) implicit temporary}$ 1688 \end{cfa} 1676 1689 This allows complex values to be succinctly and efficiently passed to functions, without the syntactic overhead of explicit definition of a temporary variable or the runtime cost of pass-by-value. 1677 1690 \CC allows a similar binding, but only for @const@ references; the more general semantics of \CFA are an attempt to avoid the \newterm{const hell} problem, in which addition of a @const@ qualifier to one reference requires a cascading chain of added qualifiers. … … 1680 1693 \subsection{Constructors and Destructors} 1681 1694 1682 One of the strengths of C is the control over memory management it gives programmers, allowing resource release to be more consistent and precisely timed than is possible with garbage-collected memory management. 1683 However, this manual approach to memory management is often verbose, and it is useful to manage resources other than memory (\eg file handles) using the same mechanism as memory. 1684 \CC is well-known for an approach to manual memory management that addresses both these issues, Resource Aquisition Is Initialization (RAII), implemented by means of special \newterm{constructor} and \newterm{destructor} functions; we have implemented a similar feature in \CFA. 1685 While RAII is a common feature of object-oriented programming languages, its inclusion in \CFA does not violate the design principle that \CFA retain the same procedural paradigm as C. 1686 In particular, \CFA does not implement class-based encapsulation: neither the constructor nor any other function has privileged access to the implementation details of a type, except through the translation-unit-scope method of opaque structs provided by C. 1687 1688 In \CFA, a constructor is a function named @?{}@, while a destructor is a function named @^?{}@; like other \CFA operators, these names represent the syntax used to call the constructor or destructor, \eg @x{ ... };@ or @^x{};@. 1689 Every constructor and destructor must have a return type of @void@, and its first parameter must have a reference type whose base type is the type of the object the function constructs or destructs. 1690 This first parameter is informally called the @this@ parameter, as in many object-oriented languages, though a programmer may give it an arbitrary name. 1691 Destructors must have exactly one parameter, while constructors allow passing of zero or more additional arguments along with the @this@ parameter. 1692 1693 \begin{cfa} 1694 struct Array { 1695 int * data; 1696 int len; 1695 One of the strengths (and weaknesses) of C is control over memory management, allowing resource release to be more consistent and precisely timed than possible with garbage-collected memory-management. 1696 However, this manual approach is often verbose, furthermore it is useful to manage resources other than memory (\eg file handles) using the same mechanism as memory. 1697 \CC addresses these issues using Resource Aquisition Is Initialization (RAII), implemented by means of \newterm{constructor} and \newterm{destructor} functions; 1698 \CFA adopts constructors and destructors (and @finally@) to facilitate RAII. 1699 While constructors and destructors are a common feature of object-oriented programming-languages, they are independent capabilities allowing \CFA to retain a procedural paradigm. 1700 Specifically, \CFA constructors and destructors are denotes by name and first parameter-type versus name and nesting in an aggregate type. 1701 1702 In \CFA, a constructor is named @?{}@ and a destructor is named @^?{}@; 1703 like other \CFA operators, these names represent the syntax used to call the constructor or destructor, \eg @x{ ... };@ or @^x{...};@. 1704 The name @{}@ comes from the syntax for the initializer: @struct S { int i, j; } s = `{` 2, 3 `}`@. 1705 The constructor and destructor have return type @void@ and a first parameter of reference to the object type to be constructed or destructs. 1706 While the first parameter is informally called the @this@ parameter, as in object-oriented languages, any variable name may be used. 1707 Both constructors and destructors allow additional parametes after the @this@ parameter for specifying values for initialization/de-initialization\footnote{Destruction parameters are useful for specifying storage-management actions, such as de-initialize but not de-allocate.}. 1708 \begin{cfa} 1709 struct VLA { 1710 int len, * data; 1697 1711 }; 1698 1699 void ?{}( Array& arr ) { 1700 arr.len = 10; 1701 arr.data = calloc( arr.len, sizeof(int) ); 1702 } 1703 1704 void ^?{}( Array& arr ) { 1705 free( arr.data ); 1706 } 1707 1708 { 1709 Array x; 1710 `?{}(x);` $\C{// implicitly compiler-generated}$ 1712 void ?{}( VLA& vla ) with ( vla ) { $\C{// default constructor}$ 1713 len = 10; data = calloc( len ); 1714 } 1715 void ^?{}( VLA& vla ) { $\C{// destructor}$ 1716 free( vla.data ); 1717 } 1718 { VLA x; `?{}(x);` $\C{// compiler generated constructor call}$ 1711 1719 // ... use x 1712 `^?{}(x);` $\C{// implicitly compiler-generated}$ 1713 } 1714 \end{cfa} 1715 1716 In the example above, a \newterm{default constructor} (\ie one with no parameters besides the @this@ parameter) and destructor are defined for the @Array@ struct, a dynamic array of @int@. 1717 @Array@ is an example of a \newterm{managed type} in \CFA, a type with a non-trivial constructor or destructor, or with a field of a managed type. 1718 As in the example, all instances of managed types are implicitly constructed upon allocation, and destructed upon deallocation; this ensures proper initialization and cleanup of resources contained in managed types, in this case the @data@ array on the heap. 1720 `^?{}(x);` } $\C{// compiler generated desturctor call}$ 1721 \end{cfa} 1722 @VLA@ is an example of a \newterm{managed type}\footnote{A managed type affects the runtime environment versus being self-contained.} in \CFA: a type requiring a non-trivial constructor or destructor, or with a field of a managed type. 1723 A managed types is implicitly constructed upon allocation, and destructed upon deallocation to ensure proper interaction of runtime resources, in this case the @data@ array in the heap. 1719 1724 The exact details of the placement of these implicit constructor and destructor calls are omitted here for brevity, the interested reader should consult \cite{Schluntz17}. 1720 1725 1721 Constructor calls are intended to seamlessly integrate with existing C initialization syntax, providing a simple and familiar syntax to veteran C programmers and allowing constructor calls to be inserted into legacy C code with minimal code changes. 1722 As such, \CFA also provides syntax for \newterm{copy initialization} and \newterm{initialization parameters}: 1723 1724 \begin{cfa} 1725 void ?{}( Array& arr, Array other ); 1726 1727 void ?{}( Array& arr, int size, int fill ); 1728 1729 Array y = { 20, 0xDEADBEEF }, z = y; 1726 Constructor calls seamlessly integrate with existing C initialization syntax, providing a simple and familiar syntax to C programmers and allowing constructor calls to be inserted into legacy C code with minimal code changes. 1727 As such, \CFA also provides syntax for \newterm{initialization} and \newterm{copy}: 1728 \begin{cfa} 1729 void ?{}( VLA & vla, int size, int fill ); $\C{// initialization}$ 1730 void ?{}( VLA & vla, VLA other ); $\C{// copy}$ 1731 VLA y = { 20, 0xdeadbeef }, // initialization 1732 z = y; // copy 1730 1733 \end{cfa} 1731 1734 … … 1738 1741 1739 1742 \begin{cfa} 1740 Arraya, b;1743 VLA a, b; 1741 1744 a{}; $\C{// default construct}$ 1742 1745 b{ a }; $\C{// copy construct}$ … … 1759 1762 In rare situations user programmers may not wish to have constructors and destructors called; in these cases, \CFA provides an ``escape hatch'' to not call them. 1760 1763 If a variable is initialized using the syntax \lstinline|S x @= {}| it will be an \newterm{unmanaged object}, and will not have constructors or destructors called. 1761 Any C initializer can be the right-hand side of an \lstinline|@=| initializer, \eg \lstinline| Arraya @= { 0, 0x0 }|, with the usual C initialization semantics.1764 Any C initializer can be the right-hand side of an \lstinline|@=| initializer, \eg \lstinline|VLA a @= { 0, 0x0 }|, with the usual C initialization semantics. 1762 1765 In addition to the expressive power, \lstinline|@=| provides a simple path for migrating legacy C code to \CFA, by providing a mechanism to incrementally convert initializers; the \CFA design team decided to introduce a new syntax for this escape hatch because we believe that our RAII implementation will handle the vast majority of code in a desirable way, and we wished to maintain familiar syntax for this common case. 1763 1766 … … 1845 1848 In keeping with the general \CFA approach of adding features while respecting ``the C way'' of doing things, we have extended both C's polymorphic zero and typed literal syntax to interoperate with user-defined types, while maintaining a backwards-compatible semantics. 1846 1849 1850 1847 1851 \subsection{0/1} 1848 1852 … … 1856 1860 \CFA also includes a special type for @1@, @one_t@; like @zero_t@, @one_t@ has built-in implicit conversions to the various integral types so that @1@ maintains its expected semantics in legacy code. 1857 1861 The addition of @one_t@ allows generic algorithms to handle the unit value uniformly for types where that is meaningful. 1858 \TODO{Make this sentence true} In particular, polymorphic functions in the \CFA prelude define @++x@ and @x++@ in terms of @x += 1@, allowing users to idiomatically define all forms of increment for a type @T@ by defining the single function @T& ?+=(T&, one_t)@; analogous overloads for the decrement operators are present as well. 1862 \TODO{Make this sentence true} In particular, polymorphic functions in the \CFA prelude define @++x@ and @x++@ in terms of @x += 1@, allowing users to idiomatically define all forms of increment for a type @T@ by defining the single function @T & ?+=(T &, one_t)@; analogous overloads for the decrement operators are present as well. 1863 1859 1864 1860 1865 \subsection{Units} … … 2034 2039 The following shows one example where \CFA \emph{extends} an existing standard C interface to reduce complexity and provide safety. 2035 2040 C/\Celeven provide a number of complex and overlapping storage-management operation to support the following capabilities: 2036 \begin{description}[ itemsep=2pt,parsep=0pt]2041 \begin{description}[topsep=3pt,itemsep=2pt,parsep=0pt] 2037 2042 \item[fill] 2038 2043 after allocation the storage is filled with a specified character. … … 2067 2072 ip = alloc( ip, 2 * dim ); 2068 2073 ip = alloc( ip, 4 * dim, fill ); 2069 2070 ip = align_alloc( 16 );2071 ip = align_alloc( 16, fill );2072 ip = align_alloc( 16, dim );2073 ip = align_alloc( 16, dim, fill );2074 2074 \end{cfa} 2075 2075 & … … 2081 2081 ip = (int *)realloc( ip, 2 * dim * sizeof( int ) ); 2082 2082 ip = (int *)realloc( ip, 4 * dim * sizeof( int ) ); memset( ip, fill, 4 * dim * sizeof( int ) ); 2083 2083 \end{cfa} 2084 \end{tabular} 2085 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}} 2086 \begin{cfa} 2087 ip = align_alloc( 16 ); 2088 ip = align_alloc( 16, fill ); 2089 ip = align_alloc( 16, dim ); 2090 ip = align_alloc( 16, dim, fill ); 2091 \end{cfa} 2092 & 2093 \begin{cfa} 2084 2094 ip = memalign( 16, sizeof( int ) ); 2085 2095 ip = memalign( 16, sizeof( int ) ); memset( ip, fill, sizeof( int ) ); … … 2198 2208 The implicit separator character (space/blank) is a separator not a terminator. 2199 2209 The rules for implicitly adding the separator are: 2200 \begin{itemize}[ itemsep=2pt,parsep=0pt]2210 \begin{itemize}[topsep=3pt,itemsep=2pt,parsep=0pt] 2201 2211 \item 2202 2212 A separator does not appear at the start or end of a line.
Note: See TracChangeset
for help on using the changeset viewer.