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 .\"--------------------------------------------------------------------------
28 .so ../common/defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH sod-structs 3 "8 September 2015" "Straylight/Edgeware" "Sensible Object Design"
34 sod-structs \- main Sod data structures
36 .\"--------------------------------------------------------------------------
42 typedef struct SodObject__ichain_obj SodObject;
43 typedef struct SodClass__ichain_obj SodClass;
46 \h'2n'const struct sod_vtable *_vt;
50 \h'2n'const SodClass *_class;
54 struct SodObject__vt_obj {
55 \h'2n'const SodClass *_class;
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);
60 \h'4n'int (*teardown)(SodObject *\fIme\fB);
64 struct SodObject__ilayout {
66 \h'4n'struct SodObject__ichain_obj {
67 \h'6n'const struct SodObject__vt_obj *_vt;
72 extern const struct SodClass__ilayout SodObject__classobj;
73 #define SodObject__class (&SodObject__classobj.obj.cls)
75 struct SodClass__vt_obj {
76 \h'2n'const SodClass *_class;
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);
81 \h'4n'int (*teardown)(SodClass *\fIme\fB);
85 struct SodClass__ilayout {
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;
94 \h'8n'void *(*imprint)(void *\fIp\fB);
95 \h'8n'size_t n_supers;
96 \h'8n'const SodClass *const *supers;
98 \h'8n'const SodClass *const *cpl;
99 \h'8n'const SodClass *link;
100 \h'8n'const SodClass *head;
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;
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;
120 extern const struct SodClass__ilayout SodClass__classobj;
121 #define SodClass__class (&SodClass__classobj.obj.cls)
125 .\"--------------------------------------------------------------------------
128 This page describes the structure and layout
129 of standard Sod objects, classes and associated metadata.
130 Note that Sod's object system is very flexible
131 and it's possible for an extension
132 to define a new root class
133 which works very differently from the standard
137 .\"--------------------------------------------------------------------------
138 .SH COMMON INSTANCE STRUCTURE
141 a pointer to an instance actually points to an
143 structure within the instances overall layout structure.
145 Instance chains contain slots and vtable pointers,
147 All instances have the basic structure of a
148 .BR "struct sod_instance" ,
149 which has the following members.
151 .B "const struct sod_vtable *_vt"
154 which has the basic structure of a
155 .BR "struct sod_vtable" ,
158 A vtable contains static metadata needed
159 for efficient conversions and
161 and pointers to the instance's class.
162 Each chain points to a different vtable
163 All vtables have the basic structure of a
164 .BR "struct sod_vtable" ,
165 which has the following members.
167 .B "const SodClass *_class"
168 A pointer to the instance's class object.
171 The offset of this chain structure
172 above the start of the overall instance layout, in bytes.
175 from the instance chain pointer
176 finds the layout base address.
178 .\"--------------------------------------------------------------------------
179 .SH BUILT-IN ROOT OBJECTS
181 This section describes the built-in classes
185 which are the standard roots of the inheritance and metaclass graphs
189 has no direct superclasses,
192 is its own metaclass.
193 It is not possible to define root classes in module files
194 because of circularities:
203 Extensions can define additional root classes,
205 and not really to be recommended.
207 .SS The SodObject class
210 class defines no slots.
213 has no direct superclasses,
214 there is only one chain,
215 and no inherited slots or messages,
216 so the single chain contains only a vtable pointer.
220 also has only one chain,
221 the vtable contains only the standard class pointer and offset-to-base
223 In an actual instance of
225 (why would you want one?)
226 the class pointer contains the address of
228 and the offset is zero.
232 message is used to initialize a newly allocated instance.
234 This message uses a custom method combination
235 which works like the standard method combination
236 except that default behaviour
237 specific to the receiver's direct class
238 is invoked if no primary or around method overrides.
239 This default behaviour may be invoked multiple times
240 if some method calls on its
242 function more than once.
244 This default behaviour is to initialize the instance's slots
245 using the defined slot initializers,
246 and execute the initialization fragments.
247 Each slot is initialized
248 using the most specific applicable initializer,
250 Slots without an initializer
251 are left uninitialized.
253 Slots are initialized and initialization fragments executed together,
254 a superclass at a time:
255 first, the superclass's slots are initialized (if any);
256 then the superclass's initialization fragments (if any) are executed,
257 starting with the least specific superclass first.
258 Slots and initialization fragments defined by the same class
259 are processed in the order in which they appear in the class definition.
261 There are no standard keyword arguments;
262 methods on subclasses are free to
263 introduce their own in the usual way.
265 It is usual to provide complex initialization behaviour as
268 This ensures that slots have been initialized as necessary
269 before the method executes.
273 message is used to tear down an instance which is no longer required.
275 The message returns an integer flag.
276 A zero value means that the instance is safe to deallocate.
277 A nonzero value means that the instance should not be deallocated,
278 and that it is safe for the caller to simply forget about it.
279 This simple protocol may be used, for example,
280 to implement a reference-counting system.
282 This message uses a custom method combination
283 which works like the standard method combination
284 except that default behaviour is invoked if
285 no primary or around method overrides.
286 This default behaviour is to execute
287 each superclass's teardown fragments,
289 and then return zero to indicate
290 that the object is ready for deallocation.
291 Teardown fragments defined by the same class
292 are processed in the order in which they appear
293 in the class definition.
295 It is usual to provide complex teardown behaviour as
298 Logic to decide whether to allow deallocation
299 is usually implemented as
303 .SS The SodClass class
306 class defines no messages,
307 but there are a number of slots.
308 Its only direct superclass is
310 and so (like its superclass) its vtable is trivial.
312 The slots defined are as follows.
315 A pointer to the class's name.
318 A pointer to the class's nickname.
321 The size in bytes required to store an instance of the class.
324 A sufficient alignment for the class's instance storage.
326 .BI "void *(*imprint)(void *" p );
327 A pointer to a function:
332 bytes of appropriately aligned memory,
333 `imprint' this memory it so that it becomes a minimally functional
334 instance of the class:
335 all of the vtable and class pointers are properly initialized,
336 but the slots are left untouched.
337 The function returns its argument
341 The number of direct superclasses.
342 (This is zero exactly in the case of
345 .B const SodClass *const *supers;
346 A pointer to an array of
348 pointers to class objects
349 listing the class's direct superclasses,
350 in the order in which they were listed in the class definition.
354 then this pointer is null.
357 The number of superclasses in the class's class precedence list.
359 .B const SodClass *const *cpl;
360 A pointer to an array of pointers to class objects
361 listing all of the class's superclasses,
362 from most- to least-specific,
363 starting with the class itself,
368 for all class objects
371 .B const SodClass *link;
372 If the class is a chain head, then this is a null pointer;
373 otherwise it points to the class's distinguished link superclass
374 (which might or might not be a direct superclass).
376 .B const SodClass *head;
377 A pointer to the least-specific class in this class's chain;
379 .IB c ->cls.head->cls.link
391 .IB c ->cls.link->cls.head \fR.
394 The number of less specific superclasses in this class's chain.
403 .IB c ->cls.link->cls.level
407 The number of chains formed by the class's superclasses.
409 .B const struct sod_chain *chains;
410 A pointer to an array of
412 structures (see below) describing the class's superclass chains,
413 in decreasing order of specificity of their most specific classes.
414 It is always the case that
415 .IB c ->cls.chains[0].classes[ c ->cls.level]
419 .B size_t off_islots;
420 The offset of the class's
422 structure relative to its containing
425 The 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.)
429 The size required to store the class's direct slots,
430 i.e., the size of its
433 The class doesn't define any slots if and only if this is zero.
437 structure describes an individual chain of superclasses.
438 It has the following members.
441 The number of classes in the chain.
442 This is always at least one.
444 .B const SodClass *const *classes;
445 A pointer to an array of class pointers
446 listing the classes in the chain from least- to most-specific.
448 .IB classes [ i ]->cls.head
456 .IB classes [0]->cls.link
459 .IB classes [ i ]->cls.link
461 .IB classes [ "i\fR \- 1" ]
468 .B size_t off_ichain;
471 structure for this chain.
473 .B const struct sod_vtable *vt;
474 The vtable for this chain.
475 (It is possible, therefore, to duplicate the behaviour of the
477 function by walking the chain structure.
480 function is much faster, though.)
485 structure for this chain.
487 .\"--------------------------------------------------------------------------
488 .SH CLASS AND VTABLE LAYOUT
490 The layout algorithms for Sod instances and vtables are nontrivial.
491 They are defined here in full detail,
492 since they're effectively fixed by Sod's ABI compatibility guarantees,
493 so they might as well be documented for the sake of interoperating
496 Unfortunately, the descriptions are rather complicated,
497 and, for the most part not necessary to a working understanding of Sod.
498 The skeleton structure definitions shown should be more than enough
499 for readers attempting to make sense of the generated headers and tables.
501 In the description that follows,
502 uppercase letters vary over class names,
503 while the corresponding lowercase letters indicate the class nicknames.
504 Throughout, we consider a class
506 (therefore with nickname
509 .SS Generic instance structure
510 The entire state of an instance of
512 is contained in a single structure of type
516 struct \fIC\fB__ilayout {
517 \h'2n'union \fIC\fB__ichainu_\fIh\fB {
518 \h'4n'struct \fIC\fB__ichain_\fIh\fB {
519 \h'6n'const struct \fIC\fB__vt_\fIh\fB *_vt;
520 \h'6n'struct \fIH\fB__islots \fIh\fB;
522 \h'6n'struct \fIC\fB__islots {
523 \h'8n'\fItype\fB \fIslota\fB;
528 \h'4n'struct \fIA\fB__ichain_\fIh\fB \fIa\fB;
530 \h'2n'union \fIB\fB__ichainu_\fIi\fB \fIi\fB;
534 typedef struct \fIC\fB__ichain_\fIh\fB \fIC\fB;
536 The set of superclasses of
539 can be partitioned into chains
540 by following their distinguished superclass links.
541 (Formally, the chains are the equivalence classes determined by
542 the reflexive, symmetric, transitive closure of
543 the `links to' relation.)
544 Chains are identified by naming their least specific classes;
545 the least specific class in a chain is called the
547 Suppose that the chain head of the chain containing
551 (though keep in mind that it's possible that
559 structure contains one member for each of
562 The first such member is
570 this is followed by members
576 for each other chain,
582 the tail (most-specific) class of the chain.
583 The members are in decreasing order
584 of the specificity of the chains' most-specific classes.
585 (Note that all but the first of these unions
586 has already been defined as part of
587 the definition of the corresponding
592 union contains a member for each class in the chain.
599 and this is followed by corresponding members
609 in the same chain in some (unimportant) order.
610 The (somewhat obtuse) purpose of this union is to
611 engage the `common initial sequence' rule of
612 C99 (clause 6.5.2.3).
616 structure contains (in order), a pointer
623 followed by a structure
633 in the same chain which defines slots,
634 from least- to most-specific;
638 then the last member is
647 (and, indeed, defined in C's type system)
648 to be a pointer to the
650 .IB C __ichain_ h \fR.
654 structure simply contains one member for each slot defined by
656 in the order they appear in the class definition.
658 .SS Generic vtable structure
662 structure of an instance's storage
671 the vtables for the different chains
676 The instance layout splits neatly into disjoint chains.
677 This is necessary because
680 must have as a prefix the
682 for each superclass in the same chain,
683 and each slot must be stored in exactly one place.
684 The layout of vtables doesn't have this second requirement:
685 it doesn't matter that there are
686 multiple method entry pointers
687 for the same effective method
688 as long as they all work correctly.
689 Indeed, it's essential that there are multiple entry pointers,
690 because each chain's method entry function
691 will need to apply a different offset to the receiver pointer
692 before invoking the effective method.
698 has the following general structure.
700 union \fIC\fB__vtu_\fIh\fB {
701 \h'2n'struct \fIC\fB__vt_\fIh\fB {
702 \h'4n'const \fIP\fB *_class;
705 \h'4n'const \fIQ\fB *_cls_\fIj\fB;
707 \h'4n'ptrdiff_t _off_\fIi\fB;
709 \h'4n'struct \fIC\fB__vtmsgs_\fIa\fB {
710 \h'6n'\fItype\fB (*\fImsg\fB)(\fIC\fB *, \fR...\fB);
717 extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB;
742 This is mostly an irrelevant detail,
743 whose purpose is to defend against malicious compilers:
744 pointers are always to one of the inner
747 It's important only because it's the outer
749 union which is exported by name.
750 Specifically, for each chain of
753 there is an external object
763 are respectively the most and least specific classes in the chain.
765 The first member in the
768 .I root class pointer
774 Among the superclasses of
776 there must be exactly one class
778 which itself has no direct superclasses;
783 (This is a rule enforced by the Sod translator.)
796 structure of most specific superclass
803 This is followed by the
809 which is simply the offset of the
811 structure from the instance base.
813 The rest of the vtable structure is populated
814 by walking the superclass chain containing
817 For each such superclass
819 in increasing order of specificity,
820 walk the class precedence list of
822 again starting with its least-specific superclass.
823 (This complex procedure guarantees that
824 the vtable structure for a class is a prefix of
825 the vtable structure for any of its subclasses in the same chain.)
829 be some superclass of
831 which has been encountered during this traversal.
837 Examine the superclass chains of
839 in order of decreasing specificity of their most-specific classes.
842 be the chain head of such a chain,
845 be the most specific superclass of
849 If there is currently no class pointer
850 for the chain headed by
860 pointing to the appropriate
867 is the most specific superclass of
873 Examine the superclass chains of
875 in order of decreasing specificity of their most-specific classes.
878 be the chain head of such a chain.
879 If there is currently no member
888 containing the (signed) offset from the
890 structure of the chain headed by
892 to that of the chain headed by
894 within the instance's layout.
899 defines any messages,
900 and there is currently no member
915 structures contain pointers to the effective method entry functions
916 for the messages defined by a superclass.
917 There may be more than one method entry for a message,
918 but all of the entry pointers for a message appear together,
919 and entry pointers for separate messages appear
920 in the order in which the messages are defined.
921 If the receiver class has no applicable primary method for a message
922 then it's usual for the method entry pointer to be null
923 (though, as with a lot of things in Sod,
924 extensions may do something different).
926 For a standard message which takes a fixed number of arguments,
937 there is always a `main' entry point,
949 For a standard message which takes a variable number of arguments,
961 or a standard message which takes keyword arguments,
982 two entry points are defined:
983 the usual `main' entry point
984 which accepts a variable number of
986 and a `valist' entry point
987 which accepts an argument of type
989 in place of the variable portion of the argument list
1015 .SS Additional definitions
1016 In addition to the instance and vtable structures described above,
1017 the following definitions are made for each class
1024 there is a macro definition
1029 .IB me ->_vt-> c . m ( me ,
1032 which makes sending the message
1034 to an instance of (any subclass of)
1039 takes a variable number of arguments,
1040 or keyword arguments,
1041 the macro is more complicated
1042 and is only available in compilers advertising C99 support,
1043 but the effect is the same.
1044 For each variable-argument message,
1045 there is also an additional macro
1046 for calling the `valist' entry point.
1057 .IB me ->_vt-> c . m __v( me ,
1061 For each proper superclass
1065 there is a macro defined
1068 .BI * C __CONV_ a ( C
1073 which converts a (static-type) pointer to
1075 to a pointer to the same actual instance,
1076 but statically typed as a pointer to
1078 This is most useful when
1080 is not in the same chain as
1082 since in-chain upcasts are both trivial and rarely needed,
1083 but the full set is defined for the sake of completeness.
1085 Finally, the class object is defined as
1087 .B extern const struct
1093 .BI (& C __classobj. j . r )
1097 .BI (& C __classobj. k . n )
1103 contains the entire class instance.
1104 This is usually rather unwieldy.
1107 is usable as a pointer of type
1113 is the root metaclass of
1115 i.e., the metaclass of the least specific superclass of
1118 .BR "const SodClass\ *" .
1123 is defined, usable as a pointer of type
1131 are the chain's head and tail classes
1132 (i.e., the least- and most-specific classes in the chain)
1138 i.e., in the common case where
1140 metaclass is precisely the root metaclass,
1143 macro is already sufficient.
1145 .\"--------------------------------------------------------------------------
1149 .\"--------------------------------------------------------------------------
1151 Mark Wooding, <mdw@distorted.org.uk>
1153 .\"----- That's all, folks --------------------------------------------------