doc/concepts.tex: Briefly explain translation-time metaobjects.
[sod] / doc / runtime.tex
CommitLineData
62f9852b
MW
1%%% -*-latex-*-
2%%%
3%%% The runtime library
4%%%
5%%% (c) 2015 Straylight/Edgeware
6%%%
7
8%%%----- Licensing notice ---------------------------------------------------
9%%%
10%%% This file is part of the Simple Object Definition system.
11%%%
12%%% SOD is free software; you can redistribute it and/or modify
13%%% it under the terms of the GNU General Public License as published by
14%%% the Free Software Foundation; either version 2 of the License, or
15%%% (at your option) any later version.
16%%%
17%%% SOD is distributed in the hope that it will be useful,
18%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
19%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20%%% GNU General Public License for more details.
21%%%
22%%% You should have received a copy of the GNU General Public License
23%%% along with SOD; if not, write to the Free Software Foundation,
24%%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26\chapter{The runtime library} \label{ch:runtime}
27
e520bc24
MW
28This chapter describes the runtime support macros and functions provided by
29the Sod library. The common structure of object instances and classes is
30described in \xref{ch:structures}.
31
32%%%--------------------------------------------------------------------------
9e91c8e7
MW
33\section{Keyword argument support} \label{sec:runtime.keywords}
34
35This section describes the types, macros, and functions exposed in the
36@|<sod/keyword.h>| header file which provides support for defining and
d26f542d
MW
37calling functions which make use of keyword arguments; see
38\xref{sec:concepts.keywords}.
9e91c8e7
MW
39
40
41\subsection{Type definitions} \label{sec:sec:runtime.keywords.types}
42
43The header file defines two simple structure types, and a function type which
44will be described later.
45
34042b35 46\begin{describe}{type}[struct kwval]
020b9e2b
MW
47 {struct kwval \{ \\ \ind
48 const char *kw; \\
49 const void *val; \-\\
9e91c8e7
MW
50 \};}
51
52 The @|kwval| structure describes a keyword argument name/value pair. The
53 @|kw| member points to the name, as a null-terminated string. The @|val|
54 member always contains the \emph{address} of the value. (This somewhat
55 inconvenient arrangement makes the size of a @|kwval| object independent of
56 the actual argument type.)
57\end{describe}
58
34042b35 59\begin{describe}{type}[struct kwtab]
020b9e2b
MW
60 {struct kwtab \{ \\ \ind
61 const struct kwval *v; \\
62 size_t n; \-\\
9e91c8e7
MW
63 \};}
64
65 The @|kwtab| structure describes a list of keyword arguments, represented
66 as a vector of @|kwval| structures. The @|v| member points to the start of
67 the vector; the @|n| member contains the number of elements in the vector.
68\end{describe}
69
70
71\subsection{Calling functions with keyword arguments}
72\label{sec:runtime.keywords.calling}
73
74Functions which accept keyword arguments are ordinary C functions with
75variable-length argument tails. Hence, they can be called using ordinary C
76(of the right kind) and all will be well. However, argument lists must
77follow certain rules (which will be described in full below); failure to do
78this will result in \emph{undefined behaviour}.
79
80The header file provides integration with some C compilers in the form of
81macros which can be used to help the compiler diagnose errors in calls to
82keyword-accepting functions; but such support is rather limited at the
83moment. Some additional macros are provided for use in calls to such
84functions, and it is recommended that, where possible, these are used. In
85particular, it's all too easy to forget the trailing null terminator which
86marks the end of a list of keyword arguments.
87
88That said, the underlying machinery is presented first, and the convenience
89macros are described later.
90
91\subsubsection{Keyword argument mechanism}
92The argument tail, following the mandatory arguments, consists of a sequence
93of zero or more alternating keyword names, as pointers to null-terminated
94strings (with type @|const char~*|), and their argument values. This
95sequence is finally terminated by a null pointer (again with type @|const
96char~*|) in place of a keyword name.
97
98Each function may define for itself which keyword names it accepts,
99and what types the corresponding argument values should have.
100There are also (currently) three special keyword names.
101\begin{description} \let\makelabel\code
102
103\item[kw.valist] This special keyword is followed by a pointer to a
104 variable-length argument tail cursor object, of type @|va_list~*|. This
105 cursor object will be modified as the function extracts successive
106 arguments from the tail. The argument tail should consist of alternating
107 keyword names and argument values, as described above, including the first
108 keyword name. (This is therefore different from the convention used when
109 calling keyword argument parser functions: see the description of the
110 \descref{KWSET_PARSEFN}[macro]{mac} for more details about these.) The
111 argument tail may itself contain the special keywords.
112
113\item[kw.tab] This special keyword is followed by \emph{two} argument values:
114 a pointer to the base of a vector of @|kwval| structures, and the number of
115 elements in this vector (as a @|size_t|). Each element of the vector
116 describes a single keyword argument: the @|kw| member points to the
117 keyword's name, and the @|val| member points to the value.
118
119 The vector may contain special keywords. The @|val| pointer for a
120 @|kw.valist| argument should contain the address of an object of type
121 @|va_list~*| (and not point directly to the cursor object, since @|val| is
122 has type @|const void~*| but the cursor will be modified as its argument
123 tail is traversed). The @|val| pointer for a @|kw.tab| argument should
124 contain the address of a @|kwtab| structure which itself contains the base
125 address and length of the argument vector to be processed.
126
127\item[kw.unknown] This keyword is never accepted by any function. If it is
128 encountered, the @|kw_unknown| function is called to report the situation
129 as an error; see below.
130
131\end{description}
132It is possible to construct a circular structure of indirect argument lists
133(in a number of ways). Don't try to pass such a structure to a function: the
134result will be unbounded recursion or some other bad outcome.
135
136\subsubsection{Argument list structuring macros}
137The following macros are intended to help with constructing keyword argument
138lists. Their use is not essential, but may help prevent errors.
139
34042b35 140\begin{describe}{mac}[KWARGS]{KWARGS(@<body>)}
9e91c8e7
MW
141 The @<body> encloses a sequence of keyword arguments expressed as calls to
142 argument consists of a sequence of calls to the keyword-argument macros
143 described below, one after another without any separation.
144
145 In C89, macro actual arguments are not permitted to be empty; if there are
146 no keyword arguments to provide, and you're using a C89 compiler, then use
147 @|NO_KWARGS| (below) instead. If your compiler supports C99 or later, it's
148 fine to just write @|KWARGS()| instead.
149\end{describe}
150
151\begin{describe}{mac}{NO_KWARGS}
152 A marker, to be written instead of a @|KWARGS| invocation, to indicate that
153 no keyword arguments are to be passed to a function.
154
155 This is unnecessary with compilers which support C99 or later, since once
156 can use @|KWARGS()| with an empty @<body> argument.
157\end{describe}
158
159The following keyword-argument macros can be used within the @|KWARGS|
160@<body> argument.
161
34042b35 162\begin{describe}{mac}[K]{K(@<name>, @<value>)}
9e91c8e7
MW
163 Passes a keyword @<name> and its corresponding @<value>, as a pair of
164 arguments. The @<name> should be a single identifier (not a quoted
165 string). The @<value> may be any C expression of the appropriate type.
166\end{describe}
167
34042b35 168\begin{describe}{mac}[K_VALIST]{K_VALIST(@<ap>)}
9e91c8e7
MW
169 Passes an indirect variable-length argument tail. The argument @<ap>
170 should be an lvalue of type @|va_list|, which will be passed by reference.
171\end{describe}
172
34042b35 173\begin{describe}{mac}[K_TAB]{K_TAB(@<v>, @<n>)}
9e91c8e7
MW
174 Passes a vector of keyword arguments. The argument @<v> should be the base
175 address of the vector, and @<n> should be the number of elements in the
176 vector.
177\end{describe}
178
179
180\subsection{Defining functions with keyword arguments}
181\label{sec:runtime.keywords.defining}
182
183\subsubsection{Keyword sets}
184A \emph{keyword set} defines the collection of keyword arguments accepted by
185a particular function. The same keyword set may be used by several
186functions. (If your function currently accepts no keyword arguments, but you
781a8fbd 187plan to add some later, do not define a keyword set; instead, use the
9e91c8e7
MW
188@|KWPARSE_EMPTY| macro described below.)
189
190Each keyword set has a name, which is a C identifier. It's good to choose
191meaningful and distinctive names for keyword sets. Keyword set names are
192meaningful at runtime: they are used as part of the @|kw_unknown| protocol
193(\xref{sec:runtime.keywords.unknown}), and may be examined by handler
194functions, or reported to a user in error messages. For a keyword set which
195is used only by a single function, it is recommended that the set be given
196the same name as the function.
197
198The keyword arguments for a keyword set named @<set> are described by a `list
199macro' named @|@<set>{}_KWSET|. This macro takes a single argument,
781a8fbd
MW
200conventionally named @`_'. It should expand to a sequence of one or more
201list items of the form
9e91c8e7
MW
202\begin{prog}
203 _(@<type>, @<name>, @<default>)
204\end{prog}
781a8fbd 205with no separation between them. For example:
9e91c8e7 206\begin{prog}
5e4a5d53
MW
207 \#define example_KWSET(_) \macsl \\ \ind
208 _(int, x, 0) \macsl \\
9e91c8e7
MW
209 _(const char *, y, NULL)
210\end{prog}
211
212Each @<name> should be a distinct C identifier; they will be used to name
213structure members. An argument @<name> should not end with the suffix
214@`_suppliedp' (for reasons which will soon become apparent).
215
216Each @<type> should be a C @<type-name> such that
217\begin{prog}
218 @<type> @<name> ;
219\end{prog}
220is a valid declaration: so it may consist of declaration specifiers and
221(possibly qualified) pointer declarator markers, but not array or function
222markers (since they would have to be placed after the @<name>). This is the
223same requirement made by the standard \man{va_arg}{3} macro.
224
225Each @<default> should be an initializer expression or brace-enclosed list,
226suitable for use in an aggregate initializer for a variable with automatic
227storage duration. (In C89, aggregate initializers may contain only constant
228expressions; this restriction was lifted in C99.)
229
230\subsubsection{Function declaration markers}
231The following marker macros are intended to be used in both declarations and
232definitions of functions which accept keyword arguments.
233
234\begin{describe}{mac}{KWTAIL}
235 The @|KWTAIL| is expected to be used at the end of function parameter type
236 list to indicate that the function accepts keyword arguments; if there are
237 preceding mandatory arguments then the @|KWTAIL| marker should be separated
238 from them with a comma @`,'. (It is permitted for a function parameter
239 type list to contain only a @|KWTAIL| marker.)
240
241 Specifically, the macro declares a mandatory argument @|const char
242 *kwfirst_| (to collect the first keyword name), and a variable-length
243 argument tail.
244
245 The \descref{KWPARSE}[macro]{mac} assumes that the enclosing function's
246 argument list ends with a @|KWTAIL| marker.
247\end{describe}
248
249\begin{describe}{mac}{KWCALL}
250 The @|KWCALL| macro acts as a declaration specifier for functions which
251 accept keyword arguments. Its effect is to arrange for the compiler to
252 check, as far as is possible, that calls to the function are well-formed
253 according to the keyword-argument rules. The exact checking performed
254 depends on the compiler's abilities (and how well supported the compiler
255 is): it may check that every other argument is a string; it may check that
256 the list is terminated with a null pointer; it may not do anything at all.
257 Again, this marker should be included in a function's definition and in any
258 declarations.
259\end{describe}
260
261\subsubsection{Auxiliary definitions}
262The following macros define data types and functions used for collecting
263keyword arguments.
264
34042b35 265\begin{describe}{mac}[KWSET_STRUCT]{KWSET_STRUCT(@<set>);}
9e91c8e7
MW
266 The @|KWSET_STRUCT| macro defines a \emph{keyword structure} named @|struct
267 @<set>{}_kwargs|. For each argument defined in the keyword set, this
268 structure contains two members: one has exactly the @<name> and @<type>
269 listed in the keyword set definition; the other is a 1-bit-wide bitfield of
270 type @|unsigned int| named @|@<name>{}_suppliedp|.
271\end{describe}
272
34042b35 273\begin{describe}{mac}[KWDECL]
9e91c8e7
MW
274 {@<declaration-specifiers> KWDECL(@<set>, @<kw>);}
275 The macro declares and initializes a keyword argument structure variable
276 named @<kw> for the named keyword @<set>. The optional
781a8fbd
MW
277 @<declaration-specifiers> may provide additional storage-class specifiers,
278 qualifiers, or other declaration specifiers. The @`_suppliedp' flags are
279 initialized to zero; the other members are initialized with the
280 corresponding defaults from the keyword-set definition.
9e91c8e7
MW
281\end{describe}
282
34042b35 283\begin{describe}{mac}[KWSET_PARSEFN]
9e91c8e7
MW
284 {@<declaration-specifiers> KWSET_PARSEFN(@<set>)}
285
286 The macro @|KWSET_PARSEFN| defines a keyword argument \emph{parser
287 function}
288 \begin{prog}
020b9e2b
MW
289 void @<set>{}_kwparse%
290 (\=struct @<set>{}_kwargs *@<kw>,
291 const char *@<kwfirst>, va_list *@<ap>, \+\\
292 const struct kwval *@<v>, size_t @<n>);
9e91c8e7
MW
293 \end{prog}
294 The macro call can (and usually will) be preceded by storage class
295 specifiers such as @|static|, for example to adjust the linkage of the
296 name.\footnote{%
297 I don't recommend declaring parser functions @|inline|: parser functions
298 are somewhat large, and modern compilers are pretty good at figuring out
299 whether to inline static functions.} %
300
301 The function's behaviour is as follows. It parses keyword arguments from a
302 variable-length argument tail, and/or a vector of @|kwval| structures.
303 When a keyword argument is recognized, for some keyword @<name>, the
304 keyword argument structure pointed to by @<kw> is updated: the flag
305 @|@<name>{}_suppliedp| is set to 1; and the argument value is stored (by
306 simple assignment) in the @<name> member.
307
308 Hence, if the @`_suppliedp' members are initialized to zero, the caller can
309 determine which keyword arguments were supplied. It is not possible to
310 discover whether two or more arguments have the same keyword: in this case,
311 the value from the last such argument is left in the keyword argument
312 structure, and any values from earlier arguments are lost. (For this
313 purpose, the argument vector @<v> is scanned \emph{after} the
314 variable-length argument tail captured in @<ap>.)
315
5b0c3a98 316 The variable-argument tail is read from the list described by @|*@<ap>|.
9e91c8e7
MW
317 The argument tail is expected to consist of alternating keyword strings (as
318 ordinary null-terminated strings) and the corresponding values, terminated
319 by a null pointer of type @|const char~*| in place of a keyword; except
320 that the first keyword (or terminating null pointer, if no arguments are
321 provided) is expected to have been extracted already and provided as the
322 @<kwfirst> argument; the first argument retrieved using the @|va_list|
323 cursor object should then be the value corresponding to the keyword named
324 by @<kwfirst>.\footnote{%
325 This slightly unusual convention makes it possible for a function to
326 collect the first keyword as a separate mandatory argument, which is
327 essential if there are no other mandatory arguments. It also means that
328 the compiler will emit a diagnostic if you attempt to call a function
329 which expects keyword arguments, but don't supply any and forget the null
330 pointer which terminates the (empty) list.} %
331 If @<kwfirst> is a null pointer, then @<ap> need not be a valid pointer;
02840f3d 332 otherwise, the cursor object @|*@<ap>| will be modified as the function
9e91c8e7
MW
333 extracts successive arguments from the tail.
334
335 The keyword vector is read from the vector of @|kwval| structures starting
336 at address @<v> and containing the following @<n> items. If @<n> is zero
337 then @<v> need not be a valid pointer.
338
339 The function also handles the special @|kw.valist| and @|kw.tab| arguments
340 described above (\xref{sec:runtime.keywords.calling}). If an unrecognized
341 keyword argument is encountered, then \descref{kw_unknown}{fun} is called.
342\end{describe}
343
344\subsubsection{Parsing keywords}
345The following macros make use of the definitions described above to actually
346make a function's keyword arguments available to it.
347
34042b35 348\begin{describe}{mac}[KW_PARSE]{KW_PARSE(@<set>, @<kw>, @<kwfirst>);}
9e91c8e7
MW
349 The @|KW_PARSE| macro invokes a keyword argument parsing function. The
350 @<set> argument should name a keyword set; @<kw> should be an lvalue of
351 type @|struct @<set>{}_kwargs|; and @<kwfirst> should be the name of the
352 enclosing function's last mandatory argument, which must have type @|const
353 char~*|.
354
355 It calls the function @|@<set>{}_kwparse| with five arguments: the address
356 of the keyword argument structure @<kw>; the string pointer @<kwfirst>; the
357 address of a temporary argument-tail cursor object of type @|va_list|,
358 constructed on the assumption that @<kwfirst> is the enclosing function's
359 final keyword argument; a null pointer; and the value zero (signifying an
360 empty keyword-argument vector).
361
362 If the variable @<kw> was declared using \descref{KWDECL}{mac} and the
363 function @|@<set>{}_kwparse| has been defined using
364 \descref{KWSET_PARSEFN}{mac} then the effect is to parse the keyword
365 arguments passed to the function and set the members of @<kw>
366 appropriately.
367\end{describe}
368
34042b35 369\begin{describe}{mac}[KWPARSE]{KWPARSE(@<set>);}
9e91c8e7
MW
370 The macro @|KWPARSE| (note the lack of underscore) combines
371 \descref{KWDECL}{mac} and \descref{KW_PARSE}{mac}. It declares and
372 initializes a keyword argument structure variable with the fixed name
373 @|kw|, and parses the keyword arguments provided to the enclosing function,
374 storing the results in @|kw|. It assumes that the first keyword name is in
375 an argument named @|kwfirst_|, as set up by the
376 \descref{KWTAIL}[marker]{mac}.
377
378 The macro expands both to a variable declaration and a statement: in C89,
379 declarations must precede statements, so under C89 rules this macro must
380 appear exactly between the declarations at the head of a brace-enclosed
381 block (typically the function body) and the statements at the end. This
382 restriction was lifted in C99, so the macro may appear anywhere in the
383 function body. However, it is recommended that callers avoid taking
384 actions which might require cleanup before attempting to parse their
385 keyword arguments, since keyword argument parsing functions invoke the
386 @|kw_unknown| handler (\xref{sec:runtime.keywords.unknown}) if they
387 encounter an unknown keyword, and the calling function will not get a
388 chance to tidy up after itself if this happens.
389\end{describe}
390
391As mentioned above, it is not permitted to define an empty keyword set.
392(Specifically, invoking \descref{KWSET_STRUCT}{mac} for an empty keyword set
393would result in attempting to define a structure with no members, which C
394doesn't allow.) On the other hand, keyword arguments are a useful extension
395mechanism, and it's useful to be able to define a function which doesn't
396currently accept any keywords, but which might in the future be extended to
397allow keyword arguments.
398
34042b35 399\begin{describe}{mac}[KW_PARSE_EMPTY]{KW_PARSE_EMPTY(@<set>, @<kwfirst>);}
9e91c8e7
MW
400 This is an analogue to \descref{KW_PARSE}{mac} which checks the keyword
401 argument list for a function which accepts no keyword arguments.
402
403 It calls the \descref{kw_parseempty}[function]{fun} with five arguments:
404 the @<set> name, as a string; the string pointer @<kwfirst>; the address of
405 a temporary argument-tail cursor object of type @|va_list|, constructed on
406 the assumption that @<kwfirst> is the enclosing function's final keyword
407 argument; a null pointer; and the value zero (signifying an empty
408 keyword-argument vector).
409
410 The effect is to check that the argument tail contains no keyword arguments
411 other than the special predefined ones.
412\end{describe}
413
34042b35 414\begin{describe}{mac}[KWPARSE_EMPTY]{KWPARSE_EMPTY(@<set>);}
9e91c8e7
MW
415 This is an analogue to \descref{KWPARSE}{mac} which checks that the
416 enclosing function has been passed no keyword arguments other than the
417 special predefined ones. It assumes that the first keyword name is in an
418 argument named @|kwfirst_|, as set up by the \descref{KWTAIL}[marker]{mac}.
419\end{describe}
420
34042b35 421\begin{describe}{fun}[kw_parseempty]
020b9e2b
MW
422 {void kw_parseempty%
423 (\=const char *@<set>,
424 const char *@<kwfirst>, va_list *@<ap>, \+\\
425 const struct kwval *@<v>, size_t @<n>);}
9e91c8e7
MW
426 This function checks an keyword argument list to make sure that contains no
427 keyword arguments (other than the special ones described in
428 \xref{sec:runtime.keywords.calling}).
429
430 The @<set> argument should point to a null-terminated string: this will be
431 reported as the keyword set name to \descref{kw_unknown}{fun}, though it
432 need not (and likely will not) refer to any defined keyword set. The
433 remaining arguments are as for the keyword parsing functions defined by the
434 \descref{KWSET_PARSEFN}[macro]{mac}.
435\end{describe}
436
437\subsection{Function wrappers} \label{sec:runtime.keywords.wrappers}
438
439Most users will not need the hairy machinery involving argument vectors.
440Their main use is in defining \emph{wrapper functions}. Suppose there is a
441function @<f> which accepts some keyword arguments, and we want to write a
442function @<g> which accepts the same keywords recognized by @<f> and some
443additional ones. Unfortunately @<f> may behave differently depending on
444whether or not a particular keyword argument is supplied at all, but it's not
445possible to synthesize a valid @|va_list| other than by simply capturing a
446live argument tail, and it's not possible to decide at runtime whether or not
447to include some arguments in a function call. It's still possible to write
448@<g>, by building a vector of keyword arguments, collected one-by-one
449depending on the corresponding @`_suppliedp' flags.
450
451A few macros are provided to make this task easier.
452
34042b35 453\begin{describe}{mac}[KW_COUNT]{KW_COUNT(@<set>)}
9e91c8e7
MW
454 Returns the number of keywords defined in a keyword set named @<set>.
455\end{describe}
456
34042b35 457\begin{describe}{mac}[KW_COPY]
9e91c8e7
MW
458 {KW_COPY(@<fromset>, @<toset>, @<kw>, @<v>, @<n>);}
459
460 The macro @|KW_COPY| populates a vector of @|kwval| structures from a
461 keyword-argument structure.
462
463 The @<fromset> and @<toset> arguments should be the names of keyword sets;
464 @<kw> should be an lvalue of type @|@<fromset>{}_kwargs|; @<v> should be
465 the base address of a sufficiently large vector of @|struct kwval| objects;
466 and @<n> should be an lvalue of some appropriate integer type. The
467 @<toset> must be a subset of @<fromset>: i.e., for every keyword defined in
468 @<toset> there is a keyword defined in @<fromset> with the same name and
469 type.
470
471 Successive elements of @<v>, starting at index @<n>, are filled in to refer
472 to the keyword arguments defined in @<toset> whose @`_suppliedp' flag is
473 set in the argument structure pointed to by @<kw>; for each such argument,
474 a pointer to the keyword name is stored in the corresponding vector
475 element's @|kw| member, and a pointer to the argument value, held in the
476 keyword argument structure, is stored in the vector element's @|val|
477 member.
478
479 At the end of this, the index @<n> is advanced so as to contain the index
480 of the first unused element of @<v>. Hence, at most @|KW_COUNT(@<toset>)|
481 elements of @<v> will be used.
482\end{describe}
483
484
485\subsection{Handling unknown-keyword errors}
486\label{sec:runtime.keywords.unknown}
487
488When parsing a variable-length argument tail, it is not possible to continue
489after encountering an unknown keyword name. This is because it is necessary
490to know the (promoted) type of the following argument value in order to skip
491past it; but the only clue provided as to the type is the keyword name, which
492in this case is meaningless.
493
494In this situation, the parser functions generated by
495\descref{KWSET_PARSEFN}{mac} (and the \descref{kw_parseempty}[function]{fun})
496call @|kw_unknown|.
497
34042b35 498\begin{describe}{fun}[kw_unknown]
9e91c8e7
MW
499 {void kw_unknown(const char *@<set>, const char *@<kw>);}
500
501 This is a function of two arguments: @<set> points to the name of the
502 keyword set expected by the caller, as a null-terminated string; and @<kw>
503 is the unknown keyword which was encountered. All that @|kw_unknown| does
504 is invoke the function whose address is stored in the global variable
505 \descref{kw_unkhook}{var} with the same arguments.
506
507 This function never returns to its caller: if the @|kw_unkhook| function
508 returns (which it shouldn't) then @|kw_unknown| writes a fatal error
509 message to the standard error stream and calls \man{abort}{3}.
510\end{describe}
511
34042b35 512\begin{describe}{type}[kw_unkhookfn]
9e91c8e7
MW
513 {typedef void kw_unkhookfn(const char *@<set>, const char *@<kw>);}
514
515 The @|kw_unkhookfn| type is the type of unknown-keyword handler functions.
516 A handler function is given two arguments, both of which are pointers to
517 null-terminated strings: @<set> is the name of the keyword set expected;
518 and @<kw> is the name of the offending unknown keyword.
519\end{describe}
520
34042b35 521\begin{describe}{var}[kw_unkhook]{kw_unkhookfn *kw_unkhook}
9e91c8e7
MW
522 This variable\footnote{%
523 Having a single global hook variable is obviously inadequate for a modern
524 library, but dealing with multiple threads isn't currently possible
525 without writing (moderately complex) system-specific code which would be
526 out of place in this library. The author's intention is that the hook
527 variable @|kw_unkhook| be `owned' by some external library which can make
528 its functionality available to client programs in a safer and more
529 convenient way. On Unix-like platforms (including Cygwin) that library
530 will be (a later version of) \textbf{mLib}; other platforms will likely
531 need different arrangements. The author is willing to coordinate any
532 such efforts.} %
533 holds the current unknown-keyword handler function. It will be invoked by
534 \descref{kw_unknown}{fun}. The function may take whatever action seems
535 appropriate, but should not return to its caller.
536
537 Initially, this variable points to the
538 \descref{kw_defunknown}[function]{fun}.
539\end{describe}
540
34042b35 541\begin{describe}{fun}[kw_defunknown]
9e91c8e7
MW
542 {void kw_defunknown(const char *@<set>, const char *@<kw>);}
543 This function simply writes a message to standard error, to the effect that
544 the keyword named by @<kw> is not known in the keyword set @<set>, and
545 calls \man{abort}{3}.
546
547 This function is the default value of the \descref{kw_unkhook}[hook
548 variable]{var}.
549\end{describe}
550
551As an example of the kind of special effect which can be achieved using this
552hook, the following hacking answers whether a function recognizes a
553particular keyword argument.
554
555\begin{prog}
556 \#define KWARGS_TEST(k, val) KWARGS(K(k, val) K(kw.unknown, 0))
020b9e2b 557 \\+
9e91c8e7 558 static jmp_buf kw_test_jmp;
020b9e2b
MW
559 \\+
560 static void kw_test_unknown(const char *set, const char *kw) \\
561 \{ \\ \ind
562 if (strcmp(kw, "kw.unknown")) longjmp(kw_test_jmp, 1); \\
563 else longjmp(kw_test_jmp, 2); \-\\
564 \} \\+
565
5e4a5d53
MW
566 \#define KW_TEST(flag, set, call) do \{ \macsl \\ \ind
567 kw_unkhookfn *oldunk = kw_unkhook; \macsl \\
568 kw_unkhook = kw_test_unknown; \macsl \\
569 switch (setjmp(kw_test_jmp)) \{ \macsl \\ \ind
570 case 0: call; abort(); \macsl \\
571 case 1: flag = 1; break; \macsl \\
572 case 2: flag = 0; break; \macsl \\
573 default: abort(); \macsl\-\\
574 \} \macsl \\
575 kw_unkhook = oldunk; \macsl\-\\
020b9e2b
MW
576 \} while (0) \\+
577
578 @/* Example of use */ \\
579 int f; \\
9e91c8e7 580 KW_TEST(f, somefunc(1, "two", 3, KWARGS_TEST("shiny", 68.7))); \\
888d5e58 581 /\=* \comment{now @|f| is nonzero if @|somefunc| accepts the
020b9e2b
MW
582 @|shiny| keyword} \+\\
583 {}* \comment{(which we hope wants a @|double| argument)} \\
9e91c8e7
MW
584 {}*/
585\end{prog}
586
587%%%--------------------------------------------------------------------------
e520bc24
MW
588\section{Object system support} \label{sec:runtime.object}
589
590This section describes the macros and functions exposed in the @|<sod/sod.h>|
591header file which provide assistance for working with Sod classes and
592objects.
62f9852b
MW
593
594The runtime support functionality defined here generally expects that
595instances and classes inherit from the standard @|SodObject| root object.
596While the translator can (at some effort) support alternative roots, they
597will require different run-time support machinery.
598
e520bc24 599
4e3a7cb2 600\subsection{Layout utilities} \label{sec:runtime.object.layout}
e520bc24 601
4e3a7cb2
MW
602The following macros are useful in finding one's way around an instance
603layout structure, given various levels of information about what kind of
604object one is dealing with, or for computing the tables which are used for
605this kind of navigation.
62f9852b
MW
606
607These macros are mostly intended for use in code generated by the Sod
608translator. Others may find them useful for special effects, but they can be
609tricky to understand and use correctly and can't really be recommended for
610general use.
611
34042b35 612\begin{describe}{mac}[SOD_OFFSETDIFF]
09efeb89 613 {ptrdiff_t SOD_OFFSETDIFF(@<type>, @<member>_1, @<member>_2);}
62f9852b
MW
614 Returns the signed offset between two members of a structure or union type.
615
616 Given a structure or union type @<type>, and two member names @<member>_1
617 and @<member>_2, then @|SOD_OFFSETDIFF| gives the difference, in bytes,
618 between the addresses of objects @|$x$.@<member>_1| and @|$x$.@<member>_2|
619 for any object $x$ of type @<type>.
620
621 This macro is used internally when generating vtables and is not expected
622 to be very useful elsewhere.
623\end{describe}
624
34042b35 625\begin{describe}{mac}[SOD_ILAYOUT]
09efeb89 626 {@<cls>{}__ilayout *SOD_ILAYOUT(@<cls>, @<chead>, const void *@<obj>);}
62f9852b
MW
627 Recovers the instance layout base address from a pointer to one of its
628 instance chains.
629
630 Specifically, given a class name @<cls>, the nickname @<chead> of the least
631 specific class in one of @<cls>'s superclass chains, and a pointer @<obj>
632 to the instance storage for the chain containing @<chead> within a direct
633 instance of @<cls> (i.e., not an instance of any proper subclass),
634 @|SOD_ILAYOUT| returns the a pointer to the layout structure containing
635 @<obj>.
636
637 This macro is used internally in effective method bodies and is not
638 expected to be very useful elsewhere since it's unusual to have such
639 specific knowledge about the dynamic type of an instance. The
640 @|SOD_INSTBASE| macro (described below) is more suited to general use.
641\end{describe}
642
34042b35 643\begin{describe}{mac}[SOD_INSTBASE]{void *SOD_INSTBASE(const @<cls> *@<obj>)}
62f9852b
MW
644 Finds the base address of an instance's layout.
645
646 Given a pointer @<obj> to an instance, @|SOD_INSTBASE| returns the base
647 address of the storage allocated to @<obj>. This is useful if you want to
648 free a dynamically allocated instance, for example.
649
650 This macro needs to look up an offset in @<obj>'s vtable to do its work.
651 Compare @|SOD_ILAYOUT| above, which is faster but requires precise
652 knowledge of the instance's dynamic class.
653\end{describe}
654
62f9852b 655
4e3a7cb2 656\subsection{Classes} \label{sec:runtime.object.class}
62f9852b 657
4e3a7cb2
MW
658The following macros and functions query the runtime relationships between
659instances and classes.
62f9852b 660
34042b35 661\begin{describe}{mac}[SOD_CLASSOF]
4e3a7cb2
MW
662 {const SodClass *SOD_CLASSOF(const @<cls> *@<obj>);}
663 Returns the class object describing an instance's dynamic class.
62f9852b 664
4e3a7cb2
MW
665 Given a pointer @<obj> to an instance, @|SOD_CLASSOF| returns a pointer to
666 @<obj>'s dynamic class, which (assuming @<obj> is typed correctly in the
667 first place) will be a subclass of @<cls>. (If you wanted the class object
668 for @<cls> itself, it's called @|@<cls>{}__class|.)
62f9852b
MW
669\end{describe}
670
34042b35 671\begin{describe}{fun}[sod_subclassp]
0ed1c8a9 672 {int sod_subclassp(const SodClass *@<sub>, const SodClass *@<super>);}
62f9852b
MW
673
674 Decide whether one class @<sub> is actually a subclass of another class
675 @<super>.
676
677 The @<sod_subclassp> function returns nonzero if and only if
678 @<sub> is a subclass of @<super>.
679
680 This involves a run-time trawl through the class structures: while some
681 effort has been made to make it perform well it's still not very fast.
682\end{describe}
683
4e3a7cb2
MW
684
685\subsection{Conversions} \label{sec:runtime.object.conversions}
686
687The following macros and functions are used to convert instance pointers of
688some (static) type into instance pointers of other static types to the same
689instance.
690
34042b35 691\begin{describe}{mac}[SOD_XCHAIN]
3d0d9e36 692 {void *SOD_XCHAIN(@<chead>, const @<cls> *@<obj>);}
4e3a7cb2
MW
693 Performs a `cross-chain upcast'.
694
695 Given a pointer @<obj> to an instance of a class of type @<cls> and the
696 nickname @<chead> of the least specific class in one of @<cls>'s superclass
697 chains which does not contain @<cls> itself, @|SOD_XCHAIN| returns the
698 address of that chain's storage within the instance layout as a raw
699 @|void~*| pointer. (Note that @<cls> is not mentioned explicitly.)
700
701 This macro is used by the generated @|@<cls>{}__CONV_@<c>| conversion
702 macros, which you are encouraged to use instead where possible.
703\end{describe}
704
705\begin{describe*}
34042b35 706 {\dhead{mac}[SOD_CONVERT]
4e3a7cb2 707 {@<cls> *SOD_CONVERT(@<cls>, const void *@<obj>);}
34042b35 708 \dhead{fun}[sod_convert]
4e3a7cb2
MW
709 {void *sod_convert(const SodClass *@<cls>, const void *@<obj>);}}
710 Perform general conversions (up-, down-, and cross-casts) on instance
62f9852b
MW
711 pointers.
712
4e3a7cb2
MW
713 Given a class @<cls> and a pointer @<obj> to an instance, return an
714 appropriately converted pointer to @<obj> if @<obj> is indeed an instance
715 of (some subclass of) @<cls>; otherwise return a null pointer.
716
717 The @|SOD_CONVERT| macro expects @<cls> to be a class name; the
718 @|sod_convert| function expects a pointer to a class object instead.
62f9852b
MW
719
720 This involves a run-time trawl through the class structures: while some
721 effort has been made to make it perform well it's still not very fast. For
722 upcasts (where @<cls> is a superclass of the static type of @<obj>) the
723 automatically defined conversion macros should be used instead, because
4e3a7cb2
MW
724 they're much faster and can't fail.
725
726 When the target class is known statically, it's slightly more convenient to
727 use the @|SOD_CONVERT| macro than the @|sod_convert| function, since the
728 class object name is longer and uglier, and the macro returns a pointer of
729 the correct type.
730\end{describe*}
731
732
733\subsection{Instance lifecycle}
734\label{sec:runtime.object.lifecycle}
735
736The following macros and functions manage the standard steps along an
737instance's lifecycle.
738
a142609c
MW
739\subsubsection{Low-level operations}
740The following macros and functions are agnostic with respect to storage
741allocation strategies. They don't concern themselves with allocation or
742deallocation, and applications are free to use any suitable mechanism.
743
744\begin{describe*}
34042b35 745 {\dhead{mac}[SOD_INIT]
a142609c 746 {@<cls> *SOD_INIT(@<cls>, void *@<p>, @<keywords>);}
34042b35 747 \dhead{fun}[sod_init]
a142609c 748 {void *sod_init(const SodClass *@<cls>, void *@<p>, \dots);}
34042b35 749 \dhead{fun}[sod_initv]
a142609c
MW
750 {void *sod_initv(const SodClass *@<cls>, void *@<p>, va_list @<ap>);}}
751 Imprints and initializes an instance of a class @<cls> in the storage
752 starting at address~@<p>.
753
754 The direct class for the new instance is specified as a class name to
755 @|SOD_INIT|, or a pointer to a class object to the functions.
756
757 Keyword arguments for the initialization message may be provided. The
758 @|SOD_INIT| macro expects a single preprocessor-time argument which is
759 a use of one of \descref{KWARGS}{mac} or \descref{NO_KWARGS}{mac}; the
760 @|sod_init| function expects the keywords as a variable-length argument
761 tail; and @|sod_initv| expects the keywords to be passed indirectly,
762 through the captured argument-tail cursor @<ap>.
763
764 The return value is an instance pointer for the class @<cls>; the
765 @|SOD_INIT| macro will have converted it to the correct type, so it should
766 probably be used where possible. In fact, this is guaranteed to be equal
767 to @<p> by the layout rules described in
768 \xref{sec:structures.layout.instance}.
769\end{describe*}
770
34042b35 771\begin{describe}{fun}[sod_teardown]{int sod_teardown(void *@<p>);}
a42893dd
MW
772 Tears down an instance of a class, releasing any resources it holds.
773
774 This function is a very thin wrapper around sending the @|obj.teardown|
775 message. See the description of that message
776 (page~\pageref{msg:obj.teardown}) and \xref{sec:concepts.lifecycle.death}
777 for details.
778\end{describe}
779
4e3a7cb2
MW
780\subsubsection{Automatic storage duration}
781The following macro constructs an instance with automatic storage duration.
782
34042b35 783\begin{describe}{mac}[SOD_DECL]{SOD_DECL(@<cls>, @<var>, @<keywords>);}
4e3a7cb2
MW
784 Declares and initializes an instance with automatic storage duration.
785
786 Given a class name @<cls> and an identifier @<var>, @|SOD_DECL| declares
787 @<var> to be a pointer to an instance of @<cls>. The instance is
788 initialized in the sense that its vtable and class pointers have been set
789 up, and slots for which initializers are defined are set to the appropriate
790 initial values.
791
a142609c
MW
792 Keyword arguments for the initialization message may be provided. The
793 macro expects a single preprocessor-time argument which is a use of one of
794 \descref{KWARGS}{mac} or \descref{NO_KWARGS}{mac}.
795
4e3a7cb2 796 The instance has automatic storage duration: pointers to it will become
a42893dd
MW
797 invalid when control exits the scope of the declaration. If necessary, the
798 instance should be torn down before this happens, using the
799 \descref{sod_teardown}[function]{fun}.
800\end{describe}
801
802\subsubsection{Dynamic allocation}
803The following macros and functions deal with objects allocated from the
804standard C heap. They don't work in freestanding implementations where
805@|malloc| and @|free| are not available.
806
807\begin{describe*}
34042b35
MW
808 {\dhead{mac}[SOD_MAKE]{@<cls> *SOD_MAKE(@<cls>, @<keywords>);}
809 \dhead{fun}[sod_make]{void *sod_make(const SodClass *@<cls>, \dots);}
810 \dhead{fun}[sod_makev]
a42893dd
MW
811 {void *sod_makev(const SodClass *@<cls>, va_list @<ap>);}}
812 Constructs and returns a pointer to a new instance of @<cls>.
813
814 The direct class for the new instance is specified as a class name to
815 @|SOD_MAKE|, or a class object to the functions.
816
817 Keyword arguments for the initialization message may be provided. The
818 @|SOD_MAKE| macro expects a single preprocessor-time argument which is
819 a use of one of \descref{KWARGS}{mac} or \descref{NO_KWARGS}{mac}; the
820 @|sod_make| function expects the keywords as a variable-length argument
821 tail; and @|sod_makev| expects the keywords to be passed indirectly,
822 through the captured argument-tail cursor @<ap>.
823
824 Storage for the new instance will have been allocated using the standard
825 @|malloc| function. The easiest way to destroy the instance, when it is no
826 longer needed, is probably to call the
827 \descref{sod_destroy}[function]{fun}.
828
829 The return value is an instance pointer for the class @<cls>; the
830 @|SOD_MAKE| macro will have converted it to the correct type, so it should
831 probably be used where possible.
832\end{describe*}
833
34042b35 834\begin{describe}{fun}[sod_destroy]{int sod_destroy(void *@<p>);}
a42893dd
MW
835 Tears down and frees an instance allocated using @|malloc|.
836
837 The pointer @<p> should be an instance pointer, i.e., a pointer to any of
838 an instance's chains. The instance is torn down, by sending it the
839 \descref{obj.teardown}[message]{msg}. If the instance reports itself ready
840 for deallocation, then its storage is released using @|free|. The return
841 value is the value returned by the @|obj.teardown| message.
62f9852b
MW
842\end{describe}
843
844%%%----- That's all, folks --------------------------------------------------
845
846%%% Local variables:
847%%% mode: LaTeX
848%%% TeX-master: "sod.tex"
849%%% TeX-PDF-mode: t
850%%% End: