Changeset 36e05a2
 Timestamp:
 Apr 3, 2017, 9:10:57 AM (5 years ago)
 Branches:
 aaronthesis, armeh, cleanupdtors, deferred_resn, demangler, enum, forallpointerdecay, jacob/cs343translation, jenkinssandbox, master, newast, newastuniqueexpr, newenv, no_list, persistentindexer, pthreademulation, qualifiedEnum, resolvnew, with_gc
 Children:
 8396044, fc19129
 Parents:
 ea73caf
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

doc/generic_types/generic_types.tex
rea73caf r36e05a2 133 133 \item Extensions introduced by \CFA must be translated in the most efficient way possible. 134 134 \end{enumerate} 135 The purpose of these goals is to ensure that existing C codebases can be converted to \CFA incrementally and with minimal effort, and that programmers who know C can productively generate \CFA code without training beyond the features they wish to employ. In its current implementation, \CFA is compiled by translating it to the GCCdialect of C~\citep{GCCExtensions}, allowing it to leverage the portability and code optimizations provided by GCC, meeting goals (1)(3).135 These goals ensure existing C codebases can be converted to \CFA incrementally and with minimal effort, and C programmers can productively generate \CFA code without training beyond the features they wish to employ. In its current implementation, \CFA is compiled by translating it to the GCCdialect of C~\citep{GCCExtensions}, allowing it to leverage the portability and code optimizations provided by GCC, meeting goals (1)(3). Ultimately, a compiler is necessary for advanced features and optimal performance. 136 136 137 137 \CFA has been previously extended with polymorphic functions and name overloading (including operator overloading) by \citet{Bilson03}, and deterministicallyexecuted constructors and destructors by \citet{Schluntz17}. This paper describes how generic and tuple types are designed and implemented in \CFA in accordance with both the backward compatibility goals and existing features described above. … … 167 167 \begin{lstlisting} 168 168 forall( otype T ` { int ?<?( T, T ); }` ) $\C{// type assertion}$ 169 void qsort( const T * arr, size_t dim);169 void qsort( const T * arr, size_t size ); 170 170 forall( otype T ` { int ?<?( T, T ); }` ) $\C{// type assertion}$ 171 T * bsearch( T key, const T * arr, size_t dim);171 T * bsearch( T key, const T * arr, size_t size ); 172 172 double vals[10] = { /* 10 floatingpoint values */ }; 173 173 qsort( vals, 10 ); $\C{// sort array}$ … … 190 190 but providing a typesafe \CFA overloaded wrapper. 191 191 \begin{lstlisting} 192 forall( otype T  { int ?<?( T, T ); } ) T * bsearch( T key, const T * arr, size_t dim) {192 forall( otype T  { int ?<?( T, T ); } ) T * bsearch( T key, const T * arr, size_t size ) { 193 193 int comp( const void * t1, const void * t2 ) { /* as above with double changed to T */ } 194 return (T *)bsearch( &key, arr, dim, sizeof(T), comp );195 } 196 forall( otype T  { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t dim) {197 T *result = bsearch( key, arr, dim); $\C{// call first version}$198 return result ? result  arr : dim; $\C{// pointer subtraction includes sizeof(T)}$194 return (T *)bsearch( &key, arr, size, sizeof(T), comp ); 195 } 196 forall( otype T  { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t size ) { 197 T *result = bsearch( key, arr, size ); $\C{// call first version}$ 198 return result ? result  arr : size; $\C{// pointer subtraction includes sizeof(T)}$ 199 199 } 200 200 double * val = bsearch( 5.0, vals, 10 ); $\C{// selection based on return type}$ … … 262 262 }; 263 263 forall( otype T  summable( T ) ) 264 T sum( T a[$\,$], size_t dim) {265 T total = { 0 }; 266 for ( unsigned int i = 0; i < dim; i += 1 )264 T sum( T a[$\,$], size_t size ) { 265 T total = { 0 }; $\C{// instantiate T from 0}$ 266 for ( unsigned int i = 0; i < size; i += 1 ) 267 267 total += a[i]; $\C{// select appropriate +}$ 268 268 return total;
Note: See TracChangeset
for help on using the changeset viewer.