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