3 .\" Description of the main Sod data structures
5 .\" (c) 2015 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the Sensible Object Design, an object system for C.
12 .\" SOD is free software; you can redistribute it and/or modify
13 .\" it under the terms of the GNU Library General Public License as
14 .\" published by the Free Software Foundation; either version 2 of the
15 .\" License, or (at your option) any later version.
17 .\" SOD is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 .\" GNU Library General Public License for more details.
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.
27 .\" Highlight using terminal escapes, rather than overstriking.
30 .\" String definitions and font selection.
39 .\" .hP TEXT -- start an indented paragraph with TEXT hanging off to the left
42 \h'-\w'\fB\\$1\ \fP'u'\fB\\$1\ \fP\c
45 .\"--------------------------------------------------------------------------
46 .TH sod-structs 3 "8 September 2015" "Straylight/Edgeware" "Sensible Object Design"
49 sod-structs \- main Sod data structures
51 .\"--------------------------------------------------------------------------
57 typedef struct SodObject__ichain_obj SodObject;
58 typedef struct SodClass__ichain_obj SodClass;
61 \h'2n'const struct sod_vtable *_vt;
65 \h'2n'const SodClass *_class;
69 struct SodObject__vt_obj {
70 \h'2n'const SodClass *_class;
74 struct SodObject__ilayout {
76 \h'4n'struct SodObject__ichain_obj {
77 \h'6n'const struct SodObject__vt_obj *_vt;
82 extern const struct SodClass__ilayout SodObject__classobj;
83 #define SodObject__class (&SodObject__classobj.obj.cls)
85 struct SodClass__vt_obj {
86 \h'2n'const SodClass *_class;
90 struct SodObject__ilayout {
92 \h'4n'struct SodClass__ichain_obj {
93 \h'6n'const struct SodClass__vt_obj *_vt;
94 \h'6n'struct SodClass__islots {
95 \h'8n'const char *name;
96 \h'8n'const char *nick;
98 \h'8n'void *(*imprint)(void *\fIp\fB);
99 \h'8n'void *(*init)(void *\fIp\fB);
100 \h'8n'size_t n_supers;
101 \h'8n'const SodClass *const *supers;
103 \h'8n'const SodClass *const *cpl;
104 \h'8n'const SodClass *link;
105 \h'8n'const SodClass *head;
107 \h'8n'size_t n_chains;
108 \h'8n'const struct sod_chain *chains;
109 \h'8n'size_t off_islots;
110 \h'8n'size_t islotsz;
118 \h'2n'size_t n_classes;
119 \h'2n'const SodClass *const *classes;
120 \h'2n'size_t off_ichain;
121 \h'2n'const struct sod_vtable *vt;
122 \h'2n'size_t ichainsz;
125 extern const struct SodClass__ilayout SodClass__classobj;
126 #define SodClass__class (&SodClass__classobj.obj.cls)
130 .\"--------------------------------------------------------------------------
133 This page describes the structure and layout
134 of standard Sod objects, classes and associated metadata.
135 Note that Sod's object system is very flexible
136 and it's possible for an extension
137 to define a new root class
138 which works very differently from the standard
142 .\"--------------------------------------------------------------------------
143 .SH COMMON INSTANCE STRUCTURE
146 a pointer to an instance actually points to an
148 structure within the instances overall layout structure.
150 Instance chains contain slots and vtable pointers,
152 All instances have the basic structure of a
153 .BR "struct sod_instance" ,
154 which has the following members.
156 .B "const struct sod_vtable *_vt"
159 which has the basic structure of a
160 .BR "struct sod_vtable" ,
163 A vtable contains static metadata needed
164 for efficient conversions and
166 and pointers to the instance's class.
167 Each chain points to a different vtable
168 All vtables have the basic structure of a
169 .BR "struct sod_vtable" ,
170 which has the following members.
172 .B "const SodClass *_class"
173 A pointer to the instance's class object.
176 The offset of this chain structure
177 above the start of the overall instance layout, in bytes.
180 from the instance chain pointer
181 finds the layout base address.
183 .\"--------------------------------------------------------------------------
184 .SH BUILT-IN ROOT OBJECTS
186 This section describes the built-in classes
190 which are the standard roots of the inheritance and metaclass graphs
194 has no direct superclasses,
197 is its own metaclass.
198 It is not possible to define root classes in module files
199 because of circularities:
208 Extensions can define additional root classes,
210 and not really to be recommended.
212 .SS The SodObject class
215 class defines no slots or messages.
218 has no direct superclasses,
219 there is only one chain,
220 and no inherited slots or messages,
221 so the single chain contains only a vtable pointer.
223 Since there are no messages,
226 also has only one chain,
227 the vtable contains only the standard class pointer and offset-to-base
229 In an actual instance of
231 (why would you want one?)
232 the class pointer contains the address of
234 and the offset is zero.
236 .SS The SodClass class
239 class defines no messages,
240 but there are a number of slots.
241 Its only direct superclass is
243 and so (like its superclass) its vtable is trivial.
245 The slots defined are as follows.
248 A pointer to the class's name.
251 A pointer to the class's nickname.
254 The size in bytes required to store an instance of the class.
256 .BI "void *(*imprint)(void *" p );
257 A pointer to a function:
262 bytes of appropriately aligned memory,
263 `imprint' this memory it so that it becomes a minimally functional
264 instance of the class:
265 all of the vtable and class pointers are properly initialized,
266 but the slots are left untouched.
267 The function returns its argument
270 .BI "void *(*init)(void *" p );
271 A pointer to a function:
274 to an imprinted instance,
275 initialize all of its slots for which initializers are defined.
276 Other slots are left untouched.
277 The function returns its argument
281 The number of direct superclasses.
282 (This is zero exactly in the case of
285 .B const SodClass *const *supers;
286 A pointer to an array of
288 pointers to class objects
289 listing the class's direct superclasses,
290 in the order in which they were listed in the class definition.
294 then this pointer is null.
297 The number of superclasses in the class's class precedence list.
299 .B const SodClass *const *cpl;
300 A pointer to an array of pointers to class objects
301 listing all of the class's superclasses,
302 from most- to least-specific,
303 starting with the class itself,
308 for all class objects
311 .B const SodClass *link;
312 If the class is a chain head, then this is a null pointer;
313 otherwise it points to the class's distinguished link superclass
314 (which might or might not be a direct superclass).
316 .B const SodClass *head;
317 A pointer to the least-specific class in this class's chain;
319 .IB c ->cls.head->cls.link
331 .IB c ->cls.link->cls.head \fR.
334 The number of less specific superclasses in this class's chain.
343 .IB c ->cls.link->cls.level
347 The number of chains formed by the class's superclasses.
349 .B const struct sod_chain *chains;
350 A pointer to an array of
352 structures (see below) describing the class's superclass chains,
353 in decreasing order of specificity of their most specific classes.
354 It is always the case that
355 .IB c ->cls.chains[0].classes[ c ->cls.level]
359 .B size_t off_islots;
360 The offset of the class's
362 structure relative to its containing
365 The class doesn't define any slots if and only if this is zero.
366 (The offset can't be zero because the vtable pointer is at offset zero.)
369 The size required to store the class's direct slots,
370 i.e., the size of its
373 The class doesn't define any slots if and only if this is zero.
377 structure describes an individual chain of superclasses.
378 It has the following members.
381 The number of classes in the chain.
382 This is always at least one.
384 .B const SodClass *const *classes;
385 A pointer to an array of class pointers
386 listing the classes in the chain from least- to most-specific.
388 .IB classes [ i ]->cls.head
396 .IB classes [0]->cls.link
399 .IB classes [ i ]->cls.link
401 .IB classes [ "i\fR \- 1" ]
408 .B size_t off_ichain;
411 structure for this chain.
413 .B const struct sod_vtable *vt;
414 The vtable for this chain.
415 (It is possible, therefore, to duplicate the behaviour of the
417 function by walking the chain structure.
420 function is much faster, though.)
425 structure for this chain.
427 .\"--------------------------------------------------------------------------
428 .SH CLASS AND VTABLE LAYOUT
430 The layout algorithms for Sod instances and vtables are nontrivial.
431 They are defined here in full detail,
432 since they're effectively fixed by Sod's ABI compatibility guarantees,
433 so they might as well be documented for the sake of interoperating
436 Unfortunately, the descriptions are rather complicated,
437 and, for the most part not necessary to a working understanding of Sod.
438 The skeleton structure definitions shown should be more than enough
439 for readers attempting to make sense of the generated headers and tables.
441 In the description that follows,
442 uppercase letters vary over class names,
443 while the corresponding lowercase letters indicate the class nicknames.
444 Throughout, we consider a class
446 (therefore with nickname
449 .SS Generic instance structure
450 The entire state of an instance of
452 is contained in a single structure of type
458 struct \fIC\fB__ilayout {
459 \h'2n'union \fIC\fB__ichainu_\fIh\fB {
460 \h'4n'struct \fIC\fB__ichain_\fIh\fB {
461 \h'6n'const struct \fIC\fB__vt_\fIh\fB *_vt;
462 \h'6n'struct \fIH\fB__islots \fIh\fB;
464 \h'6n'struct \fIC\fB__islots {
465 \h'8n'\fItype\fB \fIslota\fB;
470 \h'4n'struct \fIH\fB__ichain_\fIh\fB \fIh\fB;
472 \h'2n'union \fIB\fB__ichainu_\fIi\fB \fIi\fB;
476 typedef struct \fIC\fB__ichain_\fIh\fB \fIC\fB;
480 The set of superclasses of
483 can be partitioned into chains
484 by following their distinguished superclass links.
485 (Formally, the chains are the equivalence classes determined by
486 the reflexive, symmetric, transitive closure of
487 the `links to' relation.)
488 Chains are identified by naming their least specific classes;
489 the least specific class in a chain is called the
491 Suppose that the chain head of the chain containing
495 (though keep in mind that it's possible that
503 structure contains one member for each of
506 The first such member is
514 this is followed by members
520 for each other chain,
526 the tail (most-specific) class of the chain.
527 The members are in decreasing order
528 of the specificity of the chains' most-specific classes.
529 (Note that all but the first of these unions
530 has already been defined as part of
531 the definition of the corresponding
536 union contains a member for each class in the chain.
543 and this is followed by corresponding members
553 in the same chain in some (unimportant) order.
557 (and, indeed, defined in C's type system)
558 to be a pointer to the
560 .IB C __ichain_ h \fR.
564 structure contains (in order), a pointer
571 followed by a structure
581 in the same chain which defines slots,
582 from least- to most-specific;
586 then the last member is
594 structure simply contains one member for each slot defined by
596 in the order they appear in the class definition.
598 .SS Generic vtable structure
602 structure of an instance's storage
611 the vtables for the different chains
616 The instance layout split neatly into disjoint chains.
617 This is necessary because
620 must have as a prefix the
622 for each superclass in the same chain,
623 and each slot must be stored in exactly one place.
624 The layout of vtables doesn't have this second requirement:
625 it doesn't matter that there are
626 multiple method entry pointers
627 for the same effective method
628 as long as they all work correctly.
629 Indeed, it's essential that they do,
630 because each chain's method entry function
631 will need to apply a different offset to the receiver pointer
632 before invoking the effective method.
638 has the following general structure.
642 union \fIC\fB__vtu_\fIh\fB {
643 \h'2n'struct \fIC\fB__vt_\fIh\fB {
644 \h'4n'const \fIP\fB *_class;
647 \h'4n'const \fIQ\fB *_cls_\fIj\fB;
649 \h'4n'ptrdiff_t _off_\fIi\fB;
651 \h'4n'struct \fIC\fB__vtmsgs_\fIa\fB {
652 \h'6n'\fItype\fB (*\fImsg\fB)(\fIC\fB *, \fR...\fB);
659 extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB;
680 This is mostly an irrelevant detail,
681 whose purpose is to defend against malicious compilers:
682 pointers are always to one of the inner
685 It's important only because it's the outer
687 union which is exported by name.
688 Specifically, for each chain of
691 there is an external object
701 are respectively the most and least specific classes in the chain.
703 The first member in the
706 .I root class pointer
712 Among the superclasses of
714 there must be exactly one class
716 which itself has no direct superclasses;
721 (This is a rule enforced by the Sod translator.)
734 structure of most specific superclass
741 This is followed by the
747 which is simply the offset of the
749 structure from the instance base.
751 The rest of the vtable structure is populated
752 by walking the superclass chain containing
755 For each such superclass
757 in increasing order of specificity,
758 walk the class precedence list of
760 again starting with its least-specific superclass.
761 (This complex procedure guarantees that
762 the vtable structure for a class is a prefix of
763 the vtable structure for any of its subclasses in the same chain.)
767 be some superclass of
769 which has been encountered during this traversal.
775 Examine the superclass chains of
777 in order of decreasing specificity of their most-specific classes.
780 be the chain head of such a chain,
783 be the most specific superclass of
787 Then, if there is currently no class pointer of type
797 pointing to the appropriate
804 Examine the superclass chains of
806 in order of decreasing specificity of their most-specific classes.
809 be the chain head of such a chain.
810 If there is currently no member
819 containing the (signed) offset from the
821 structure of the chain headed by
823 to that of the chain headed by
825 within the instance's layout.
830 defines any messages,
831 and there is currently no member
846 structures contain pointers to the effective method entry functions
847 for the messages defined by a superclass.
848 There may be more than one method entry for a message,
849 but all of the entry pointers for a message appear together,
850 and entry pointers for separate messages appear
851 in the order in which the messages are defined.
852 If the receiver class has no applicable primary method for a message
853 then it's usual for the method entry pointer to be null
854 (though, as with a lot of things in Sod,
855 extensions may do something different).
857 For a standard message which takes a fixed number of arguments,
868 there is always a `main' entry point,
880 For a standard message which takes a variable number of arguments,
892 or a standard message which takes keyword arguments,
913 two entry points are defined:
914 the usual `main' entry point
915 which accepts a variable number of
917 and a `valist' entry point
918 which accepts an argument of type
920 in place of the variable portion of the argument list
946 .SS Additional definitions
947 In addition to the instance and vtable structures described above,
948 the following definitions are made for each class
955 there is a macro definition
960 .IB me ->_vt-> c . m ( me ,
963 which makes sending the message
965 to an instance of (any subclass of)
970 takes a variable number of arguments,
971 or keyword arguments,
972 the macro is more complicated
973 and is only available in compilers advertising C99 support,
974 but the effect is the same.
975 For each variable-argument message,
976 there is also an additional macro
977 for calling the `valist' entry point.
988 .IB me ->_vt-> c . m __v( me ,
992 For each proper superclass
996 there is a macro defined
999 .BI * C __CONV_ a ( C
1004 which converts a (static-type) pointer to
1006 to a pointer to the same actual instance,
1007 but statically typed as a pointer to
1009 This is most useful when
1011 is not in the same chain as
1013 since in-chain upcasts are both trivial and rarely needed,
1014 but the full set is defined for the sake of completeness.
1016 Finally, the class object is defined as
1018 .B extern const struct
1024 .BI (& C __classobj. j . r )
1028 contains the entire class instance.
1029 This is usually rather unwieldy.
1032 is usable as a pointer of type
1038 is the root metaclass of
1040 i.e., the metaclass of the least specific superclass of
1043 .BR "const SodClass *" .
1045 .\"--------------------------------------------------------------------------
1049 .\"--------------------------------------------------------------------------
1051 Mark Wooding, <mdw@distorted.org.uk>
1053 .\"----- That's all, folks --------------------------------------------------