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