chiark / gitweb /
src/c-types-impl.lisp: Refactor pretty-printing of function types.
[sod] / doc / clang.tex
index e8bb1b549aa3c07f5d1a76cfc99cf08544b6351b..38e4b208ec24b076cd61f83d53cd08b87723b56f 100644 (file)
@@ -65,11 +65,12 @@ specified to return interned objects: programs may rely on receiving the same
 (@|eq|) type object for similar (possibly merely @|equal|) arguments.  Where
 not specified, clients may still not rely on receiving fresh objects.
 
-A convenient S-expression notation is provided by the @|c-type| macro.  Use
-of this macro is merely an abbreviation for corresponding use of the various
-constructor functions, and therefore interns type objects in the same manner.
-The syntax accepted by the macro can be extended in order to support new
-classes: see @|defctype|, @|c-type-alias| and @|define-c-type-syntax|.
+A convenient S-expression notation is provided by the
+\descref{c-type}[macro]{mac}.  Use of this macro is merely an abbreviation
+for corresponding use of the various constructor functions, and therefore
+interns type objects in the same manner.  The syntax accepted by the macro
+can be extended in order to support new classes: see \descref{defctype}{mac},
+\descref{c-type-alias}{mac} and \descref{define-c-type-syntax}{mac}.
 
 The descriptions of each of the various classes include descriptions of the
 initargs which may be passed to @|make-instance| when constructing a new
@@ -80,14 +81,15 @@ syntax are strongly recommended over direct use of @|make-instance|.
 There are two protocols for printing C types.  Unfortunately they have
 similar names.
 \begin{itemize}
-\item The @|print-c-type| function prints a C type value using the
-  S-expression notation.  It is mainly useful for diagnostic purposes.
-\item The @|pprint-c-type| function prints a C type as a C-syntax
-  declaration.
+\item The \descref{print-c-type}[function]{gf} prints a C type value using
+  the S-expression notation.  It is mainly useful for diagnostic purposes.
+\item The \descref{pprint-c-type}[function]{gf} prints a C type as a
+  C-syntax declaration.
 \end{itemize}
 Neither generic function defines a default primary method; subclasses of
 @|c-type| must define their own methods in order to print correctly.
 
+
 \subsection{The C type root class} \label{sec:clang.c-types.root}
 
 \begin{describe}{cls}{c-type ()}
@@ -100,6 +102,7 @@ Neither generic function defines a default primary method; subclasses of
   The class @|c-type| is abstract.
 \end{describe}
 
+
 \subsection{C type S-expression notation} \label{sec:clang.c-types.sexp}
 
 The S-expression representation of a type is described syntactically as a
@@ -177,6 +180,7 @@ type specifier.  Type specifiers fit into two syntactic categories.
   default method.
 \end{describe}
 
+
 \subsection{Comparing C types} \label{sec:clang.c-types.cmp}
 
 It is necessary to compare C types for equality, for example when checking
@@ -207,6 +211,7 @@ argument lists for methods.  This is done by @|c-type-equal-p|.
   \end{describe}
 \end{describe}
 
+
 \subsection{Outputting C types} \label{sec:clang.c-types.output}
 
 \begin{describe}{gf}{pprint-c-type @<c-type> @<stream> @<kernel>}
@@ -243,15 +248,14 @@ argument lists for methods.  This is done by @|c-type-equal-p|.
   directly attached.  If the @<kernel> function intends to provide its own
   additional declarator operators, it should check the @<priority> in order
   to determine whether parentheses are necessary.  See also the
