Commit | Line | Data |
---|---|---|
dea4d055 MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; Builtin module provides the root of the class graph | |
4 | ;;; | |
5 | ;;; (c) 2009 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
10 | ;;; This file is part of the Sensble Object Design, an object system for C. | |
11 | ;;; | |
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. | |
16 | ;;; | |
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. | |
21 | ;;; | |
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. | |
25 | ||
26 | (cl:in-package #:sod) | |
27 | ||
28 | ;;;-------------------------------------------------------------------------- | |
29 | ;;; Infrastructure. | |
30 | ||
31 | (defvar *class-slot-alist* nil) | |
32 | ||
33 | (defun add-class-slot-function (name function) | |
ea578bb4 | 34 | "Attach a slot function to the `*class-slot-alist*'. |
dea4d055 MW |
35 | |
36 | The FUNCTION is invoked with one argument, which is a `sod-class' object | |
37 | to which it should add a slot. If a function with the same NAME is | |
38 | already defined then that function is replaced; otherwise a new name/ | |
39 | function pair is defined. | |
40 | ||
41 | Functions are are invoked in the order in which their names were first | |
42 | added." | |
43 | ||
44 | (aif (assoc name *class-slot-alist* :test #'string=) | |
45 | (setf (cdr it) function) | |
46 | (asetf *class-slot-alist* (append it (list (cons name function)))))) | |
47 | ||
48 | (defmacro define-class-slot | |
49 | (name (class &optional stream) type init &body prepare) | |
50 | "Define a new class slot. | |
51 | ||
ea578bb4 MW |
52 | The slot will be called NAME (a string) and will be of TYPE (which should |
53 | be a type S-expression). The slot's (static) initializer will be | |
54 | constructed by printing the value of INIT, which is evaluated with CLASS | |
55 | bound to the class object being constructed. If any PREPARE forms are | |
56 | provided, then they are evaluated as a progn, with CLASS bound to the | |
dea4d055 MW |
57 | class object, and STREAM bound to the output stream it should write on." |
58 | ||
59 | (with-gensyms (classvar) | |
60 | `(add-class-slot-function | |
61 | ',name | |
62 | (lambda (,classvar) | |
63 | (make-sod-slot ,classvar ,name (c-type ,type) | |
52a79ab8 | 64 | (make-property-set :slot-class 'sod-class-slot |
dea4d055 MW |
65 | :initializer-function |
66 | (lambda (,class) | |
67 | ,init) | |
68 | ,@(and prepare | |
69 | `(:prepare-function | |
70 | (lambda (,class ,stream) | |
71 | ,@prepare))))))))) | |
72 | ||
73 | ;;;-------------------------------------------------------------------------- | |
74 | ;;; Basic information. | |
75 | ||
76 | (define-class-slot "name" (class) const-string | |
77 | (prin1-to-string (sod-class-name class))) | |
78 | ||
79 | (define-class-slot "nick" (class) const-string | |
80 | (prin1-to-string (sod-class-nickname class))) | |
81 | ||
82 | ;;;-------------------------------------------------------------------------- | |
83 | ;;; Instance allocation and initialization. | |
84 | ||
85 | (define-class-slot "initsz" (class) size-t | |
86 | (format nil "sizeof(struct ~A)" (ilayout-struct-tag class))) | |
87 | ||
88 | (define-class-slot "imprint" (class stream) | |
89 | (* (fun (* void) ("/*p*/" (* void)))) | |
90 | (format nil "~A__imprint" class) | |
91 | (let ((ilayout (sod-class-ilayout class))) | |
92 | (format stream "~&~: | |
93 | /* Imprint raw memory with instance structure. */ | |
94 | static void *~A__imprint(void *p) | |
95 | { | |
96 | struct ~A *sod__obj = p; | |
97 | ||
ea578bb4 | 98 | ~:{sod__obj->~A.~A._vt = &~A;~:^~% ~} |
dea4d055 MW |
99 | return (p); |
100 | }~2%" | |
101 | class | |
102 | (ilayout-struct-tag class) | |
103 | (mapcar (lambda (ichain) | |
104 | (let* ((head (ichain-head ichain)) | |
105 | (tail (ichain-tail ichain))) | |
106 | (list (sod-class-nickname head) | |
107 | (sod-class-nickname tail) | |
108 | (vtable-name class head)))) | |
109 | (ilayout-ichains ilayout))))) | |
110 | ||
111 | (define-class-slot "init" (class stream) | |
112 | (* (fun (* void) ("/*p*/" (* void)))) | |
113 | (format nil "~A__init" class) | |
114 | ||
115 | ;; FIXME this needs a metaobject protocol | |
ea578bb4 MW |
116 | (let ((ilayout (sod-class-ilayout class)) |
117 | (used nil)) | |
dea4d055 | 118 | (format stream "~&~: |
ea578bb4 MW |
119 | /* Provide initial values for an instance's slots. */ |
120 | static void *~A__init(void *p)~%{~%" class) | |
dea4d055 | 121 | (dolist (ichain (ilayout-ichains ilayout)) |
ea578bb4 | 122 | (let ((ich (format nil "sod__obj->~A.~A" |
dea4d055 MW |
123 | (sod-class-nickname (ichain-head ichain)) |
124 | (sod-class-nickname (ichain-tail ichain))))) | |
125 | (dolist (item (ichain-body ichain)) | |
126 | (etypecase item | |
127 | (vtable-pointer | |
128 | nil) | |
129 | (islots | |
130 | (let ((isl (format nil "~A.~A" | |
131 | ich | |
132 | (sod-class-nickname (islots-class item))))) | |
133 | (dolist (slot (islots-slots item)) | |
134 | (let ((dslot (effective-slot-direct-slot slot)) | |
135 | (init (effective-slot-initializer slot))) | |
136 | (when init | |
ea578bb4 MW |
137 | (unless used |
138 | (format stream | |
9ec578d9 MW |
139 | " struct ~A *sod__obj = ~A__imprint(p);~2%" |
140 | (ilayout-struct-tag class) class) | |
ea578bb4 | 141 | (setf used t)) |
dea4d055 MW |
142 | (format stream " ~A.~A =" isl |
143 | (sod-slot-name dslot)) | |
144 | (ecase (sod-initializer-value-kind init) | |
145 | (:simple (write (sod-initializer-value-form init) | |
146 | :stream stream | |
147 | :pretty nil :escape nil) | |
148 | (format stream ";~%")) | |
149 | (:compound (format stream " (~A) {" | |
150 | (sod-slot-type dslot)) | |
151 | (write (sod-initializer-value-form init) | |
152 | :stream stream | |
153 | :pretty nil :escape nil) | |
154 | (format stream "};~%")))))))))))) | |
ea578bb4 MW |
155 | (unless used |
156 | (format stream " ~A__imprint(p);~%" class)) | |
dea4d055 MW |
157 | (format stream "~&~: |
158 | return (p); | |
159 | }~2%"))) | |
160 | ||
161 | ;;;-------------------------------------------------------------------------- | |
162 | ;;; Superclass structure. | |
163 | ||
164 | (define-class-slot "n_supers" (class) size-t | |
165 | (length (sod-class-direct-superclasses class))) | |
166 | ||
167 | (define-class-slot "supers" (class stream) | |
168 | (* (* (class "SodClass" :const) :const)) | |
169 | (if (null (sod-class-direct-superclasses class)) 0 | |
170 | (format nil "~A__supers" class)) | |
171 | (let ((supers (sod-class-direct-superclasses class))) | |
172 | (when supers | |
173 | (format stream "~&~: | |
174 | /* Direct superclasses. */ | |
175 | static const SodClass *const ~A__supers[] = { | |
176 | ~{~A__class~^,~% ~} | |
177 | };~2%" | |
178 | class supers)))) | |
179 | ||
180 | (define-class-slot "n_cpl" (class) size-t | |
181 | (length (sod-class-precedence-list class))) | |
182 | ||
183 | (define-class-slot "cpl" (class stream) | |
184 | (* (* (class "SodClass" :const) :const)) | |
185 | (format nil "~A__cpl" class) | |
186 | (format stream "~&~: | |
187 | /* Class precedence list. */ | |
188 | static const SodClass *const ~A__cpl[] = { | |
189 | ~{~A__class~^,~% ~} | |
190 | };~2%" | |
191 | class (sod-class-precedence-list class))) | |
192 | ||
193 | ;;;-------------------------------------------------------------------------- | |
194 | ;;; Chain structure. | |
195 | ||
196 | (define-class-slot "link" (class) (* (class "SodClass" :const)) | |
197 | (aif (sod-class-chain-link class) | |
198 | (format nil "~A__class" it) | |
199 | 0)) | |
200 | ||
201 | (define-class-slot "head" (class) (* (class "SodClass" :const)) | |
202 | (format nil "~A__class" (sod-class-chain-head class))) | |
203 | ||
204 | (define-class-slot "level" (class) size-t | |
205 | (position class (reverse (sod-class-chain class)))) | |
206 | ||
207 | (define-class-slot "n_chains" (class) size-t | |
208 | (length (sod-class-chains class))) | |
209 | ||
210 | (define-class-slot "chains" (class stream) (* (struct "sod_chain" :const)) | |
211 | (format nil "~A__chains" class) | |
212 | (let ((chains (sod-class-chains class))) | |
213 | (format stream "~&~: | |
214 | /* Chain structure. */ | |
215 | ~1@*~:{static const SodClass *const ~A__chain_~A[] = { | |
216 | ~{~A__class~^,~% ~} | |
217 | };~:^~2%~} | |
218 | ||
219 | ~0@*static const struct sod_chain ~A__chains[] = { | |
9ec578d9 MW |
220 | ~:{ { ~ |
221 | /* n_classes = */ ~3@*~A, | |
222 | /* classes = */ ~0@*~A__chain_~A, | |
223 | /* off_ichain = */ ~4@*offsetof(struct ~A, ~A), | |
224 | /* vt = */ (const struct sod_vtable *)&~A, | |
225 | /* ichainsz = */ sizeof(struct ~A) }~:^,~%~} | |
dea4d055 MW |
226 | };~2%" |
227 | class ;0 | |
228 | (mapcar (lambda (chain) ;1 | |
229 | (let* ((head (sod-class-chain-head (car chain))) | |
230 | (chain-nick (sod-class-nickname head))) | |
9ec578d9 MW |
231 | (list class chain-nick ;0 1 |
232 | (reverse chain) ;2 | |
233 | (length chain) ;3 | |
234 | (ilayout-struct-tag class) chain-nick ;4 5 | |
235 | (vtable-name class head) ;6 | |
236 | (ichain-struct-tag (car chain) head)))) ;7 | |
dea4d055 MW |
237 | chains)))) |
238 | ||
239 | ;;;-------------------------------------------------------------------------- | |
240 | ;;; Class-specific layout. | |
241 | ||
242 | (define-class-slot "off_islots" (class) size-t | |
9ec578d9 MW |
243 | (if (sod-class-slots class) |
244 | (format nil "offsetof(struct ~A, ~A)" | |
245 | (ichain-struct-tag class (sod-class-chain-head class)) | |
246 | (sod-class-nickname class)) | |
247 | "0")) | |
dea4d055 MW |
248 | |
249 | (define-class-slot "islotsz" (class) size-t | |
9ec578d9 MW |
250 | (if (sod-class-slots class) |
251 | (format nil "sizeof(struct ~A)" | |
252 | (islots-struct-tag class)) | |
253 | "0")) | |
dea4d055 MW |
254 | |
255 | ;;;-------------------------------------------------------------------------- | |
256 | ;;; Bootstrapping the class graph. | |
257 | ||
258 | (defun bootstrap-classes (module) | |
ea578bb4 MW |
259 | "Bootstrap the braid in MODULE. |
260 | ||
261 | This builds the fundamental recursive braid, where `SodObject' is an | |
262 | instance of `SodClass', and `SodClass' is a subclass of `SodObject' (and | |
263 | an instance of itself)." | |
dea4d055 MW |
264 | (let* ((sod-object (make-sod-class "SodObject" nil |
265 | (make-property-set :nick 'obj))) | |
266 | (sod-class (make-sod-class "SodClass" (list sod-object) | |
267 | (make-property-set :nick 'cls))) | |
268 | (classes (list sod-object sod-class))) | |
269 | ||
270 | ;; Sort out the recursion. | |
271 | (setf (slot-value sod-class 'chain-link) sod-object) | |
272 | (dolist (class classes) | |
273 | (setf (slot-value class 'metaclass) sod-class)) | |
274 | ||
275 | ;; Predeclare the class types. | |
276 | (dolist (class classes) | |
277 | (make-class-type (sod-class-name class))) | |
278 | ||
279 | ;; Attach the class slots. | |
280 | (dolist (slot *class-slot-alist*) | |
281 | (funcall (cdr slot) sod-class)) | |
282 | ||
283 | ;; These classes are too closely intertwined. We must partially finalize | |
284 | ;; them together by hand. This is cloned from `finalize-sod-class'. | |
285 | (dolist (class classes) | |
286 | (with-slots (class-precedence-list chain-head chain chains) class | |
287 | (setf class-precedence-list (compute-cpl class)) | |
288 | (setf (values chain-head chain chains) (compute-chains class)))) | |
289 | ||
290 | ;; Done. | |
291 | (dolist (class classes) | |
292 | (finalize-sod-class class) | |
293 | (add-to-module module class)))) | |
294 | ||
73c17a81 | 295 | (export '*builtin-module*) |
ea578bb4 MW |
296 | (defvar *builtin-module* nil |
297 | "The builtin module.") | |
298 | ||
73c17a81 | 299 | (export 'make-builtin-module) |
dea4d055 | 300 | (defun make-builtin-module () |
ea578bb4 MW |
301 | "Construct the builtin module. |
302 | ||
9ec578d9 MW |
303 | This involves constructing the braid (which is done in |
304 | `bootstrap-classes') and defining a few obvious type names which users | |
305 | will find handy. | |
ea578bb4 MW |
306 | |
307 | Returns the newly constructed module, and stores it in the variable | |
308 | `*builtin-module*'." | |
dea4d055 MW |
309 | (let ((module (make-instance 'module |
310 | :name (make-pathname :name "SOD-BASE" | |
311 | :type "SOD" | |
312 | :case :common) | |
ea578bb4 | 313 | :state nil))) |
9ec578d9 MW |
314 | (with-module-environment (module) |
315 | (dolist (name '("va_list" "size_t" "ptrdiff_t")) | |
316 | (add-to-module module (make-instance 'type-item :name name))) | |
317 | (flet ((header-name (name) | |
318 | (concatenate 'string "\"" (string-downcase name) ".h\"")) | |
319 | (add-includes (reason &rest names) | |
320 | (let ((text (with-output-to-string (out) | |
321 | (dolist (name names) | |
322 | (format out "#include ~A~%" name))))) | |
323 | (add-to-module module | |
324 | (make-instance 'code-fragment-item | |
325 | :reason reason | |
326 | :constraints nil | |
327 | :name :includes | |
328 | :fragment text))))) | |
329 | (add-includes :c (header-name "sod")) | |
330 | (add-includes :h "<stddef.h>")) | |
331 | (bootstrap-classes module)) | |
ea578bb4 | 332 | (setf *builtin-module* module))) |
dea4d055 MW |
333 | |
334 | ;;;----- That's all, folks -------------------------------------------------- |