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