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;
99 \h'8n'void *(*imprint)(void *\fIp\fB);
100 \h'8n'void *(*init)(void *\fIp\fB);
101 \h'8n'size_t n_supers;
102 \h'8n'const SodClass *const *supers;
104 \h'8n'const SodClass *const *cpl;
105 \h'8n'const SodClass *link;
106 \h'8n'const SodClass *head;
108 \h'8n'size_t n_chains;
109 \h'8n'const struct sod_chain *chains;
110 \h'8n'size_t off_islots;
111 \h'8n'size_t islotsz;
119 \h'2n'size_t n_classes;
120 \h'2n'const SodClass *const *classes;
121 \h'2n'size_t off_ichain;
122 \h'2n'const struct sod_vtable *vt;
123 \h'2n'size_t ichainsz;
126 extern const struct SodClass__ilayout SodClass__classobj;
127 #define SodClass__class (&SodClass__classobj.obj.cls)
131 .\"--------------------------------------------------------------------------
134 This page describes the structure and layout
135 of standard Sod objects, classes and associated metadata.
136 Note that Sod's object system is very flexible
137 and it's possible for an extension
138 to define a new root class
139 which works very differently from the standard
143 .\"--------------------------------------------------------------------------
144 .SH COMMON INSTANCE STRUCTURE
147 a pointer to an instance actually points to an
149 structure within the instances overall layout structure.
151 Instance chains contain slots and vtable pointers,
153 All instances have the basic structure of a
154 .BR "struct sod_instance" ,
155 which has the following members.
157 .B "const struct sod_vtable *_vt"
160 which has the basic structure of a
161 .BR "struct sod_vtable" ,
164 A vtable contains static metadata needed
165 for efficient conversions and
167 and pointers to the instance's class.
168 Each chain points to a different vtable
169 All vtables have the basic structure of a
170 .BR "struct sod_vtable" ,
171 which has the following members.
173 .B "const SodClass *_class"
174 A pointer to the instance's class object.
177 The offset of this chain structure
178 above the start of the overall instance layout, in bytes.
181 from the instance chain pointer
182 finds the layout base address.
184 .\"--------------------------------------------------------------------------
185 .SH BUILT-IN ROOT OBJECTS
187 This section describes the built-in classes
191 which are the standard roots of the inheritance and metaclass graphs
195 has no direct superclasses,
198 is its own metaclass.
199 It is not possible to define root classes in module files
200 because of circularities:
209 Extensions can define additional root classes,
211 and not really to be recommended.
213 .SS The SodObject class
216 class defines no slots or messages.
219 has no direct superclasses,
220 there is only one chain,
221 and no inherited slots or messages,
222 so the single chain contains only a vtable pointer.
224 Since there are no messages,
227 also has only one chain,
228 the vtable contains only the standard class pointer and offset-to-base
230 In an actual instance of
232 (why would you want one?)
233 the class pointer contains the address of
235 and the offset is zero.
237 .SS The SodClass class
240 class defines no messages,
241 but there are a number of slots.
242 Its only direct superclass is
244 and so (like its superclass) its vtable is trivial.
246 The slots defined are as follows.
249 A pointer to the class's name.
252 A pointer to the class's nickname.
255 The size in bytes required to store an instance of the class.
258 A sufficient alignment for the class's instance storage.
260 .BI "void *(*imprint)(void *" p );
261 A pointer to a function:
266 bytes of appropriately aligned memory,
267 `imprint' this memory it so that it becomes a minimally functional
268 instance of the class:
269 all of the vtable and class pointers are properly initialized,
270 but the slots are left untouched.
271 The function returns its argument
274 .BI "void *(*init)(void *" p );
275 A pointer to a function:
280 bytes of appropriately aligned memory,
281 initialize an instance of the class in it:
282 all of the vtable and class pointers are initialized,
283 as are slots for which initializers are defined.
284 Other slots are left untouched.
285 The function returns its argument
289 The number of direct superclasses.
290 (This is zero exactly in the case of
293 .B const SodClass *const *supers;
294 A pointer to an array of
296 pointers to class objects
297 listing the class's direct superclasses,
298 in the order in which they were listed in the class definition.
302 then this pointer is null.
305 The number of superclasses in the class's class precedence list.
307 .B const SodClass *const *cpl;
308 A pointer to an array of pointers to class objects
309 listing all of the class's superclasses,
310 from most- to least-specific,
311 starting with the class itself,
316 for all class objects
319 .B const SodClass *link;
320 If the class is a chain head, then this is a null pointer;
321 otherwise it points to the class's distinguished link superclass
322 (which might or might not be a direct superclass).
324 .B const SodClass *head;
325 A pointer to the least-specific class in this class's chain;
327 .IB c ->cls.head->cls.link
339 .IB c ->cls.link->cls.head \fR.
342 The number of less specific superclasses in this class's chain.
351 .IB c ->cls.link->cls.level
355 The number of chains formed by the class's superclasses.
357 .B const struct sod_chain *chains;
358 A pointer to an array of
360 structures (see below) describing the class's superclass chains,
361 in decreasing order of specificity of their most specific classes.
362 It is always the case that
363 .IB c ->cls.chains[0].classes[ c ->cls.level]
367 .B size_t off_islots;
368 The offset of the class's
370 structure relative to its containing
373 The class doesn't define any slots if and only if this is zero.
374 (The offset can't be zero because the vtable pointer is at offset zero.)
377 The size required to store the class's direct slots,
378 i.e., the size of its
381 The class doesn't define any slots if and only if this is zero.
385 structure describes an individual chain of superclasses.
386 It has the following members.
389 The number of classes in the chain.
390 This is always at least one.
392 .B const SodClass *const *classes;
393 A pointer to an array of class pointers
394 listing the classes in the chain from least- to most-specific.
396 .IB classes [ i ]->cls.head
404 .IB classes [0]->cls.link
407 .IB classes [ i ]->cls.link
409 .IB classes [ "i\fR \- 1" ]
416 .B size_t off_ichain;
419 structure for this chain.
421 .B const struct sod_vtable *vt;
422 The vtable for this chain.
423 (It is possible, therefore, to duplicate the behaviour of the
425 function by walking the chain structure.
428 function is much faster, though.)
433 structure for this chain.
435 .\"--------------------------------------------------------------------------
436 .SH CLASS AND VTABLE LAYOUT
438 The layout algorithms for Sod instances and vtables are nontrivial.
439 They are defined here in full detail,
440 since they're effectively fixed by Sod's ABI compatibility guarantees,
441 so they might as well be documented for the sake of interoperating
444 Unfortunately, the descriptions are rather complicated,
445 and, for the most part not necessary to a working understanding of Sod.
446 The skeleton structure definitions shown should be more than enough
447 for readers attempting to make sense of the generated headers and tables.
449 In the description that follows,
450 uppercase letters vary over class names,
451 while the corresponding lowercase letters indicate the class nicknames.
452 Throughout, we consider a class
454 (therefore with nickname
457 .SS Generic instance structure
458 The entire state of an instance of
460 is contained in a single structure of type
466 struct \fIC\fB__ilayout {
467 \h'2n'union \fIC\fB__ichainu_\fIh\fB {
468 \h'4n'struct \fIC\fB__ichain_\fIh\fB {
469 \h'6n'const struct \fIC\fB__vt_\fIh\fB *_vt;
470 \h'6n'struct \fIH\fB__islots \fIh\fB;
472 \h'6n'struct \fIC\fB__islots {
473 \h'8n'\fItype\fB \fIslota\fB;
478 \h'4n'struct \fIH\fB__ichain_\fIh\fB \fIh\fB;
480 \h'2n'union \fIB\fB__ichainu_\fIi\fB \fIi\fB;
484 typedef struct \fIC\fB__ichain_\fIh\fB \fIC\fB;
488 The set of superclasses of
491 can be partitioned into chains
492 by following their distinguished superclass links.
493 (Formally, the chains are the equivalence classes determined by
494 the reflexive, symmetric, transitive closure of
495 the `links to' relation.)
496 Chains are identified by naming their least specific classes;
497 the least specific class in a chain is called the
499 Suppose that the chain head of the chain containing
503 (though keep in mind that it's possible that
511 structure contains one member for each of
514 The first such member is
522 this is followed by members
528 for each other chain,
534 the tail (most-specific) class of the chain.
535 The members are in decreasing order
536 of the specificity of the chains' most-specific classes.
537 (Note that all but the first of these unions
538 has already been defined as part of
539 the definition of the corresponding
544 union contains a member for each class in the chain.
551 and this is followed by corresponding members
561 in the same chain in some (unimportant) order.
565 (and, indeed, defined in C's type system)
566 to be a pointer to the
568 .IB C __ichain_ h \fR.
572 structure contains (in order), a pointer
579 followed by a structure
589 in the same chain which defines slots,
590 from least- to most-specific;
594 then the last member is
602 structure simply contains one member for each slot defined by
604 in the order they appear in the class definition.
606 .SS Generic vtable structure
610 structure of an instance's storage
619 the vtables for the different chains
624 The instance layout split neatly into disjoint chains.
625 This is necessary because
628 must have as a prefix the
630 for each superclass in the same chain,
631 and each slot must be stored in exactly one place.
632 The layout of vtables doesn't have this second requirement:
633 it doesn't matter that there are
634 multiple method entry pointers
635 for the same effective method
636 as long as they all work correctly.
637 Indeed, it's essential that they do,
638 because each chain's method entry function
639 will need to apply a different offset to the receiver pointer
640 before invoking the effective method.
646 has the following general structure.
650 union \fIC\fB__vtu_\fIh\fB {
651 \h'2n'struct \fIC\fB__vt_\fIh\fB {
652 \h'4n'const \fIP\fB *_class;
655 \h'4n'const \fIQ\fB *_cls_\fIj\fB;
657 \h'4n'ptrdiff_t _off_\fIi\fB;
659 \h'4n'struct \fIC\fB__vtmsgs_\fIa\fB {
660 \h'6n'\fItype\fB (*\fImsg\fB)(\fIC\fB *, \fR...\fB);
667 extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB;
688 This is mostly an irrelevant detail,
689 whose purpose is to defend against malicious compilers:
690 pointers are always to one of the inner
693 It's important only because it's the outer
695 union which is exported by name.
696 Specifically, for each chain of
699 there is an external object
709 are respectively the most and least specific classes in the chain.
711 The first member in the
714 .I root class pointer
720 Among the superclasses of
722 there must be exactly one class
724 which itself has no direct superclasses;
729 (This is a rule enforced by the Sod translator.)
742 structure of most specific superclass
749 This is followed by the
755 which is simply the offset of the
757 structure from the instance base.
759 The rest of the vtable structure is populated
760 by walking the superclass chain containing
763 For each such superclass
765 in increasing order of specificity,
766 walk the class precedence list of
768 again starting with its least-specific superclass.
769 (This complex procedure guarantees that
770 the vtable structure for a class is a prefix of
771 the vtable structure for any of its subclasses in the same chain.)
775 be some superclass of
777 which has been encountered during this traversal.
783 Examine the superclass chains of
785 in order of decreasing specificity of their most-specific classes.
788 be the chain head of such a chain,
791 be the most specific superclass of
795 Then, if there is currently no class pointer of type
805 pointing to the appropriate
812 Examine the superclass chains of
814 in order of decreasing specificity of their most-specific classes.
817 be the chain head of such a chain.
818 If there is currently no member
827 containing the (signed) offset from the
829 structure of the chain headed by
831 to that of the chain headed by
833 within the instance's layout.
838 defines any messages,
839 and there is currently no member
854 structures contain pointers to the effective method entry functions
855 for the messages defined by a superclass.
856 There may be more than one method entry for a message,
857 but all of the entry pointers for a message appear together,
858 and entry pointers for separate messages appear
859 in the order in which the messages are defined.
860 If the receiver class has no applicable primary method for a message
861 then it's usual for the method entry pointer to be null
862 (though, as with a lot of things in Sod,
863 extensions may do something different).
865 For a standard message which takes a fixed number of arguments,
876 there is always a `main' entry point,
888 For a standard message which takes a variable number of arguments,
900 two entry points are defined:
901 the usual `main' entry point
902 which accepts a variable number of
904 and a `valist' entry point
905 which accepts an argument of type
907 in place of the variable portion of the argument list.
932 .SS Additional definitions
933 In addition to the instance and vtable structures described above,
934 the following definitions are made for each class
941 there is a macro definition
946 .IB me ->_vt-> c . m ( me ,
949 which makes sending the message
951 to an instance of (any subclass of)
956 takes a variable number of arguments,
957 the macro is more complicated
958 and is only available in compilers advertising C99 support,
959 but the effect is the same.
960 For each variable-argument message,
961 there is also an additional macro
962 for calling the `valist' entry point.
973 .IB me ->_vt-> c . m __v( me ,
977 For each proper superclass
981 there is a macro defined
984 .BI * C __CONV_ a ( C
989 which converts a (static-type) pointer to
991 to a pointer to the same actual instance,
992 but statically typed as a pointer to
994 This is most useful when
996 is not in the same chain as
998 since in-chain upcasts are both trivial and rarely needed,
999 but the full set is defined for the sake of completeness.
1001 Finally, the class object is defined as
1003 .B extern const struct
1009 .BI (& C __classobj. j . r )
1013 contains the entire class instance.
1014 This is usually rather unwieldy.
1017 is usable as a pointer of type
1023 is the root metaclass of
1025 i.e., the metaclass of the least specific superclass of
1028 .BR "const SodClass *" .
1030 .\"--------------------------------------------------------------------------
1034 .\"--------------------------------------------------------------------------
1036 Mark Wooding, <mdw@distorted.org.uk>
1038 .\"----- That's all, folks --------------------------------------------------