Add a new class slot `align', holding the instance layout alignment.
[sod] / lib / sod.h
1 /* -*-c-*-
2 *
3 * Sensible Object Design header file
4 *
5 * (c) 2009 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Sensible Object Design, an object system for C.
11 *
12 * The SOD Runtime Library is free software; you can redistribute it and/or
13 * modify 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.
16 *
17 * The SOD Runtime 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.
21 *
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.
26 */
27
28 #ifndef SOD_H
29 #define SOD_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*----- Preliminary utilities ---------------------------------------------*/
36
37 /* Various hacks for checking compiler versions. */
38 #define SOD__GCC_P(maj, min) \
39 (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
40
41 #ifdef __GNUC__
42 # define SOD__EXTENSION __extension__
43 #else
44 # define SOD__EXTENSION
45 #endif
46
47 /* --- @SOD__HAVE_VARARGS_MACROS@ --- *
48 *
49 * Use: Defined if the compiler supports C99-style variadic macros.
50 *
51 * This is more complicated than just checking the value of
52 * @__STDC_VERSION__@ because GCC has traditionally claimed C89
53 * by default, but provides the functionality anyway unless it's
54 * been explicitly turned off.
55 */
56
57 #if __STDC_VERSION__ >= 199901
58 /* The feature exists. All is well with the world. */
59
60 # define SOD__HAVE_VARARGS_MACROS
61
62 #elif SOD__GCC_P(3, 0)
63 /* We're using GCC, which is trying to deny it but we don't believe it.
64 * Unfortunately there's a fly in the ointment: if `-pedantic' -- or,
65 * worse, `-pedantic-errors' -- is set, then GCC will warn about these
66 * macros being defined, and there isn't a way to detect pedantry from the
67 * preprocessor.
68 *
69 * We must deploy bodges. There doesn't seem to be a good way to suppress
70 * particular warnings from the preprocessor: in particular, messing about
71 * with `pragma GCC diagnostic' doesn't help. So we're left with this
72 * hack: just declare all Sod-generated header files which try to do
73 * varargs macro things to be `system headers', which means that GCC's
74 * preprocessor will let them get away with all manner of nefarious stuff.
75 */
76
77 # define SOD__HAVE_VARARGS_MACROS
78 # define SOD__VARARGS_MACROS_PREAMBLE _Pragma("GCC system_header")
79
80 #endif
81
82 /* Make sure this gratuitous hack is understood, at least vacuously. */
83 #ifndef SOD__VARARGS_MACROS_PREAMBLE
84 # define SOD__VARARGS_MACROS_PREAMBLE
85 #endif
86
87 /* We're going to want to make use of this ourselves. */
88 SOD__VARARGS_MACROS_PREAMBLE
89
90 /* --- @SOD__ALIGNOF@ --- *
91 *
92 * Arguments: @type@ = a C type name, consisting of declaration specifiers
93 * and `*[QUALIFIERS]' declarator operators
94 *
95 * Returns: A sufficient alignment for objects of the given @type@, as a
96 * @size_t@.
97 */
98
99 #if __STDC_VERSION__ >= 201112
100 # define SOD__ALIGNOF(type) _Alignof(type)
101 #elif SOD__GCC_P(4, 7)
102 # define SOD__ALIGNOF(type) __extension__ _Alignof(type)
103 #elif defined(__GNUC__)
104 # define SOD__ALIGNOF(type) __alignof__(type)
105 #else
106 # define SOD__ALIGNOF(type) \
107 offsetof(struct { char sod__x; type sod__y; }, sod__y)
108 #endif
109
110 /* --- @SOD__CAR@ --- *
111 *
112 * Arguments: @...@ = a nonempty list of arguments
113 *
114 * Returns: The first argument only.
115 */
116
117 #ifdef SOD__HAVE_VARARGS_MACROS
118 # define SOD__CAR(...) SOD__CARx(__VA_LIST__, _)
119 # define SOD__CARx(a, ...) a
120 #endif
121
122 /*----- Header files ------------------------------------------------------*/
123
124 #include <stdarg.h>
125 #include <stddef.h>
126
127 #include "sod-base.h"
128
129 /*----- Data structures ---------------------------------------------------*/
130
131 /* A skeletal vtable structure. At the beginning of every ichain is a
132 * pointer to one of these.
133 */
134 struct sod_vtable {
135 const SodClass *_class; /* Pointer to class object */
136 size_t _base; /* Offset to instance base */
137 };
138
139 /* A skeletal instance structure. Every instance pointer points to one of
140 * these.
141 */
142 struct sod_instance {
143 const struct sod_vtable *_vt; /* Pointer to (chain's) vtable */
144 };
145
146 /* Information about a particular chain of superclasses. In each class,
147 * there's a pointer to an array of these. If you search hard enough, you'll
148 * be able to find out a fair amount of information about an instance and its
149 * class.
150 */
151 struct sod_chain {
152 size_t n_classes; /* Number of classes in chain */
153 const SodClass *const *classes; /* Vector of classes, head first */
154 size_t off_ichain; /* Offset of ichain from base */
155 const struct sod_vtable *vt; /* Chain's vtable pointer */
156 size_t ichainsz; /* Size of the ichain structure */
157 };
158
159 /*----- Infrastructure macros ---------------------------------------------*/
160
161 /* --- @SOD_XCHAIN@ --- *
162 *
163 * Arguments: @chead@ = nickname of target chain's head
164 * @obj@ = pointer to an instance chain
165 *
166 * Returns: Pointer to target chain, as a @void *@.
167 *
168 * Use: Utility for implementing cross-chain upcasts. It's probably
169 * not that clever to use this macro directly; it's used to make
170 * the automatically-generated upcast macros more palatable.
171 */
172
173 #define SOD_XCHAIN(chead, obj) \
174 ((void *)((char *)(obj) + (obj)->_vt->_off_##chead))
175
176 /* --- @SOD_OFFSETDIFF@ --- *
177 *
178 * Arguments: @type@ = a simple (i.e., declaratorless) type name
179 * @mema, memb@ = members of @type@
180 *
181 * Returns: The relative offset from @mema@ to @memb@, as a @ptrdiff_t@.
182 *
183 * Use: Computes a signed offset between structure members.
184 */
185
186 #define SOD_OFFSETDIFF(type, mema, memb) \
187 ((ptrdiff_t)offsetof(type, memb) - (ptrdiff_t)offsetof(type, mema))
188
189 /* --- @SOD_ILAYOUT@ --- *
190 *
191 * Arguments: @cls@ = name of a class
192 * @chead@ = nickname of chain head of @cls@
193 * @obj@ = pointer to the @chead@ ichain of an (exact) instance
194 * of @cls@
195 *
196 * Returns: A pointer to the instance's base, cast as a pointer to the
197 * ilayout structure.
198 *
199 * Use: Finds an instance's base address given a pointer to one of
200 * its ichains, if you know precisely the instance's class and
201 * which chain you're pointing to. If you don't, then (a) you
202 * want @SOD_INSTBASE@ below, and (b) you'll have the wrong
203 * ilayout anyway.
204 *
205 * This macro is not intended to be used directly outside of
206 * automatically generated effective method and trampoline
207 * functions, which have the kinds of specific knowledge
208 * necessary to use it safely.
209 */
210
211 #define SOD_ILAYOUT(cls, chead, obj) \
212 ((struct cls##__ilayout *) \
213 ((char *)(obj) - offsetof(struct cls##__ilayout, chead)))
214
215 /*----- Utility macros ----------------------------------------------------*/
216
217 /* --- @SOD_CLASSOF@ --- *
218 *
219 * Arguments: @p@ = pointer to an instance chain
220 *
221 * Returns: A pointer to the instance's class, as a @const SodClass *@.
222 */
223
224 #define SOD_CLASSOF(obj) ((const SodClass *)(obj)->_vt->_class)
225
226 /* --- @SOD_INSTBASE@ --- *
227 *
228 * Arguments: @obj@ = pointer to an instance (i.e., the address of one of
229 * its instance chains)
230 *
231 * Returns: The base address of @obj@'s instance layout, as a @void *@.
232 *
233 * Use: Finds the base address of an instance. If you know the
234 * dynamic class of the object then @SOD_ILAYOUT@ is faster. If
235 * you don't, this is the right macro, but your options for
236 * doing something sensible with the result are limited, mostly
237 * to simple memory management operations such as freeing or
238 * zeroizing the instance structure.
239 */
240
241 #define SOD_INSTBASE(obj) ((void *)((char *)(obj) - (obj)->_vt->_base))
242
243 /* --- @SOD_CONVERT@ --- *
244 *
245 * Arguments: @cls@ = a class type name
246 * @const void *obj@ = a pointer to an instance
247 *
248 * Returns: Pointer to appropriate instance ichain, or null if the
249 * instance isn't of the specified class.
250 *
251 * Use: This is a simple wrapper around the @sod_convert@, which
252 * you should see for full details. It accepts a class type
253 * name rather than a pointer to a class object, and arranges to
254 * return a pointer of the correct type.
255 */
256
257 #define SOD_CONVERT(cls, obj) ((cls *)sod_convert(cls##__class, (obj)))
258
259 /* --- @SOD_DECL@ --- *
260 *
261 * Arguments: @cls_@ = a class type name
262 * @var_@ = a variable name
263 *
264 * Use: Declare @var_@ as a pointer to an initialized instance of
265 * @cls_@ with automatic lifetime.
266 */
267
268 #define SOD_DECL(cls_, var_) \
269 struct cls_##__ilayout var_##__layout; \
270 cls_ *var_ = cls_##__class->cls.init(&var_##__layout)
271
272 /*----- Functions provided ------------------------------------------------*/
273
274 /* --- @sod_subclassp@ --- *
275 *
276 * Arguments: @const SodClass *sub, *super@ = pointers to two classes
277 *
278 * Returns: Nonzero if @c@ is a subclass of @d@.
279 */
280
281 extern int sod_subclassp(const SodClass */*sub*/, const SodClass */*super*/);
282
283 /* --- @sod_convert@ --- *
284 *
285 * Arguments: @const SodClass *cls@ = desired class object
286 * @const void *obj@ = pointer to instance
287 *
288 * Returns: Pointer to appropriate ichain of object, or null if the
289 * instance isn't of the specified class.
290 *
291 * Use: General down/cross-casting function.
292 *
293 * Upcasts can be performed efficiently using the automatically
294 * generated macros. In particular, upcasts within a chain are
295 * trivial; cross-chain upcasts require information from vtables
296 * but are fairly fast. This function is rather slower, but is
297 * much more general.
298 *
299 * Suppose we have an instance of a class C, referred to by a
300 * pointer to an instance of one of C's superclasses S. If T
301 * is some other superclass of C then this function will return
302 * a pointer to C suitable for use as an instance of T. If T
303 * is not a superclass of C, then the function returns null.
304 * (If the pointer doesn't point to an instance of some class
305 * then the behaviour is undefined.) Note that you don't need
306 * to know what either C or S actually are.
307 */
308
309 extern void *sod_convert(const SodClass */*cls*/, const void */*obj*/);
310
311 /*----- That's all, folks -------------------------------------------------*/
312
313 #ifdef __cplusplus
314 }
315 #endif
316
317 #endif