doc/structures.tex: Fix fake property lists for the builtin classes.
[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>|.
35The definitions described in sections \ref{sec:structures.layout} are defined
36in the header file generated by the containing module.
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
48\begin{describe}[struct sod_instance]{type}
49 {struct sod_instance \{ \\ \ind
50 const struct sod_vtable *_vt; \- \\
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
60\begin{describe}[struct sod_vtable]{type}
61 {struct sod_vtable \{ \\ \ind
62 const SodClass *_class; \\
63 size_t _base; \- \\
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
0a2d4b68 90
62f9852b
MW
91\subsection{The SodObject class} \label{sec:structures.root.sodobject}
92
9caad6bd
MW
93\begin{figure}[tbp]
94 \begin{tabular}{p{10pt}p{10pt}}
95 \begin{prog}
96 struct SodObject__ilayout \{ \\ \ind
97 union \{ \\ \ind
98 struct SodObject__ichain_obj \{ \\ \ind
99 const struct SodObject__vt_obj *_vt; \- \\
e160ec73 100 \} obj; \- \\
9caad6bd
MW
101 \} obj; \- \\
102 \};
103 \end{prog}
104 &
105 \begin{prog}
106 struct SodObject__vt_obj \{ \\ \ind
107 const SodClass *_class; \\
108 size_t _base; \- \\
109 \};
110 \end{prog} \\
111 \end{tabular}
112 \caption{Instance and vtable layout of @|SodObject|}
113 \label{fig:structures.root.sodobject}
114\end{figure}
115
116\begin{describe}[SodObject]{cls}
bd441d33 117 {[nick = obj, metaclass = SodClass, lisp_metaclass = sod_class] \\
9caad6bd
MW
118 class SodObject \{ \}}
119
120 The @|SodObject| class defines no slots or messages. Because @|SodObject|
121 has no direct superclasses, there is only one chain, and no inherited
122 slots or messages, so the single chain contains only a vtable pointer.
123
124 Since there are no messages, and @|SodClass| also has only one chain, the
125 vtable contains only the standard class pointer and offset-to-base members.
126 In a direct instance of @|SodObject| (why would you want one?) the class
127 pointer contains the address of @|SodObject__class| and the offset is zero.
128
129 The instance and vtable layout of @|SodObject| is shown in
130 \xref{fig:structures.root.sodobject}.
131\end{describe}
62f9852b 132
0a2d4b68 133
62f9852b
MW
134\subsection{The SodClass class} \label{sec:structures.root.sodclass}
135
9caad6bd 136\begin{describe}[SodClass]{cls}
bd441d33
MW
137 {[nick = cls, link = SodObject] \\
138 class SodClass : SodObject \{ \\ \ind
9caad6bd
MW
139 const char *name; \\
140 const char *nick; \\
141 size_t initsz; \\
142 void *(*imprint)(void *@<p>); \\
143 void *(*init)(void *@<p>); \\
144 size_t n_supers; \\
145 const SodClass *const *supers; \\
146 size_t n_cpl; \\
147 const SodClass *const *cpl; \\
148 const SodClass *link; \\
149 const SodClass *head; \\
150 size_t level; \\
151 size_t n_chains; \\
152 const struct sod_chain *chains; \\
153 size_t off_islots; \\
154 size_t islotsz; \- \\
155 \}}
156
157 The @|SodClass| class defines no messages, but there are a number of slots.
158 Its only direct superclass is @|SodObject| and so (like its superclass) its
159 vtable is trivial.
160
161 The slots defined are as follows.
162 \begin{description} \let\makelabel\code
163
164 \item[name] A pointer to the class's name.
165
166 \item[nick] A pointer to the class's nickname.
167
168 \item[initsz] The size in bytes required to store an instance of the class.
169
170 \item[imprint] A pointer to a function: given a pointer @<p> to at least
171 @<initsz> bytes of appropriately aligned memory, `imprint' this memory it
172 so that it becomes a minimally functional instance of the class: all of
173 the vtable and class pointers are properly initialized, but the slots are
174 left untouched. The function returns its argument @<p>.
175
176 \item[init] A pointer to a function: given a pointer @<p> to at least
177 @<initsz> bytes of appropriately aligned memory, initialize an instance
178 of the class in it: all of the vtable and class pointers are initialized,
179 as are slots for which initializers are defined. Other slots are left
180 untouched. The function returns its argument @<p>.
181
182 \item[n_supers] The number of direct superclasses. (This is zero exactly
183 in the case of @|SodObject|.)
184
185 \item[supers] A pointer to an array of @<n_supers> pointers to class
186 objects listing the class's direct superclasses, in the order in which
187 they were listed in the class definition. If @<n_supers> is zero, then
188 this pointer is null.
189
190 \item[n_cpl] The number of superclasses in the class's class precedence
191 list.
192
193 \item[cpl] A pointer to an array of pointers to class objects listing all
194 of the class's superclasses, from most- to least-specific, starting with
195 the class itself, so $c@->@|cls|.@|cpl|[0] = c$ for all class objects
196 $c$.
197
198 \item[link] If the class is a chain head, then this is a null pointer;
199 otherwise it points to the class's distinguished link superclass (which
200 might or might not be a direct superclass).
201
202 \item[head] A pointer to the least-specific class in this class's chain; so
203 $c@->@|cls|.@|head|@->@|cls|.@|link|$ is always null, and either
204 $c@->@|cls|.@|link|$ is null (in which case $c@->@|cls|.@|head| = c$) or
205 $c@->@|cls|.@|head| = c@->@|cls|.@|link|@->@|cls|.@|head|$.
206
207 \item[level] The number of less specific superclasses in this class's
208 chain. If $c@->@|cls|.@|link|$ is null then $c@->@|cls|.@|level|$ is
209 zero; otherwise $c@->@|cls|.@|level| =
210 c@->@|cls|.@|link|@->@|cls|.@|level| + 1$.
211
212 \item[n_chains] The number of chains formed by the class's superclasses.
213
214 \item[chains] A pointer to an array of @|struct sod_chain| structures (see
215 below) describing the class's superclass chains, in decreasing order of
216 specificity of their most specific classes. It is always the case that
217 $c@->@|cls|.@|chains|[0].@|classes|[c@->@|cls|.@|level|] = c$.
218
219 \item[off_islots] The offset of the class's @|islots| structure relative to
220 its containing @|ichain| structure. The class doesn't define any slots
221 if and only if this is zero. (The offset can't be zero because the
222 vtable pointer is at offset zero.)
223
224 \item[islotsz] The size required to store the class's direct slots, i.e.,
225 the size of its @|islots| structure. The class doesn't define any slots
226 if and only if this is zero.
227
228 \end{description}
229\end{describe}
230
231\begin{describe}[struct sod_chain]{type}
232 {struct sod_chain \{ \\ \ind
233 size_t n_classes; \\
234 const SodClass *const *classes; \\
235 size_t off_ichain; \\
236 const struct sod_vtable *vt; \\
237 size_t ichainsz; \- \\
238 \};}
239
b5229c16
MW
240 The @|struct sod_chain| structure describes an individual chain of
241 superclasses. It has the following members.
242 \begin{description} \let\makelabel\code
9caad6bd 243
b5229c16
MW
244 \item[n_classes] The number of classes in the chain. This is always at
245 least one.
9caad6bd 246
b5229c16
MW
247 \item[classes] A pointer to an array of class pointers listing the classes
248 in the chain from least- to most-specific. So
249 $@<classes>[i]@->@|cls|.@|head| = @<classes>[0]$ for all $0 \le i <
250 @<n_classes>$, $@<classes>[0]@->@|cls|.@|link|$ is always null, and
251 $@<classes>[i]@->@|cls|.@|link| = @<classes>[i - 1]$ if $1 \le i <
252 @<n_classes>$.
9caad6bd 253
b5229c16 254 \item[off_ichain] The size of the @|ichain| structure for this chain.
9caad6bd 255
b5229c16
MW
256 \item[vt] The vtable for this chain. (It is possible, therefore, to
257 partially duplicate the behaviour of the @<imprint> function by walking
258 the chain structure.\footnote{%
259 There isn't enough information readily available to fill in the class
260 pointers correctly.} %
261 The @<imprint> function is much faster, though.)
9caad6bd 262
b5229c16 263 \item[ichainsz] The size of the @|ichain| structure for this chain.
9caad6bd 264
b5229c16
MW
265 \end{description}
266\end{describe}
62f9852b
MW
267
268%%%--------------------------------------------------------------------------
269\section{Class and vtable layout} \label{sec:structures.layout}
270
271The layout algorithms for Sod instances and vtables are nontrivial. They are
272defined here in full detail, since they're effectively fixed by Sod's ABI
273compatibility guarantees, so they might as well be documented for the sake of
274interoperating programs.
275
276Unfortunately, the descriptions are rather complicated, and, for the most
277part not necessary to a working understanding of Sod. The skeleton structure
278definitions shown should be more than enough for readers attempting to make
279sense of the generated headers and tables.
280
281In the description that follows, uppercase letters vary over class names,
282while the corresponding lowercase letters indicate the class nicknames.
283Throughout, we consider a class $C$ (therefore with nickname $c$).
284
0a2d4b68 285
62f9852b
MW
286\subsection{Generic instance structure}
287\label{sec:structures.layout.instance}
288
289The entire state of an instance of $C$ is contained in a single structure of
290type @|struct $C$__ilayout|.
291
292\begin{prog}
293 struct $C$__ilayout \{ \\ \ind
294 union $C$__ichainu_$h$ \{ \\ \ind
295 struct $C$__ichain_$h$ \{ \\ \ind
296 const struct $C$__vt_$h$ *_vt; \\
297 struct $H$__islots $h$; \\
298 \quad$\vdots$ \\
299 struct $C$__islots \{ \\ \ind
300 @<type>_1 @<slot>_1; \\
301 \quad$\vdots$ \\
302 @<type>_n @<slot>_n; \- \\
303 \} $c$; \- \\
304 \} $c$; \\
305 struct $H$__ichain_$h$ $h$; \\
306 \quad$\vdots$ \- \\
307 \} $h$; \\
308 union $B$__ichainu_$i$ $i$; \\
309 \quad$\vdots$ \- \\
310 \};
311 \\[\bigskipamount]
312 typedef struct $C$__ichain_$h$ $C$;
313\end{prog}
314
315The set of superclasses of $C$, including itself, can be partitioned into
316chains by following their distinguished superclass links. (Formally, the
317chains are the equivalence classes determined by the reflexive, symmetric,
318transitive closure of the `links to' relation.) Chains are identified by
319naming their least specific classes; the least specific class in a chain is
320called the \emph{chain head}. Suppose that the chain head of the chain
321containing $C$ itself is named $H$ (though keep in mind that it's possible
322that .$H$ is in fact $C$ itself.)
323
324\subsubsection{The ilayout structure}
325The @|ilayout| structure contains one member for each of $C$'s superclass
326chains. The first such member is
327\begin{prog}
328 union $C$__ichainu_$h$ $h$;
329\end{prog}
330described below; this is followed by members
331\begin{prog}
332 union $B$__ichainu_$i$ $i$;
333\end{prog}
334for each other chain, where $I$ is the head and $B$ the tail (most-specific)
335class of the chain. The members are in decreasing order of the specificity
336of the chains' most-specific classes. (Note that all but the first of these
337unions has already been defined as part of the definition of the
338corresponding $B$.)
339
340\subsubsection{The ichainu union}
341The @|ichainu| union contains a member for each class in the chain. The
342first is
343\begin{prog}
344 struct $C$__ichain_$h$ $c$;
345\end{prog}
346and this is followed by corresponding members
347\begin{prog}
348 struct $A$__ichain_$h$ $a$;
349\end{prog}
350for each of $C$'s superclasses $A$ in the same chain in some (unimportant)
351order.
352
353\subsubsection{The ichain structure}
354The
355@|ichain|
356structure contains (in order), a pointer
357\begin{prog}
358 const struct $C$__vt_$h$ *_vt;
359\end{prog}
360followed by a structure
361\begin{prog}
362 struct $A$__islots $a$;
363\end{prog}
364for each superclass $A$ of $C$ in the same chain which defines slots, from
365least- to most-specific; if $C$ defines any slots, then the last member is
366\begin{prog}
367 struct $C$__islots $c$;
368\end{prog}
369A `pointer to $C$' is always assumed (and, indeed, defined in C's
370type system) to be a pointer to the @|struct $C$__ichain_$h$|.
371
372\subsubsection{The islots structure}
373Finally, the @|islots| structure simply contains one member for each slot
374defined by $C$ in the order they appear in the class definition.
375
0a2d4b68 376
62f9852b
MW
377\subsection{Generic vtable structure} \label{sec:structures.layout.vtable}
378
379As described above, each @|ichain| structure of an instance's storage has a
380vtable pointer
381\begin{prog}
382 const struct $C$__vt_$h$ *_vt;
383\end{prog}
384In general, the vtables for the different chains will have \emph{different}
385structures.
386
387The instance layout split neatly into disjoint chains. This is necessary
388because each @|ichain| must have as a prefix the @|ichain| for each
389superclass in the same chain, and each slot must be stored in exactly one
390place. The layout of vtables doesn't have this second requirement: it
391doesn't matter that there are multiple method entry pointers for the same
392effective method as long as they all work correctly. Indeed, it's essential
393that they do, because each chain's method entry function will need to apply a
394different offset to the receiver pointer before invoking the effective
395method.
396
397A vtable for a class $C$ with chain head $H$ has the following general
398structure.
399\begin{prog}
400 union $C$__vtu_$h$ \{ \\ \ind
401 struct $C$__vt_$h$ \{ \\ \ind
402 const $P$ *_class; \\
403 size_t _base; \\
404 \quad$\vdots$ \\
405 const $Q$ *_cls_$j$; \\
406 \quad$\vdots$ \\
407 ptrdiff_t _off_$i$; \\
408 \quad$\vdots$ \\
409 struct $C$__vtmsgs_$a$ \{ \\ \ind
410 @<type> (*@<msg>)($C$ *, $\dots$); \\
411 \quad$\vdots$ \- \\
412 \} $a$; \\
413 \quad$\vdots$ \- \\
414 \} $c$; \- \\
415 \};
416 \\[\bigskipamount]
417 extern const union $C$__vtu_$h$ $C$__vtable_$h$;
418\end{prog}
419
420\subsubsection{The vtu union}
421The outer layer is a @|union $C$__vtu_$h$| containing a member
422\begin{prog}
423 struct $A$__vt_$h$ $a$;
424\end{prog}
425for each of $C$'s superclasses $A$ in the same chain, with $C$ itself listed
426first.
427
428This is mostly an irrelevant detail,
429whose purpose is to defend against malicious compilers:
430pointers are always to one of the inner
431@|vt|
432structures.
433It's important only because it's the outer
434@|vtu|
435union which is exported by name.
436Specifically, for each chain of
437$C$'s
438superclasses
439there is an external object
440\begin{prog}
441 const union $A$__vtu_$i$ $C$__vtable_$i$;
442\end{prog}
443where $A$ and $I$ are respectively the most and least specific classes in the
444chain.
445
446\subsubsection{The vt structure}
447The first member in the @|vt| structure is the \emph{root class pointer}
448\begin{prog}
449 const $P$ *_class;
450\end{prog}
451Among the superclasses of $C$ there must be exactly one class $O$ which
452itself has no direct superclasses; this is the \emph{root superclass} of $C$.
453(This is a rule enforced by the Sod translator.) The metaclass $R$ of $O$ is
454then the \emph{root metaclass} of $C$. The @|_class| member points to the
455@|ichain| structure of most specific superclass $P$ of $M$ in the same chain
456as $R$.
457
458This is followed by the \emph{base offset}
459\begin{prog}
460 size_t _base;
461\end{prog}
462which is simply the offset of the @|ichain| structure from the instance base.
463
464The rest of the vtable structure is populated by walking the superclass chain
465containing $C$ as follows. For each such superclass $B$, in increasing order
466of specificity, walk the class precedence list of $B$, again starting with
467its least-specific superclass. (This complex procedure guarantees that the
468vtable structure for a class is a prefix of the vtable structure for any of
469its subclasses in the same chain.)
470
471So, let $A$ be some superclass of $C$ which has been encountered during this
472traversal.
473
474\begin{itemize}
475
476\item Let $N$ be the metaclass of $A$. Examine the superclass chains of $N$
477 in order of decreasing specificity of their most-specific classes. Let $J$
478 be the chain head of such a chain, and let $Q$ be the most specific
479 superclass of $M$ in the same chain as $J$. Then, if there is currently no
480 class pointer of type $Q$, then add a member
481 \begin{prog}
482 const $Q$ *_cls_$j$;
483 \end{prog}
484 to the vtable pointing to the appropriate @|islots| structure within $M$'s
485 class object.
486
487\item Examine the superclass chains of $A$ in order of decreasing specificity
488 of their most-specific classes. Let $I$ be the chain head of such a chain.
489 If there is currently no member @|_off_$i$| then add a member
490 \begin{prog}
491 ptrdiff_t _off_$i$;
492 \end{prog}
493 to the vtable, containing the (signed) offset from the @|ichain| structure
494 of the chain headed by $h$ to that of the chain headed by $i$ within the
495 instance's layout.
496
497\item If class $A$ defines any messages, and there is currently no member
498 $a$, then add a member
499 \begin{prog}
500 struct $C$__vtmsgs_$a$ $a$;
501 \end{prog}
502 to the vtable. See below.
503
504\end{itemize}
505
506\subsubsection{The vtmsgs structure}
507Finally, the @|vtmsgs| structures contain pointers to the effective method
508entry functions for the messages defined by a superclass. There may be more
509than one method entry for a message, but all of the entry pointers for a
510message appear together, and entry pointers for separate messages appear in
511the order in which the messages are defined. If the receiver class has no
512applicable primary method for a message then it's usual for the method entry
513pointer to be null (though, as with a lot of things in Sod, extensions may do
514something different).
515
516For a standard message which takes a fixed number of arguments, defined as
517\begin{prog}
518 @<type>_0 $m$(@<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n);
519\end{prog}
520there is always a `main' entry point,
521\begin{prog}
522 @<type>_0 $m$($C$ *me, @<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n);
523\end{prog}
524
525For a standard message which takes a variable number of arguments,
526defined as
527\begin{prog}
528 @<type>_0 $m$(@<type>_1 @<arg>_1, $\ldots$, @<type>_n @<arg>_n, \dots);
529\end{prog}
530two entry points are defined: the usual `main' entry point which accepts a
531variable number of arguments, and a `valist' entry point which accepts an
532argument of type @|va_list| in place of the variable portion of the argument
533list.
534\begin{prog}
535 @<type>_0 $m$($C$ *me, @<type>_1 @<arg>_1, $\ldots$,
536 @<type>_n @<arg>_n, \dots); \\
537 @<type>_0 $m$__v($C$ *me, @<type>_1 @<arg>_1, $\ldots$,
538 @<type>_n @<arg>_n, va_list sod__ap);
539\end{prog}
540
0a2d4b68 541
b8101b23 542\subsection{Additional definitions} \label{sec:structures.layout.additional}
62f9852b
MW
543
544In addition to the instance and vtable structures described above, the
545following definitions are made for each class $C$.
546
547For each message $m$ directly defined by $C$ there is a macro definition
548\begin{prog}
549 \#define $C$_$m$(@<me>, $\ldots$) @<me>@->_vt@->$c$.$m$(@<me>, $\ldots$)
550\end{prog}
551which makes sending the message $m$ to an instance of (any subclass of) $C$
552somewhat less ugly.
553
554If $m$ takes a variable number of arguments, the macro is more complicated
555and is only available in compilers advertising C99 support, but the effect is
556the same. For each variable-argument message, there is also an additional
557macro for calling the `valist' entry point.
558\begin{prog}
559 \#define $C$_$m$__v(@<me>, $\ldots$, @<sod__ap>)
560 @<me>@->_vt@->$c$.$m$__v(@<me>, $\ldots$, @<sod__ap>)
561\end{prog}
562
563For each proper superclass $A$ of $C$, there is a macro defined
564\begin{prog}
565 $A$ *$C$__CONV_$a$($C$ *_obj);
566\end{prog}
567(named in \emph{upper case}) which converts a (static-type) pointer to $C$ to
568a pointer to the same actual instance, but statically typed as a pointer to
569$A$. This is most useful when $A$ is not in the same chain as $C$ since
570in-chain upcasts are both trivial and rarely needed, but the full set is
571defined for the sake of completeness.
572
573Finally, the class object is defined as
574\begin{prog}
575 extern const struct $R$__ilayout $C$__classobj; \\
576 \#define $C$__class (\&$C$__classobj.$j$.$r$)
577\end{prog}
578The exported symbol @|$C$__classobj| contains the entire class instance.
579This is usually rather unwieldy. The macro @|$C$__class| is usable as a
580pointer of type @|const $R$~*|, where $R$ is the root metaclass of $C$, i.e.,
581the metaclass of the least specific superclass of $C$; usually this is
582@|const SodClass~*|.
583
584%%%----- That's all, folks --------------------------------------------------
585
586%%% Local variables:
587%%% mode: LaTeX
588%%% TeX-master: "sod.tex"
589%%% TeX-PDF-mode: t
590%%% End: