src/output-{proto,impl}.lisp: `sequencer''s :constraints initarg takes names.
[sod] / src / class-output.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Output for classes
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 ;;; Walking the layout tree.
30
31 (defmethod hook-output progn ((class sod-class) reason sequencer)
32 (with-slots ((ilayout %ilayout) vtables methods effective-methods) class
33 (hook-output ilayout reason sequencer)
34 (dolist (method methods) (hook-output method reason sequencer))
35 (dolist (method effective-methods) (hook-output method reason sequencer))
36 (dolist (vtable vtables) (hook-output vtable reason sequencer))))
37
38 (defmethod hook-output progn ((ilayout ilayout) reason sequencer)
39 (with-slots (ichains) ilayout
40 (dolist (ichain ichains) (hook-output ichain reason sequencer))))
41
42 (defmethod hook-output progn ((ichain ichain) reason sequencer)
43 (dolist (item (ichain-body ichain))
44 (hook-output item reason sequencer)))
45
46 (defmethod hook-output progn ((islots islots) reason sequencer)
47 (dolist (slot (islots-slots islots))
48 (hook-output slot reason sequencer)))
49
50 (defmethod hook-output progn ((vtable vtable) reason sequencer)
51 (with-slots (body) vtable
52 (dolist (item body) (hook-output item reason sequencer))))
53
54 (defmethod hook-output progn ((vtmsgs vtmsgs) reason sequencer)
55 (with-slots (entries) vtmsgs
56 (dolist (entry entries) (hook-output entry reason sequencer))))
57
58 ;;;--------------------------------------------------------------------------
59 ;;; Classes.
60
61 (defmethod hook-output progn ((class sod-class) (reason (eql :h)) sequencer)
62
63 ;; Main output sequencing.
64 (sequence-output (stream sequencer)
65
66 :constraint
67 ((:classes :start)
68 (class :banner)
69 (class :islots :start) (class :islots :slots) (class :islots :end)
70 (class :vtmsgs :start) (class :vtmsgs :end)
71 (class :vtables :start) (class :vtables :end)
72 (class :vtable-externs) (class :vtable-externs-after)
73 (class :methods :start) (class :methods) (class :methods :end)
74 (class :ichains :start) (class :ichains :end)
75 (class :ilayout :start) (class :ilayout :slots) (class :ilayout :end)
76 (class :conversions)
77 (class :message-macros)
78 (class :object)
79 (:classes :end))
80
81 (:typedefs
82 (format stream "typedef struct ~A ~A;~%"
83 (ichain-struct-tag class (sod-class-chain-head class)) class))
84
85 ((class :banner)
86 (banner (format nil "Class ~A" class) stream))
87 ((class :vtable-externs-after)
88 (terpri stream))
89
90 ((class :vtable-externs)
91 (format stream "/* Vtable structures. */~%"))
92
93 ((class :object)
94 (let ((metaclass (sod-class-metaclass class))
95 (metaroot (find-root-metaclass class)))
96 (format stream "/* The class object. */~@
97 extern const struct ~A ~A__classobj;~@
98 #define ~:*~A__class (&~:*~A__classobj.~A.~A)~2%"
99 (ilayout-struct-tag metaclass) class
100 (sod-class-nickname (sod-class-chain-head metaroot))
101 (sod-class-nickname metaroot)))))
102
103 ;; Maybe generate an islots structure.
104 (when (sod-class-slots class)
105 (dolist (slot (sod-class-slots class))
106 (hook-output slot 'islots sequencer))
107 (sequence-output (stream sequencer)
108 ((class :islots :start)
109 (format stream "/* Instance slots. */~@
110 struct ~A {~%"
111 (islots-struct-tag class)))
112 ((class :islots :end)
113 (format stream "};~2%"))))
114
115 ;; Declare the direct methods.
116 (when (sod-class-methods class)
117 (sequence-output (stream sequencer)
118 ((class :methods :start)
119 (format stream "/* Direct methods. */~%"))
120 ((class :methods :end)
121 (terpri stream))))
122
123 ;; Provide upcast macros which do the right thing.
124 (when (sod-class-direct-superclasses class)
125 (sequence-output (stream sequencer)
126 ((class :conversions)
127 (let ((chain-head (sod-class-chain-head class)))
128 (format stream "/* Conversion macros. */~%")
129 (dolist (super (cdr (sod-class-precedence-list class)))
130 (let ((super-head (sod-class-chain-head super)))
131 (format stream "#define ~:@(~A__CONV_~A~)(_obj) ((~A *)~
132 ~:[SOD_XCHAIN(~A, (_obj))~;(_obj)~])~%"
133 class (sod-class-nickname super) super
134 (eq chain-head super-head)
135 (sod-class-nickname super-head))))
136 (terpri stream)))))
137
138 ;; Provide convenience macros for sending the newly defined messages. (The
139 ;; macros work on all subclasses too.)
140 ;;
141 ;; We need each message's method entry type for this, so we need to dig it
142 ;; out of the vtmsgs structure. Indeed, the vtmsgs for this class contains
143 ;; entries for precisely the messages we want to make macros for.
144 (when (sod-class-messages class)
145 (sequence-output (stream sequencer)
146 ((class :message-macros)
147 (let* ((vtable (find (sod-class-chain-head class)
148 (sod-class-vtables class)
149 :key #'vtable-chain-head))
150 (vtmsgs (find-if (lambda (item)
151 (and (typep item 'vtmsgs)
152 (eql (vtmsgs-class item) class)))
153 (vtable-body vtable))))
154 (format stream "/* Message invocation macros. */~%")
155 (dolist (entry (vtmsgs-entries vtmsgs))
156 (let* ((type (method-entry-function-type entry))
157 (args (c-function-arguments type))
158 (in-names nil) (out-names nil) (varargsp nil) (me "me"))
159 (do ((args args (cdr args)))
160 ((endp args))
161 (let* ((raw-name (princ-to-string (argument-name (car args))))
162 (name (if (find raw-name
163 (list "_vt"
164 (sod-class-nickname class)
165 (method-entry-slot-name entry))
166 :test #'string=)
167 (format nil "sod__a_~A" raw-name)
168 raw-name)))
169 (cond ((and (cdr args) (eq (cadr args) :ellipsis))
170 (setf varargsp t)
171 (unless in-names (setf me "SOD_CAR(__VA_ARGS__)"))
172 (push (format nil "/*~A*/ ..." name) in-names)
173 (push "__VA_ARGS__" out-names)
174 (return))
175 (t
176 (push name in-names)
177 (push name out-names)))))
178 (when varargsp
179 (format stream "#if __STDC_VERSION__ >= 199901~%"))
180 (format stream "#define ~A(~{~A~^, ~}) ~
181 ~A->_vt->~A.~A(~{~A~^, ~})~%"
182 (message-macro-name class entry)
183 (nreverse in-names)
184 me
185 (sod-class-nickname class)
186 (method-entry-slot-name entry)
187 (nreverse out-names))
188 (when varargsp
189 (format stream "#endif~%"))))
190 (terpri stream)))))
191
192 ;; Generate vtmsgs structure for all superclasses.
193 (hook-output (car (sod-class-vtables class)) 'vtmsgs sequencer))
194
195 ;;;--------------------------------------------------------------------------
196 ;;; Instance structure.
197
198 (defmethod hook-output progn ((slot sod-slot)
199 (reason (eql 'islots))
200 sequencer)
201 (sequence-output (stream sequencer)
202 (((sod-slot-class slot) :islots :slots)
203 (pprint-logical-block (stream nil :prefix " " :suffix ";")
204 (pprint-c-type (sod-slot-type slot) stream (sod-slot-name slot)))
205 (terpri stream))))
206
207 (defmethod hook-output progn ((ilayout ilayout) (reason (eql :h)) sequencer)
208 (with-slots ((class %class) ichains) ilayout
209 (sequence-output (stream sequencer)
210 ((class :ilayout :start)
211 (format stream "/* Instance layout. */~@
212 struct ~A {~%"
213 (ilayout-struct-tag class)))
214 ((class :ilayout :end)
215 (format stream "};~2%")))
216 (dolist (ichain ichains)
217 (hook-output ichain 'ilayout sequencer))))
218
219 (defmethod hook-output progn ((ichain ichain) (reason (eql :h)) sequencer)
220 (with-slots ((class %class) chain-head chain-tail) ichain
221 (when (eq class chain-tail)
222 (sequence-output (stream sequencer)
223 :constraint ((class :ichains :start)
224 (class :ichain chain-head :start)
225 (class :ichain chain-head :slots)
226 (class :ichain chain-head :end)
227 (class :ichains :end))
228 ((class :ichain chain-head :start)
229 (format stream "/* Instance chain structure. */~@
230 struct ~A {~%"
231 (ichain-struct-tag chain-tail chain-head)))
232 ((class :ichain chain-head :end)
233 (format stream "};~2%")
234 (format stream "/* Union of equivalent superclass chains. */~@
235 union ~A {~@
236 ~:{ struct ~A ~A;~%~}~
237 };~2%"
238 (ichain-union-tag chain-tail chain-head)
239
240 ;; Make sure the most specific class is first: only the
241 ;; first element of a union can be statically initialized in
242 ;; C90.
243 (mapcar (lambda (super)
244 (list (ichain-struct-tag super chain-head)
245 (sod-class-nickname super)))
246 (sod-class-chain chain-tail))))))))
247
248 (defmethod hook-output progn ((ichain ichain)
249 (reason (eql 'ilayout))
250 sequencer)
251 (with-slots ((class %class) chain-head chain-tail) ichain
252 (sequence-output (stream sequencer)
253 ((class :ilayout :slots)
254 (format stream " union ~A ~A;~%"
255 (ichain-union-tag chain-tail chain-head)
256 (sod-class-nickname chain-head))))))
257
258 (defmethod hook-output progn ((vtptr vtable-pointer)
259 (reason (eql :h))
260 sequencer)
261 (with-slots ((class %class) chain-head chain-tail) vtptr
262 (sequence-output (stream sequencer)
263 ((class :ichain chain-head :slots)
264 (format stream " const struct ~A *_vt;~%"
265 (vtable-struct-tag chain-tail chain-head))))))
266
267 (defmethod hook-output progn ((islots islots) (reason (eql :h)) sequencer)
268 (with-slots ((class %class) subclass slots) islots
269 (sequence-output (stream sequencer)
270 ((subclass :ichain (sod-class-chain-head class) :slots)
271 (format stream " struct ~A ~A;~%"
272 (islots-struct-tag class)
273 (sod-class-nickname class))))))
274
275 ;;;--------------------------------------------------------------------------
276 ;;; Vtable structure.
277
278 (defmethod hook-output progn ((method sod-method)
279 (reason (eql :h))
280 sequencer)
281 (with-slots ((class %class)) method
282 (sequence-output (stream sequencer)
283 ((class :methods)
284 (let ((type (sod-method-function-type method)))
285 (princ "extern " stream)
286 (pprint-c-type (commentify-function-type type) stream
287 (sod-method-function-name method))
288 (format stream ";~%"))))))
289
290 (defmethod hook-output progn ((vtable vtable) (reason (eql :h)) sequencer)
291 (with-slots ((class %class) chain-head chain-tail) vtable
292 (when (eq class chain-tail)
293 (sequence-output (stream sequencer)
294 :constraint ((class :vtables :start)
295 (class :vtable chain-head :start)
296 (class :vtable chain-head :slots)
297 (class :vtable chain-head :end)
298 (class :vtables :end))
299 ((class :vtable chain-head :start)
300 (format stream "/* Vtable structure. */~@
301 struct ~A {~%"
302 (vtable-struct-tag chain-tail chain-head)))
303 ((class :vtable chain-head :end)
304 (format stream "};~2%")
305 (format stream "/* Union of equivalent superclass vtables. */~@
306 union ~A {~@
307 ~:{ struct ~A ~A;~%~}~
308 };~2%"
309 (vtable-union-tag chain-tail chain-head)
310
311 ;; As for the ichain union, make sure the most specific
312 ;; class is first.
313 (mapcar (lambda (super)
314 (list (vtable-struct-tag super chain-head)
315 (sod-class-nickname super)))
316 (sod-class-chain chain-tail))))))
317 (sequence-output (stream sequencer)
318 ((class :vtable-externs)
319 (format stream "~@<extern const union ~A ~2I~_~A;~:>~%"
320 (vtable-union-tag chain-tail chain-head)
321 (vtable-name class chain-head))))))
322
323 (defmethod hook-output progn ((vtmsgs vtmsgs) (reason (eql :h)) sequencer)
324 (with-slots ((class %class) subclass chain-head chain-tail) vtmsgs
325 (sequence-output (stream sequencer)
326 ((subclass :vtable chain-head :slots)
327 (format stream " struct ~A ~A;~%"
328 (vtmsgs-struct-tag subclass class)
329 (sod-class-nickname class))))))
330
331 (defmethod hook-output progn ((vtmsgs vtmsgs)
332 (reason (eql 'vtmsgs))
333 sequencer)
334 (when (vtmsgs-entries vtmsgs)
335 (with-slots ((class %class) subclass) vtmsgs
336 (sequence-output (stream sequencer)
337 :constraint ((subclass :vtmsgs :start)
338 (subclass :vtmsgs class :start)
339 (subclass :vtmsgs class :slots)
340 (subclass :vtmsgs class :end)
341 (subclass :vtmsgs :end))
342 ((subclass :vtmsgs class :start)
343 (format stream "/* Messages protocol from class ~A */~@
344 struct ~A {~%"
345 class
346 (vtmsgs-struct-tag subclass class)))
347 ((subclass :vtmsgs class :end)
348 (format stream "};~2%"))))))
349
350 (defmethod hook-output progn ((entry method-entry)
351 (reason (eql 'vtmsgs))
352 sequencer)
353 (let* ((method (method-entry-effective-method entry))
354 (message (effective-method-message method))
355 (class (effective-method-class method))
356 (function-type (method-entry-function-type entry))
357 (commented-type (commentify-function-type function-type))
358 (pointer-type (make-pointer-type commented-type)))
359 (sequence-output (stream sequencer)
360 ((class :vtmsgs (sod-message-class message) :slots)
361 (pprint-logical-block (stream nil :prefix " " :suffix ";")
362 (pprint-c-type pointer-type stream (method-entry-slot-name entry)))
363 (terpri stream)))))
364
365 (defmethod hook-output progn ((cptr class-pointer)
366 (reason (eql :h))
367 sequencer)
368 (with-slots ((class %class) chain-head metaclass meta-chain-head) cptr
369 (sequence-output (stream sequencer)
370 ((class :vtable chain-head :slots)
371 (format stream " const ~A *~:[_class~;~:*_cls_~A~];~%"
372 metaclass
373 (and (sod-class-direct-superclasses meta-chain-head)
374 (sod-class-nickname meta-chain-head)))))))
375
376 (defmethod hook-output progn ((boff base-offset) (reason (eql :h)) sequencer)
377 (with-slots ((class %class) chain-head) boff
378 (sequence-output (stream sequencer)
379 ((class :vtable chain-head :slots)
380 (write-line " size_t _base;" stream)))))
381
382 (defmethod hook-output progn ((choff chain-offset)
383 (reason (eql :h))
384 sequencer)
385 (with-slots ((class %class) chain-head target-head) choff
386 (sequence-output (stream sequencer)
387 ((class :vtable chain-head :slots)
388 (format stream " ptrdiff_t _off_~A;~%"
389 (sod-class-nickname target-head))))))
390
391 ;;;--------------------------------------------------------------------------
392 ;;; Implementation output.
393
394 (export '*instance-class*)
395 (defvar *instance-class*)
396
397 (defmethod hook-output progn ((class sod-class) (reason (eql :c)) sequencer)
398 (sequence-output (stream sequencer)
399
400 :constraint
401 ((:classes :start)
402 (class :banner)
403 (class :direct-methods :start) (class :direct-methods :end)
404 (class :effective-methods)
405 (class :vtables :start) (class :vtables :end)
406 (class :object :prepare) (class :object :start) (class :object :end)
407 (:classes :end))
408
409 ((class :banner)
410 (banner (format nil "Class ~A" class) stream))
411
412 ((class :object :start)
413 (format stream "~
414 /* The class object. */
415 const struct ~A ~A__classobj = {~%"
416 (ilayout-struct-tag (sod-class-metaclass class))
417 class))
418 ((class :object :end)
419 (format stream "};~2%")))
420
421 (let ((*instance-class* class))
422 (hook-output (sod-class-ilayout (sod-class-metaclass class))
423 'class sequencer)))
424
425 ;;;--------------------------------------------------------------------------
426 ;;; Direct and effective methods.
427
428 (defmethod hook-output progn ((method delegating-direct-method)
429 (reason (eql :c))
430 sequencer)
431 (with-slots ((class %class) body) method
432 (unless body
433 (return-from hook-output))
434 (sequence-output (stream sequencer)
435 ((class :direct-method method :start)
436 (format stream "#define CALL_NEXT_METHOD (next_method(~{~A~^, ~}))~%"
437 (mapcar #'argument-name
438 (c-function-arguments (sod-method-next-method-type
439 method)))))
440 ((class :direct-method method :end)
441 (format stream "#undef CALL_NEXT_METHOD~%")))))
442
443 (defmethod hook-output progn ((method sod-method)
444 (reason (eql :c))
445 sequencer)
446 (with-slots ((class %class) body) method
447 (unless body
448 (return-from hook-output))
449 (sequence-output (stream sequencer)
450 :constraint ((class :direct-methods :start)
451 (class :direct-method method :start)
452 (class :direct-method method :body)
453 (class :direct-method method :end)
454 (class :direct-methods :end))
455 ((class :direct-method method :body)
456 (pprint-c-type (sod-method-function-type method)
457 stream
458 (sod-method-function-name method))
459 (format stream "~&{~%")
460 (write body :stream stream :pretty nil :escape nil)
461 (format stream "~&}~%"))
462 ((class :direct-method method :end)
463 (terpri stream)))))
464
465 (defmethod hook-output progn ((method basic-effective-method)
466 (reason (eql :c))
467 sequencer)
468 (with-slots ((class %class) functions) method
469 (sequence-output (stream sequencer)
470 ((class :effective-methods)
471 (dolist (func functions)
472 (write func :stream stream :escape nil :circle nil))))))
473
474 ;;;--------------------------------------------------------------------------
475 ;;; Vtables.
476
477 (defmethod hook-output progn ((vtable vtable) (reason (eql :c)) sequencer)
478 (with-slots ((class %class) chain-head chain-tail) vtable
479 (sequence-output (stream sequencer)
480 :constraint ((class :vtables :start)
481 (class :vtable chain-head :start)
482 (class :vtable chain-head :end)
483 (class :vtables :end))
484 ((class :vtable chain-head :start)
485 (format stream "/* Vtable for ~A chain. */~@
486 const union ~A ~A = { {~%"
487 chain-head
488 (vtable-union-tag chain-tail chain-head)
489 (vtable-name class chain-head)))
490 ((class :vtable chain-head :end)
491 (format stream "} };~2%")))))
492
493 (defmethod hook-output progn ((cptr class-pointer)
494 (reason (eql :c))
495 sequencer)
496 (with-slots ((class %class) chain-head metaclass meta-chain-head) cptr
497 (sequence-output (stream sequencer)
498 :constraint ((class :vtable chain-head :start)
499 (class :vtable chain-head :class-pointer metaclass)
500 (class :vtable chain-head :end))
501 ((class :vtable chain-head :class-pointer metaclass)
502 (format stream " /* ~21@A = */ &~A__classobj.~A.~A,~%"
503 (if (sod-class-direct-superclasses meta-chain-head)
504 (format nil "_cls_~A"
505 (sod-class-nickname meta-chain-head))
506 "_class")
507 class
508 (sod-class-nickname meta-chain-head)
509 (sod-class-nickname metaclass))))))
510
511 (defmethod hook-output progn ((boff base-offset) (reason (eql :c)) sequencer)
512 (with-slots ((class %class) chain-head) boff
513 (sequence-output (stream sequencer)
514 :constraint ((class :vtable chain-head :start)
515 (class :vtable chain-head :base-offset)
516 (class :vtable chain-head :end))
517 ((class :vtable chain-head :base-offset)
518 (format stream " /* ~21@A = */ offsetof(struct ~A, ~A),~%"
519 "_base"
520 (ilayout-struct-tag class)
521 (sod-class-nickname chain-head))))))
522
523 (defmethod hook-output progn ((choff chain-offset)
524 (reason (eql :c))
525 sequencer)
526 (with-slots ((class %class) chain-head target-head) choff
527 (sequence-output (stream sequencer)
528 :constraint ((class :vtable chain-head :start)
529 (class :vtable chain-head :chain-offset target-head)
530 (class :vtable chain-head :end))
531 ((class :vtable chain-head :chain-offset target-head)
532 (format stream " /* ~21@A = */ SOD_OFFSETDIFF(struct ~A, ~A, ~A),~%"
533 (format nil "_off_~A" (sod-class-nickname target-head))
534 (ilayout-struct-tag class)
535 (sod-class-nickname chain-head)
536 (sod-class-nickname target-head))))))
537
538 (defmethod hook-output progn ((vtmsgs vtmsgs) (reason (eql :c)) sequencer)
539 (with-slots ((class %class) subclass chain-head) vtmsgs
540 (sequence-output (stream sequencer)
541 :constraint ((subclass :vtable chain-head :start)
542 (subclass :vtable chain-head :vtmsgs class :start)
543 (subclass :vtable chain-head :vtmsgs class :slots)
544 (subclass :vtable chain-head :vtmsgs class :end)
545 (subclass :vtable chain-head :end))
546 ((subclass :vtable chain-head :vtmsgs class :start)
547 (format stream " { /* Method entries for ~A messages. */~%"
548 class))
549 ((subclass :vtable chain-head :vtmsgs class :end)
550 (format stream " },~%")))))
551
552 (defmethod hook-output progn ((entry method-entry)
553 (reason (eql :c))
554 sequencer)
555 (with-slots ((method %method) chain-head chain-tail role) entry
556 (let* ((message (effective-method-message method))
557 (class (effective-method-class method))
558 (super (sod-message-class message)))
559 (sequence-output (stream sequencer)
560 ((class :vtable chain-head :vtmsgs super :slots)
561 (format stream " /* ~19@A = */ ~A,~%"
562 (method-entry-slot-name entry)
563 (method-entry-function-name method chain-head role)))))))
564
565 ;;;--------------------------------------------------------------------------
566 ;;; Filling in the class object.
567
568 (defmethod hook-output progn ((ichain ichain)
569 (reason (eql 'class))
570 sequencer)
571 (with-slots ((class %class) chain-head) ichain
572 (sequence-output (stream sequencer)
573 :constraint ((*instance-class* :object :start)
574 (*instance-class* :object chain-head :ichain :start)
575 (*instance-class* :object chain-head :ichain :end)
576 (*instance-class* :object :end))
577 ((*instance-class* :object chain-head :ichain :start)
578 (format stream " { { /* ~A ichain */~%"
579 (sod-class-nickname chain-head)))
580 ((*instance-class* :object chain-head :ichain :end)
581 (format stream " } },~%")))))
582
583 (defmethod hook-output progn ((islots islots)
584 (reason (eql 'class))
585 sequencer)
586 (with-slots ((class %class)) islots
587 (let ((chain-head (sod-class-chain-head class)))
588 (sequence-output (stream sequencer)
589 :constraint ((*instance-class* :object chain-head :ichain :start)
590 (*instance-class* :object class :slots :start)
591 (*instance-class* :object class :slots)
592 (*instance-class* :object class :slots :end)
593 (*instance-class* :object chain-head :ichain :end))
594 ((*instance-class* :object class :slots :start)
595 (format stream " { /* Class ~A */~%" class))
596 ((*instance-class* :object class :slots :end)
597 (format stream " },~%"))))))
598
599 (defmethod hook-output progn ((vtptr vtable-pointer)
600 (reason (eql 'class))
601 sequencer)
602 (with-slots ((class %class) chain-head chain-tail) vtptr
603 (sequence-output (stream sequencer)
604 :constraint ((*instance-class* :object chain-head :ichain :start)
605 (*instance-class* :object chain-head :vtable)
606 (*instance-class* :object chain-head :ichain :end))
607 ((*instance-class* :object chain-head :vtable)
608 (format stream " /* ~17@A = */ &~A.~A,~%"
609 "_vt"
610 (vtable-name class chain-head)
611 (sod-class-nickname chain-tail))))))
612
613 (defgeneric find-class-initializer (slot class)
614 (:method ((slot effective-slot) (class sod-class))
615 (let ((dslot (effective-slot-direct-slot slot)))
616 (or (some (lambda (super)
617 (find dslot (sod-class-class-initializers super)
618 :test #'sod-initializer-slot))
619 (sod-class-precedence-list class))
620 (effective-slot-initializer slot)))))
621
622 (defgeneric output-class-initializer (slot instance stream)
623 (:method ((slot sod-class-effective-slot) (instance sod-class) stream)
624 (let ((func (effective-slot-initializer-function slot))
625 (direct-slot (effective-slot-direct-slot slot)))
626 (if func
627 (format stream " /* ~15@A = */ ~A,~%"
628 (sod-slot-name direct-slot)
629 (funcall func instance))
630 (call-next-method))))
631 (:method ((slot effective-slot) (instance sod-class) stream)
632 (let ((init (find-class-initializer slot instance))
633 (direct-slot (effective-slot-direct-slot slot)))
634 (ecase (sod-initializer-value-kind init)
635 (:simple (format stream " /* ~15@A = */ ~A,~%"
636 (sod-slot-name direct-slot)
637 (sod-initializer-value-form init)))
638 (:compound (format stream " /* ~15@A = */ ~@<{ ~;~A~; },~:>~%"
639 (sod-slot-name direct-slot)
640 (sod-initializer-value-form init)))))))
641
642 (defmethod hook-output progn ((slot sod-class-effective-slot)
643 (reason (eql 'class))
644 sequencer)
645 (let ((instance *instance-class*)
646 (func (effective-slot-prepare-function slot)))
647 (when func
648 (sequence-output (stream sequencer)
649 ((instance :object :prepare)
650 (funcall func instance stream))))))
651
652 (defmethod hook-output progn ((slot effective-slot)
653 (reason (eql 'class))
654 sequencer)
655 (with-slots ((class %class) (dslot slot)) slot
656 (let ((instance *instance-class*)
657 (super (sod-slot-class dslot)))
658 (sequence-output (stream sequencer)
659 ((instance :object super :slots)
660 (output-class-initializer slot instance stream))))))
661
662 ;;;----- That's all, folks --------------------------------------------------