src/{builtin,final,frontent}.lisp: `clear-the-decks' makes builtin module.
[sod] / src / builtin.lisp
CommitLineData
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. */
94static void *~A__imprint(void *p)
95{
96 struct ~A *sod__obj = p;
97
c2438e62 98 ~:{sod__obj->~A.~A._vt = &~A.~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)
c2438e62
MW
108 (vtable-name class head)
109 (sod-class-nickname tail))))
dea4d055
MW
110 (ilayout-ichains ilayout)))))
111
112(define-class-slot "init" (class stream)
113 (* (fun (* void) ("/*p*/" (* void))))
114 (format nil "~A__init" class)
115
116 ;; FIXME this needs a metaobject protocol
ea578bb4
MW
117 (let ((ilayout (sod-class-ilayout class))
118 (used nil))
dea4d055 119 (format stream "~&~:
ea578bb4
MW
120/* Provide initial values for an instance's slots. */
121static void *~A__init(void *p)~%{~%" class)
dea4d055 122 (dolist (ichain (ilayout-ichains ilayout))
ea578bb4 123 (let ((ich (format nil "sod__obj->~A.~A"
dea4d055
MW
124 (sod-class-nickname (ichain-head ichain))
125 (sod-class-nickname (ichain-tail ichain)))))
126 (dolist (item (ichain-body ichain))
127 (etypecase item
128 (vtable-pointer
129 nil)
130 (islots
131 (let ((isl (format nil "~A.~A"
132 ich
133 (sod-class-nickname (islots-class item)))))
134 (dolist (slot (islots-slots item))
135 (let ((dslot (effective-slot-direct-slot slot))
136 (init (effective-slot-initializer slot)))
137 (when init
ea578bb4
MW
138 (unless used
139 (format stream
9ec578d9
MW
140 " struct ~A *sod__obj = ~A__imprint(p);~2%"
141 (ilayout-struct-tag class) class)
ea578bb4 142 (setf used t))
dea4d055
MW
143 (format stream " ~A.~A =" isl
144 (sod-slot-name dslot))
145 (ecase (sod-initializer-value-kind init)
146 (:simple (write (sod-initializer-value-form init)
147 :stream stream
148 :pretty nil :escape nil)
149 (format stream ";~%"))
150 (:compound (format stream " (~A) {"
151 (sod-slot-type dslot))
152 (write (sod-initializer-value-form init)
153 :stream stream
154 :pretty nil :escape nil)
155 (format stream "};~%"))))))))))))
ea578bb4
MW
156 (unless used
157 (format stream " ~A__imprint(p);~%" class))
dea4d055
MW
158 (format stream "~&~:
159 return (p);
160}~2%")))
161
162;;;--------------------------------------------------------------------------
163;;; Superclass structure.
164
165(define-class-slot "n_supers" (class) size-t
166 (length (sod-class-direct-superclasses class)))
167
168(define-class-slot "supers" (class stream)
169 (* (* (class "SodClass" :const) :const))
170 (if (null (sod-class-direct-superclasses class)) 0
171 (format nil "~A__supers" class))
172 (let ((supers (sod-class-direct-superclasses class)))
173 (when supers
174 (format stream "~&~:
175/* Direct superclasses. */
176static const SodClass *const ~A__supers[] = {
177 ~{~A__class~^,~% ~}
178};~2%"
179 class supers))))
180
181(define-class-slot "n_cpl" (class) size-t
182 (length (sod-class-precedence-list class)))
183
184(define-class-slot "cpl" (class stream)
185 (* (* (class "SodClass" :const) :const))
186 (format nil "~A__cpl" class)
187 (format stream "~&~:
188/* Class precedence list. */
189static const SodClass *const ~A__cpl[] = {
190 ~{~A__class~^,~% ~}
191};~2%"
192 class (sod-class-precedence-list class)))
193
194;;;--------------------------------------------------------------------------
195;;; Chain structure.
196
197(define-class-slot "link" (class) (* (class "SodClass" :const))
198 (aif (sod-class-chain-link class)
199 (format nil "~A__class" it)
200 0))
201
202(define-class-slot "head" (class) (* (class "SodClass" :const))
203 (format nil "~A__class" (sod-class-chain-head class)))
204
205(define-class-slot "level" (class) size-t
206 (position class (reverse (sod-class-chain class))))
207
208(define-class-slot "n_chains" (class) size-t
209 (length (sod-class-chains class)))
210
211(define-class-slot "chains" (class stream) (* (struct "sod_chain" :const))
212 (format nil "~A__chains" class)
213 (let ((chains (sod-class-chains class)))
214 (format stream "~&~:
215/* Chain structure. */
216~1@*~:{static const SodClass *const ~A__chain_~A[] = {
217 ~{~A__class~^,~% ~}
218};~:^~2%~}
219
220~0@*static const struct sod_chain ~A__chains[] = {
9ec578d9
MW
221~:{ { ~
222 /* n_classes = */ ~3@*~A,
223 /* classes = */ ~0@*~A__chain_~A,
224 /* off_ichain = */ ~4@*offsetof(struct ~A, ~A),
225 /* vt = */ (const struct sod_vtable *)&~A,
226 /* ichainsz = */ sizeof(struct ~A) }~:^,~%~}
dea4d055
MW
227};~2%"
228 class ;0
229 (mapcar (lambda (chain) ;1
230 (let* ((head (sod-class-chain-head (car chain)))
231 (chain-nick (sod-class-nickname head)))
9ec578d9
MW
232 (list class chain-nick ;0 1
233 (reverse chain) ;2
234 (length chain) ;3
235 (ilayout-struct-tag class) chain-nick ;4 5
236 (vtable-name class head) ;6
237 (ichain-struct-tag (car chain) head)))) ;7
dea4d055
MW
238 chains))))
239
240;;;--------------------------------------------------------------------------
241;;; Class-specific layout.
242
243(define-class-slot "off_islots" (class) size-t
9ec578d9
MW
244 (if (sod-class-slots class)
245 (format nil "offsetof(struct ~A, ~A)"
246 (ichain-struct-tag class (sod-class-chain-head class))
247 (sod-class-nickname class))
248 "0"))
dea4d055
MW
249
250(define-class-slot "islotsz" (class) size-t
9ec578d9
MW
251 (if (sod-class-slots class)
252 (format nil "sizeof(struct ~A)"
253 (islots-struct-tag class))
254 "0"))
dea4d055
MW
255
256;;;--------------------------------------------------------------------------
257;;; Bootstrapping the class graph.
258
259(defun bootstrap-classes (module)
ea578bb4
MW
260 "Bootstrap the braid in MODULE.
261
262 This builds the fundamental recursive braid, where `SodObject' is an
263 instance of `SodClass', and `SodClass' is a subclass of `SodObject' (and
264 an instance of itself)."
dea4d055
MW
265 (let* ((sod-object (make-sod-class "SodObject" nil
266 (make-property-set :nick 'obj)))
267 (sod-class (make-sod-class "SodClass" (list sod-object)
268 (make-property-set :nick 'cls)))
269 (classes (list sod-object sod-class)))
270
271 ;; Sort out the recursion.
272 (setf (slot-value sod-class 'chain-link) sod-object)
273 (dolist (class classes)
274 (setf (slot-value class 'metaclass) sod-class))
275
276 ;; Predeclare the class types.
277 (dolist (class classes)
278 (make-class-type (sod-class-name class)))
279
280 ;; Attach the class slots.
281 (dolist (slot *class-slot-alist*)
282 (funcall (cdr slot) sod-class))
283
284 ;; These classes are too closely intertwined. We must partially finalize
285 ;; them together by hand. This is cloned from `finalize-sod-class'.
286 (dolist (class classes)
287 (with-slots (class-precedence-list chain-head chain chains) class
288 (setf class-precedence-list (compute-cpl class))
289 (setf (values chain-head chain chains) (compute-chains class))))
290
291 ;; Done.
292 (dolist (class classes)
293 (finalize-sod-class class)
294 (add-to-module module class))))
295
73c17a81 296(export '*builtin-module*)
ea578bb4
MW
297(defvar *builtin-module* nil
298 "The builtin module.")
299
73c17a81 300(export 'make-builtin-module)
dea4d055 301(defun make-builtin-module ()
ea578bb4
MW
302 "Construct the builtin module.
303
9ec578d9
MW
304 This involves constructing the braid (which is done in
305 `bootstrap-classes') and defining a few obvious type names which users
306 will find handy.
ea578bb4
MW
307
308 Returns the newly constructed module, and stores it in the variable
309 `*builtin-module*'."
dea4d055
MW
310 (let ((module (make-instance 'module
311 :name (make-pathname :name "SOD-BASE"
312 :type "SOD"
313 :case :common)
ea578bb4 314 :state nil)))
9ec578d9
MW
315 (with-module-environment (module)
316 (dolist (name '("va_list" "size_t" "ptrdiff_t"))
317 (add-to-module module (make-instance 'type-item :name name)))
318 (flet ((header-name (name)
319 (concatenate 'string "\"" (string-downcase name) ".h\""))
320 (add-includes (reason &rest names)
321 (let ((text (with-output-to-string (out)
322 (dolist (name names)
323 (format out "#include ~A~%" name)))))
324 (add-to-module module
325 (make-instance 'code-fragment-item
326 :reason reason
327 :constraints nil
328 :name :includes
329 :fragment text)))))
330 (add-includes :c (header-name "sod"))
331 (add-includes :h "<stddef.h>"))
332 (bootstrap-classes module))
ea578bb4 333 (setf *builtin-module* module)))
dea4d055 334
54c01772
MW
335(define-clear-the-decks builtin-module
336 (unless *builtin-module* (make-builtin-module)))
337
dea4d055 338;;;----- That's all, folks --------------------------------------------------