Changeset 41b3ddd


Ignore:
Timestamp:
Sep 23, 2015, 11:02:48 PM (9 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
bfee448
Parents:
01414f1
Message:

begin documentation for generic types

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/refrat/refrat.tex

    r01414f1 r41b3ddd  
    1010\usepackage{varioref}
    1111\usepackage{listings}
     12\usepackage{comment}
    1213\usepackage{latexsym}                                   % \Box
    1314\usepackage{mathptmx}                                   % better math font with "times"
     
    112113
    113114\lstdefinelanguage{CFA}[ANSI]{C}%
    114   {morekeywords={asm,_Atomic,catch,catchResume,choose,_Complex,context,disable,dtype,enable,
     115  {morekeywords={asm,_At,_Atomic,catch,catchResume,choose,_Complex,context,disable,dtype,enable,
    115116        fallthru,finally,forall,ftype,_Imaginary,lvalue,restrict,throw,throwResume,try,type,},
    116117}
     
    184185
    185186% No ``Scope'' or ``Normative references'' chapters yet.
     187
     188
    186189\setcounter{chapter}{2}
    187190\chapter{Terms, definitions, and symbols}
     191
    188192Terms from the {\c11} standard used in this document have the same meaning as in the {\c11}
    189193standard.
    190194
    191195% No ``Conformance'' or ``Environment'' chapters yet.
     196
     197
    192198\setcounter{chapter}{5}
    193199\chapter{Language}
     200
     201
    194202\section{Notation}
    195203The syntax notation used in this document is the same as is used in the {\c11} standard, with one
     
    235243\end{rationale}
    236244
     245
     246\setcounter{subsection}{8}
     247\subsection{Generic Types}
     248
     249
     250\subsubsection{Semantics}
     251
     252CFA provides a capability for generic types; using this capability a single "generic type generator"
     253can be written that can represent multiple concrete type instantiations by substitution of the "type
     254parameters" of the generic type for concrete types. Syntactically a generic type generator is
     255represented by putting a forall specifier on a struct or union declaration, as defined in section
     2566.7.2.5. An instantiation of the generic type is written by specifying the type parameters in
     257parentheses after the name of the generic type generator, as in the following example:
     258\begin{lstlisting}
     259forall( type T ) struct pair {
     260        T x;
     261        T y;
     262};
     263
     264pair( int ) p = { 3, 14 };
     265\end{lstlisting}
     266
     267The type parameters in an instantiation of a generic type must satisfy any constraints in the forall
     268specifier on the type generator declaration. The instantiation then has the semantics that would
     269result if the type parameters were substituted into the type generator declaration by macro
     270substitution.
     271
     272Polymorphic functions may have generic types as parameters, and those generic types may use type
     273parameters of the polymorphic function as type parameters of the generic type, as in the following
     274example:
     275\begin{lstlisting}
     276forall( type T ) void swap( pair(T) *p ) {
     277        T z = p->x;
     278        p->x = p->y;
     279        p->y = z;
     280}
     281\end{lstlisting}
     282
     283
     284\subsubsection{Constraints}
     285
     286To avoid unduly constraining implementors, the generic type generator definition must be visible at
     287any point where it is instantiated.  Forward declarations of generic type generators are not
     288forbidden, but the definition must be visible to instantiate the generic type.  Equivalently,
     289instantiations of generic types are not allowed to be incomplete types.
     290
     291\examples
     292\begin{lstlisting}
     293forall( type T ) struct A;
     294
     295forall( type T ) struct B {
     296        A(T) *a;  // legal, but cannot instantiate B(T)
     297};
     298
     299B(T) x; // illegal, *x.a is of an incomplete generic type
     300
     301forall( type T ) struct A {
     302        B( T ) *b;
     303};
     304
     305B( T ) y; // legal, *x.a is now of a complete generic type
     306
     307
     308// box.h:
     309        forall( type T ) struct box;
     310        forall( type T ) box( T ) *make_box( T );
     311        forall( type T ) void use_box( box( T ) *b );
     312       
     313// main.c:
     314        box( int ) *b = make_box( 42 ); // illegal, def'n of box not visible
     315        use_box( b ); // illegal
     316\end{lstlisting}
     317
     318
    237319\section{Conversions}
    238320\CFA defines situations where values of one type are automatically converted to another type.
     
    242324
    243325\subsection{Arithmetic operands}
    244 \setcounter{subsubsection}{7}
    245 
    246 
     326
     327
     328\setcounter{subsubsection}{8}
    247329\subsubsection{Safe arithmetic conversions}
     330
    248331In C, a pattern of conversions known as the \define{usual arithmetic conversion}s is used with most
    249332binary arithmetic operators to convert the operands to a common type and determine the type of the
     
    302385
    303386\subsection{Other operands}
     387
     388
    304389\setcounter{subsubsection}{3}
    305 
    306 
    307390\subsubsection{Anonymous structures and unions}
    308391\label{anon-conv}
     
    33193402
    33203403\setcounter{subsubsection}{4}
    3321 \subsubsection{Forall specifiers}\label{forall}
     3404\subsubsection{Forall specifiers}
     3405\label{forall}
    33223406
    33233407\begin{syntax}
     
    33263410\end{syntax}
    33273411
     3412\begin{comment}
    33283413\constraints
    33293414If the \nonterm{declaration-specifiers} of a declaration that contains a \nonterm{forall-specifier}
     
    33393424members' type be?
    33403425\end{rationale}
     3426\end{comment}
    33413427
    33423428\semantics
     
    33443430identifiers, function and object identifiers with \Index{no linkage}.
    33453431
    3346 If, in the declaration ``\lstinline$T D1$'', \lstinline$T$ contains \nonterm{forall-specifier}s and
    3347 \lstinline$D1$ has the form
     3432If, in the declaration ``\lstinline$T D$'', \lstinline$T$ contains \nonterm{forall-specifier}s and
     3433\lstinline$D$ has the form
    33483434\begin{lstlisting}
    33493435D( @\normalsize\nonterm{parameter-type-list}@ )
     
    33563442assertions that use an inferred parameter of a function declarator are \Index{assertion parameter}s
    33573443of that function declarator.
     3444
     3445\begin{comment}
    33583446\begin{rationale}
    33593447Since every inferred parameter is used by some parameter, inference can be understood as a single
     
    33763464\end{lstlisting}
    33773465\end{rationale}
     3466\end{comment}
    33783467
    33793468If a function declarator is part of a function definition, its inferred parameters and assertion
Note: See TracChangeset for help on using the changeset viewer.