doc/concepts.tex: Link to `SodObject' for the `init' message.
[sod] / doc / structures.tex
CommitLineData
62f9852b
MW
1%%% -*-latex-*-
2%%%
3%%% In-depth exploration of the generated structures
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{Object structures} \label{ch:structures}
27
28This chapter describes the structure and layout of standard Sod objects,
29classes and associated metadata. Note that Sod's object system is very
30flexible and it's possible for an extension to define a new root class which
31works very differently from the standard @|SodObject| described here.
32
33The concrete types described in \xref{sec:structures.common} and
34\ref{sec:structures.root} are declared by the header file @|<sod/sod.h>|.
43073476
MW
35The definitions described in \xref{sec:structures.layout} are defined in the
36header file generated by the containing module.
62f9852b
MW
37
38%%%--------------------------------------------------------------------------
39\section{Common instance structure} \label{sec:structures.common}
40
41As described below, a pointer to an instance actually points to an
42\emph{instance chain} structure within the instances overall layout
43structure.
44
45Instance chains contain slots and vtable pointers, as described below. All
9caad6bd
MW
46instances have the basic structure of a @|struct sod_instance|.
47
34042b35 48\begin{describe}{type}[struct sod_instance]
020b9e2b
MW
49 {struct sod_instance \{ \\ \ind
50 const struct sod_vtable *_vt; \-\\
9caad6bd
MW
51 \};}
52
53 The basic structure of all instances. Members are as follows.
54 \begin{description} \let\makelabel\code
55 \item[_vt] A pointer to a \emph{vtable}, which has the basic structure of a
56 @|struct sod_vtable|, described below.
57 \end{description}
58\end{describe}
59
34042b35 60\begin{describe}{type}[struct sod_vtable]
020b9e2b
MW
61 {struct sod_vtable \{ \\ \ind
62 const SodClass *_class; \\
63 size_t _base; \-\\
9caad6bd
MW
64 \};}
65
66 A vtable contains static metadata needed for efficient conversions and
67 message dispatch, and pointers to the instance's class. Each chain points
68 to a different vtable. All vtables have the basic structure of a @|struct
69 sod_vtable|, which has the following members.
70 \begin{description} \let\makelabel\code
71 \item[_class] A pointer to the instance's class object.
72 \item[_base] The offset of this chain structure above the start of the
73 overall instance layout, in bytes. Subtracting @|_base| from the
74 instance chain pointer finds the layout base address.
75 \end{description}
76\end{describe}
62f9852b
MW
77
78%%%--------------------------------------------------------------------------
79\section{Built-in root objects} \label{sec:structures.root}
80
81This section describes the built-in classes @|SodObject| and @|SodClass|,
82which are the standard roots of the inheritance and metaclass graphs
83respectively. Specifically, @|SodObject| has no direct superclasses, and
84@|SodClass| is its own metaclass. It is not possible to define root classes
85in module files because of circularities: @|SodObject| has @|SodClass| as its
86metaclass, and @|SodClass| is a subclass of @|SodObject|. Extensions can
87define additional root classes, but this is tricky, and not really to be
88recommended.
89
4faea17d
MW
90The class definitions shown in the synopses are intended to be informative,
91but are fictional and can't really work: these classes are really defined by
92Lisp code in the Sod translator, in order to deal with the circularities
93involved at the top of the class/metaclass graph (see
94\xref{sec:concepts.metaclasses.runtime}).
95
0a2d4b68 96
62f9852b
MW
97\subsection{The SodObject class} \label{sec:structures.root.sodobject}
98
9caad6bd
MW
99\begin{figure}[tbp]
100 \begin{tabular}{p{10pt}p{10pt}}
4effe575 101 \begin{nprog}
020b9e2b
MW
102 struct SodObject__ilayout \{ \\ \ind
103 union \{ \\ \ind
104 struct SodObject__ichain_obj \{ \\ \ind
105 const struct SodObject__vt_obj *_vt; \-\\
106 \} obj; \-\\
107 \} obj; \-\\
9caad6bd 108 \};
4effe575 109 \end{nprog}
9caad6bd 110 &
4effe575 111 \begin{nprog}
020b9e2b
MW
112 struct SodObject__vt_obj \{ \\ \ind
113 const SodClass *_class; \\
114 size_t _base; \\
115 struct SodObject__vtmsgs_obj \{ \\ \ind
116 void (*init)(SodObject *me, ...); \\
117 void (*init__v)(SodObject *me, va_list); \\
118 int (*teardown)(SodObject *me); \-\\
119 \} obj; \-\\
9caad6bd 120 \};
020b9e2b 121 \end{nprog} \\
9caad6bd
MW
122 \end{tabular}
123 \caption{Instance and vtable layout of @|SodObject|}
124 \label{fig:structures.root.sodobject}
125\end{figure}
126
34042b35 127\begin{describe}{cls}[SodObject]
020b9e2b
MW
128 {[nick = obj, metaclass = SodClass,
129 lisp_metaclass = sod_class] \\
130 class SodObject \{ \\ \ind
2f1ed4eb 131 void init(?); \-\\
a142609c 132 \}}
9caad6bd 133
a142609c
MW
134 The @|SodObject| class defines no slots. Because @|SodObject| has no
135 direct superclasses, there is only one chain, and no inherited slots or
136 messages, so the single chain contains only a vtable pointer.
9caad6bd 137
a142609c
MW
138 Since @|SodClass| also has only one chain, the vtable contains only the
139 standard class pointer and offset-to-base members. In a direct instance of
140 @|SodObject| (why would you want one?) the class pointer contains the
141 address of @|SodObject__class| and the offset is zero.
9caad6bd
MW
142
143 The instance and vtable layout of @|SodObject| is shown in
144 \xref{fig:structures.root.sodobject}.
a142609c 145
a42893dd 146 The following messages are defined.
a142609c 147
34042b35 148 \begin{describe}{msg}[obj.init]{void init(?);}
a142609c
MW
149 Initialize a newly allocated instance.
150
151 This message uses a custom method combination which works like the
152 standard method combination except that default behaviour specific to the
153 receiver's direct class is invoked if no primary or around method
154 overrides. This default behaviour may be invoked multiple times if some
155 method calls on its @|next_method| function more than once.
156
157 This default behaviour is to initialize the instance's slots using the
a42893dd
MW
158 defined slot initializers, and execute the initialization fragments.
159 Each slot is initialized using the most specific applicable initializer,
160 if any. Slots without an initializer are left uninitialized.
a142609c 161
a42893dd
MW
162 Slots are initialized and initialization fragments executed together, a
163 superclass at a time: first, the superclass's slots are initialized (if
164 any); then the superclass's initialization fragments (if any) are
165 executed, starting with the least specific superclass first. Slots and
166 initialization fragments defined by the same class are processed in the
167 order in which they appear in the class definition.
27ec3825 168
a142609c
MW
169 There are no standard keyword arguments; methods on subclasses are free
170 to introduce their own in the usual way.
171
172 It is usual to provide complex initialization behaviour as @|after|
173 methods. This ensures that slots have been initialized as necessary
174 before the method executes.
175
176 For more details on instance construction, see
177 \xref{sec:concepts.lifecycle.birth}.
178 \end{describe}
a42893dd 179
34042b35 180 \begin{describe}{msg}[obj.teardown]{int teardown();}
a42893dd
MW
181 Teardown an instance which is no longer required.
182
183 The message returns an integer flag. A zero value means that the
184 instance is safe to deallocate. A nonzero value means that the instance
185 should not be deallocated, and that it is safe for the caller to simply
186 forget about it. This simple protocol may be used, for example, to
187 implement a reference-counting system.
188
189 This message uses a custom method combination which works like the
190 standard method combination except that default behaviour is invoked if
191 no primary or around method overrides.
192
193 This default behaviour is to execute each superclass's teardown
194 fragments, most specific first, and then return zero to indicate that the
195 object is ready for deallocation. Teardown fragments defined by the same
196 class are processed in the order in which they appear in the class
197 definition.
198
199 It is usual to provide complex teardown behaviour as @|before| methods.
200 Logic to decide whether to allow deallocation is usually implemented as
201 @|around| methods.
202 \end{describe}
9caad6bd 203\end{describe}
62f9852b 204
0a2d4b68 205
62f9852b
MW
206\subsection{The SodClass class} \label{sec:structures.root.sodclass}
207
34042b35 208\begin{describe}{cls}[SodClass]
020b9e2b 209 {[nick = cls, link = SodObject] \\
fd040f06 210 class SodClass: SodObject \{ \\ \ind
020b9e2b
MW
211 const char *name; \\
212 const char *nick; \\
213 size_t initsz; \\
214 size_t align; \\
215 void *(*imprint)(void *@<p>); \\
216 size_t n_supers; \\
217 const SodClass *const *supers; \\
218 size_t n_cpl; \\
219 const SodClass *const *cpl; \\
220 const SodClass *link; \\
221 const SodClass *head; \\
222 size_t level; \\
223 size_t n_chains; \\
224 const struct sod_chain *chains; \\
225 size_t off_islots; \\
226 size_t islotsz; \-\\
9caad6bd
MW
227 \}}
228
a142609c
MW
229 The @|SodClass| class defines no additional messages , but there are a
230 number of slots. Its only direct superclass is @|SodObject| and so (like
231 its superclass) its vtable is simple.
9caad6bd
MW
232
233 The slots defined are as follows.
234 \begin{description} \let\makelabel\code
235
236 \item[name] A pointer to the class's name.
237
238 \item[nick] A pointer to the class's nickname.
239
240 \item[initsz] The size in bytes required to store an instance of the class.
241
8c2c58ae
MW
242 \item[align] A sufficient alignment for the class's instance storage.
243
9caad6bd
MW
244 \item[imprint] A pointer to a function: given a pointer @<p> to at least
245 @<initsz> bytes of appropriately aligned memory, `imprint' this memory it
246 so that it becomes a minimally functional instance of the class: all of
247 the vtable and class pointers are properly initialized, but the slots are
248 left untouched. The function returns its argument @<p>.
249
9caad6bd
MW
250 \item[n_supers] The number of direct superclasses. (This is zero exactly
251 in the case of @|SodObject|.)
252
253 \item[supers] A pointer to an array of @<n_supers> pointers to class
254 objects listing the class's direct superclasses, in the order in which
255 they were listed in the class definition. If @<n_supers> is zero, then
256 this pointer is null.
257
258 \item[n_cpl] The number of superclasses in the class's class precedence
259 list.
260
261 \item[cpl] A pointer to an array of pointers to class objects listing all
262 of the class's superclasses, from most- to least-specific, starting with
ac8ddb83 263 the class itself, so $@|$c$@->cls.cpl[0]| = c$ for all class objects
9caad6bd
MW
264 $c$.
265
266 \item[link] If the class is a chain head, then this is a null pointer;
267 otherwise it points to the class's distinguished link superclass (which
268 might or might not be a direct superclass).
269
270 \item[head] A pointer to the least-specific class in this class's chain; so
ac8ddb83
MW
271 @|$c$@->cls.head@->cls.link| is always null, and either @|$c$@->cls.link|
272 is null (in which case $@|$c$@->cls.head| = c$) or $@|$c$@->cls.head| =
273 @|$c$@->cls.link@->cls.head|$.
9caad6bd
MW
274
275 \item[level] The number of less specific superclasses in this class's
ac8ddb83
MW
276 chain. If @|$c$@->cls.link| is null then @|$c$@->cls.level| is zero;
277 otherwise $@|$c$@->cls.level| = @|$c$@->cls.link@->cls.level| + 1$.
9caad6bd
MW
278
279 \item[n_chains] The number of chains formed by the class's superclasses.
280
281 \item[chains] A pointer to an array of @|struct sod_chain| structures (see
282 below) describing the class's superclass chains, in decreasing order of
283 specificity of their most specific classes. It is always the case that
ac8ddb83 284 $@|$c$@->cls.chains[0].classes[$c$@->cls.level]| = c$.
9caad6bd
MW
285
286 \item[off_islots] The offset of the class's @|islots| structure relative to
287 its containing @|ichain| structure. The class doesn't define any slots
288 if and only if this is zero. (The offset can't be zero because the
289 vtable pointer is at offset zero.)
290
291 \item[islotsz] The size required to store the class's direct slots, i.e.,
292 the size of its @|islots| structure. The class doesn't define any slots
293 if and only if this is zero.
294
295 \end{description}
296\end{describe}
297
34042b35 298\begin{describe}{type}[struct sod_chain]
020b9e2b
MW
299 {struct sod_chain \{ \\ \ind
300 size_t n_classes; \\
301 const SodClass *const *classes; \\
302 size_t off_ichain; \\
303 const struct sod_vtable *vt; \\
304 size_t ichainsz; \-\\
9caad6bd
MW
305 \};}
306
b5229c16
MW
307 The @|struct sod_chain| structure describes an individual chain of
308 superclasses. It has the following members.
309 \begin{description} \let\makelabel\code
9caad6bd 310
b5229c16
MW
311 \item[n_classes] The number of classes in the chain. This is always at
312 least one.
9caad6bd 313
b5229c16
MW
314 \item[classes] A pointer to an array of class pointers listing the classes
315 in the chain from least- to most-specific. So
ac8ddb83
MW
316 $@|@<classes>[$i$]@->cls.head| = @|@<classes>[0]|$ for all $0 \le i <
317 @<n_classes>$, @|@<classes>[0]@->cls.link| is always null, and
318 $@|@<classes>[$i$]@->cls.link| = @|@<classes>[$i - 1$]|$ if $1 \le i <
b5229c16 319 @<n_classes>$.
9caad6bd 320
b5229c16 321 \item[off_ichain] The size of the @|ichain| structure for this chain.
9caad6bd 322
b5229c16
MW
323 \item[vt] The vtable for this chain. (It is possible, therefore, to
324 partially duplicate the behaviour of the @<imprint> function by walking
325 the chain structure.\footnote{%
326 There isn't enough information readily available to fill in the class
327 pointers correctly.} %
328 The @<imprint> function is much faster, though.)
9caad6bd 329
b5229c16 330 \item[ichainsz] The size of the @|ichain| structure for this chain.
9caad6bd 331
b5229c16
MW
332 \end{description}
333\end{describe}
62f9852b
MW
334
335%%%--------------------------------------------------------------------------
336\section{Class and vtable layout} \label{sec:structures.layout}
337
338The layout algorithms for Sod instances and vtables are nontrivial. They are
339defined here in full detail, since they're effectively fixed by Sod's ABI
340compatibility guarantees, so they might as well be documented for the sake of
341interoperating programs.
342
343Unfortunately, the descriptions are rather complicated, and, for the most
344part not necessary to a working understanding of Sod. The skeleton structure
345definitions shown should be more than enough for readers attempting to make
346sense of the generated headers and tables.
347
348In the description that follows, uppercase letters vary over class names,
349while the corresponding lowercase letters indicate the class nicknames.
350Throughout, we consider a class $C$ (therefore with nickname $c$).
351
0a2d4b68 352
62f9852b
MW
353\subsection{Generic instance structure}
354\label{sec:structures.layout.instance}
355
356The entire state of an instance of $C$ is contained in a single structure of
357type @|struct $C$__ilayout|.
358
359\begin{prog}
020b9e2b
MW
360 struct $C$__ilayout \{ \\ \ind
361 union $C$__ichainu_$h$ \{ \\ \ind
362 struct $C$__ichain_$h$ \{ \\ \ind
363 const struct $C$__vt_$h$ *_vt; \\
364 struct $H$__islots $h$; \\
365 \quad$\vdots$ \\
366 struct $C$__islots \{ \\ \ind
367 @<type>_1 @<slot>_1; \\
368 \quad$\vdots$ \\
369 @<type>_n @<slot>_n; \-\\
370 \} $c$; \-\\
371 \} $c$; \\
faf3eb58 372 struct $A$__ichain_$h$ $a$; \\
020b9e2b
MW
373 \quad$\vdots$ \-\\
374 \} $h$; \\
375 union $B$__ichainu_$i$ $i$; \\
376 \quad$\vdots$ \-\\
377 \}; \\+
378
62f9852b
MW
379 typedef struct $C$__ichain_$h$ $C$;
380\end{prog}
381
382The set of superclasses of $C$, including itself, can be partitioned into
383chains by following their distinguished superclass links. (Formally, the
384chains are the equivalence classes determined by the reflexive, symmetric,
385transitive closure of the `links to' relation.) Chains are identified by
386naming their least specific classes; the least specific class in a chain is
387called the \emph{chain head}. Suppose that the chain head of the chain
388containing $C$ itself is named $H$ (though keep in mind that it's possible
02840f3d 389that $H$ is in fact $C$ itself.)
62f9852b
MW
390
391\subsubsection{The ilayout structure}
392The @|ilayout| structure contains one member for each of $C$'s superclass
393chains. The first such member is
394\begin{prog}
395 union $C$__ichainu_$h$ $h$;
396\end{prog}
397described below; this is followed by members
398\begin{prog}
399 union $B$__ichainu_$i$ $i$;
400\end{prog}
401for each other chain, where $I$ is the head and $B$ the tail (most-specific)
402class of the chain. The members are in decreasing order of the specificity
403of the chains' most-specific classes. (Note that all but the first of these
404unions has already been defined as part of the definition of the
405corresponding $B$.)
406
407\subsubsection{The ichainu union}
408The @|ichainu| union contains a member for each class in the chain. The
409first is
410\begin{prog}
411 struct $C$__ichain_$h$ $c$;
412\end{prog}
413and this is followed by corresponding members
414\begin{prog}
415 struct $A$__ichain_$h$ $a$;
416\end{prog}
417for each of $C$'s superclasses $A$ in the same chain in some (unimportant)
f9bc613c 418order. The (somewhat obtuse) purpose of this union is to engage the `common
9cd5cf15 419initial sequence' rule of \cite[6.5.2.3]{ISO:1990:IIP,ANSI:1999:AII}.
62f9852b
MW
420
421\subsubsection{The ichain structure}
e97b1677 422The @|ichain| structure contains (in order), a pointer
62f9852b
MW
423\begin{prog}
424 const struct $C$__vt_$h$ *_vt;
425\end{prog}
426followed by a structure
427\begin{prog}
428 struct $A$__islots $a$;
429\end{prog}
430for each superclass $A$ of $C$ in the same chain which defines slots, from
431least- to most-specific; if $C$ defines any slots, then the last member is
432\begin{prog}
433 struct $C$__islots $c$;
434\end{prog}
435A `pointer to $C$' is always assumed (and, indeed, defined in C's
436type system) to be a pointer to the @|struct $C$__ichain_$h$|.
437
438\subsubsection{The islots structure}
439Finally, the @|islots| structure simply contains one member for each slot
440defined by $C$ in the order they appear in the class definition.
441
0a2d4b68 442
62f9852b
MW
443\subsection{Generic vtable structure} \label{sec:structures.layout.vtable}
444
445As described above, each @|ichain| structure of an instance's storage has a
446vtable pointer
447\begin{prog}
448 const struct $C$__vt_$h$ *_vt;
449\end{prog}
450In general, the vtables for the different chains will have \emph{different}
451structures.
452
756e9293 453The instance layout splits neatly into disjoint chains. This is necessary
62f9852b
MW
454because each @|ichain| must have as a prefix the @|ichain| for each
455superclass in the same chain, and each slot must be stored in exactly one
456place. The layout of vtables doesn't have this second requirement: it
457doesn't matter that there are multiple method entry pointers for the same
458effective method as long as they all work correctly. Indeed, it's essential
74bf1b29
MW
459that there are multiple entry pointers, because each chain's method entry
460function will need to apply a different offset to the receiver pointer before
461invoking the effective method.
62f9852b
MW
462
463A vtable for a class $C$ with chain head $H$ has the following general
464structure.
465\begin{prog}
020b9e2b
MW
466 union $C$__vtu_$h$ \{ \\ \ind
467 struct $C$__vt_$h$ \{ \\ \ind
468 const $P$ *_class; \\
469 size_t _base; \\
470 \quad$\vdots$ \\
471 const $Q$ *_cls_$j$; \\
472 \quad$\vdots$ \\
473 ptrdiff_t _off_$i$; \\
474 \quad$\vdots$ \\
475 struct $C$__vtmsgs_$a$ \{ \\ \ind
476 @<type> (*@<msg>)($C$ *, $\dots$); \\
477 \quad$\vdots$ \-\\
478 \} $a$; \\
479 \quad$\vdots$ \-\\
480 \} $c$; \-\\
481 \}; \\+
482
62f9852b
MW
483 extern const union $C$__vtu_$h$ $C$__vtable_$h$;
484\end{prog}
485
fa388683
MW
486In the following, let $M$ be the metaclass of $C$.
487
62f9852b
MW
488\subsubsection{The vtu union}
489The outer layer is a @|union $C$__vtu_$h$| containing a member
490\begin{prog}
491 struct $A$__vt_$h$ $a$;
492\end{prog}
493for each of $C$'s superclasses $A$ in the same chain, with $C$ itself listed
494first.
495
02840f3d
MW
496This is mostly an irrelevant detail, whose purpose is to defend against
497malicious compilers: pointers are always to one of the inner @|vt|
f231c300
MW
498structures. It's important only because it's the outer @|vtu| union which is
499exported by name. Specifically, for each chain of $C$'s superclasses there
500is an external object
62f9852b
MW
501\begin{prog}
502 const union $A$__vtu_$i$ $C$__vtable_$i$;
503\end{prog}
504where $A$ and $I$ are respectively the most and least specific classes in the
505chain.
506
507\subsubsection{The vt structure}
508The first member in the @|vt| structure is the \emph{root class pointer}
509\begin{prog}
510 const $P$ *_class;
511\end{prog}
512Among the superclasses of $C$ there must be exactly one class $O$ which
513itself has no direct superclasses; this is the \emph{root superclass} of $C$.
514(This is a rule enforced by the Sod translator.) The metaclass $R$ of $O$ is
515then the \emph{root metaclass} of $C$. The @|_class| member points to the
516@|ichain| structure of most specific superclass $P$ of $M$ in the same chain
517as $R$.
518
519This is followed by the \emph{base offset}
520\begin{prog}
521 size_t _base;
522\end{prog}
523which is simply the offset of the @|ichain| structure from the instance base.
524
525The rest of the vtable structure is populated by walking the superclass chain
526containing $C$ as follows. For each such superclass $B$, in increasing order
527of specificity, walk the class precedence list of $B$, again starting with
528its least-specific superclass. (This complex procedure guarantees that the
529vtable structure for a class is a prefix of the vtable structure for any of
530its subclasses in the same chain.)
531
532So, let $A$ be some superclass of $C$ which has been encountered during this
533traversal.
534
535\begin{itemize}
536
537\item Let $N$ be the metaclass of $A$. Examine the superclass chains of $N$
538 in order of decreasing specificity of their most-specific classes. Let $J$
9a9a7b35
MW
539 be the chain head of such a chain. If there is currently no class pointer
540 for the chain headed by $J$, then add a member
62f9852b
MW
541 \begin{prog}
542 const $Q$ *_cls_$j$;
543 \end{prog}
544 to the vtable pointing to the appropriate @|islots| structure within $M$'s
9a9a7b35
MW
545 class object, where $Q$ is the most specific superclass of $M$ in the same
546 chain as $J$.
62f9852b
MW
547
548\item Examine the superclass chains of $A$ in order of decreasing specificity
549 of their most-specific classes. Let $I$ be the chain head of such a chain.
550 If there is currently no member @|_off_$i$| then add a member
551 \begin{prog}
552 ptrdiff_t _off_$i$;
553 \end{prog}
554 to the vtable, containing the (signed) offset from the @|ichain| structure
555 of the chain headed by $h$ to that of the chain headed by $i$ within the
556 instance's layout.
557
558\item If class $A$ defines any messages, and there is currently no member
559 $a$, then add a member
560 \begin{prog}
561 struct $C$__vtmsgs_$a$ $a$;
562 \end{prog}
563 to the vtable. See below.
564
565\end{itemize}
566
567\subsubsection{The vtmsgs structure}
568Finally, the @|vtmsgs| structures contain pointers to the effective method
569entry functions for the messages defined by a superclass. There may be more
570than one method entry for a message, but all of the entry pointers for a
571message appear together, and entry pointers for separate messages appear in
572the order in which the messages are defined. If the receiver class has no
573applicable primary method for a message then it's usual for the method entry
574pointer to be null (though, as with a lot of things in Sod, extensions may do
575something different).
576
577For a standard message which takes a fixed number of arguments, defined as
578\begin{prog}
579 @<type>_0 $m$(@<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n);
580\end{prog}
581there is always a `main' entry point,
582\begin{prog}
583 @<type>_0 $m$($C$ *me, @<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n);
584\end{prog}
585
586For a standard message which takes a variable number of arguments,
587defined as
588\begin{prog}
589 @<type>_0 $m$(@<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n, \dots);
590\end{prog}
43073476
MW
591or a standard message which takes keyword arguments, defined as
592\begin{prog}
020b9e2b
MW
593 @<type>_0 $m$(\=@<type>_1 @<arg>_1,
594 $\ldots$,
595 @<type>_n @<arg>_n? \+\\
596 @<type>_{n+1} @<kw>_{n+1} @[= @<dflt>_{n+1}@],
597 $\ldots$,
d47e7cbb 598 @<type>_{n'} @<kw>_{n'} @[= @<dflt>_{n'}@]);
43073476 599\end{prog}
62f9852b
MW
600two entry points are defined: the usual `main' entry point which accepts a
601variable number of arguments, and a `valist' entry point which accepts an
602argument of type @|va_list| in place of the variable portion of the argument
43073476 603list or keywords.
62f9852b
MW
604\begin{prog}
605 @<type>_0 $m$($C$ *me, @<type>_1 @<arg>_1, $\ldots$,
020b9e2b 606 @<type>_n @<arg>_n, \dots); \\
62f9852b
MW
607 @<type>_0 $m$__v($C$ *me, @<type>_1 @<arg>_1, $\ldots$,
608 @<type>_n @<arg>_n, va_list sod__ap);
609\end{prog}
610
0a2d4b68 611
b8101b23 612\subsection{Additional definitions} \label{sec:structures.layout.additional}
62f9852b
MW
613
614In addition to the instance and vtable structures described above, the
615following definitions are made for each class $C$.
616
617For each message $m$ directly defined by $C$ there is a macro definition
618\begin{prog}
619 \#define $C$_$m$(@<me>, $\ldots$) @<me>@->_vt@->$c$.$m$(@<me>, $\ldots$)
620\end{prog}
621which makes sending the message $m$ to an instance of (any subclass of) $C$
622somewhat less ugly.
623
43073476
MW
624If $m$ takes a variable number of arguments, or keyword arguments, the macro
625is more complicated and is only available in compilers advertising C99
626support, but the effect is the same. For each variable-argument message,
627there is also an additional macro for calling the `valist' entry point.
62f9852b
MW
628\begin{prog}
629 \#define $C$_$m$__v(@<me>, $\ldots$, @<sod__ap>)
630 @<me>@->_vt@->$c$.$m$__v(@<me>, $\ldots$, @<sod__ap>)
631\end{prog}
632
633For each proper superclass $A$ of $C$, there is a macro defined
634\begin{prog}
635 $A$ *$C$__CONV_$a$($C$ *_obj);
636\end{prog}
637(named in \emph{upper case}) which converts a (static-type) pointer to $C$ to
638a pointer to the same actual instance, but statically typed as a pointer to
639$A$. This is most useful when $A$ is not in the same chain as $C$ since
640in-chain upcasts are both trivial and rarely needed, but the full set is
641defined for the sake of completeness.
642
643Finally, the class object is defined as
644\begin{prog}
020b9e2b 645 extern const struct $R$__ilayout $C$__classobj; \\
489173a5
MW
646 \#define $C$__class (\&$C$__classobj.$j$.$r$) \\
647 \#define $C$__cls_$k$ (\&$C$__classobj.$k$.$n$) \\
648 \quad$\vdots$
62f9852b
MW
649\end{prog}
650The exported symbol @|$C$__classobj| contains the entire class instance.
651This is usually rather unwieldy. The macro @|$C$__class| is usable as a
652pointer of type @|const $R$~*|, where $R$ is the root metaclass of $C$, i.e.,
653the metaclass of the least specific superclass of $C$; usually this is
489173a5
MW
654@|const SodClass~*|. For each chain of $C$'s metaclass, a macro
655@|$C$__cls_$k$| is defined, usable as a pointer of type @|const $N$~*|, where
656$K$ and $N$ are the chain's head and tail classes (i.e., the least- and
657most-specific classes in the chain) respectively; this macro is
658\emph{omitted} if $N = R$, i.e., in the common case where $C$'s metaclass is
659precisely the root metaclass, since the existing @|$C$__class| macro is
660already sufficient.
661
62f9852b
MW
662
663%%%----- That's all, folks --------------------------------------------------
664
665%%% Local variables:
666%%% mode: LaTeX
667%%% TeX-master: "sod.tex"
668%%% TeX-PDF-mode: t
669%%% End: