3 .\" Description of the main Sod data structures
5 .\" (c) 2015 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the Sensble 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 General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or
15 .\" (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 General Public License for more details.
22 .\" You should have received a copy of the GNU General Public License
23 .\" along with SOD; if not, write to the Free Software Foundation,
24 .\" Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 .\" String definitions and font selection.
36 .\" .hP TEXT -- start an indented paragraph with TEXT hanging off to the left
39 \h'-\w'\fB\\$1\ \fP'u'\fB\\$1\ \fP\c
42 .\"--------------------------------------------------------------------------
43 .TH sod-structs 3 "8 September 2015" "Straylight/Edgeware" "Sensible Object Design"
46 sod-structs \- main Sod data structures
48 .\"--------------------------------------------------------------------------
54 typedef struct SodObject__ichain_obj SodObject;
55 typedef struct SodClass__ichain_obj SodClass;
58 \h'2n'const struct sod_vtable *_vt;
62 \h'2n'const SodClass *_class;
66 struct SodObject__vt_obj {
67 \h'2n'const SodClass *_class;
71 struct SodObject__ilayout {
73 \h'4n'struct SodObject__ichain_obj {
74 \h'6n'const struct SodObject__vt_obj *_vt;
79 extern const struct SodClass__ilayout SodObject__classobj;
80 #define SodObject__class (&SodObject__classobj.obj.cls)
82 struct SodClass__vt_obj {
83 \h'2n'const SodClass *_class;
87 struct SodObject__ilayout {
89 \h'4n'struct SodClass__ichain_obj {
90 \h'6n'const struct SodClass__vt_obj *_vt;
91 \h'6n'struct SodClass__islots {
92 \h'8n'const char *name;
93 \h'8n'const char *nick;
95 \h'8n'void *(*imprint)(void *\fIp\fB);
96 \h'8n'void *(*init)(void *\fIp\fB);
97 \h'8n'size_t n_supers;
98 \h'8n'const SodClass *const *supers;
100 \h'8n'const SodClass *const *cpl;
101 \h'8n'const SodClass *link;
102 \h'8n'const SodClass *head;
104 \h'8n'size_t n_chains;
105 \h'8n'const struct sod_chain *chains;
106 \h'8n'size_t off_islots;
107 \h'8n'size_t islotsz;
115 \h'2n'size_t n_classes;
116 \h'2n'const SodClass *const *classes;
117 \h'2n'size_t off_ichain;
118 \h'2n'const struct sod_vtable *vt;
119 \h'2n'size_t ichainsz;
122 extern const struct SodClass__ilayout SodClass__classobj;
123 #define SodClass__class (&SodClass__classobj.obj.cls)
127 .\"--------------------------------------------------------------------------
130 This page describes the structure and layout
131 of standard Sod objects, classes and associated metadata.
132 Note that Sod's object system is very flexible
133 and it's possible for an extension
134 to define a new root class
135 which works very differently from the standard
139 .\"--------------------------------------------------------------------------
140 .SH COMMON INSTANCE STRUCTURE
143 a pointer to an instance actually points to an
145 structure within the instances overall layout structure.
147 Instance chains contain slots and vtable pointers,
149 All instances have the basic structure of a
150 .BR "struct sod_instance" ,
151 which has the following members.
153 .B "const struct sod_vtable *_vt"
156 which has the basic structure of a
157 .BR "struct sod_vtable" ,
160 A vtable contains static metadata needed
161 for efficient conversions and
163 and pointers to the instance's class.
164 Each chain points to a different vtable
165 All vtables have the basic structure of a
166 .BR "struct sod_vtable" ,
167 which has the following members.
169 .B "const SodClass *_class"
170 A pointer to the instance's class object.
173 The offset of this chain structure
174 above the start of the overall instance layout, in bytes.
177 from the instance chain pointer
178 finds the layout base address.
180 .\"--------------------------------------------------------------------------
181 .SH BUILT-IN ROOT OBJECTS
183 This section describes the built-in classes
187 which are the standard roots of the inheritance and metaclass graphs
191 has no direct superclasses,
194 is its own metaclass.
195 It is not possible to define root classes because of circularities:
199 as its metaclass, and
203 Extensions can define additional root classes,
205 and not really to be recommended.
207 .SS The SodObject class
210 class defines no slots or messages.
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.
218 Since there are no messages,
221 also has only one chain,
222 the vtable contains only the standard class pointer and offset-to-base
224 In an actual instance of
226 (why would you want one?)
227 the class pointer contains the address of
229 and the offset is zero.
231 .SS The SodClass class
234 class defines no messages,
235 but there are a number of slots.
236 Its only direct superclass is
238 and so (like its superclass) its vtable is trivial.
240 The slots defined are as follows.
243 A pointer to the class's name.
246 A pointer to the class's nickname.
249 The size in bytes required to store an instance of the class.
251 .BI "void *(*imprint)(void *" p );
252 A pointer to a function:
257 bytes of appropriately aligned memory,
258 `imprint' this memory it so that it becomes a minimally functional
259 instance of the class:
260 all of the vtable and class pointers are properly initialized,
261 but the slots are left untouched.
262 The function returns its argument
265 .BI "void *(*init)(void *" p );
266 A pointer to a function:
271 bytes of appropriately aligned memory,
272 initialize an instance of the class in it:
273 all of the vtable and class pointers are initialized,
274 as are slots for which initializers are defined.
275 Other slots are left untouched.
276 The function returns its argument
280 The number of direct superclasses.
281 (This is zero exactly in the case of
284 .B const SodClass *const *supers;
285 A pointer to an array of
287 pointers to class objects
288 listing the class's direct superclasses,
289 in the order in which they were listed in the class definition.
293 then this pointer is null.
296 The number of superclasses in the class's class precedence list.
298 .B const SodClass *const *cpl;
299 A pointer to an array of pointers to class objects
300 listing all of the class's superclasses,
301 from most- to least-specific,
302 starting with the class itself,
307 for all class objects
310 .B const SodClass *link;
311 If the class is a chain head, then this is a null pointer;
312 otherwise it points to the class's distinguished link superclass
313 (which might or might not be a direct superclass).
315 .B const SodClass *head;
316 A pointer to the least-specific class in this class's chain;
318 .IB c ->cls.head->cls.link
330 .IB c ->cls.link->cls.head \fR.
333 The number of less specific superclasses in this class's chain.
342 .IB c ->cls.link->cls.level
346 The number of chains formed by the class's superclasses.
348 .B const struct sod_chain *chains;
349 A pointer to an array of
351 structures (see below) describing the class's superclass chains,
352 in decreasing order of specificity of their most specific classes.
353 It is always the case that
354 .IB c ->cls.chains[0].classes[ c ->cls.level]
358 .B size_t off_islots;
359 The offset of the class's
361 structure relative to its containing
364 The class doesn't define any slots if and only if this is zero.
365 (The offset can't be zero because the vtable pointer is at offset zero.)
368 The size required to store the class's direct slots,
369 i.e., the size of its
372 The class doesn't define any slots if and only if this is zero.
376 structure describes an individual chain of superclasses.
377 It has the following members.
380 The number of classes in the chain.
381 This is always at least one.
383 .B const SodClass *const *classes;
384 A pointer to an array of class pointers
385 listing the classes in the chain from least- to most-specific.
387 .IB classes [ i ]->cls.head
395 .IB classes [0]->cls.link
398 .IB classes [ i ]->cls.link
400 .IB classes [ "i\fR \- 1" ]
407 .B size_t off_ichain;
410 structure for this chain.
412 .B const struct sod_vtable *vt;
413 The vtable for this chain.
414 (It is possible, therefore, to duplicate the behaviour of the
416 function by walking the chain structure.
419 function is much faster, though.)
424 structure for this chain.
426 .\"--------------------------------------------------------------------------
427 .SH CLASS AND VTABLE LAYOUT
429 The layout algorithms for Sod instances and vtables are nontrivial.
430 They are defined here in full detail,
431 since they're effectively fixed by Sod's ABI compatibility guarantees,
432 so they might as well be documented for the sake of interoperating
435 Unfortunately, the descriptions are rather complicated,
436 and, for the most part not necessary to a working understanding of Sod.
437 The skeleton structure definitions shown should be more than enough
438 for readers attempting to make sense of the generated headers and tables.
440 In the description that follows,
441 uppercase letters vary over class names,
442 while the corresponding lowercase letters indicate the class nicknames.
443 Throughout, we consider a class
445 (therefore with nickname
448 .SS Generic instance structure
449 The entire state of an instance of
451 is contained in a single structure of type
457 struct \fIC\fB__ilayout {
458 \h'2n'union \fIC\fB__ichainu_\fIh\fB {
459 \h'4n'struct \fIC\fB__ichain_\fIh\fB {
460 \h'6n'const struct \fIC\fB__vt_\fIh\fB *_vt;
461 \h'6n'struct \fIH\fB__islots \fIh\fB;
463 \h'6n'struct \fIC\fB__islots {
464 \h'8n'\fItype\fB \fIslota\fB;
469 \h'4n'struct \fIH\fB__ichain_\fIh\fB \fIh\fB;
471 \h'2n'union \fIB\fB__ichainu_\fIi\fB \fIi\fB;
475 typedef struct \fIC\fB__ichain_\fIh\fB \fIC\fB;
479 The set of superclasses of
482 can be partitioned into chains
483 by following their distinguished superclass links.
484 (Formally, the chains are the equivalence classes determined by
485 the reflexive, symmetric, transitive closure of
486 the `links to' relation.)
487 Chains are identified by naming their least specific classes;
488 the least specific class in a chain is called the
490 Suppose that the chain head of the chain containing
494 (though keep in mind that it's possible that
502 structure contains one member for each of
505 The first such member is
513 this is followed by members
519 for each other chain,
525 the tail (most-specific) class of the chain.
526 The members are in decreasing order
527 of the specificity of the chains' most-specific classes.
528 (Note that all but the first of these unions
529 has already been defined as part of
530 the definition of the corresponding
535 union contains a member for each class in the chain.
542 and this is followed by corresponding members
549 .IR C 's superclasses
551 in the same chain in some (unimportant) order.
555 (and, indeed, defined in C's type system)
556 to be a pointer to the
558 .IB C __ichain_ h \fR.
562 structure contains (in order), a pointer
569 followed by a structure
579 in the same chain which defines slots,
580 from least- to most-specific;
584 then the last member is
592 structure simply contains one member for each slot defined by
594 in the order they appear in the class definition.
596 .SS Generic vtable structure
600 structure of an instance's storage
609 the vtables for the different chains
614 The instance layout split neatly into disjoint chains.
615 This is necessary because
618 must have as a prefix the
620 for each superclass in the same chain, and
621 each slot must be stored in exactly one place.
622 The layout of vtables doesn't have this second requirement:
623 it doesn't matter that there are
624 multiple method entry pointers
625 for the same effective method
626 as long as they all work correctly.
632 has the following general structure.
636 union \fIC\fB__vtu_\fIh\fB {
637 \h'2n'struct \fIC\fB__vt_\fIh\fB {
638 \h'4n'const \fIP\fB *_class;
641 \h'4n'const \fIQ\fB *_cls_\fIj\fB;
643 \h'4n'ptrdiff_t _off_\fIi\fB;
645 \h'4n'struct \fIC\fB__vtmsgs_\fIa\fB {
646 \h'6n'\fItype\fB (*\fImsg\fB)(\fIC\fB *, \fR...\fB);
653 extern const union \fIC\fB__vtu_\fIh\fB \fIC\fB__vtable_\fIh\fB;
676 This is mostly an irrelevant detail,
677 whose purpose is to defend against malicious compilers:
678 pointers are always to one of the inner
681 It's important only because it's the outer
683 union which is exported by name.
684 Specifically, for each chain of
687 there is an external object
697 are respectively the most and least specific classes in the chain.
699 The first member in the
702 .I root class pointer
708 Among the superclasses of
710 there must be exactly one class
712 which itself has no direct superclasses;
717 (This is a rule enforced by the Sod translator.)
730 structure of most specific superclass
737 This is followed by the
743 which is simply the offset of the
745 structure from the instance base.
747 The rest of the vtable structure is populated
748 by walking the superclass chain containing
751 For each such superclass
753 in increasing order of specificity,
754 walk the class precedence list of
756 again starting with its least-specific superclass.
757 (This complex procedure guarantees that
758 the vtable structure for a class is a prefix of
759 the vtable structure for any of its subclasses in the same chain.)
763 be some superclass of
765 which has been encountered during this traversal.
771 Examine the superclass chains of
773 in order of decreasing specificity of their most-specific classes.
776 be the chain head of such a chain,
779 be the most specific superclass of
783 Then, if there is currently no class pointer of type
793 pointing to the appropriate
800 Examine the superclass chains of
802 in order of decreasing specificity of their most-specific classes.
805 be the chain head of such a chain.
806 If there is currently no member
815 containing the (signed) offset from the
817 structure of the chain headed by
819 to that of the chain headed by
821 within the instance's layout.
826 defines any messages,
827 and there is currently no member
842 structures contain pointers to the effective method entry functions
843 for the messages defined by a superclass.
844 There may be more than one method entry for a message,
845 but all of the entry pointers for a message appear together,
846 and entry pointers for separate messages appear
847 in the order in which the messages are defined.
848 If the receiver class has no applicable primary method for a message
849 then it's usual for the method entry pointer to be null
850 (though, as with a lot of things in Sod,
851 extensions may do something different).
853 For a standard message which takes a fixed number of arguments,
864 there is always a `main' entry point,
876 For a standard message which takes a variable number of arguments,
888 two entry points are defined:
889 the usual `main' entry point
890 which accepts a variable number of
892 and a `valist' entry point
893 which accepts an argument of type
895 in place of the variable portion of the argument list.
920 .SS Additional definitions
921 In addition to the instance and vtable structures described above,
922 the following definitions are made for each class
929 there is a macro definition
934 .IB me ->_vt-> c . m ( me ,
937 which makes sending the message
939 to an instance of (any subclass of)
944 takes a variable number of arguments,
945 the macro is more complicated
946 and is only available in compilers advertising C99 support,
947 but the effect is the same.
948 For each variable-argument message,
949 there is also an additional macro
950 for calling the `valist' entry point.
961 .IB me ->_vt-> c . m __v( me ,
965 For each proper superclass
969 there is a macro defined
972 .BI * C __CONV_ a ( C
977 which converts a (static-type) pointer to
979 to a pointer to the same actual instance,
980 but statically typed as a pointer to
982 This is most useful when
984 is not in the same chain as
986 since in-chain upcasts are both trivial and rarely needed,
987 but the full set is defined for the sake of completeness.
989 Finally, the class object is defined as
991 .B extern const struct
997 .BI (& C __classobj. j . r )
1001 contains the entire class instance.
1002 This is usually rather unwieldy.
1005 is usable as a pointer of type
1011 is the root metaclass of
1013 i.e., the metaclass of the least specific superclass of
1016 .BR "const SodClass *" .
1018 .\"--------------------------------------------------------------------------
1022 .\"--------------------------------------------------------------------------
1024 Mark Wooding, <mdw@distorted.org.uk>
1026 .\"----- That's all, folks --------------------------------------------------