Commit | Line | Data |
---|---|---|
47de28ae MW |
1 | .\" -*-nroff-*- |
2 | .\" | |
3 | .\" Description of the main Sod data structures | |
4 | .\" | |
5 | .\" (c) 2015 Straylight/Edgeware | |
6 | .\" | |
7 | . | |
8 | .\"----- Licensing notice --------------------------------------------------- | |
9 | .\" | |
e0808c47 | 10 | .\" This file is part of the Sensible Object Design, an object system for C. |
47de28ae MW |
11 | .\" |
12 | .\" SOD is free software; you can redistribute it and/or modify | |
65aaa02c MW |
13 | .\" it under the terms of the GNU Library General Public License as |
14 | .\" published by the Free Software Foundation; either version 2 of the | |
15 | .\" License, or (at your option) any later version. | |
47de28ae MW |
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 | |
65aaa02c | 20 | .\" GNU Library General Public License for more details. |
47de28ae | 21 | .\" |
65aaa02c MW |
22 | .\" You should have received a copy of the GNU Library General Public |
23 | .\" License along with SOD; if not, write to the Free | |
24 | .\" Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | .\" MA 02111-1307, USA. | |
47de28ae | 26 | . |
9ed8eb2a | 27 | .\" Highlight using terminal escapes, rather than overstriking. |
47de28ae | 28 | .\"\X'tty: sgr 1' |
9ed8eb2a | 29 | . |
47de28ae MW |
30 | .\" String definitions and font selection. |
31 | .ie t \{\ | |
32 | . ds o \(bu | |
33 | . if \n(.g .fam P | |
34 | .\} | |
35 | .el \{\ | |
36 | . ds o o | |
37 | .\} | |
38 | . | |
39 | .\" .hP TEXT -- start an indented paragraph with TEXT hanging off to the left | |
40 | .de hP | |
41 | .IP | |
42 | \h'-\w'\fB\\$1\ \fP'u'\fB\\$1\ \fP\c | |
43 | .. | |
44 | . | |
45 | .\"-------------------------------------------------------------------------- | |
46 | .TH sod-structs 3 "8 September 2015" "Straylight/Edgeware" "Sensible Object Design" | |
47 | . | |
48 | .SH NAME | |
49 | sod-structs \- main Sod data structures | |
50 | . | |
51 | .\"-------------------------------------------------------------------------- | |
52 | .SH SYNOPSIS | |
53 | .nf | |
54 | .ft B | |
55 | #include <sod/sod.h> | |
56 | ||
57 | typedef struct SodObject__ichain_obj SodObject; | |
58 | typedef struct SodClass__ichain_obj SodClass; | |
59 | ||
60 | struct sod_instance { | |
61 | \h'2n'const struct sod_vtable *_vt; | |
62 | }; | |
63 | ||
64 | struct sod_vtable { | |
65 | \h'2n'const SodClass *_class; | |
66 | \h'2n'size_t _base; | |
67 | }; | |
68 | ||
69 | struct SodObject__vt_obj { | |
70 | \h'2n'const SodClass *_class; | |
71 | \h'2n'size_t _base; | |
a142609c MW |
72 | \h'2n'struct SodObject__vtmsgs_obj { |
73 | \h'4n'void (*init)(SodObject *\fIme\fB, ...); | |
74 | \h'4n'void (*init__v)(SodObject *\fIme\fB, va_list); | |
75 | \h'2n'} obj; | |
47de28ae MW |
76 | }; |
77 | ||
78 | struct SodObject__ilayout { | |
79 | \h'2n'union { | |
80 | \h'4n'struct SodObject__ichain_obj { | |
81 | \h'6n'const struct SodObject__vt_obj *_vt; | |
82 | \h'4n'}; | |
83 | \h'2n'} obj; | |
84 | }; | |
85 | ||
86 | extern const struct SodClass__ilayout SodObject__classobj; | |
87 | #define SodObject__class (&SodObject__classobj.obj.cls) | |
88 | ||
89 | struct SodClass__vt_obj { | |
90 | \h'2n'const SodClass *_class; | |
91 | \h'2n'size_t _base; | |
a142609c MW |
92 | \h'2n'struct SodClass__vtmsgs_obj { |
93 | \h'4n'void (*init)(SodClass *\fIme\fB, ...); | |
94 | \h'4n'void (*init__v)(SodClass *\fIme\fB, va_list); | |
95 | \h'2n'} obj; | |
47de28ae MW |
96 | }; |
97 | ||
98 | struct SodObject__ilayout { | |
99 | \h'2n'union { | |
100 | \h'4n'struct SodClass__ichain_obj { | |
101 | \h'6n'const struct SodClass__vt_obj *_vt; | |
102 | \h'6n'struct SodClass__islots { | |
103 | \h'8n'const char *name; | |
104 | \h'8n'const char *nick; | |
105 | \h'8n'size_t initsz; | |
106 | \h'8n'void *(*imprint)(void *\fIp\fB); | |
47de28ae MW |
107 | \h'8n'size_t n_supers; |
108 | \h'8n'const SodClass *const *supers; | |
109 | \h'8n'size_t n_cpl; | |
110 | \h'8n'const SodClass *const *cpl; | |
111 | \h'8n'const SodClass *link; | |
112 | \h'8n'const SodClass *head; | |
113 | \h'8n'size_t level; | |
114 | \h'8n'size_t n_chains; | |
115 | \h'8n'const struct sod_chain *chains; | |
116 | \h'8n'size_t off_islots; | |
117 | \h'8n'size_t islotsz; | |
118 | \h'6n'} cls; | |
119 | \h'4n'}; | |
120 | \h'4n'SodObject obj; | |
121 | \h'2n'} obj; | |
122 | }; | |
123 | ||
124 | struct sod_chain { | |
125 | \h'2n'size_t n_classes; | |
126 | \h'2n'const SodClass *const *classes; | |
127 | \h'2n'size_t off_ichain; | |
128 | \h'2n'const struct sod_vtable *vt; | |
129 | \h'2n'size_t ichainsz; | |
130 | }; | |
131 | ||
132 | extern const struct SodClass__ilayout SodClass__classobj; | |
133 | #define SodClass__class (&SodClass__classobj.obj.cls) | |
134 | .fi | |
135 | .ft P | |
136 | . | |
137 | .\"-------------------------------------------------------------------------- | |
138 | .SH DESCRIPTION | |
139 | . | |
140 | This page describes the structure and layout | |
141 | of standard Sod objects, classes and associated metadata. | |
142 | Note that Sod's object system is very flexible | |
143 | and it's possible for an extension | |
144 | to define a new root class | |
145 | which works very differently from the standard | |
146 | .B SodObject | |
147 | described here. | |
148 | . | |
149 | .\"-------------------------------------------------------------------------- | |
150 | .SH COMMON INSTANCE STRUCTURE | |
151 | . | |
152 | As described below, | |
153 | a pointer to an instance actually points to an | |
154 | .I "instance chain" | |
155 | structure within the instances overall layout structure. | |
156 | .PP | |
157 | Instance chains contain slots and vtable pointers, | |
158 | as described below. | |
159 | All instances have the basic structure of a | |
160 | .BR "struct sod_instance" , | |
161 | which has the following members. | |
162 | .TP | |
163 | .B "const struct sod_vtable *_vt" | |
164 | A pointer to a | |
165 | .IR vtable , | |
166 | which has the basic structure of a | |
167 | .BR "struct sod_vtable" , | |
168 | described below. | |
169 | .PP | |
170 | A vtable contains static metadata needed | |
171 | for efficient conversions and | |
172 | message dispatch, | |
173 | and pointers to the instance's class. | |
174 | Each chain points to a different vtable | |
175 | All vtables have the basic structure of a | |
176 | .BR "struct sod_vtable" , | |
177 | which has the following members. | |
178 | .TP | |
179 | .B "const SodClass *_class" | |
180 | A pointer to the instance's class object. | |
181 | .TP | |
182 | .B "size_t _base;" | |
183 | The offset of this chain structure | |
184 | above the start of the overall instance layout, in bytes. | |
185 | Subtracting | |
186 | .B _base | |
187 | from the instance chain pointer | |
188 | finds the layout base address. | |
189 | . | |
190 | .\"-------------------------------------------------------------------------- | |
191 | .SH BUILT-IN ROOT OBJECTS | |
192 | . | |
193 | This section describes the built-in classes | |
194 | .B SodObject | |
195 | and | |
196 | .BR SodClass , | |
197 | which are the standard roots of the inheritance and metaclass graphs | |
198 | respectively. | |
199 | Specifically, | |
200 | .B SodObject | |
201 | has no direct superclasses, | |
202 | and | |
203 | .B SodClass | |
204 | is its own metaclass. | |
9c4a4110 MW |
205 | It is not possible to define root classes in module files |
206 | because of circularities: | |
47de28ae MW |
207 | .B SodObject |
208 | has | |
209 | .B SodClass | |
9c4a4110 MW |
210 | as its metaclass, |
211 | and | |
47de28ae MW |
212 | .B SodClass |
213 | is a subclass of | |
214 | .BR SodObject . | |
215 | Extensions can define additional root classes, | |
216 | but this is tricky, | |
217 | and not really to be recommended. | |
218 | . | |
219 | .SS The SodObject class | |
220 | The | |
221 | .B SodObject | |
a142609c | 222 | class defines no slots. |
47de28ae MW |
223 | Because |
224 | .B SodObject | |
225 | has no direct superclasses, | |
226 | there is only one chain, | |
227 | and no inherited slots or messages, | |
228 | so the single chain contains only a vtable pointer. | |
229 | .PP | |
a142609c | 230 | Since |
47de28ae MW |
231 | .B SodClass |
232 | also has only one chain, | |
233 | the vtable contains only the standard class pointer and offset-to-base | |
234 | members. | |
235 | In an actual instance of | |
236 | .B SodObject | |
237 | (why would you want one?) | |
238 | the class pointer contains the address of | |
239 | .B SodObject__class | |
240 | and the offset is zero. | |
a142609c MW |
241 | .PP |
242 | The | |
243 | .B init | |
244 | message is used to initialize a newly allocated instance. | |
245 | .PP | |
246 | This message uses a custom method combination | |
247 | which works like the standard method combination | |
248 | except that default behaviour | |
249 | specific to the receiver's direct class | |
250 | is invoked if no primary or around method overrides. | |
251 | This default behaviour may be invoked multiple times | |
252 | if some method calls on its | |
253 | .B next_method | |
254 | function more than once. | |
255 | .PP | |
256 | This default behaviour is to initialize the instance's slots | |
257 | using the defined slot initializers: | |
258 | each slot is initialized | |
259 | using the most specific applicable initializer, | |
260 | if any. | |
261 | Slots without an initializer | |
262 | are left uninitialized. | |
263 | .PP | |
264 | Slots are initialized in reverse-precedence order | |
265 | of their defining classes; | |
266 | i.e., slots defined by a less specific superclass are initialized | |
267 | earlier than slots defined by a more specific superclass. | |
268 | Slots defined by the same class are initialized in the order in which | |
269 | they appear in the class definition. | |
270 | .PP | |
271 | There are no standard keyword arguments; | |
272 | methods on subclasses are free to | |
273 | introduce their own in the usual way. | |
274 | .PP | |
275 | It is usual to provide complex initialization behaviour as | |
276 | .B after | |
277 | methods. | |
278 | This ensures that slots have been initialized as necessary | |
279 | before the method executes. | |
47de28ae MW |
280 | . |
281 | .SS The SodClass class | |
282 | The | |
283 | .B SodClass | |
284 | class defines no messages, | |
285 | but there are a number of slots. | |
286 | Its only direct superclass is | |
287 | .B SodObject | |
288 | and so (like its superclass) its vtable is trivial. | |
289 | .PP | |
290 | The slots defined are as follows. | |
291 | .TP | |
292 | .B const char *name; | |
293 | A pointer to the class's name. | |
294 | .TP | |
295 | .B const char *nick; | |
296 | A pointer to the class's nickname. | |
297 | .TP | |
298 | .B size_t initsz; | |
299 | The size in bytes required to store an instance of the class. | |
300 | .TP | |
301 | .BI "void *(*imprint)(void *" p ); | |
302 | A pointer to a function: | |
303 | given a pointer | |
304 | .I p | |
305 | to at least | |
306 | .I initsz | |
307 | bytes of appropriately aligned memory, | |
308 | `imprint' this memory it so that it becomes a minimally functional | |
309 | instance of the class: | |
310 | all of the vtable and class pointers are properly initialized, | |
311 | but the slots are left untouched. | |
312 | The function returns its argument | |
313 | .IR p . | |
314 | .TP | |
47de28ae MW |
315 | .B size_t n_supers; |
316 | The number of direct superclasses. | |
317 | (This is zero exactly in the case of | |
318 | .BR SodObject .) | |
319 | .TP | |
320 | .B const SodClass *const *supers; | |
321 | A pointer to an array of | |
322 | .I n_supers | |
323 | pointers to class objects | |
324 | listing the class's direct superclasses, | |
325 | in the order in which they were listed in the class definition. | |
326 | If | |
327 | .I n_supers | |
328 | is zero, | |
329 | then this pointer is null. | |
330 | .TP | |
331 | .B size_t n_cpl; | |
332 | The number of superclasses in the class's class precedence list. | |
333 | .TP | |
334 | .B const SodClass *const *cpl; | |
335 | A pointer to an array of pointers to class objects | |
336 | listing all of the class's superclasses, | |
337 | from most- to least-specific, | |
338 | starting with the class itself, | |
339 | so | |
340 | .IB c ->cls.cpl[0] | |
341 | = | |
342 | .I c | |
343 | for all class objects | |
344 | .IR c . | |
345 | .TP | |
346 | .B const SodClass *link; | |
347 | If the class is a chain head, then this is a null pointer; | |
348 | otherwise it points to the class's distinguished link superclass | |
349 | (which might or might not be a direct superclass). | |
350 | .TP | |
351 | .B const SodClass *head; | |
352 | A pointer to the least-specific class in this class's chain; | |
353 | so | |
354 | .IB c ->cls.head->cls.link | |
355 | is always null, | |
356 | and either | |
357 | .IB c ->cls.link | |
358 | is null | |
359 | (in which case | |
360 | .IB c ->cls.head | |
361 | = | |
362 | .IR c ) | |
363 | or | |
364 | .IB c ->cls.head | |
365 | = | |
366 | .IB c ->cls.link->cls.head \fR. | |
367 | .TP | |
368 | .B size_t level; | |
369 | The number of less specific superclasses in this class's chain. | |
370 | If | |
371 | .IB c ->cls.link | |
372 | is null then | |
373 | .IB c ->cls.level | |
374 | is zero; | |
375 | otherwise | |
376 | .IB c ->cls.level | |
377 | = | |
378 | .IB c ->cls.link->cls.level | |
379 | + 1. | |
380 | .TP | |
381 | .B size_t n_chains; | |
382 | The number of chains formed by the class's superclasses. | |
383 | .TP | |
384 | .B const struct sod_chain *chains; | |
385 | A pointer to an array of | |
386 | .B struct sod_chain | |
387 | structures (see below) describing the class's superclass chains, | |
388 | in decreasing order of specificity of their most specific classes. | |
389 | It is always the case that | |
390 | .IB c ->cls.chains[0].classes[ c ->cls.level] | |
391 | = | |
392 | .IR c . | |
393 | .TP | |
394 | .B size_t off_islots; | |
395 | The offset of the class's | |
396 | .B islots | |
397 | structure relative to its containing | |
398 | .B ichain | |
399 | structure. | |
400 | The class doesn't define any slots if and only if this is zero. | |
401 | (The offset can't be zero because the vtable pointer is at offset zero.) | |
402 | .TP | |
403 | .B size_t islotsz; | |
404 | The size required to store the class's direct slots, | |
405 | i.e., the size of its | |
406 | .B islots | |
407 | structure. | |
408 | The class doesn't define any slots if and only if this is zero. | |
409 | .PP | |
410 | The | |
411 | .B struct sod_chain | |
412 | structure describes an individual chain of superclasses. | |
413 | It has the following members. | |
414 | .TP | |
415 | .B size_t n_classes; | |
416 | The number of classes in the chain. | |
417 | This is always at least one. | |
418 | .TP | |
419 | .B const SodClass *const *classes; | |
420 | A pointer to an array of class pointers | |
421 | listing the classes in the chain from least- to most-specific. | |
422 | So | |
423 | .IB classes [ i ]->cls.head | |
424 | = | |
425 | .IB classes [0] | |
426 | for all | |
427 | 0 \(<= | |
428 | .I i | |
429 | < | |
430 | .IR n_classes , | |
431 | .IB classes [0]->cls.link | |
432 | is always null, | |
433 | and | |
434 | .IB classes [ i ]->cls.link | |
435 | = | |
436 | .IB classes [ "i\fR \- 1" ] | |
437 | if | |
438 | 1 \(<= | |
439 | .I i | |
440 | < | |
441 | .IR n_classes . | |
442 | .TP | |
443 | .B size_t off_ichain; | |
444 | The size of the | |
445 | .B ichain | |
446 | structure for this chain. | |
447 | .TP | |
448 | .B const struct sod_vtable *vt; | |
449 | The vtable for this chain. | |
450 | (It is possible, therefore, to duplicate the behaviour of the | |
451 | .I imprint | |
452 | function by walking the chain structure. | |
453 | The | |
454 | .I imprint | |
455 | function is much faster, though.) | |
456 | .TP | |
457 | .B size_t ichainsz; | |
458 | The size of the | |
459 | .B ichain | |
460 | structure for this chain. | |
461 | . | |
462 | .\"-------------------------------------------------------------------------- | |
463 | .SH CLASS AND VTABLE LAYOUT | |
464 | . | |
465 | The layout algorithms for Sod instances and vtables are nontrivial. | |
466 | They are defined here in full detail, | |
467 | since they're effectively fixed by Sod's ABI compatibility guarantees, | |
468 | so they might as well be documented for the sake of interoperating | |
469 | programs. | |
470 | .PP | |
471 | Unfortunately, the descriptions are rather complicated, | |
472 | and, for the most part not necessary to a working understanding of Sod. | |
473 | The skeleton structure definitions shown should be more than enough | |
474 | for readers attempting to make sense of the generated headers and tables. | |
475 | .PP | |
476 | In the description that follows, | |
477 | uppercase letters vary over class names, | |
478 | while the corresponding lowercase letters indicate the class nicknames. | |
479 | Throughout, we consider a class | |
480 | .I C | |
481 | (therefore with nickname | |
482 | .IR c ). | |
483 | . | |
484 | .SS Generic instance structure | |
485 | The entire state of an instance of | |
486 | .I C | |
487 | is contained in a single structure of type | |
488 | .B struct | |
489 | .IB C __ilayout \fR. | |
490 | .IP | |
491 | .nf | |
492 | .ft B | |
493 | struct \fIC\fB__ilayout { | |
494 | \h'2n'union \fIC\fB__ichainu_\fIh\fB { | |
495 | \h'4n'struct \fIC\fB__ichain_\fIh\fB { | |
496 | \h'6n'const struct \fIC\fB__vt_\fIh\fB *_vt; | |
497 | \h'6n'struct \fIH\fB__islots \fIh\fB; | |
498 | \h'6n'\fR...\fB | |
499 | \h'6n'struct \fIC\fB__islots { | |
500 | \h'8n'\fItype\fB \fIslota\fB; | |
501 | \h'8n'\fR...\fB | |
502 | \h'6n'} \fIc\fB; | |
503 | \h'4n'} \fIc\fB; | |
504 | \h'4n'\fR...\fB | |
505 | \h'4n'struct \fIH\fB__ichain_\fIh\fB \fIh\fB; | |
506 | \h'2n'} \fIh\fB; | |
507 | \h'2n'union \fIB\fB__ichainu_\fIi\fB \fIi\fB; | |
508 | \h'2n'\fR...\fB | |
509 | }; | |
510 | ||
511 | typedef struct \fIC\fB__ichain_\fIh\fB \fIC\fB; | |
512 | .ft P | |
513 | .fi | |
514 | .PP | |
515 | The set of superclasses of | |
516 | .IR C , | |
517 | including itself, | |
518 | can be partitioned into chains | |
519 | by following their distinguished superclass links. | |
520 | (Formally, the chains are the equivalence classes determined by | |
521 | the reflexive, symmetric, transitive closure of | |
522 | the `links to' relation.) | |
523 | Chains are identified by naming their least specific classes; | |
524 | the least specific class in a chain is called the | |
525 | .IR "chain head" . | |
526 | Suppose that the chain head of the chain containing | |
527 | .I C | |
528 | itself is named | |
529 | .I H | |
530 | (though keep in mind that it's possible that | |
531 | .I H | |
532 | is in fact | |
533 | .I C | |
534 | itself.) | |
535 | .PP | |
536 | The | |
537 | .B ilayout | |
538 | structure contains one member for each of | |
539 | .IR C 's | |
540 | superclass chains. | |
541 | The first such member is | |
542 | .IP | |
543 | .B | |
544 | .B union | |
545 | .IB C __ichainu_ h | |
546 | .IB h ; | |
547 | .PP | |
548 | described below; | |
549 | this is followed by members | |
550 | .IP | |
551 | .B union | |
552 | .IB B __ichainu_ i | |
553 | .IB i ; | |
554 | .PP | |
555 | for each other chain, | |
556 | where | |
557 | .I I | |
558 | is the head | |
559 | and | |
560 | .I B | |
561 | the tail (most-specific) class of the chain. | |
562 | The members are in decreasing order | |
563 | of the specificity of the chains' most-specific classes. | |
564 | (Note that all but the first of these unions | |
565 | has already been defined as part of | |
566 | the definition of the corresponding | |
567 | .IR B .) | |
568 | .PP | |
569 | The | |
570 | .B ichainu | |
571 | union contains a member for each class in the chain. | |
572 | The first is | |
573 | .IP | |
574 | .B struct | |
575 | .IB C __ichain_ h | |
576 | .IB c ; | |
577 | .PP | |
578 | and this is followed by corresponding members | |
579 | .IP | |
580 | .B struct | |
581 | .IB A __ichain_ h | |
582 | .IB a ; | |
583 | .PP | |
584 | for each of | |
9c4a4110 MW |
585 | .IR C 's |
586 | superclasses | |
47de28ae MW |
587 | .IR A |
588 | in the same chain in some (unimportant) order. | |
589 | A `pointer to | |
590 | .IR C ' | |
591 | is always assumed | |
592 | (and, indeed, defined in C's type system) | |
593 | to be a pointer to the | |
594 | .B struct | |
595 | .IB C __ichain_ h \fR. | |
596 | .PP | |
597 | The | |
598 | .B ichain | |
599 | structure contains (in order), a pointer | |
600 | .IP | |
601 | .B const | |
602 | .B struct | |
603 | .IB C __vt_ h | |
604 | .B *_vt; | |
605 | .PP | |
606 | followed by a structure | |
607 | .IP | |
608 | .B struct | |
609 | .IB A __islots | |
610 | .IB a ; | |
611 | .PP | |
612 | for each superclass | |
613 | .I A | |
614 | of | |
615 | .IR C | |
616 | in the same chain which defines slots, | |
617 | from least- to most-specific; | |
618 | if | |
619 | .I C | |
620 | defines any slots, | |
621 | then the last member is | |
622 | .IP | |
623 | .B struct | |
624 | .IB C __islots | |
625 | .IB c ; | |
626 | .PP | |
627 | Finally, the | |
628 | .B islots | |
629 | structure simply contains one member for each slot defined by | |
630 | .I C | |
631 | in the order they appear in the class definition. | |
632 | . | |
633 | .SS Generic vtable structure | |
634 | As described above, | |
635 | each | |
636 | .B ichain | |
637 | structure of an instance's storage | |
638 | has a vtable pointer | |
639 | .IP | |
640 | .B const | |
641 | .B struct | |
642 | .IB C __vt_ h | |
643 | .B *_vt; | |
644 | .PP | |
645 | In general, | |
646 | the vtables for the different chains | |
647 | will have | |
648 | .I different | |
649 | structures. | |
650 | .PP | |
651 | The instance layout split neatly into disjoint chains. | |
652 | This is necessary because | |
653 | each | |
654 | .B ichain | |
655 | must have as a prefix the | |
656 | .B ichain | |
9c4a4110 MW |
657 | for each superclass in the same chain, |
658 | and each slot must be stored in exactly one place. | |
47de28ae MW |
659 | The layout of vtables doesn't have this second requirement: |
660 | it doesn't matter that there are | |
661 | multiple method entry pointers | |
662 | for the same effective method | |
663 | as long as they all work correctly. | |
9c4a4110 MW |
664 | Indeed, it's essential that they do, |
665 | because each chain's method entry function | |
666 | will need to apply a different offset to the receiver pointer | |
667 | before invoking the effective method. | |
47de28ae MW |
668 | .PP |
669 | A vtable for a class | |
670 | .I C | |
671 | with chain head | |
672 | .I H | |
673 | has the following general structure. | |
674 | .IP | |
675 | .nf | |
676 | .ft B | |
677 | union \fIC\fB__vtu_\fIh\fB { | |
678 | \h'2n'struct \fIC\fB__vt_\fIh\fB { | |
679 | \h'4n'const \fIP\fB *_class; | |
680 | \h'4n'size_t _base; | |
681 | \h'4n'\fR...\fB | |
682 | \h'4n'const \fIQ\fB *_cls_\fIj\fB; | |
683 | \h'4n'\fR...\fB | |
684 | \h'4n'ptrdiff_t _off_\fIi\fB; | |
685 | \h'4n'\fR...\fB | |
686 | \h'4n'struct \fIC\fB__vtmsgs_\fIa\fB { | |
687 | \h'6n'\fItype\fB (*\fImsg\fB)(\fIC\fB *, \fR...\fB); | |
688 | \h'6n'\fR...\fB | |
689 | \h'4n'} \fIa\fB; | |
690 | \h'4n'\fR...\fB | |
691 | \h'2n'} \fIc\fB; | |
692 | }; | |
693 | ||
694 | extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB; | |
695 | .ft P | |
696 | .fi | |
697 | .PP | |
698 | The outer layer is a | |
47de28ae MW |
699 | .B union |
700 | .IB C __vtu_ h | |
47de28ae MW |
701 | containing a member |
702 | .IP | |
703 | .B struct | |
704 | .IB A __vt_ h | |
705 | .IB a ; | |
706 | .PP | |
707 | for each of | |
708 | .IR C 's | |
709 | superclasses | |
710 | .I A | |
711 | in the same chain, | |
712 | with | |
713 | .I C | |
714 | itself listed first. | |
715 | This is mostly an irrelevant detail, | |
716 | whose purpose is to defend against malicious compilers: | |
717 | pointers are always to one of the inner | |
718 | .B vt | |
719 | structures. | |
720 | It's important only because it's the outer | |
721 | .B vtu | |
722 | union which is exported by name. | |
723 | Specifically, for each chain of | |
724 | .IR C 's | |
725 | superclasses | |
726 | there is an external object | |
727 | .IP | |
728 | .B const union | |
729 | .IB A __vtu_ i | |
730 | .IB C __vtable_ i ; | |
731 | .PP | |
732 | where | |
733 | .I A | |
734 | and | |
735 | .I I | |
736 | are respectively the most and least specific classes in the chain. | |
737 | .PP | |
738 | The first member in the | |
739 | .B vt | |
740 | structure is the | |
741 | .I root class pointer | |
742 | .IP | |
743 | .B const | |
744 | .IR P | |
745 | .B *_class; | |
746 | .PP | |
747 | Among the superclasses of | |
748 | .I C | |
749 | there must be exactly one class | |
750 | .I O | |
751 | which itself has no direct superclasses; | |
752 | this is the | |
753 | .I root superclass | |
754 | of | |
755 | .IR C . | |
756 | (This is a rule enforced by the Sod translator.) | |
757 | The metaclass | |
758 | .I R | |
759 | of | |
9c4a4110 | 760 | .I O |
47de28ae MW |
761 | is then the |
762 | .I root metaclass | |
763 | of | |
764 | .IR C . | |
765 | The | |
766 | .B _class | |
767 | member points to the | |
768 | .B ichain | |
769 | structure of most specific superclass | |
770 | .I P | |
771 | of | |
772 | .I M | |
773 | in the same chain as | |
774 | .IR R . | |
775 | .PP | |
776 | This is followed by the | |
777 | .I base offset | |
778 | .IP | |
779 | .B size_t | |
780 | .B _base; | |
781 | .PP | |
782 | which is simply the offset of the | |
783 | .B ichain | |
784 | structure from the instance base. | |
785 | .PP | |
786 | The rest of the vtable structure is populated | |
787 | by walking the superclass chain containing | |
788 | .I C | |
789 | as follows. | |
790 | For each such superclass | |
791 | .IR B , | |
792 | in increasing order of specificity, | |
793 | walk the class precedence list of | |
794 | .IR B , | |
795 | again starting with its least-specific superclass. | |
796 | (This complex procedure guarantees that | |
797 | the vtable structure for a class is a prefix of | |
798 | the vtable structure for any of its subclasses in the same chain.) | |
799 | .PP | |
800 | So, let | |
801 | .I A | |
802 | be some superclass of | |
803 | .I C | |
804 | which has been encountered during this traversal. | |
805 | .hP \*o | |
806 | Let | |
807 | .I N | |
808 | be the metaclass of | |
809 | .IR A . | |
810 | Examine the superclass chains of | |
811 | .I N | |
812 | in order of decreasing specificity of their most-specific classes. | |
813 | Let | |
814 | .I J | |
815 | be the chain head of such a chain, | |
816 | and let | |
817 | .I Q | |
818 | be the most specific superclass of | |
819 | .I M | |
820 | in the same chain as | |
821 | .IR J . | |
822 | Then, if there is currently no class pointer of type | |
9c4a4110 | 823 | .IR Q , |
47de28ae MW |
824 | then add a member |
825 | .RS | |
826 | .IP | |
827 | .B const | |
828 | .I Q | |
829 | .BI *_cls_ j ; | |
830 | .PP | |
831 | to the vtable | |
832 | pointing to the appropriate | |
833 | .B islots | |
834 | structure within | |
835 | .IR M 's | |
836 | class object. | |
837 | .RE | |
838 | .hP \*o | |
839 | Examine the superclass chains of | |
840 | .I A | |
841 | in order of decreasing specificity of their most-specific classes. | |
842 | Let | |
843 | .I I | |
844 | be the chain head of such a chain. | |
845 | If there is currently no member | |
846 | .BI _off_ i | |
847 | then add a member | |
848 | .RS | |
849 | .IP | |
850 | .B ptrdiff_t | |
851 | .BI _off_ i ; | |
852 | .PP | |
853 | to the vtable, | |
854 | containing the (signed) offset from the | |
855 | .B ichain | |
856 | structure of the chain headed by | |
857 | .I h | |
858 | to that of the chain headed by | |
859 | .I i | |
860 | within the instance's layout. | |
861 | .RE | |
862 | .hP \*o | |
863 | If class | |
864 | .I A | |
865 | defines any messages, | |
866 | and there is currently no member | |
9c4a4110 | 867 | .IR a , |
47de28ae MW |
868 | then add a member |
869 | .RS | |
870 | .IP | |
871 | .B struct | |
872 | .IB C __vtmsgs_ a | |
873 | .IB a ; | |
874 | .PP | |
875 | to the vtable. | |
876 | See below. | |
877 | .RE | |
878 | .PP | |
879 | Finally, the | |
880 | .B vtmsgs | |
881 | structures contain pointers to the effective method entry functions | |
882 | for the messages defined by a superclass. | |
883 | There may be more than one method entry for a message, | |
884 | but all of the entry pointers for a message appear together, | |
885 | and entry pointers for separate messages appear | |
886 | in the order in which the messages are defined. | |
887 | If the receiver class has no applicable primary method for a message | |
888 | then it's usual for the method entry pointer to be null | |
889 | (though, as with a lot of things in Sod, | |
890 | extensions may do something different). | |
891 | .PP | |
892 | For a standard message which takes a fixed number of arguments, | |
893 | defined as | |
894 | .IP | |
895 | .I tr | |
896 | .IB m ( \c | |
897 | .I t1 | |
898 | .IB a1 , | |
899 | .RB ... , | |
900 | .I tn | |
901 | .IB an ); | |
902 | .PP | |
903 | there is always a `main' entry point, | |
904 | .IP | |
905 | .I tr | |
906 | .BI (* m )( \c | |
907 | .I C | |
908 | .BI * me , | |
909 | .I t1 | |
910 | .IB a1 , | |
911 | .RB ... , | |
912 | .I tn | |
913 | .IB an ); | |
914 | .PP | |
915 | For a standard message which takes a variable number of arguments, | |
916 | defined as | |
917 | .IP | |
918 | .I tr | |
919 | .IB m ( \c | |
920 | .I t1 | |
921 | .IB a1 , | |
922 | .RB ... , | |
923 | .I tn | |
924 | .IB an , | |
925 | .B ...); | |
926 | .PP | |
43073476 MW |
927 | or a standard message which takes keyword arguments, |
928 | defined as | |
929 | .IP | |
930 | .I tr | |
931 | .IB m ( \c | |
932 | .I t1 | |
933 | .IB a1 , | |
934 | .RB ... , | |
935 | .I tn | |
936 | .IB an ?\& | |
937 | .IR tn +1 | |
938 | .IR kn +1 | |
939 | .RB [ = | |
940 | .IR dn +1] \c | |
941 | .B , | |
942 | .I tm | |
943 | .I km | |
944 | .RB [ = | |
945 | .IR dm ] \c | |
946 | ); | |
947 | .PP | |
47de28ae MW |
948 | two entry points are defined: |
949 | the usual `main' entry point | |
950 | which accepts a variable number of | |
951 | arguments, | |
952 | and a `valist' entry point | |
953 | which accepts an argument of type | |
954 | .B va_list | |
43073476 MW |
955 | in place of the variable portion of the argument list |
956 | or keywords. | |
47de28ae MW |
957 | .IP |
958 | .I tr | |
959 | .BI (* m )( \c | |
960 | .I C | |
961 | .BI * me , | |
962 | .I t1 | |
963 | .IB a1 , | |
964 | .RB ... , | |
965 | .I tn | |
966 | .IB an , | |
967 | .B ...); | |
968 | .br | |
969 | .I tr | |
970 | .BI (* m __v)( \c | |
971 | .I C | |
972 | .BI * me , | |
973 | .I t1 | |
974 | .IB a1 , | |
975 | .RB ... , | |
976 | .I tn | |
977 | .IB an , | |
978 | .B va_list | |
979 | .IB sod__ap ); | |
980 | . | |
981 | .SS Additional definitions | |
982 | In addition to the instance and vtable structures described above, | |
983 | the following definitions are made for each class | |
984 | .IR C . | |
985 | .PP | |
986 | For each message | |
987 | .I m | |
988 | directly defined by | |
989 | .I C | |
990 | there is a macro definition | |
991 | .IP | |
992 | .B #define | |
993 | .IB C _ m ( me , | |
994 | .RB ... ) | |
995 | .IB me ->_vt-> c . m ( me , | |
996 | .RB ... ) | |
997 | .PP | |
998 | which makes sending the message | |
999 | .I m | |
1000 | to an instance of (any subclass of) | |
1001 | .I C | |
1002 | somewhat less ugly. | |
1003 | If | |
1004 | .I m | |
1005 | takes a variable number of arguments, | |
43073476 | 1006 | or keyword arguments, |
47de28ae MW |
1007 | the macro is more complicated |
1008 | and is only available in compilers advertising C99 support, | |
1009 | but the effect is the same. | |
1010 | For each variable-argument message, | |
1011 | there is also an additional macro | |
1012 | for calling the `valist' entry point. | |
1013 | .IP | |
1014 | .B #define | |
1015 | .IB C _ m __v( me , | |
1016 | .RB ..., | |
1017 | .IB sod__ap ) | |
1018 | .if !t \{\ | |
1019 | \e | |
1020 | .br | |
1021 | \h'4m'\c | |
1022 | .\} | |
1023 | .IB me ->_vt-> c . m __v( me , | |
1024 | .RB ..., | |
1025 | .IB sod__ap ) | |
1026 | .PP | |
1027 | For each proper superclass | |
1028 | .I A | |
1029 | of | |
1030 | .IR C , | |
1031 | there is a macro defined | |
1032 | .IP | |
1033 | .I A | |
1034 | .BI * C __CONV_ a ( C | |
1035 | .BI * _obj ); | |
1036 | .PP | |
1037 | (named in | |
1038 | .IR "upper case" ) | |
1039 | which converts a (static-type) pointer to | |
1040 | .I C | |
1041 | to a pointer to the same actual instance, | |
1042 | but statically typed as a pointer to | |
1043 | .IR A . | |
1044 | This is most useful when | |
1045 | .I A | |
1046 | is not in the same chain as | |
1047 | .I C | |
1048 | since in-chain upcasts are both trivial and rarely needed, | |
1049 | but the full set is defined for the sake of completeness. | |
1050 | .PP | |
1051 | Finally, the class object is defined as | |
1052 | .IP | |
1053 | .B extern const struct | |
1054 | .IB R __ilayout | |
1055 | .IB C __classobj; | |
1056 | .br | |
1057 | .B #define | |
1058 | .IB C __class | |
1059 | .BI (& C __classobj. j . r ) | |
1060 | .PP | |
1061 | The exported symbol | |
1062 | .IB C __classobj | |
1063 | contains the entire class instance. | |
1064 | This is usually rather unwieldy. | |
1065 | The macro | |
1066 | .IB C __class | |
1067 | is usable as a pointer of type | |
1068 | .B const | |
1069 | .I R | |
1070 | .BR * , | |
1071 | where | |
1072 | .I R | |
1073 | is the root metaclass of | |
1074 | .IR C , | |
1075 | i.e., the metaclass of the least specific superclass of | |
1076 | .IR C ; | |
1077 | usually this is | |
1078 | .BR "const SodClass *" . | |
1079 | . | |
1080 | .\"-------------------------------------------------------------------------- | |
1081 | .SH SEE ALSO | |
1082 | .BR sod (3). | |
1083 | . | |
1084 | .\"-------------------------------------------------------------------------- | |
1085 | .SH AUTHOR | |
1086 | Mark Wooding, <mdw@distorted.org.uk> | |
1087 | . | |
1088 | .\"----- That's all, folks -------------------------------------------------- |