lib/sod-structs.3: Fix misnamed `struct SodClass__ilayout'.
[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
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
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.
629A `pointer to
630.IR C '
631is always assumed
632(and, indeed, defined in C's type system)
633to be a pointer to the
634.B struct
635.IB C __ichain_ h \fR.
636.PP
637The
638.B ichain
639structure contains (in order), a pointer
640.IP
641.B const
642.B struct
643.IB C __vt_ h
644.B *_vt;
645.PP
646followed by a structure
647.IP
648.B struct
649.IB A __islots
650.IB a ;
651.PP
652for each superclass
653.I A
654of
655.IR C
656in the same chain which defines slots,
657from least- to most-specific;
658if
659.I C
660defines any slots,
661then the last member is
662.IP
663.B struct
664.IB C __islots
665.IB c ;
666.PP
667Finally, the
668.B islots
669structure simply contains one member for each slot defined by
670.I C
671in the order they appear in the class definition.
672.
673.SS Generic vtable structure
674As described above,
675each
676.B ichain
677structure of an instance's storage
678has a vtable pointer
679.IP
680.B const
681.B struct
682.IB C __vt_ h
683.B *_vt;
684.PP
685In general,
686the vtables for the different chains
687will have
688.I different
689structures.
690.PP
691The instance layout split neatly into disjoint chains.
692This is necessary because
693each
694.B ichain
695must have as a prefix the
696.B ichain
9c4a4110
MW
697for each superclass in the same chain,
698and each slot must be stored in exactly one place.
47de28ae
MW
699The layout of vtables doesn't have this second requirement:
700it doesn't matter that there are
701multiple method entry pointers
702for the same effective method
703as long as they all work correctly.
9c4a4110
MW
704Indeed, it's essential that they do,
705because each chain's method entry function
706will need to apply a different offset to the receiver pointer
707before invoking the effective method.
47de28ae
MW
708.PP
709A vtable for a class
710.I C
711with chain head
712.I H
713has the following general structure.
714.IP
715.nf
716.ft B
717union \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
734extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB;
735.ft P
736.fi
737.PP
fa388683
MW
738In the following,
739let
740.I M
741be the metaclass of
742.IR C .
743.PP
47de28ae 744The outer layer is a
47de28ae
MW
745.B union
746.IB C __vtu_ h
47de28ae
MW
747containing a member
748.IP
749.B struct
750.IB A __vt_ h
751.IB a ;
752.PP
753for each of
754.IR C 's
755superclasses
756.I A
757in the same chain,
758with
759.I C
760itself listed first.
761This is mostly an irrelevant detail,
762whose purpose is to defend against malicious compilers:
763pointers are always to one of the inner
764.B vt
765structures.
766It's important only because it's the outer
767.B vtu
768union which is exported by name.
769Specifically, for each chain of
770.IR C 's
771superclasses
772there is an external object
773.IP
774.B const union
775.IB A __vtu_ i
776.IB C __vtable_ i ;
777.PP
778where
779.I A
780and
781.I I
782are respectively the most and least specific classes in the chain.
783.PP
784The first member in the
785.B vt
786structure is the
787.I root class pointer
788.IP
789.B const
790.IR P
791.B *_class;
792.PP
793Among the superclasses of
794.I C
795there must be exactly one class
796.I O
797which itself has no direct superclasses;
798this is the
799.I root superclass
800of
801.IR C .
802(This is a rule enforced by the Sod translator.)
803The metaclass
804.I R
805of
9c4a4110 806.I O
47de28ae
MW
807is then the
808.I root metaclass
809of
810.IR C .
811The
812.B _class
813member points to the
814.B ichain
815structure of most specific superclass
816.I P
817of
818.I M
819in the same chain as
820.IR R .
821.PP
822This is followed by the
823.I base offset
824.IP
825.B size_t
826.B _base;
827.PP
828which is simply the offset of the
829.B ichain
830structure from the instance base.
831.PP
832The rest of the vtable structure is populated
833by walking the superclass chain containing
834.I C
835as follows.
836For each such superclass
837.IR B ,
838in increasing order of specificity,
839walk the class precedence list of
840.IR B ,
841again starting with its least-specific superclass.
842(This complex procedure guarantees that
843the vtable structure for a class is a prefix of
844the vtable structure for any of its subclasses in the same chain.)
845.PP
846So, let
847.I A
848be some superclass of
849.I C
850which has been encountered during this traversal.
851.hP \*o
852Let
853.I N
854be the metaclass of
855.IR A .
856Examine the superclass chains of
857.I N
858in order of decreasing specificity of their most-specific classes.
859Let
860.I J
861be the chain head of such a chain,
862and let
863.I Q
864be the most specific superclass of
865.I M
866in the same chain as
867.IR J .
868Then, if there is currently no class pointer of type
9c4a4110 869.IR Q ,
47de28ae
MW
870then add a member
871.RS
872.IP
873.B const
874.I Q
875.BI *_cls_ j ;
876.PP
877to the vtable
878pointing to the appropriate
879.B islots
880structure within
881.IR M 's
882class object.
883.RE
884.hP \*o
885Examine the superclass chains of
886.I A
887in order of decreasing specificity of their most-specific classes.
888Let
889.I I
890be the chain head of such a chain.
891If there is currently no member
892.BI _off_ i
893then add a member
894.RS
895.IP
896.B ptrdiff_t
897.BI _off_ i ;
898.PP
899to the vtable,
900containing the (signed) offset from the
901.B ichain
902structure of the chain headed by
903.I h
904to that of the chain headed by
905.I i
906within the instance's layout.
907.RE
908.hP \*o
909If class
910.I A
911defines any messages,
912and there is currently no member
9c4a4110 913.IR a ,
47de28ae
MW
914then add a member
915.RS
916.IP
917.B struct
918.IB C __vtmsgs_ a
919.IB a ;
920.PP
921to the vtable.
922See below.
923.RE
924.PP
925Finally, the
926.B vtmsgs
927structures contain pointers to the effective method entry functions
928for the messages defined by a superclass.
929There may be more than one method entry for a message,
930but all of the entry pointers for a message appear together,
931and entry pointers for separate messages appear
932in the order in which the messages are defined.
933If the receiver class has no applicable primary method for a message
934then it's usual for the method entry pointer to be null
935(though, as with a lot of things in Sod,
936extensions may do something different).
937.PP
938For a standard message which takes a fixed number of arguments,
939defined 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
949there 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
961For a standard message which takes a variable number of arguments,
962defined 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
43073476
MW
973or a standard message which takes keyword arguments,
974defined 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
47de28ae
MW
994two entry points are defined:
995the usual `main' entry point
996which accepts a variable number of
997arguments,
998and a `valist' entry point
999which accepts an argument of type
1000.B va_list
43073476
MW
1001in place of the variable portion of the argument list
1002or keywords.
47de28ae
MW
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
1028In addition to the instance and vtable structures described above,
1029the following definitions are made for each class
1030.IR C .
1031.PP
1032For each message
1033.I m
1034directly defined by
1035.I C
1036there 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
1044which makes sending the message
1045.I m
1046to an instance of (any subclass of)
1047.I C
1048somewhat less ugly.
1049If
1050.I m
1051takes a variable number of arguments,
43073476 1052or keyword arguments,
47de28ae
MW
1053the macro is more complicated
1054and is only available in compilers advertising C99 support,
1055but the effect is the same.
1056For each variable-argument message,
1057there is also an additional macro
1058for 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
1073For each proper superclass
1074.I A
1075of
1076.IR C ,
1077there 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" )
1085which converts a (static-type) pointer to
1086.I C
1087to a pointer to the same actual instance,
1088but statically typed as a pointer to
1089.IR A .
1090This is most useful when
1091.I A
1092is not in the same chain as
1093.I C
1094since in-chain upcasts are both trivial and rarely needed,
1095but the full set is defined for the sake of completeness.
1096.PP
1097Finally, 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
1107The exported symbol
1108.IB C __classobj
1109contains the entire class instance.
1110This is usually rather unwieldy.
1111The macro
1112.IB C __class
1113is usable as a pointer of type
1114.B const
1115.I R
1116.BR * ,
1117where
1118.I R
1119is the root metaclass of
1120.IR C ,
1121i.e., the metaclass of the least specific superclass of
1122.IR C ;
1123usually this is
1124.BR "const SodClass *" .
1125.
1126.\"--------------------------------------------------------------------------
1127.SH SEE ALSO
1128.BR sod (3).
1129.
1130.\"--------------------------------------------------------------------------
1131.SH AUTHOR
1132Mark Wooding, <mdw@distorted.org.uk>
1133.
1134.\"----- That's all, folks --------------------------------------------------