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);
78 struct SodObject__ilayout {
80 \h'4n'struct SodObject__ichain_obj {
81 \h'6n'const struct SodObject__vt_obj *_vt;
86 extern const struct SodClass__ilayout SodObject__classobj;
87 #define SodObject__class (&SodObject__classobj.obj.cls)
89 struct SodClass__vt_obj {
90 \h'2n'const SodClass *_class;
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);
98 struct SodObject__ilayout {
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;
106 \h'8n'void *(*imprint)(void *\fIp\fB);
107 \h'8n'size_t n_supers;
108 \h'8n'const SodClass *const *supers;
110 \h'8n'const SodClass *const *cpl;
111 \h'8n'const SodClass *link;
112 \h'8n'const SodClass *head;
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;
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;
132 extern const struct SodClass__ilayout SodClass__classobj;
133 #define SodClass__class (&SodClass__classobj.obj.cls)
137 .\"--------------------------------------------------------------------------
140 This page describes the structure and layout
141 of standard Sod objects, classes and associated metadata.
142 Note that Sod's object system is very flexible
143 and it's possible for an extension
144 to define a new root class
145 which works very differently from the standard
149 .\"--------------------------------------------------------------------------
150 .SH COMMON INSTANCE STRUCTURE
153 a pointer to an instance actually points to an
155 structure within the instances overall layout structure.
157 Instance chains contain slots and vtable pointers,
159 All instances have the basic structure of a
160 .BR "struct sod_instance" ,
161 which has the following members.
163 .B "const struct sod_vtable *_vt"
166 which has the basic structure of a
167 .BR "struct sod_vtable" ,
170 A vtable contains static metadata needed
171 for efficient conversions and
173 and pointers to the instance's class.
174 Each chain points to a different vtable
175 All vtables have the basic structure of a
176 .BR "struct sod_vtable" ,
177 which has the following members.
179 .B "const SodClass *_class"
180 A pointer to the instance's class object.
183 The offset of this chain structure
184 above the start of the overall instance layout, in bytes.
187 from the instance chain pointer
188 finds the layout base address.
190 .\"--------------------------------------------------------------------------
191 .SH BUILT-IN ROOT OBJECTS
193 This section describes the built-in classes
197 which are the standard roots of the inheritance and metaclass graphs
201 has no direct superclasses,
204 is its own metaclass.
205 It is not possible to define root classes in module files
206 because of circularities:
215 Extensions can define additional root classes,
217 and not really to be recommended.
219 .SS The SodObject class
222 class defines no slots.
225 has no direct superclasses,
226 there is only one chain,
227 and no inherited slots or messages,
228 so the single chain contains only a vtable pointer.
232 also has only one chain,
233 the vtable contains only the standard class pointer and offset-to-base
235 In an actual instance of
237 (why would you want one?)
238 the class pointer contains the address of
240 and the offset is zero.
244 message is used to initialize a newly allocated instance.
246 This message uses a custom method combination
247 which works like the standard method combination
248 except that default behaviour
249 specific to the receiver's direct class
250 is invoked if no primary or around method overrides.
251 This default behaviour may be invoked multiple times
252 if some method calls on its
254 function more than once.
256 This default behaviour is to initialize the instance's slots
257 using the defined slot initializers:
258 each slot is initialized
259 using the most specific applicable initializer,
261 Slots without an initializer
262 are left uninitialized.
264 Slots are initialized in reverse-precedence order
265 of their defining classes;
266 i.e., slots defined by a less specific superclass are initialized
267 earlier than slots defined by a more specific superclass.
268 Slots defined by the same class are initialized in the order in which
269 they appear in the class definition.
271 There are no standard keyword arguments;
272 methods on subclasses are free to
273 introduce their own in the usual way.
275 It is usual to provide complex initialization behaviour as
278 This ensures that slots have been initialized as necessary
279 before the method executes.
281 .SS The SodClass class
284 class defines no messages,
285 but there are a number of slots.
286 Its only direct superclass is
288 and so (like its superclass) its vtable is trivial.
290 The slots defined are as follows.
293 A pointer to the class's name.
296 A pointer to the class's nickname.
299 The size in bytes required to store an instance of the class.
301 .BI "void *(*imprint)(void *" p );
302 A pointer to a function:
307 bytes of appropriately aligned memory,
308 `imprint' this memory it so that it becomes a minimally functional
309 instance of the class:
310 all of the vtable and class pointers are properly initialized,
311 but the slots are left untouched.
312 The function returns its argument
316 The number of direct superclasses.
317 (This is zero exactly in the case of
320 .B const SodClass *const *supers;
321 A pointer to an array of
323 pointers to class objects
324 listing the class's direct superclasses,
325 in the order in which they were listed in the class definition.
329 then this pointer is null.
332 The number of superclasses in the class's class precedence list.
334 .B const SodClass *const *cpl;
335 A pointer to an array of pointers to class objects
336 listing all of the class's superclasses,
337 from most- to least-specific,
338 starting with the class itself,
343 for all class objects
346 .B const SodClass *link;
347 If the class is a chain head, then this is a null pointer;
348 otherwise it points to the class's distinguished link superclass
349 (which might or might not be a direct superclass).
351 .B const SodClass *head;
352 A pointer to the least-specific class in this class's chain;
354 .IB c ->cls.head->cls.link
366 .IB c ->cls.link->cls.head \fR.
369 The number of less specific superclasses in this class's chain.
378 .IB c ->cls.link->cls.level
382 The number of chains formed by the class's superclasses.
384 .B const struct sod_chain *chains;
385 A pointer to an array of
387 structures (see below) describing the class's superclass chains,
388 in decreasing order of specificity of their most specific classes.
389 It is always the case that
390 .IB c ->cls.chains[0].classes[ c ->cls.level]
394 .B size_t off_islots;
395 The offset of the class's
397 structure relative to its containing
400 The 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.)
404 The size required to store the class's direct slots,
405 i.e., the size of its
408 The class doesn't define any slots if and only if this is zero.
412 structure describes an individual chain of superclasses.
413 It has the following members.
416 The number of classes in the chain.
417 This is always at least one.
419 .B const SodClass *const *classes;
420 A pointer to an array of class pointers
421 listing the classes in the chain from least- to most-specific.
423 .IB classes [ i ]->cls.head
431 .IB classes [0]->cls.link
434 .IB classes [ i ]->cls.link
436 .IB classes [ "i\fR \- 1" ]
443 .B size_t off_ichain;
446 structure for this chain.
448 .B const struct sod_vtable *vt;
449 The vtable for this chain.
450 (It is possible, therefore, to duplicate the behaviour of the
452 function by walking the chain structure.
455 function is much faster, though.)
460 structure for this chain.
462 .\"--------------------------------------------------------------------------
463 .SH CLASS AND VTABLE LAYOUT
465 The layout algorithms for Sod instances and vtables are nontrivial.
466 They are defined here in full detail,
467 since they're effectively fixed by Sod's ABI compatibility guarantees,
468 so they might as well be documented for the sake of interoperating
471 Unfortunately, the descriptions are rather complicated,
472 and, for the most part not necessary to a working understanding of Sod.
473 The skeleton structure definitions shown should be more than enough
474 for readers attempting to make sense of the generated headers and tables.
476 In the description that follows,
477 uppercase letters vary over class names,
478 while the corresponding lowercase letters indicate the class nicknames.
479 Throughout, we consider a class
481 (therefore with nickname
484 .SS Generic instance structure
485 The entire state of an instance of
487 is contained in a single structure of type
493 struct \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;
499 \h'6n'struct \fIC\fB__islots {
500 \h'8n'\fItype\fB \fIslota\fB;
505 \h'4n'struct \fIH\fB__ichain_\fIh\fB \fIh\fB;
507 \h'2n'union \fIB\fB__ichainu_\fIi\fB \fIi\fB;
511 typedef struct \fIC\fB__ichain_\fIh\fB \fIC\fB;
515 The set of superclasses of
518 can be partitioned into chains
519 by following their distinguished superclass links.
520 (Formally, the chains are the equivalence classes determined by
521 the reflexive, symmetric, transitive closure of
522 the `links to' relation.)
523 Chains are identified by naming their least specific classes;
524 the least specific class in a chain is called the
526 Suppose that the chain head of the chain containing
530 (though keep in mind that it's possible that
538 structure contains one member for each of
541 The first such member is
549 this is followed by members
555 for each other chain,
561 the tail (most-specific) class of the chain.
562 The members are in decreasing order
563 of the specificity of the chains' most-specific classes.
564 (Note that all but the first of these unions
565 has already been defined as part of
566 the definition of the corresponding
571 union contains a member for each class in the chain.
578 and this is followed by corresponding members
588 in the same chain in some (unimportant) order.
592 (and, indeed, defined in C's type system)
593 to be a pointer to the
595 .IB C __ichain_ h \fR.
599 structure contains (in order), a pointer
606 followed by a structure
616 in the same chain which defines slots,
617 from least- to most-specific;
621 then the last member is
629 structure simply contains one member for each slot defined by
631 in the order they appear in the class definition.
633 .SS Generic vtable structure
637 structure of an instance's storage
646 the vtables for the different chains
651 The instance layout split neatly into disjoint chains.
652 This is necessary because
655 must have as a prefix the
657 for each superclass in the same chain,
658 and each slot must be stored in exactly one place.
659 The layout of vtables doesn't have this second requirement:
660 it doesn't matter that there are
661 multiple method entry pointers
662 for the same effective method
663 as long as they all work correctly.
664 Indeed, it's essential that they do,
665 because each chain's method entry function
666 will need to apply a different offset to the receiver pointer
667 before invoking the effective method.
673 has the following general structure.
677 union \fIC\fB__vtu_\fIh\fB {
678 \h'2n'struct \fIC\fB__vt_\fIh\fB {
679 \h'4n'const \fIP\fB *_class;
682 \h'4n'const \fIQ\fB *_cls_\fIj\fB;
684 \h'4n'ptrdiff_t _off_\fIi\fB;
686 \h'4n'struct \fIC\fB__vtmsgs_\fIa\fB {
687 \h'6n'\fItype\fB (*\fImsg\fB)(\fIC\fB *, \fR...\fB);
694 extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB;
715 This is mostly an irrelevant detail,
716 whose purpose is to defend against malicious compilers:
717 pointers are always to one of the inner
720 It's important only because it's the outer
722 union which is exported by name.
723 Specifically, for each chain of
726 there is an external object
736 are respectively the most and least specific classes in the chain.
738 The first member in the
741 .I root class pointer
747 Among the superclasses of
749 there must be exactly one class
751 which itself has no direct superclasses;
756 (This is a rule enforced by the Sod translator.)
769 structure of most specific superclass
776 This is followed by the
782 which is simply the offset of the
784 structure from the instance base.
786 The rest of the vtable structure is populated
787 by walking the superclass chain containing
790 For each such superclass
792 in increasing order of specificity,
793 walk the class precedence list of
795 again starting with its least-specific superclass.
796 (This complex procedure guarantees that
797 the vtable structure for a class is a prefix of
798 the vtable structure for any of its subclasses in the same chain.)
802 be some superclass of
804 which has been encountered during this traversal.
810 Examine the superclass chains of
812 in order of decreasing specificity of their most-specific classes.
815 be the chain head of such a chain,
818 be the most specific superclass of
822 Then, if there is currently no class pointer of type
832 pointing to the appropriate
839 Examine the superclass chains of
841 in order of decreasing specificity of their most-specific classes.
844 be the chain head of such a chain.
845 If there is currently no member
854 containing the (signed) offset from the
856 structure of the chain headed by
858 to that of the chain headed by
860 within the instance's layout.
865 defines any messages,
866 and there is currently no member
881 structures contain pointers to the effective method entry functions
882 for the messages defined by a superclass.
883 There may be more than one method entry for a message,
884 but all of the entry pointers for a message appear together,
885 and entry pointers for separate messages appear
886 in the order in which the messages are defined.
887 If the receiver class has no applicable primary method for a message
888 then it's usual for the method entry pointer to be null
889 (though, as with a lot of things in Sod,
890 extensions may do something different).
892 For a standard message which takes a fixed number of arguments,
903 there is always a `main' entry point,
915 For a standard message which takes a variable number of arguments,
927 or a standard message which takes keyword arguments,
948 two entry points are defined:
949 the usual `main' entry point
950 which accepts a variable number of
952 and a `valist' entry point
953 which accepts an argument of type
955 in place of the variable portion of the argument list
981 .SS Additional definitions
982 In addition to the instance and vtable structures described above,
983 the following definitions are made for each class
990 there is a macro definition
995 .IB me ->_vt-> c . m ( me ,
998 which makes sending the message
1000 to an instance of (any subclass of)
1005 takes a variable number of arguments,
1006 or keyword arguments,
1007 the macro is more complicated
1008 and is only available in compilers advertising C99 support,
1009 but the effect is the same.
1010 For each variable-argument message,
1011 there is also an additional macro
1012 for calling the `valist' entry point.
1023 .IB me ->_vt-> c . m __v( me ,
1027 For each proper superclass
1031 there is a macro defined
1034 .BI * C __CONV_ a ( C
1039 which converts a (static-type) pointer to
1041 to a pointer to the same actual instance,
1042 but statically typed as a pointer to
1044 This is most useful when
1046 is not in the same chain as
1048 since in-chain upcasts are both trivial and rarely needed,
1049 but the full set is defined for the sake of completeness.
1051 Finally, the class object is defined as
1053 .B extern const struct
1059 .BI (& C __classobj. j . r )
1063 contains the entire class instance.
1064 This is usually rather unwieldy.
1067 is usable as a pointer of type
1073 is the root metaclass of
1075 i.e., the metaclass of the least specific superclass of
1078 .BR "const SodClass *" .
1080 .\"--------------------------------------------------------------------------
1084 .\"--------------------------------------------------------------------------
1086 Mark Wooding, <mdw@distorted.org.uk>
1088 .\"----- That's all, folks --------------------------------------------------