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