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;
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'4n'int (*teardown)(SodObject *\fIme\fB);
79 struct SodObject__ilayout {
81 \h'4n'struct SodObject__ichain_obj {
82 \h'6n'const struct SodObject__vt_obj *_vt;
87 extern const struct SodClass__ilayout SodObject__classobj;
88 #define SodObject__class (&SodObject__classobj.obj.cls)
90 struct SodClass__vt_obj {
91 \h'2n'const SodClass *_class;
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);
96 \h'4n'int (*teardown)(SodClass *\fIme\fB);
100 struct SodObject__ilayout {
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;
108 \h'8n'void *(*imprint)(void *\fIp\fB);
109 \h'8n'size_t n_supers;
110 \h'8n'const SodClass *const *supers;
112 \h'8n'const SodClass *const *cpl;
113 \h'8n'const SodClass *link;
114 \h'8n'const SodClass *head;
116 \h'8n'size_t n_chains;
117 \h'8n'const struct sod_chain *chains;
118 \h'8n'size_t off_islots;
119 \h'8n'size_t islotsz;
127 \h'2n'size_t n_classes;
128 \h'2n'const SodClass *const *classes;
129 \h'2n'size_t off_ichain;
130 \h'2n'const struct sod_vtable *vt;
131 \h'2n'size_t ichainsz;
134 extern const struct SodClass__ilayout SodClass__classobj;
135 #define SodClass__class (&SodClass__classobj.obj.cls)
139 .\"--------------------------------------------------------------------------
142 This page describes the structure and layout
143 of standard Sod objects, classes and associated metadata.
144 Note that Sod's object system is very flexible
145 and it's possible for an extension
146 to define a new root class
147 which works very differently from the standard
151 .\"--------------------------------------------------------------------------
152 .SH COMMON INSTANCE STRUCTURE
155 a pointer to an instance actually points to an
157 structure within the instances overall layout structure.
159 Instance chains contain slots and vtable pointers,
161 All instances have the basic structure of a
162 .BR "struct sod_instance" ,
163 which has the following members.
165 .B "const struct sod_vtable *_vt"
168 which has the basic structure of a
169 .BR "struct sod_vtable" ,
172 A vtable contains static metadata needed
173 for efficient conversions and
175 and pointers to the instance's class.
176 Each chain points to a different vtable
177 All vtables have the basic structure of a
178 .BR "struct sod_vtable" ,
179 which has the following members.
181 .B "const SodClass *_class"
182 A pointer to the instance's class object.
185 The offset of this chain structure
186 above the start of the overall instance layout, in bytes.
189 from the instance chain pointer
190 finds the layout base address.
192 .\"--------------------------------------------------------------------------
193 .SH BUILT-IN ROOT OBJECTS
195 This section describes the built-in classes
199 which are the standard roots of the inheritance and metaclass graphs
203 has no direct superclasses,
206 is its own metaclass.
207 It is not possible to define root classes in module files
208 because of circularities:
217 Extensions can define additional root classes,
219 and not really to be recommended.
221 .SS The SodObject class
224 class defines no slots.
227 has no direct superclasses,
228 there is only one chain,
229 and no inherited slots or messages,
230 so the single chain contains only a vtable pointer.
234 also has only one chain,
235 the vtable contains only the standard class pointer and offset-to-base
237 In an actual instance of
239 (why would you want one?)
240 the class pointer contains the address of
242 and the offset is zero.
246 message is used to initialize a newly allocated instance.
248 This message uses a custom method combination
249 which works like the standard method combination
250 except that default behaviour
251 specific to the receiver's direct class
252 is invoked if no primary or around method overrides.
253 This default behaviour may be invoked multiple times
254 if some method calls on its
256 function more than once.
258 This default behaviour is to initialize the instance's slots
259 using the defined slot initializers,
260 and execute the initialization fragments.
261 Each slot is initialized
262 using the most specific applicable initializer,
264 Slots without an initializer
265 are left uninitialized.
267 Slots are initialized and initialization fragments executed together,
268 a superclass at a time:
269 first, the superclass's slots are initialized (if any);
270 then the superclass's initialization fragments (if any) are executed,
271 starting with the least specific superclass first.
272 Slots and initialization fragments defined by the same class
273 are processed in the order in which they appear in the class definition.
275 There are no standard keyword arguments;
276 methods on subclasses are free to
277 introduce their own in the usual way.
279 It is usual to provide complex initialization behaviour as
282 This ensures that slots have been initialized as necessary
283 before the method executes.
287 message is used to tear down an instance which is no longer required.
289 The message returns an integer flag.
290 A zero value means that the instance is safe to deallocate.
291 A nonzero value means that the instance should not be deallocated,
292 and that it is safe for the caller to simply forget about it.
293 This simple protocol may be used, for example,
294 to implement a reference-counting system.
296 This message uses a custom method combination
297 which works like the standard method combination
298 except that default behaviour is invoked if
299 no primary or around method overrides.
300 This default behaviour is to execute
301 each superclass's teardown fragments,
303 and then return zero to indicate
304 that the object is ready for deallocation.
305 Teardown fragments defined by the same class
306 are processed in the order in which they appear
307 in the class definition.
309 It is usual to provide complex teardown behaviour as
312 Logic to decide whether to allow deallocation
313 is usually implemented as
317 .SS The SodClass class
320 class defines no messages,
321 but there are a number of slots.
322 Its only direct superclass is
324 and so (like its superclass) its vtable is trivial.
326 The slots defined are as follows.
329 A pointer to the class's name.
332 A pointer to the class's nickname.
335 The size in bytes required to store an instance of the class.
337 .BI "void *(*imprint)(void *" p );
338 A pointer to a function:
343 bytes of appropriately aligned memory,
344 `imprint' this memory it so that it becomes a minimally functional
345 instance of the class:
346 all of the vtable and class pointers are properly initialized,
347 but the slots are left untouched.
348 The function returns its argument
352 The number of direct superclasses.
353 (This is zero exactly in the case of
356 .B const SodClass *const *supers;
357 A pointer to an array of
359 pointers to class objects
360 listing the class's direct superclasses,
361 in the order in which they were listed in the class definition.
365 then this pointer is null.
368 The number of superclasses in the class's class precedence list.
370 .B const SodClass *const *cpl;
371 A pointer to an array of pointers to class objects
372 listing all of the class's superclasses,
373 from most- to least-specific,
374 starting with the class itself,
379 for all class objects
382 .B const SodClass *link;
383 If the class is a chain head, then this is a null pointer;
384 otherwise it points to the class's distinguished link superclass
385 (which might or might not be a direct superclass).
387 .B const SodClass *head;
388 A pointer to the least-specific class in this class's chain;
390 .IB c ->cls.head->cls.link
402 .IB c ->cls.link->cls.head \fR.
405 The number of less specific superclasses in this class's chain.
414 .IB c ->cls.link->cls.level
418 The number of chains formed by the class's superclasses.
420 .B const struct sod_chain *chains;
421 A pointer to an array of
423 structures (see below) describing the class's superclass chains,
424 in decreasing order of specificity of their most specific classes.
425 It is always the case that
426 .IB c ->cls.chains[0].classes[ c ->cls.level]
430 .B size_t off_islots;
431 The offset of the class's
433 structure relative to its containing
436 The class doesn't define any slots if and only if this is zero.
437 (The offset can't be zero because the vtable pointer is at offset zero.)
440 The size required to store the class's direct slots,
441 i.e., the size of its
444 The class doesn't define any slots if and only if this is zero.
448 structure describes an individual chain of superclasses.
449 It has the following members.
452 The number of classes in the chain.
453 This is always at least one.
455 .B const SodClass *const *classes;
456 A pointer to an array of class pointers
457 listing the classes in the chain from least- to most-specific.
459 .IB classes [ i ]->cls.head
467 .IB classes [0]->cls.link
470 .IB classes [ i ]->cls.link
472 .IB classes [ "i\fR \- 1" ]
479 .B size_t off_ichain;
482 structure for this chain.
484 .B const struct sod_vtable *vt;
485 The vtable for this chain.
486 (It is possible, therefore, to duplicate the behaviour of the
488 function by walking the chain structure.
491 function is much faster, though.)
496 structure for this chain.
498 .\"--------------------------------------------------------------------------
499 .SH CLASS AND VTABLE LAYOUT
501 The layout algorithms for Sod instances and vtables are nontrivial.
502 They are defined here in full detail,
503 since they're effectively fixed by Sod's ABI compatibility guarantees,
504 so they might as well be documented for the sake of interoperating
507 Unfortunately, the descriptions are rather complicated,
508 and, for the most part not necessary to a working understanding of Sod.
509 The skeleton structure definitions shown should be more than enough
510 for readers attempting to make sense of the generated headers and tables.
512 In the description that follows,
513 uppercase letters vary over class names,
514 while the corresponding lowercase letters indicate the class nicknames.
515 Throughout, we consider a class
517 (therefore with nickname
520 .SS Generic instance structure
521 The entire state of an instance of
523 is contained in a single structure of type
529 struct \fIC\fB__ilayout {
530 \h'2n'union \fIC\fB__ichainu_\fIh\fB {
531 \h'4n'struct \fIC\fB__ichain_\fIh\fB {
532 \h'6n'const struct \fIC\fB__vt_\fIh\fB *_vt;
533 \h'6n'struct \fIH\fB__islots \fIh\fB;
535 \h'6n'struct \fIC\fB__islots {
536 \h'8n'\fItype\fB \fIslota\fB;
541 \h'4n'struct \fIH\fB__ichain_\fIh\fB \fIh\fB;
543 \h'2n'union \fIB\fB__ichainu_\fIi\fB \fIi\fB;
547 typedef struct \fIC\fB__ichain_\fIh\fB \fIC\fB;
551 The set of superclasses of
554 can be partitioned into chains
555 by following their distinguished superclass links.
556 (Formally, the chains are the equivalence classes determined by
557 the reflexive, symmetric, transitive closure of
558 the `links to' relation.)
559 Chains are identified by naming their least specific classes;
560 the least specific class in a chain is called the
562 Suppose that the chain head of the chain containing
566 (though keep in mind that it's possible that
574 structure contains one member for each of
577 The first such member is
585 this is followed by members
591 for each other chain,
597 the tail (most-specific) class of the chain.
598 The members are in decreasing order
599 of the specificity of the chains' most-specific classes.
600 (Note that all but the first of these unions
601 has already been defined as part of
602 the definition of the corresponding
607 union contains a member for each class in the chain.
614 and this is followed by corresponding members
624 in the same chain in some (unimportant) order.
628 (and, indeed, defined in C's type system)
629 to be a pointer to the
631 .IB C __ichain_ h \fR.
635 structure contains (in order), a pointer
642 followed by a structure
652 in the same chain which defines slots,
653 from least- to most-specific;
657 then the last member is
665 structure simply contains one member for each slot defined by
667 in the order they appear in the class definition.
669 .SS Generic vtable structure
673 structure of an instance's storage
682 the vtables for the different chains
687 The instance layout split neatly into disjoint chains.
688 This is necessary because
691 must have as a prefix the
693 for each superclass in the same chain,
694 and each slot must be stored in exactly one place.
695 The layout of vtables doesn't have this second requirement:
696 it doesn't matter that there are
697 multiple method entry pointers
698 for the same effective method
699 as long as they all work correctly.
700 Indeed, it's essential that they do,
701 because each chain's method entry function
702 will need to apply a different offset to the receiver pointer
703 before invoking the effective method.
709 has the following general structure.
713 union \fIC\fB__vtu_\fIh\fB {
714 \h'2n'struct \fIC\fB__vt_\fIh\fB {
715 \h'4n'const \fIP\fB *_class;
718 \h'4n'const \fIQ\fB *_cls_\fIj\fB;
720 \h'4n'ptrdiff_t _off_\fIi\fB;
722 \h'4n'struct \fIC\fB__vtmsgs_\fIa\fB {
723 \h'6n'\fItype\fB (*\fImsg\fB)(\fIC\fB *, \fR...\fB);
730 extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB;
751 This is mostly an irrelevant detail,
752 whose purpose is to defend against malicious compilers:
753 pointers are always to one of the inner
756 It's important only because it's the outer
758 union which is exported by name.
759 Specifically, for each chain of
762 there is an external object
772 are respectively the most and least specific classes in the chain.
774 The first member in the
777 .I root class pointer
783 Among the superclasses of
785 there must be exactly one class
787 which itself has no direct superclasses;
792 (This is a rule enforced by the Sod translator.)
805 structure of most specific superclass
812 This is followed by the
818 which is simply the offset of the
820 structure from the instance base.
822 The rest of the vtable structure is populated
823 by walking the superclass chain containing
826 For each such superclass
828 in increasing order of specificity,
829 walk the class precedence list of
831 again starting with its least-specific superclass.
832 (This complex procedure guarantees that
833 the vtable structure for a class is a prefix of
834 the vtable structure for any of its subclasses in the same chain.)
838 be some superclass of
840 which has been encountered during this traversal.
846 Examine the superclass chains of
848 in order of decreasing specificity of their most-specific classes.
851 be the chain head of such a chain,
854 be the most specific superclass of
858 Then, if there is currently no class pointer of type
868 pointing to the appropriate
875 Examine the superclass chains of
877 in order of decreasing specificity of their most-specific classes.
880 be the chain head of such a chain.
881 If there is currently no member
890 containing the (signed) offset from the
892 structure of the chain headed by
894 to that of the chain headed by
896 within the instance's layout.
901 defines any messages,
902 and there is currently no member
917 structures contain pointers to the effective method entry functions
918 for the messages defined by a superclass.
919 There may be more than one method entry for a message,
920 but all of the entry pointers for a message appear together,
921 and entry pointers for separate messages appear
922 in the order in which the messages are defined.
923 If the receiver class has no applicable primary method for a message
924 then it's usual for the method entry pointer to be null
925 (though, as with a lot of things in Sod,
926 extensions may do something different).
928 For a standard message which takes a fixed number of arguments,
939 there is always a `main' entry point,
951 For a standard message which takes a variable number of arguments,
963 or a standard message which takes keyword arguments,
984 two entry points are defined:
985 the usual `main' entry point
986 which accepts a variable number of
988 and a `valist' entry point
989 which accepts an argument of type
991 in place of the variable portion of the argument list
1017 .SS Additional definitions
1018 In addition to the instance and vtable structures described above,
1019 the following definitions are made for each class
1026 there is a macro definition
1031 .IB me ->_vt-> c . m ( me ,
1034 which makes sending the message
1036 to an instance of (any subclass of)
1041 takes a variable number of arguments,
1042 or keyword arguments,
1043 the macro is more complicated
1044 and is only available in compilers advertising C99 support,
1045 but the effect is the same.
1046 For each variable-argument message,
1047 there is also an additional macro
1048 for calling the `valist' entry point.
1059 .IB me ->_vt-> c . m __v( me ,
1063 For each proper superclass
1067 there is a macro defined
1070 .BI * C __CONV_ a ( C
1075 which converts a (static-type) pointer to
1077 to a pointer to the same actual instance,
1078 but statically typed as a pointer to
1080 This is most useful when
1082 is not in the same chain as
1084 since in-chain upcasts are both trivial and rarely needed,
1085 but the full set is defined for the sake of completeness.
1087 Finally, the class object is defined as
1089 .B extern const struct
1095 .BI (& C __classobj. j . r )
1099 contains the entire class instance.
1100 This is usually rather unwieldy.
1103 is usable as a pointer of type
1109 is the root metaclass of
1111 i.e., the metaclass of the least specific superclass of
1114 .BR "const SodClass *" .
1116 .\"--------------------------------------------------------------------------
1120 .\"--------------------------------------------------------------------------
1122 Mark Wooding, <mdw@distorted.org.uk>
1124 .\"----- That's all, folks --------------------------------------------------