-  @|maybe-in-parens| macro (page~\pageref{mac:maybe-in-parens}).
+  \descref{maybe-in-parens}[macro]{mac}.
 
   The @<spacep> argument indicates whether a space needs to be printed in
   order to separate the declarator from the declaration specifiers.  A kernel
   which contains an identifier should insert a space before the identifier
   when @<spacep> is non-nil.  An `empty' kernel, as found in an abstract
   declarator (one that specifies no name), looks more pleasing without a
-  trailing space.  See also the @|c-type-space| function
-  (page~\pageref{fun:c-type-space}).
+  trailing space.  See also the \descref{c-type-space}[function]{fun}.
 
   Every concrete subclass of @|c-type| is expected to provide a primary
   method on this function.  There is no default primary method.
@@ -290,6 +294,7 @@ argument lists for methods.  This is done by @|c-type-equal-p|.
   within the @<form>s.
 \end{describe}
 
+
 \subsection{Type qualifiers and qualifiable types}
 \label{sec:clang.ctypes.qual}
 
@@ -303,8 +308,8 @@ argument lists for methods.  This is done by @|c-type-equal-p|.
   qualifiers; others keywords may be used, though this isn't recommended.
 
   Two qualifiable types are equal only if they have \emph{matching
-    qualifiers}: i.e., every qualifier attached to one is also attached to
-  the other: order is not significant, and neither is multiplicity.
+  qualifiers}: i.e., every qualifier attached to one is also attached to the
+  other: order is not significant, and neither is multiplicity.
 
   The class @|qualifiable-c-type| is abstract.
 \end{describe}
@@ -324,12 +329,13 @@ argument lists for methods.  This is done by @|c-type-equal-p|.
   type will be interned.
 \end{describe}
 
-\begin{describe}{fun}{format-qualifiers @<qualifiers>}
+\begin{describe}{fun}{format-qualifiers @<qualifiers> @> @<string>}
   Returns a string containing the qualifiers listed in @<qualifiers> in C
   syntax, with a space after each.  In particular, if @<qualifiers> is
   non-null then the final character of the returned string will be a space.
 \end{describe}
 
+
 \subsection{Leaf types} \label{sec:clang.c-types.leaf}
 
 A \emph{leaf type} is a type which is not defined in terms of another type.
@@ -515,6 +521,7 @@ In Sod, the leaf types are
   keywords).
 \end{describe}
 
+
 \subsection{Compound C types} \label{sec:code.c-types.compound}
 
 Some C types are \emph{compound types}: they're defined in terms of existing
@@ -526,10 +533,11 @@ protocol.
   this means depends on the class of @<c-type>.
 \end{describe}
 
+
 \subsection{Pointer types} \label{sec:clang.c-types.pointer}
 
-Pointers compound types.  The subtype of a pointer type is the type it points
-to.
+Pointers are compound types.  The subtype of a pointer type is the type it
+points to.
 
 \begin{describe}{cls}
     {c-pointer-type (qualifiable-c-type) \&key :qualifiers :subtype}
@@ -558,6 +566,7 @@ to.
   interned also.
 \end{describe}
 
+
 \subsection{Array types} \label{sec:clang.c-types.array}
 
 Arrays implement the compound-type protocol.  The subtype of an array type is
@@ -599,6 +608,7 @@ the array element type.
   Returns the dimensions of @<c-type>, an array type, as an immutable list.
 \end{describe}
 
+
 \subsection{Function types} \label{sec:clang.c-types.fun}
 
 Function types implement the compound-type protocol.  The subtype of a
@@ -625,8 +635,8 @@ function type is the type of the function's return value.
 \end{describe}
 
 \begin{describe*}
-  {\dhead{fun}{argument-name @<argument> @> @<name>}
-   \dhead{fun}{argument-type @<argument> @> @<c-type>}}
+    {\dhead{fun}{argument-name @<argument> @> @<name>}
+     \dhead{fun}{argument-type @<argument> @> @<c-type>}}
   Accessor functions for @|argument| objects.  They return the name (for
   @|argument-name|) or type (for @|argument-type|) from the object, as passed
   to @|make-argument|.
@@ -746,6 +756,37 @@ function type is the type of the function's return value.
   @|commentify-argument-names| to the argument list of the given type.
 \end{describe}
 
+\begin{describe}{fun}
+    {pprint-c-function-type @<return-type> @<stream>
+                            @<print-args> @<print-kernel>}
+  Provides the top-level structure for printing C function types.
+
+  Output is written to @<stream> to describe a function type returning
+  @<return-type>, whose declarator kernel (containing the name, and any
+  further type operands) will be printed by @<print-kernel>, and whose
+  arguments, if any, will be printed by @<print-args>.
+
+  The @<print-kernel> function is a standard kernel-printing function
+  following the \descref{pprint-c-type}[protocol]{gf}.
+
+  The @<print-args> function is given a single argument, which is the
+  @<stream> to print on.  It should not print the surrounding parentheses.
+
+  The output written to @<stream> looks approximately like
+  \begin{prog}
+    @<return-type> @<kernel>(@<args>)
+  \end{prog}
+\end{describe}
+
+\begin{describe}{fun}{pprint-argument-list @<args> @<stream> @> @<flag>}
+  Print an argument list to @<stream>.
+
+  The @<args> is a list of \descref{argument}[objects]{cls}, optionally
+  containing an @|:ellipsis| marker.  The function returns true if any
+  arguments were actually printed.
+\end{describe}
+
+
 \subsection{Parsing C types} \label{sec:clang.c-types.parsing}
 
 \begin{describe}{fun}
@@ -758,6 +799,7 @@ function type is the type of the function's return value.
       \nlret @<result> @<success-flag> @<consumed-flag>}
 \end{describe}
 
+
 \subsection{Class types} \label{sec:clang.c-types.class}
 
 \begin{describe}{cls}
@@ -792,6 +834,7 @@ function type is the type of the function's return value.
 This section deals with Sod's facilities for constructing and manipulating C
 expressions, declarations, instructions and definitions.
 
+
 \subsection{Temporary names} \label{sec:clang.codegen.temporaries}
 
 Many C-level objects, especially ones with external linkage or inclusion in a
@@ -859,12 +902,14 @@ Temporary names are represented by objects which implement a simple protocol.
   \begin{tabular}[C]{*2{>{\codeface}l}}                            \hlx*{hv}
     \thd{\textbf{Variable}} & \thd{\textbf{Name format}}        \\ \hlx{vhv}
     {}*sod-ap*                  & sod__ap                       \\
-    {}*sod-master-ap*           & sod__master_ap                \\ \hlx*{vh}
+    {}*sod-master-ap*           & sod__master_ap                \\
+    {}*null-pointer*            & NULL                          \\ \hlx*{vh}
   \end{tabular}
   \caption{Well-known temporary names}
   \label{tab:codegen.codegen.well-known-temps}
 \end{table}
 
+
 \subsection{Instructions} \label{sec:clang.codegen.insts}
 
 \begin{describe}{cls}{inst () \&key}
@@ -887,46 +932,59 @@ Temporary names are represented by objects which implement a simple protocol.
       @<form>^*}
 \end{describe}
 
+\begin{describe}{fun}
+    {format-banner-comment @<stream> @<control> \&rest @<args>}
+\end{describe}
+
 \begin{table}
   \begin{tabular}[C]{ll>{\codeface}l}                              \hlx*{hv}
     \thd{Class name} &
     \thd{Arguments} &
     \thd{Output format}                                         \\ \hlx{vhv}
-    @|var|      & @<name> @<type> @<init>  & @<type> @<name> @[= @<init>@];
+    @|var|      & @<name> @<type> @|\&optional| @<init>
+                                           & @<type> @<name> @[= @<init>@];
                                                                 \\ \hlx{v}
     @|set|      & @<var> @<expr>           & @<var> = @<expr>;  \\ \hlx{v}
     @|update|   & @<var> @<op> @<expr>     & @<var> @<op>= @<expr>;
                                                                 \\ \hlx{v}
+    @|cond|     & @<cond> @<conseq> @<alt> & @<cond> ? @<conseq> : @<alt>
+                                                                \\ \hlx{v}
     @|return|   & @<expr>                  & return @[@<expr>@];
                                                                 \\ \hlx{v}
     @|break|    & ---                      & break;             \\ \hlx{v}
     @|continue| & ---                      & continue;          \\ \hlx{v}
     @|expr|     & @<expr>                  & @<expr>;           \\ \hlx{v}
-    @|call|     & @<func> @<args>          & @<func>(@<arg>_1,
+    @|call|     & @<func> @|\&rest| @<args>
+                                           & @<func>(@<arg>_1,
                                                      $\ldots$,
                                                      @<arg>_n)  \\ \hlx{v}
-    @|va-start| & @<ap> @<arg>             & va_start(@<ap>, @<arg>);
-                                                                \\ \hlx{v}
-    @|va-copy|  & @<to> @<from>            & va_copy(@<to>, @<from>);
-                                                                \\ \hlx{v}
-    @|va-end|   & @<ap>                    & va_end(@<ap>);     \\ \hlx{vhv}
+    @|banner|   & @<control> @|\&rest| @<args>
+                                           & /* @<banner> */    \\ \hlx{vhv}
     @|block|    & @<decls> @<body>         & \{ @[@<decls>@] @<body> \}
                                                                 \\ \hlx{v}
-    @|if|       & @<cond> @<conseq> @<alt> & if (@<cond>) @<conseq>
+    @|if|       & @<cond> @<conseq> @|\&optional| @<alt>
+                                           & if (@<cond>) @<conseq>
                                              @[else @<alt>@]    \\ \hlx{v}
+    @|for|      & @<init> @<cond> @<update> @<body> &
+      for (@<init>; @<cond>; @<update>) @<body>                 \\ \hlx{v}
     @|while|    & @<cond> @<body>          & while (@<cond>) @<body>
                                                                 \\ \hlx{v}
     @|do-while| & @<body> @<cond>          & do @<body> while (@<cond>);
                                                                 \\ \hlx{v}
-    @|function| & @<name> @<type> @<body>  &
-      @<type>_0 @<name>(@<type>_1 @<arg>_1, $\ldots$,
-                             @<type>_n @<arg>_n @[, \dots@])
-        @<body>                                                 \\ \hlx*{vh}
+    @|function| &
+      \vtop{\hbox{\strut @<name> @<type> @<body>}
+            \hbox{\strut \quad @|\&optional @<banner>|}
+            \hbox{\strut \quad @|\&rest| @<banner-args>}} &
+      \vtop{\hbox{\strut @[/* @<banner> */@]}
+            \hbox{\strut @<type>_0 @<name>(@<type>_1 @<arg>_1, $\ldots$,
+                                           @<type>_n @<arg>_n @[, \dots@])}
+            \hbox{\strut \quad @<body>}}                        \\ \hlx*{vh}
   \end{tabular}
   \caption{Instruction classes}
   \label{tab:codegen.codegen.insts}
 \end{table}
 
+
 \subsection{Code generation} \label{sec:clang.codegen.codegen}
 
 \begin{describe}{gf}{codegen-functions @<codegen> @> @<list>}
@@ -948,6 +1006,9 @@ Temporary names are represented by objects which implement a simple protocol.
 \begin{describe}{gf}{emit-decls @<codegen> @<decls>}
 \end{describe}
 
+\begin{describe}{fun}{emit-banner @<codegen> @<control> \&rest @<args>}
+\end{describe}
+
 \begin{describe}{gf}{codegen-push @<codegen>}
 \end{describe}
 
@@ -982,6 +1043,10 @@ Temporary names are represented by objects which implement a simple protocol.
 \begin{describe}{fun}{deliver-expr @<codegen> @<target> @<expr>}
 \end{describe}
 
+\begin{describe}{fun}
+    {deliver-call @<codegen> @<target> @<func> \&rest @<args>}
+\end{describe}
+
 \begin{describe}{fun}{convert-stmts @<codegen> @<target> @<type> @<func>}
 \end{describe}