Replace the `init' class-slot function with an `init' message.
[sod] / doc / concepts.tex
CommitLineData
1f7d590d
MW
1%%% -*-latex-*-
2%%%
3%%% Conceptual background
4%%%
5%%% (c) 2015 Straylight/Edgeware
6%%%
7
8%%%----- Licensing notice ---------------------------------------------------
9%%%
e0808c47 10%%% This file is part of the Sensible Object Design, an object system for C.
1f7d590d
MW
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
3cc520db 26\chapter{Concepts} \label{ch:concepts}
1f7d590d 27
3cc520db
MW
28%%%--------------------------------------------------------------------------
29\section{Operational model} \label{sec:concepts.model}
1f7d590d 30
3cc520db
MW
31The Sod translator runs as a preprocessor, similar in nature to the
32traditional Unix \man{lex}{1} and \man{yacc}{1} tools. The translator reads
33a \emph{module} file containing class definitions and other information, and
34writes C~source and header files. The source files contain function
35definitions and static tables which are fed directly to a C~compiler; the
36header files contain declarations for functions and data structures, and are
37included by source files -- whether hand-written or generated by Sod -- which
38makes use of the classes defined in the module.
1f7d590d 39
3cc520db
MW
40Sod is not like \Cplusplus: it makes no attempt to `enhance' the C language
41itself. Sod module files describe classes, messages, methods, slots, and
42other kinds of object-system things, and some of these descriptions need to
43contain C code fragments, but this code is entirely uninterpreted by the Sod
44translator.\footnote{%
45 As long as a code fragment broadly follows C's lexical rules, and properly
46 matches parentheses, brackets, and braces, the Sod translator will copy it
47 into its output unchanged. It might, in fact, be some other kind of C-like
48 language, such as Objective~C or \Cplusplus. Or maybe even
49 Objective~\Cplusplus, because if having an object system is good, then
50 having three must be really awesome.} %
1f7d590d 51
3cc520db
MW
52The Sod translator is not a closed system. It is written in Common Lisp, and
53can load extension modules which add new input syntax, output formats, or
54altered behaviour. The interface for writing such extensions is described in
55\xref{p:lisp}. Extensions can change almost all details of the Sod object
56system, so the material in this manual must be read with this in mind: this
57manual describes the base system as provided in the distribution.
58
59%%%--------------------------------------------------------------------------
60\section{Modules} \label{sec:concepts.modules}
61
62A \emph{module} is the top-level syntactic unit of input to the Sod
63translator. As described above, given an input module, the translator
64generates C source and header files.
65
66A module can \emph{import} other modules. This makes the type names and
67classes defined in those other modules available to class definitions in the
68importing module. Sod's module system is intentionally very simple. There
69are no private declarations or attempts to hide things.
70
71As well as importing existing modules, a module can include a number of
72different kinds of \emph{items}:
73\begin{itemize}
74\item \emph{class definitions} describe new classes, possibly in terms of
75 existing classes;
76\item \emph{type name declarations} introduce new type names to Sod's
77 parser;\footnote{%
78 This is unfortunately necessary because C syntax, upon which Sod's input
79 language is based for obvious reasons, needs to treat type names
80 differently from other kinds of identifiers.} %
81 and
82\item \emph{code fragments} contain literal C code to be dropped into an
83 appropriate place in an output file.
84\end{itemize}
85Each kind of item, and, indeed, a module as a whole, can have a collection of
86\emph{properties} associated with it. A property has a \emph{name} and a
87\emph{value}. Properties are an open-ended way of attaching additional
88information to module items, so extensions can make use of them without
89having to implement additional syntax.
90
91%%%--------------------------------------------------------------------------
92\section{Classes, instances, and slots} \label{sec:concepts.classes}
93
94For the most part, Sod takes a fairly traditional view of what it means to be
95an object system.
96
97An \emph{object} maintains \emph{state} and exhibits \emph{behaviour}. An
98object's state is maintained in named \emph{slots}, each of which can store a
99C value of an appropriate (scalar or aggregate) type. An object's behaviour
100is stimulated by sending it \emph{messages}. A message has a name, and may
101carry a number of arguments, which are C values; sending a message may result
102in the state of receiving object (or other objects) being changed, and a C
103value being returned to the sender.
104
105Every object is a (direct) instance of some \emph{class}. The class
106determines which slots its instances have, which messages its instances can
107be sent, and which methods are invoked when those messages are received. The
108Sod translator's main job is to read class definitions and convert them into
109appropriate C declarations, tables, and functions. An object cannot
110(usually) change its direct class, and the direct class of an object is not
111affected by, for example, the static type of a pointer to it.
112
0a2d4b68 113
3cc520db
MW
114\subsection{Superclasses and inheritance}
115\label{sec:concepts.classes.inherit}
116
117\subsubsection{Class relationships}
118Each class has zero or more \emph{direct superclasses}.
119
120A class with no direct superclasses is called a \emph{root class}. The Sod
121runtime library includes a root class named @|SodObject|; making new root
122classes is somewhat tricky, and won't be discussed further here.
123
124Classes can have more than one direct superclass, i.e., Sod supports
125\emph{multiple inheritance}. A Sod class definition for a class~$C$ lists
126the direct superclasses of $C$ in a particular order. This order is called
127the \emph{local precedence order} of $C$, and the list which consists of $C$
128follows by $C$'s direct superclasses in local precedence order is called the
129$C$'s \emph{local precedence list}.
130
131The multiple inheritance in Sod works similarly to multiple inheritance in
132Lisp-like languages, such as Common Lisp, EuLisp, Dylan, and Python, which is
133very different from how multiple inheritance works in \Cplusplus.\footnote{%
134 The latter can be summarized as `badly'. By default in \Cplusplus, an
135 instance receives an additional copy of superclass's state for each path
136 through the class graph from the instance's direct class to that
137 superclass, though this behaviour can be overridden by declaring
138 superclasses to be @|virtual|. Also, \Cplusplus\ offers only trivial
139 method combination (\xref{sec:concepts.methods}), leaving programmers to
140 deal with delegation manually and (usually) statically.} %
141
142If $C$ is a class, then the \emph{superclasses} of $C$ are
143\begin{itemize}
144\item $C$ itself, and
145\item the superclasses of each of $C$'s direct superclasses.
146\end{itemize}
147The \emph{proper superclasses} of a class $C$ are the superclasses of $C$
148except for $C$ itself. If a class $B$ is a (direct, proper) superclass of
149$C$, then $C$ is a \emph{(direct, proper) subclass} of $B$. If $C$ is a root
150class then the only superclass of $C$ is $C$ itself, and $C$ has no proper
151superclasses.
152
153If an object is a direct instance of class~$C$ then the object is also an
154(indirect) instance of every superclass of $C$.
155
156If $C$ has a proper superclass $B$, then $B$ is not allowed to have $C$ has a
157direct superclass. In different terms, if we construct a graph, whose
158vertices are classes, and draw an edge from each class to each of its direct
159superclasses, then this graph must be acyclic. In yet other terms, the `is a
160superclass of' relation is a partial order on classes.
161
162\subsubsection{The class precedence list}
163This partial order is not quite sufficient for our purposes. For each class
164$C$, we shall need to extend it into a total order on $C$'s superclasses.
165This calculation is called \emph{superclass linearization}, and the result is
166a \emph{class precedence list}, which lists each of $C$'s superclasses
167exactly once. If a superclass $B$ precedes (resp.\ follows) some other
168superclass $A$ in $C$'s class precedence list, then we say that $B$ is a more
169(resp.\ less) \emph{specific} superclass of $C$ than $A$ is.
170
171The superclass linearization algorithm isn't fixed, and extensions to the
172translator can introduce new linearizations for special effects, but the
173following properties are expected to hold.
174\begin{itemize}
175\item The first class in $C$'s class precedence list is $C$ itself; i.e.,
176 $C$ is always its own most specific superclass.
177\item If $A$ and $B$ are both superclasses of $C$, and $A$ is a proper
178 superclass of $B$ then $A$ appears after $B$ in $C$'s class precedence
179 list, i.e., $B$ is a more specific superclass of $C$ than $A$ is.
180\end{itemize}
181The default linearization algorithm used in Sod is the \emph{C3} algorithm,
182which has a number of good properties described in~\cite{FIXME:C3}.
183It works as follows.
184\begin{itemize}
185\item A \emph{merge} of some number of input lists is a single list
186 containing each item that is in any of the input lists exactly once, and no
187 other items; if an item $x$ appears before an item $y$ in any input list,
188 then $x$ also appears before $y$ in the merge. If a collection of lists
189 have no merge then they are said to be \emph{inconsistent}.
190\item The class precedence list of a class $C$ is a merge of the local
191 precedence list of $C$ together with the class precedence lists of each of
192 $C$'s direct superclasses.
193\item If there are no such merges, then the definition of $C$ is invalid.
194\item Suppose that there are multiple candidate merges. Consider the
195 earliest position in these candidate merges at which they disagree. The
196 \emph{candidate classes} at this position are the classes appearing at this
197 position in the candidate merges. Each candidate class must be a
781a8fbd 198 superclass of distinct direct superclasses of $C$, since otherwise the
3cc520db
MW
199 candidates would be ordered by their common subclass's class precedence
200 list. The class precedence list contains, at this position, that candidate
201 class whose subclass appears earliest in $C$'s local precedence order.
202\end{itemize}
203
204\subsubsection{Class links and chains}
205The definition for a class $C$ may distinguish one of its proper superclasses
206as being the \emph{link superclass} for class $C$. Not every class need have
207a link superclass, and the link superclass of a class $C$, if it exists, need
208not be a direct superclass of $C$.
209
210Superclass links must obey the following rule: if $C$ is a class, then there
781a8fbd
MW
211must be no three superclasses $X$, $Y$ and~$Z$ of $C$ such that $Z$ is the
212link superclass of both $X$ and $Y$. As a consequence of this rule, the
3cc520db
MW
213superclasses of $C$ can be partitioned into linear \emph{chains}, such that
214superclasses $A$ and $B$ are in the same chain if and only if one can trace a
215path from $A$ to $B$ by following superclass links, or \emph{vice versa}.
216
217Since a class links only to one of its proper superclasses, the classes in a
218chain are naturally ordered from most- to least-specific. The least specific
219class in a chain is called the \emph{chain head}; the most specific class is
220the \emph{chain tail}. Chains are often named after their chain head
221classes.
222
223\subsection{Names}
224\label{sec:concepts.classes.names}
225
226Classes have a number of other attributes:
227\begin{itemize}
228\item A \emph{name}, which is a C identifier. Class names must be globally
229 unique. The class name is used in the names of a number of associated
230 definitions, to be described later.
231\item A \emph{nickname}, which is also a C identifier. Unlike names,
232 nicknames are not required to be globally unique. If $C$ is any class,
233 then all the superclasses of $C$ must have distinct nicknames.
234\end{itemize}
235
0a2d4b68 236
3cc520db
MW
237\subsection{Slots} \label{sec:concepts.classes.slots}
238
239Each class defines a number of \emph{slots}. Much like a structure member, a
240slot has a \emph{name}, which is a C identifier, and a \emph{type}. Unlike
241many other object systems, different superclasses of a class $C$ can define
242slots with the same name without ambiguity, since slot references are always
243qualified by the defining class's nickname.
244
245\subsubsection{Slot initializers}
246As well as defining slot names and types, a class can also associate an
247\emph{initial value} with each slot defined by itself or one of its
248subclasses. A class $C$ provides an \emph{initialization function} (see
d24d47f5
MW
249\xref{sec:concepts.lifecycle.birth}, and \xref{sec:structures.root.sodclass})
250which sets the slots of a \emph{direct} instance of the class to the correct
3cc520db
MW
251initial values. If several of $C$'s superclasses define initializers for the
252same slot then the initializer from the most specific such class is used. If
253none of $C$'s superclasses define an initializer for some slot then that slot
781a8fbd 254will be left uninitialized.
3cc520db
MW
255
256The initializer for a slot with scalar type may be any C expression. The
257initializer for a slot with aggregate type must contain only constant
258expressions if the generated code is expected to be processed by a
259implementation of C89. Initializers will be evaluated once each time an
260instance is initialized.
261
0a2d4b68 262
3cc520db
MW
263\subsection{C language integration} \label{sec:concepts.classes.c}
264
265For each class~$C$, the Sod translator defines a C type, the \emph{class
266type}, with the same name. This is the usual type used when considering an
267object as an instance of class~$C$. No entire object will normally have a
268class type,\footnote{%
269 In general, a class type only captures the structure of one of the
270 superclass chains of an instance. A full instance layout contains multiple
271 chains. See \xref{sec:structures.layout} for the full details.} %
272so access to instances is almost always via pointers.
273
274\subsubsection{Access to slots}
275The class type for a class~$C$ is actually a structure. It contains one
276member for each class in $C$'s superclass chain, named with that class's
277nickname. Each of these members is also a structure, containing the
278corresponding class's slots, one member per slot. There's nothing special
279about these slot members: C code can access them in the usual way.
280
281For example, if @|MyClass| has the nickname @|mine|, and defines a slot @|x|
282of type @|int|, then the simple function
283\begin{prog}
c18d6aba 284 int get_x(MyClass *m) \{ return (m@->mine.x); \}
3cc520db
MW
285\end{prog}
286will extract the value of @|x| from an instance of @|MyClass|.
287
288All of this means that there's no such thing as `private' or `protected'
289slots. If you want to hide implementation details, the best approach is to
290stash them in a dynamically allocated private structure, and leave a pointer
291to it in a slot. (This will also help preserve binary compatibility, because
292the private structure can grow more members as needed. See
293\xref{sec:fixme.compatibility} for more details.
294
295\subsubsection{Class objects}
296In Sod's object system, classes are objects too. Therefore classes are
297themselves instances; the class of a class is called a \emph{metaclass}. The
298consequences of this are explored in \xref{sec:concepts.metaclasses}. The
299\emph{class object} has the same name as the class, suffixed with
300`@|__class|'\footnote{%
301 This is not quite true. @|$C$__class| is actually a macro. See
302 \xref{sec:structures.layout.additional} for the gory details.} %
303and its type is usually @|SodClass|; @|SodClass|'s nickname is @|cls|.
304
305A class object's slots contain or point to useful information, tables and
306functions for working with that class's instances. (The @|SodClass| class
307doesn't define any messages, so it doesn't have any methods. In Sod, a class
308slot containing a function pointer is not at all the same thing as a method.)
309
3cc520db
MW
310\subsubsection{Conversions}
311Suppose one has a value of type pointer to class type of some class~$C$, and
312wants to convert it to a pointer to class type of some other class~$B$.
313There are three main cases to distinguish.
314\begin{itemize}
315\item If $B$ is a superclass of~$C$, in the same chain, then the conversion
316 is an \emph{in-chain upcast}. The conversion can be performed using the
317 appropriate generated upcast macro (see below), or by simply casting the
318 pointer, using C's usual cast operator (or the \Cplusplus\ @|static_cast<>|
319 operator).
320\item If $B$ is a superclass of~$C$, in a different chain, then the
321 conversion is a \emph{cross-chain upcast}. The conversion is more than a
322 simple type change: the pointer value must be adjusted. If the direct
323 class of the instance in question is not known, the conversion will require
324 a lookup at runtime to find the appropriate offset by which to adjust the
325 pointer. The conversion can be performed using the appropriate generated
326 upcast macro (see below); the general case is handled by the macro
58f9b400 327 \descref{SOD_XCHAIN}{mac}.
3cc520db
MW
328\item If $B$ is a subclass of~$C$ then the conversion is an \emph{upcast};
329 otherwise the conversion is a~\emph{cross-cast}. In either case, the
330 conversion can fail: the object in question might not be an instance of~$B$
58f9b400
MW
331 at all. The macro \descref{SOD_CONVERT}{mac} and the function
332 \descref{sod_convert}{fun} perform general conversions. They return a null
781a8fbd
MW
333 pointer if the conversion fails. (There are therefore your analogue to the
334 \Cplusplus @|dynamic_cast<>| operator.)
3cc520db
MW
335\end{itemize}
336The Sod translator generates macros for performing both in-chain and
337cross-chain upcasts. For each class~$C$, and each proper superclass~$B$
338of~$C$, a macro is defined: given an argument of type pointer to class type
339of~$C$, it returns a pointer to the same instance, only with type pointer to
340class type of~$B$, adjusted as necessary in the case of a cross-chain
341conversion. The macro is named by concatenating
342\begin{itemize}
343\item the name of class~$C$, in upper case,
344\item the characters `@|__CONV_|', and
345\item the nickname of class~$B$, in upper case;
346\end{itemize}
347e.g., if $C$ is named @|MyClass|, and $B$'s name is @|SuperClass| with
348nickname @|super|, then the macro @|MYCLASS__CONV_SUPER| converts a
349@|MyClass~*| to a @|SuperClass~*|. See
350\xref{sec:structures.layout.additional} for the formal description.
351
352%%%--------------------------------------------------------------------------
9e91c8e7
MW
353\section{Keyword arguments} \label{sec:concepts.keywords}
354
355In standard C, the actual arguments provided to a function are matched up
356with the formal arguments given in the function definition according to their
357ordering in a list. Unless the (rather cumbersome) machinery for dealing
358with variable-length argument tails (@|<stdarg.h>|) is used, exactly the
359correct number of arguments must be supplied, and in the correct order.
360
361A \emph{keyword argument} is matched by its distinctive \emph{name}, rather
362than by its position in a list. Keyword arguments may be \emph{omitted},
363causing some default behaviour by the function. A function can detect
364whether a particular keyword argument was supplied: so the default behaviour
365need not be the same as that caused by any specific value of the argument.
366
367Keyword arguments can be provided in three ways.
368\begin{enumerate}
369\item Directly, as a variable-length argument tail, consisting (for the most
370 part) of alternating keyword names, as pointers to null-terminated strings,
371 and argument values, and terminated by a null pointer. This is somewhat
372 error-prone, and the support library defines some macros which help ensure
373 that keyword argument lists are well formed.
374\item Indirectly, through a @|va_list| object capturing a variable-length
375 argument tail passed to some other function. Such indirect argument tails
376 have the same structure as the direct argument tails described above.
377 Because @|va_list| objects are hard to copy, the keyword-argument support
378 library consistently passes @|va_list| objects \emph{by reference}
379 throughout its programming interface.
380\item Indirectly, through a vector of @|struct kwval| objects, each of which
381 contains a keyword name, as a pointer to a null-terminated string, and the
382 \emph{address} of a corresponding argument value. (This indirection is
383 necessary so that the items in the vector can be of uniform size.)
384 Argument vectors are rather inconvenient to use, but are the only practical
385 way in which a caller can decide at runtime which arguments to include in a
386 call, which is useful when writing wrapper functions.
387\end{enumerate}
388
389Keyword arguments are provided as a general feature for C functions.
43073476 390However, Sod has special support for messages which accept keyword arguments
a142609c
MW
391(\xref{sec:concepts.methods.keywords}); and they play an essential role in
392the instance construction protocol (\xref{sec:concepts.lifecycle.birth}).
9e91c8e7
MW
393
394%%%--------------------------------------------------------------------------
3cc520db
MW
395\section{Messages and methods} \label{sec:concepts.methods}
396
397Objects can be sent \emph{messages}. A message has a \emph{name}, and
398carries a number of \emph{arguments}. When an object is sent a message, a
399function, determined by the receiving object's class, is invoked, passing it
400the receiver and the message arguments. This function is called the
401class's \emph{effective method} for the message. The effective method can do
402anything a C function can do, including reading or updating program state or
403object slots, sending more messages, calling other functions, issuing system
404calls, or performing I/O; if it finishes, it may return a value, which is
405returned in turn to the message sender.
406
407The set of messages an object can receive, characterized by their names,
408argument types, and return type, is determined by the object's class. Each
409class can define new messages, which can be received by any instance of that
410class. The messages defined by a single class must have distinct names:
411there is no `function overloading'. As with slots
412(\xref{sec:concepts.classes.slots}), messages defined by distinct classes are
413always distinct, even if they have the same names: references to messages are
414always qualified by the defining class's name or nickname.
415
416Messages may take any number of arguments, of any non-array value type.
417Since message sends are effectively function calls, arguments of array type
418are implicitly converted to values of the corresponding pointer type. While
419message definitions may ascribe an array type to an argument, the formal
420argument will have pointer type, as is usual for C functions. A message may
421accept a variable-length argument suffix, denoted @|\dots|.
422
423A class definition may include \emph{direct methods} for messages defined by
424it or any of its superclasses.
425
426Like messages, direct methods define argument lists and return types, but
427they may also have a \emph{body}, and a \emph{role}.
428
429A direct method need not have the same argument list or return type as its
430message. The acceptable argument lists and return types for a method depend
431on the message, in particular its method combination
432(\xref{sec:concepts.methods.combination}), and the method's role.
433
434A direct method body is a block of C code, and the Sod translator usually
435defines, for each direct method, a function with external linkage, whose body
436contains a copy of the direct method body. Within the body of a direct
437method defined for a class $C$, the variable @|me|, of type pointer to class
438type of $C$, refers to the receiving object.
439
0a2d4b68 440
3cc520db
MW
441\subsection{Effective methods and method combinations}
442\label{sec:concepts.methods.combination}
443
444For each message a direct instance of a class might receive, there is a set
445of \emph{applicable methods}, which are exactly the direct methods defined on
446the object's class and its superclasses. These direct methods are combined
447together to form the \emph{effective method} for that particular class and
448message. Direct methods can be combined into an effective method in
449different ways, according to the \emph{method combination} specified by the
450message. The method combination determines which direct method roles are
451acceptable, and, for each role, the appropriate argument lists and return
452types.
453
454One direct method, $M$, is said to be more (resp.\ less) \emph{specific} than
455another, $N$, with respect to a receiving class~$C$, if the class defining
456$M$ is a more (resp.\ less) specific superclass of~$C$ than the class
457defining $N$.
458
43073476 459\subsubsection{The standard method combination}
3cc520db
MW
460The default method combination is called the \emph{standard method
461combination}; other method combinations are useful occasionally for special
462effects. The standard method combination accepts four direct method roles,
9761db0d 463called `primary' (the default), @|before|, @|after|, and @|around|.
3cc520db
MW
464
465All direct methods subject to the standard method combination must have
466argument lists which \emph{match} the message's argument list:
467\begin{itemize}
468\item the method's arguments must have the same types as the message, though
469 the arguments may have different names; and
470\item if the message accepts a variable-length argument suffix then the
471 direct method must instead have a final argument of type @|va_list|.
472\end{itemize}
b1254eb6
MW
473Primary and @|around| methods must have the same return type as the message;
474@|before| and @|after| methods must return @|void| regardless of the
475message's return type.
3cc520db
MW
476
477If there are no applicable primary methods then no effective method is
478constructed: the vtables contain null pointers in place of pointers to method
479entry functions.
480
481The effective method for a message with standard method combination works as
482follows.
483\begin{enumerate}
484
485\item If any applicable methods have the @|around| role, then the most
486 specific such method, with respect to the class of the receiving object, is
487 invoked.
488
b1254eb6 489 Within the body of an @|around| method, the variable @|next_method| is
3cc520db
MW
490 defined, having pointer-to-function type. The method may call this
491 function, as described below, any number of times.
492
b1254eb6
MW
493 If there any remaining @|around| methods, then @|next_method| invokes the
494 next most specific such method, returning whichever value that method
495 returns; otherwise the behaviour of @|next_method| is to invoke the before
496 methods (if any), followed by the most specific primary method, followed by
497 the @|around| methods (if any), and to return whichever value was returned
781a8fbd
MW
498 by the most specific primary method, as described in the following items.
499 That is, the behaviour of the least specific @|around| method's
500 @|next_method| function is exactly the behaviour that the effective method
501 would have if there were no @|around| methods. Note that if the
502 least-specific @|around| method calls its @|next_method| more than once
503 then the whole sequence of @|before|, primary, and @|after| methods occurs
504 multiple times.
3cc520db 505
b1254eb6
MW
506 The value returned by the most specific @|around| method is the value
507 returned by the effective method.
3cc520db
MW
508
509\item If any applicable methods have the @|before| role, then they are all
510 invoked, starting with the most specific.
511
512\item The most specific applicable primary method is invoked.
513
514 Within the body of a primary method, the variable @|next_method| is
515 defined, having pointer-to-function type. If there are no remaining less
516 specific primary methods, then @|next_method| is a null pointer.
517 Otherwise, the method may call the @|next_method| function any number of
518 times.
519
520 The behaviour of the @|next_method| function, if it is not null, is to
521 invoke the next most specific applicable primary method, and to return
522 whichever value that method returns.
523
b1254eb6
MW
524 If there are no applicable @|around| methods, then the value returned by
525 the most specific primary method is the value returned by the effective
526 method; otherwise the value returned by the most specific primary method is
527 returned to the least specific @|around| method, which called it via its
528 own @|next_method| function.
3cc520db
MW
529
530\item If any applicable methods have the @|after| role, then they are all
531 invoked, starting with the \emph{least} specific. (Hence, the most
b1254eb6 532 specific @|after| method is invoked with the most `afterness'.)
3cc520db
MW
533
534\end{enumerate}
535
b1254eb6
MW
536A typical use for @|around| methods is to allow a base class to set up the
537dynamic environment appropriately for the primary methods of its subclasses,
538e.g., by claiming a lock, and restore it afterwards.
3cc520db 539
9761db0d 540The @|next_method| function provided to methods with the primary and
3cc520db
MW
541@|around| roles accepts the same arguments, and returns the same type, as the
542message, except that one or two additional arguments are inserted at the
543front of the argument list. The first additional argument is always the
544receiving object, @|me|. If the message accepts a variable argument suffix,
545then the second addition argument is a @|va_list|; otherwise there is no
546second additional argument; otherwise, In the former case, a variable
547@|sod__master_ap| of type @|va_list| is defined, containing a separate copy
548of the argument pointer (so the method body can process the variable argument
549suffix itself, and still pass a fresh copy on to the next method).
550
9761db0d 551A method with the primary or @|around| role may use the convenience macro
3cc520db
MW
552@|CALL_NEXT_METHOD|, which takes no arguments itself, and simply calls
553@|next_method| with appropriate arguments: the receiver @|me| pointer, the
554argument pointer @|sod__master_ap| (if applicable), and the method's
555arguments. If the method body has overwritten its formal arguments, then
556@|CALL_NEXT_METHOD| will pass along the updated values, rather than the
557original ones.
558
781a8fbd
MW
559A primary or @|around| method which invokes its @|next_method| function is
560said to \emph{extend} the message behaviour; a method which does not invoke
561its @|next_method| is said to \emph{override} the behaviour. Note that a
562method may make a decision to override or extend at runtime.
563
43073476 564\subsubsection{Aggregating method combinations}
3cc520db
MW
565A number of other method combinations are provided. They are called
566`aggregating' method combinations because, instead of invoking just the most
567specific primary method, as the standard method combination does, they invoke
568the applicable primary methods in turn and aggregate the return values from
569each.
570
571The aggregating method combinations accept the same four roles as the
b1254eb6
MW
572standard method combination, and @|around|, @|before|, and @|after| methods
573work in the same way.
3cc520db
MW
574
575The aggregating method combinations provided are as follows.
576\begin{description} \let\makelabel\code
577\item[progn] The message must return @|void|. The applicable primary methods
578 are simply invoked in turn, most specific first.
579\item[sum] The message must return a numeric type.\footnote{%
580 The Sod translator does not check this, since it doesn't have enough
581 insight into @|typedef| names.} %
582 The applicable primary methods are invoked in turn, and their return values
583 added up. The final result is the sum of the individual values.
584\item[product] The message must return a numeric type. The applicable
585 primary methods are invoked in turn, and their return values multiplied
586 together. The final result is the product of the individual values.
587\item[min] The message must return a scalar type. The applicable primary
588 methods are invoked in turn. The final result is the smallest of the
589 individual values.
590\item[max] The message must return a scalar type. The applicable primary
591 methods are invoked in turn. The final result is the largest of the
592 individual values.
665a0455
MW
593\item[and] The message must return a scalar type. The applicable primary
594 methods are invoked in turn. If any method returns zero then the final
595 result is zero and no further methods are invoked. If all of the
596 applicable primary methods return nonzero, then the final result is the
597 result of the last primary method.
598\item[or] The message must return a scalar type. The applicable primary
599 methods are invoked in turn. If any method returns nonzero then the final
600 result is that nonzero value and no further methods are invoked. If all of
601 the applicable primary methods return zero, then the final result is zero.
3cc520db
MW
602\end{description}
603
604There is also a @|custom| aggregating method combination, which is described
605in \xref{sec:fixme.custom-aggregating-method-combination}.
606
43073476
MW
607
608\subsection{Messages with keyword arguments}
609\label{sec:concepts.methods.keywords}
610
611A message or a direct method may declare that it accepts keyword arguments.
612A message which accepts keyword arguments is called a \emph{keyword message};
613a direct method which accepts keyword arguments is called a \emph{keyword
614method}.
615
616While method combinations may set their own rules, usually keyword methods
617can only be defined on keyword messages, and all methods defined on a keyword
618message must be keyword methods. The direct methods defined on a keyword
619message may differ in the keywords they accept, both from each other, and
620from the message. If two superclasses of some common class both define
621keyword methods on the same message, and the methods both accept a keyword
622argument with the same name, then these two keyword arguments must also have
623the same type. Different applicable methods may declare keyword arguments
624with the same name but different defaults; see below.
625
626The keyword arguments acceptable in a message sent to an object are the
627keywords listed in the message definition, together with all of the keywords
628accepted by any applicable method. There is no easy way to determine at
629runtime whether a particular keyword is acceptable in a message to a given
630instance.
631
632At runtime, a direct method which accepts one or more keyword arguments
633receives an additional argument named @|suppliedp|. This argument is a small
634structure. For each keyword argument named $k$ accepted by the direct
635method, @|suppliedp| contains a one-bit-wide bitfield member of type
636@|unsigned|, also named $k$. If a keyword argument named $k$ was passed in
637the message, then @|suppliedp.$k$| is one, and $k$ contains the argument
638value; otherwise @|suppliedp.$k$| is zero, and $k$ contains the default value
639from the direct method definition if there was one, or an unspecified value
640otherwise.
641
3cc520db 642%%%--------------------------------------------------------------------------
d24d47f5
MW
643\section{The object lifecycle} \label{sec:concepts.lifecycle}
644
645\subsection{Creation} \label{sec:concepts.lifecycle.birth}
646
647Construction of a new instance of a class involves three steps.
648\begin{enumerate}
649\item \emph{Allocation} arranges for there to be storage space for the
650 instance's slots and associated metadata.
651\item \emph{Imprinting} fills in the instance's metadata, associating the
652 instance with its class.
653\item \emph{Initialization} stores appropriate initial values in the
654 instance's slots, and maybe links it into any external data structures as
655 necessary.
656\end{enumerate}
657The \descref{SOD_DECL}[macro]{mac} handles constructing instances with
a142609c
MW
658automatic storage duration (`on the stack'). Programmers can add support for
659other allocation strategies by using the \descref{SOD_INIT}[macro]{mac} and
660the \descref{sod_init}{fun} and \descref{sod_initv}{fun} functions, which
661package up imprinting and initialization.
d24d47f5
MW
662
663\subsubsection{Allocation}
664Instances of most classes (specifically including those classes defined by
665Sod itself) can be held in any storage of sufficient size. The in-memory
666layout of an instance of some class~$C$ is described by the type @|struct
667$C$__ilayout|, and if the relevant class is known at compile time then the
668best way to discover the layout size is with the @|sizeof| operator. Failing
669that, the size required to hold an instance of $C$ is available in a slot in
670$C$'s class object, as @|$C$__class@->cls.initsz|.
671
672It is not in general sufficient to declare, or otherwise allocate, an object
673of the class type $C$. The class type only describes a single chain of the
674object's layout. It is nearly always an error to use the class type as if it
675is a \emph{complete type}, e.g., to declare objects or arrays of the class
676type, or to enquire about its size or alignment requirements.
677
678Instance layouts may be declared as objects with automatic storage duration
679(colloquially, `allocated on the stack') or allocated dynamically, e.g.,
680using @|malloc|. They may be included as members of structures or unions, or
681elements of arrays. Sod's runtime system doesn't retain addresses of
682instances, so, for example, Sod doesn't make using fancy allocators which
683sometimes move objects around in memory any more difficult than it needs to
684be.
685
686There isn't any way to discover the alignment required for a particular
687class's instances at runtime; it's best to be conservative and assume that
688the platform's strictest alignment requirement applies.
689
690The following simple function correctly allocates and returns space for an
691instance of a class given a pointer to its class object @<cls>.
692\begin{prog}
693 void *allocate_instance(const SodClass *cls) \\ \ind
694 \{ return malloc(cls@->cls.initsz); \}
695\end{prog}
696
697\subsubsection{Imprinting}
698Once storage has been allocated, it must be \emph{imprinted} before it can be
699used as an instance of a class, e.g., before any messages can be sent to it.
700
701Imprinting an instance stores some metadata about its direct class in the
702instance structure, so that the rest of the program (and Sod's runtime
703library) can tell what sort of object it is, and how to use it.\footnote{%
704 Specifically, imprinting an instance's storage involves storing the
705 appropriate vtable pointers in the right places in it.} %
706A class object's @|imprint| slot points to a function which will correctly
707imprint storage for one of that class's instances.
708
709Once an instance's storage has been imprinted, it is technically possible to
710send messages to the instance; however the instance's slots are still
711uninitialized at this point, the applicable methods are unlikely to do much
712of any use unless they've been written specifically for the purpose.
713
714The following simple function imprints storage at address @<p> as an instance
715of a class, given a pointer to its class object @<cls>.
716\begin{prog}
717 void imprint_instance(const SodClass *cls, void *p) \\ \ind
718 \{ cls@->cls.imprint(p); \}
719\end{prog}
720
721\subsubsection{Initialization}
722The final step for constructing a new instance is to \emph{initialize} it, to
723establish the necessary invariants for the instance itself and the
724environment in which it operates.
725
726Details of initialization are necessarily class-specific, but typically it
727involves setting the instance's slots to appropriate values, and possibly
728linking it into some larger data structure to keep track of it.
729
a142609c
MW
730Initialization is performed by sending the imprinted instance an @|init|
731message, defined by the @|SodObject| class. This message uses a nonstandard
732method combination which works like the standard combination, except that the
733\emph{default behaviour}, if there is no overriding method, is to initialize
734the instance's slots using the initializers defined in the class and its
735superclasses. This default behaviour may be invoked multiple times if some
736method calls on its @|next_method| more than once, unless some other method
737takes steps to prevent this.
738
739The recommended way to add new initialization behaviour is to define @|after|
740methods on the @|init| message. These will be run after the slot
741initializers have been applied, in reverse precedence order.
742
743Initialization is \emph{parametrized}, so the caller may select from a space
744of possible initial states for the new instance, or to inform the new
745instance about some other objects known to the caller. Specifically, the
746@|init| message accepts keyword arguments (\xref{sec:concepts.keywords})
747which can be defined and used by methods defined on it.
d24d47f5
MW
748
749\subsubsection{Example}
750The following is a simple function, with syntactic-sugar macro, which
751allocate storage for an instance of a class, imprints and initializes it, and
752returns a pointer to the new instance.
753\begin{prog}
a142609c 754 void *make_instance(const SodClass *c, @|\dots|) \\
d24d47f5 755 \{ \\ \ind
a142609c 756 va_list ap;
d24d47f5
MW
757 void *p = malloc(c@->cls.initsz); \\
758 if (!p) return (0); \\
a142609c
MW
759 va_start(ap, c); \\
760 sod_initv(c, p, ap); \\
761 va_end(ap); \\
d24d47f5
MW
762 return (p); \- \\
763 \}
764 \\+
a142609c 765 \#define MAKE(cls, keys) (cls *)make_instance(cls\#\#__class, keys)
d24d47f5
MW
766\end{prog}
767
768
769\subsection{Destruction}
770\label{sec:concepts.lifecycle.death}
771
772Destruction of an instance, when it is no longer required, consists of two
773steps.
774\begin{enumerate}
775\item \emph{Teardown} releases any resources held by the instance and
776 disentangles it from any external data structures.
777\item \emph{Deallocation} releases the memory used to store the instance so
778 that it can be reused.
779\end{enumerate}
780
781\subsubsection{Teardown}
782Details of teardown are class-specific, but typically it involves releasing
783resources held by the instance, and possibly unlinking it from some larger
784data structure which used to keep track of it.
785
786There is no provided protocol for teardown: classes whose instances require
787teardown behaviour must define and implement an appropriate protocol of their
788own. The following class may serve for simple cases.
789\begin{prog}
790 [nick = disposable] \\
791 class DisposableObject : SodObject \{ \\- \ind
792 void release() \{ ; \} \\
793 \quad /* Release resources held by the receiver. */ \- \\-
794 \}
795 \\+
796 code c : user \{ \\- \ind
797 /* If p is a a DisposableObject then release its resources. */ \\
798 void maybe_dispose(void *p) \\
799 \{ \\ \ind
800 DisposableObject *d = SOD_CONVERT(DisposableObject, p); \\
801 if (d) DisposableObject_release(d); \- \\
802 \} \- \\
803 \}
804\end{prog}
805
806\subsubsection{Deallocation}
807The details of instance deallocation are obviously specific to the allocation
808strategy used by the instance, and this is often orthogonal from the object's
809class.
810
811The code which makes the decision to destroy an object may often not be aware
812of the object's direct class. Low-level details of deallocation often
813require the proper base address of the instance's storage, which can be
814determined using the \descref{SOD_INSTBASE}[macro]{mac}.
815
816\subsubsection{Example}
817The following is a counterpart to the @|new_instance| function
818(\xref{sec:concepts.lifecycle.birth}), which tears down and deallocates an
819instance allocated using @|malloc|.
820\begin{prog}
821 void free_instance(void *p) \\
822 \{ \\ \ind
823 SodObject *obj = p; \\
824 maybe_dispose(p); \\
825 free(SOD_INSTBASE(obj)); \- \\
826 \}
827\end{prog}
828
829%%%--------------------------------------------------------------------------
3cc520db 830\section{Metaclasses} \label{sec:concepts.metaclasses}
1f7d590d
MW
831
832%%%----- That's all, folks --------------------------------------------------
833
834%%% Local variables:
835%%% mode: LaTeX
836%%% TeX-master: "sod.tex"
837%%% TeX-PDF-mode: t
838%%% End: