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