chiark / gitweb /
0eb6324cc825110f067bbad9a87602ed93f17ce4
[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 In the following,
739 let
740 .I M
741 be the metaclass of
742 .IR C .
743 .PP
744 The outer layer is a
745 .B union
746 .IB C __vtu_ h
747 containing a member
748 .IP
749 .B struct
750 .IB A __vt_ h
751 .IB a ;
752 .PP
753 for each of
754 .IR C 's
755 superclasses
756 .I A
757 in the same chain,
758 with
759 .I C
760 itself listed first.
761 This is mostly an irrelevant detail,
762 whose purpose is to defend against malicious compilers:
763 pointers are always to one of the inner
764 .B vt
765 structures.
766 It's important only because it's the outer
767 .B vtu
768 union which is exported by name.
769 Specifically, for each chain of
770 .IR C 's
771 superclasses
772 there is an external object
773 .IP
774 .B const union
775 .IB A __vtu_ i
776 .IB C __vtable_ i ;
777 .PP
778 where
779 .I A
780 and
781 .I I
782 are respectively the most and least specific classes in the chain.
783 .PP
784 The first member in the
785 .B vt
786 structure is the
787 .I root class pointer
788 .IP
789 .B const
790 .IR P
791 .B *_class;
792 .PP
793 Among the superclasses of
794 .I C
795 there must be exactly one class
796 .I O
797 which itself has no direct superclasses;
798 this is the
799 .I root superclass
800 of
801 .IR C .
802 (This is a rule enforced by the Sod translator.)
803 The metaclass
804 .I R
805 of
806 .I O
807 is then the
808 .I root metaclass
809 of
810 .IR C .
811 The
812 .B _class
813 member points to the
814 .B ichain
815 structure of most specific superclass
816 .I P
817 of
818 .I M
819 in the same chain as
820 .IR R .
821 .PP
822 This is followed by the
823 .I base offset
824 .IP
825 .B size_t
826 .B _base;
827 .PP
828 which is simply the offset of the
829 .B ichain
830 structure from the instance base.
831 .PP
832 The rest of the vtable structure is populated
833 by walking the superclass chain containing
834 .I C
835 as follows.
836 For each such superclass
837 .IR B ,
838 in increasing order of specificity,
839 walk the class precedence list of
840 .IR B ,
841 again starting with its least-specific superclass.
842 (This complex procedure guarantees that
843 the vtable structure for a class is a prefix of
844 the vtable structure for any of its subclasses in the same chain.)
845 .PP
846 So, let
847 .I A
848 be some superclass of
849 .I C
850 which has been encountered during this traversal.
851 .hP \*o
852 Let
853 .I N
854 be the metaclass of
855 .IR A .
856 Examine the superclass chains of
857 .I N
858 in order of decreasing specificity of their most-specific classes.
859 Let
860 .I J
861 be the chain head of such a chain,
862 and let
863 .I Q
864 be the most specific superclass of
865 .I M
866 in the same chain as
867 .IR J .
868 Then, if there is currently no class pointer of type
869 .IR Q ,
870 then add a member
871 .RS
872 .IP
873 .B const
874 .I Q
875 .BI *_cls_ j ;
876 .PP
877 to the vtable
878 pointing to the appropriate
879 .B islots
880 structure within
881 .IR M 's
882 class object.
883 .RE
884 .hP \*o
885 Examine the superclass chains of
886 .I A
887 in order of decreasing specificity of their most-specific classes.
888 Let
889 .I I
890 be the chain head of such a chain.
891 If there is currently no member
892 .BI _off_ i
893 then add a member
894 .RS
895 .IP
896 .B ptrdiff_t
897 .BI _off_ i ;
898 .PP
899 to the vtable,
900 containing the (signed) offset from the
901 .B ichain
902 structure of the chain headed by
903 .I h
904 to that of the chain headed by
905 .I i
906 within the instance's layout.
907 .RE
908 .hP \*o
909 If class
910 .I A
911 defines any messages,
912 and there is currently no member
913 .IR a ,
914 then add a member
915 .RS
916 .IP
917 .B struct
918 .IB C __vtmsgs_ a
919 .IB a ;
920 .PP
921 to the vtable.
922 See below.
923 .RE
924 .PP
925 Finally, the
926 .B vtmsgs
927 structures contain pointers to the effective method entry functions
928 for the messages defined by a superclass.
929 There may be more than one method entry for a message,
930 but all of the entry pointers for a message appear together,
931 and entry pointers for separate messages appear
932 in the order in which the messages are defined.
933 If the receiver class has no applicable primary method for a message
934 then it's usual for the method entry pointer to be null
935 (though, as with a lot of things in Sod,
936 extensions may do something different).
937 .PP
938 For a standard message which takes a fixed number of arguments,
939 defined as
940 .IP
941 .I tr
942 .IB m ( \c
943 .I t1
944 .IB a1 , 
945 .RB ... ,
946 .I tn
947 .IB an );
948 .PP
949 there is always a `main' entry point,
950 .IP
951 .I tr
952 .BI (* m )( \c
953 .I C
954 .BI * me ,
955 .I t1
956 .IB a1 , 
957 .RB ... ,
958 .I tn
959 .IB an );
960 .PP
961 For a standard message which takes a variable number of arguments,
962 defined as
963 .IP
964 .I tr
965 .IB m ( \c
966 .I t1
967 .IB a1 , 
968 .RB ... ,
969 .I tn
970 .IB an , 
971 .B ...);
972 .PP
973 or a standard message which takes keyword arguments,
974 defined as
975 .IP
976 .I tr
977 .IB m ( \c
978 .I t1
979 .IB a1 ,
980 .RB ... ,
981 .I tn
982 .IB an ?\&
983 .IR tn +1
984 .IR kn +1
985 .RB [ =
986 .IR dn +1] \c
987 .B ,
988 .I tm
989 .I km
990 .RB [ =
991 .IR dm ] \c
992 );
993 .PP
994 two entry points are defined:
995 the usual `main' entry point
996 which accepts a variable number of
997 arguments,
998 and a `valist' entry point
999 which accepts an argument of type
1000 .B va_list
1001 in place of the variable portion of the argument list
1002 or keywords.
1003 .IP
1004 .I tr
1005 .BI (* m )( \c
1006 .I C
1007 .BI * me ,
1008 .I t1
1009 .IB a1 , 
1010 .RB ... ,
1011 .I tn
1012 .IB an ,
1013 .B ...);
1014 .br
1015 .I tr
1016 .BI (* m __v)( \c
1017 .I C
1018 .BI * me ,
1019 .I t1
1020 .IB a1 , 
1021 .RB ... ,
1022 .I tn
1023 .IB an ,
1024 .B va_list
1025 .IB sod__ap );
1026 .
1027 .SS Additional definitions
1028 In addition to the instance and vtable structures described above,
1029 the following definitions are made for each class
1030 .IR C .
1031 .PP
1032 For each message
1033 .I m
1034 directly defined by
1035 .I C
1036 there is a macro definition
1037 .IP
1038 .B #define
1039 .IB C _ m ( me ,
1040 .RB ... )
1041 .IB me ->_vt-> c . m ( me ,
1042 .RB ... )
1043 .PP
1044 which makes sending the message
1045 .I m
1046 to an instance of (any subclass of)
1047 .I C
1048 somewhat less ugly.
1049 If
1050 .I m
1051 takes a variable number of arguments,
1052 or keyword arguments,
1053 the macro is more complicated
1054 and is only available in compilers advertising C99 support,
1055 but the effect is the same.
1056 For each variable-argument message,
1057 there is also an additional macro
1058 for calling the `valist' entry point.
1059 .IP
1060 .B #define
1061 .IB C _ m __v( me ,
1062 .RB ...,
1063 .IB sod__ap )
1064 .if !t \{\
1065 \e
1066 .br
1067 \h'4m'\c
1068 .\}
1069 .IB me ->_vt-> c . m __v( me ,
1070 .RB ...,
1071 .IB sod__ap )
1072 .PP
1073 For each proper superclass
1074 .I A
1075 of
1076 .IR C ,
1077 there is a macro defined
1078 .IP
1079 .I A
1080 .BI * C __CONV_ a ( C
1081 .BI * _obj );
1082 .PP
1083 (named in
1084 .IR "upper case" )
1085 which converts a (static-type) pointer to
1086 .I C
1087 to a pointer to the same actual instance,
1088 but statically typed as a pointer to
1089 .IR A .
1090 This is most useful when
1091 .I A
1092 is not in the same chain as
1093 .I C
1094 since in-chain upcasts are both trivial and rarely needed,
1095 but the full set is defined for the sake of completeness.
1096 .PP
1097 Finally, the class object is defined as
1098 .IP
1099 .B extern const struct
1100 .IB R __ilayout
1101 .IB C __classobj;
1102 .br
1103 .B #define
1104 .IB C __class
1105 .BI (& C __classobj. j . r )
1106 .PP
1107 The exported symbol
1108 .IB C __classobj
1109 contains the entire class instance.
1110 This is usually rather unwieldy.
1111 The macro
1112 .IB C __class
1113 is usable as a pointer of type
1114 .B const
1115 .I R
1116 .BR * ,
1117 where
1118 .I R
1119 is the root metaclass of
1120 .IR C ,
1121 i.e., the metaclass of the least specific superclass of
1122 .IR C ;
1123 usually this is
1124 .BR "const SodClass *" .
1125 .
1126 .\"--------------------------------------------------------------------------
1127 .SH SEE ALSO
1128 .BR sod (3).
1129 .
1130 .\"--------------------------------------------------------------------------
1131 .SH AUTHOR
1132 Mark Wooding, <mdw@distorted.org.uk>
1133 .
1134 .\"----- That's all, folks --------------------------------------------------