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