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