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