3 ;;;; Copyright (C) 1998,1999,2000-2003,2006,2009-2011,2013-2015 Free Software Foundation, Inc.
4 ;;;; Copyright (C) 1993-1998 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 ;;;; This file was based upon stklos.stk from the STk distribution
24 ;;;; version 4.0.1 by Erick Gallesio <eg@unice.fr>.
27 (define-module (oop goops)
28 #:use-module (srfi srfi-1)
29 #:use-module (ice-9 match)
30 #:use-module (oop goops util)
31 #:use-module (system base target)
32 #:export-syntax (define-class class standard-define-class
33 define-generic define-accessor define-method
34 define-extended-generic define-extended-generics
36 #:export ( ;; The root of everything.
41 <foreign-slot> <protected-slot> <hidden-slot> <opaque-slot>
42 <read-only-slot> <self-slot> <protected-opaque-slot>
43 <protected-hidden-slot> <protected-read-only-slot>
44 <scm-slot> <int-slot> <float-slot> <double-slot>
46 ;; Methods are implementations of generic functions.
47 <method> <accessor-method>
49 ;; Applicable objects, either procedures or applicable structs.
50 <procedure-class> <applicable>
51 <procedure> <primitive-generic>
53 ;; Applicable structs.
54 <applicable-struct-class> <applicable-struct-with-setter-class>
55 <applicable-struct> <applicable-struct-with-setter>
56 <generic> <extended-generic>
57 <generic-with-setter> <extended-generic-with-setter>
58 <accessor> <extended-accessor>
60 ;; Types with their own allocated typecodes.
61 <boolean> <char> <list> <pair> <null> <string> <symbol>
62 <vector> <bytevector> <uvec> <foreign> <hashtable>
63 <fluid> <dynamic-state> <frame> <vm> <vm-continuation>
67 <number> <complex> <real> <integer> <fraction>
72 ;; Particular SMOB data types. All SMOB types have
73 ;; corresponding classes, which may be obtained via class-of,
74 ;; once you have an instance. Perhaps FIXME to provide a
75 ;; smob-type-name->class procedure.
76 <arbiter> <promise> <thread> <mutex> <condition-variable>
77 <regexp> <hook> <bitvector> <random-state> <async>
78 <directory> <array> <character-set>
79 <dynamic-object> <guardian> <macro>
85 <port> <input-port> <output-port> <input-output-port>
87 ;; Like SMOB types, all port types have their own classes,
88 ;; which can be accessed via `class-of' once you have an
89 ;; instance. Here we export bindings just for file ports.
91 <file-input-port> <file-output-port> <file-input-output-port>
94 ensure-metaclass ensure-metaclass-with-supers
96 make-generic ensure-generic
98 make-accessor ensure-accessor
100 class-slot-ref class-slot-set! slot-unbound slot-missing
101 slot-definition-name slot-definition-options
102 slot-definition-allocation
104 slot-definition-getter slot-definition-setter
105 slot-definition-accessor
106 slot-definition-init-value slot-definition-init-form
107 slot-definition-init-thunk slot-definition-init-keyword
108 slot-init-function class-slot-definition
110 compute-cpl compute-std-cpl compute-get-n-set compute-slots
111 compute-getter-method compute-setter-method
112 allocate-instance initialize make-instance make
113 no-next-method no-applicable-method no-method
114 change-class update-instance-for-different-class
115 shallow-clone deep-clone
117 apply-generic apply-method apply-methods
118 compute-applicable-methods %compute-applicable-methods
119 method-more-specific? sort-applicable-methods
120 class-subclasses class-methods
122 min-fixnum max-fixnum
124 ;;; *fixme* Should go into goops.c
125 instance? slot-ref-using-class
126 slot-set-using-class! slot-bound-using-class?
127 slot-exists-using-class? slot-ref slot-set! slot-bound?
128 class-name class-direct-supers class-direct-subclasses
129 class-direct-methods class-direct-slots class-precedence-list
131 generic-function-name
132 generic-function-methods method-generic-function
133 method-specializers method-formals
134 primitive-generic-generic enable-primitive-generic!
135 method-procedure accessor-method-slot-definition
136 slot-exists? make find-method get-keyword)
139 ;; XXX FIXME: figure out why the 'eval-when's in this file must use
140 ;; 'compile' and must avoid 'expand', but only in 2.2, and only when
141 ;; compiling something that imports goops, e.g. (ice-9 occam-channel),
142 ;; before (oop goops) itself has been compiled.
144 ;; First initialize the builtin part of GOOPS
145 (eval-when (compile load eval)
146 (load-extension (string-append "libguile-" (effective-version))
147 "scm_init_goops_builtins"))
149 (eval-when (compile load eval)
150 (use-modules ((language tree-il primitives) :select (add-interesting-primitive!)))
151 (add-interesting-primitive! 'class-of))
153 ;;; The standard class precedence list computation algorithm
155 ;;; Correct behaviour:
157 ;;; (define-class food ())
158 ;;; (define-class fruit (food))
159 ;;; (define-class spice (food))
160 ;;; (define-class apple (fruit))
161 ;;; (define-class cinnamon (spice))
162 ;;; (define-class pie (apple cinnamon))
163 ;;; => cpl (pie) = pie apple fruit cinnamon spice food object top
165 ;;; (define-class d ())
166 ;;; (define-class e ())
167 ;;; (define-class f ())
168 ;;; (define-class b (d e))
169 ;;; (define-class c (e f))
170 ;;; (define-class a (b c))
171 ;;; => cpl (a) = a b d c e f object top
174 (define (compute-std-cpl c get-direct-supers)
175 (define (only-non-null lst)
176 (filter (lambda (l) (not (null? l))) lst))
178 (define (merge-lists reversed-partial-result inputs)
180 ((every null? inputs)
181 (reverse! reversed-partial-result))
183 (let* ((candidate (lambda (c)
184 (and (not (any (lambda (l)
188 (candidate-car (lambda (l)
190 (candidate (car l)))))
191 (next (any candidate-car inputs)))
193 (goops-error "merge-lists: Inconsistent precedence graph"))
194 (let ((remove-next (lambda (l)
195 (if (eq? (car l) next)
198 (merge-lists (cons next reversed-partial-result)
199 (only-non-null (map remove-next inputs))))))))
200 (let ((c-direct-supers (get-direct-supers c)))
201 (merge-lists (list c)
202 (only-non-null (append (map class-precedence-list
204 (list c-direct-supers))))))
206 ;; Bootstrap version.
207 (define (compute-cpl class)
208 (compute-std-cpl class class-direct-supers))
210 ;; During boot, the specialized slot classes aren't defined yet, so we
211 ;; initialize <class> with unspecialized slots.
212 (define-syntax-rule (build-<class>-slots specialized?)
213 (let-syntax ((unspecialized-slot (syntax-rules ()
214 ((_ name) (list 'name))))
215 (specialized-slot (syntax-rules ()
218 (list 'name #:class class)
220 (list (specialized-slot layout <protected-read-only-slot>)
221 (specialized-slot flags <hidden-slot>)
222 (specialized-slot self <self-slot>)
223 (specialized-slot instance-finalizer <hidden-slot>)
224 (unspecialized-slot print)
225 (specialized-slot name <protected-hidden-slot>)
226 (specialized-slot reserved-0 <hidden-slot>)
227 (specialized-slot reserved-1 <hidden-slot>)
228 (unspecialized-slot redefined)
229 (specialized-slot h0 <int-slot>)
230 (specialized-slot h1 <int-slot>)
231 (specialized-slot h2 <int-slot>)
232 (specialized-slot h3 <int-slot>)
233 (specialized-slot h4 <int-slot>)
234 (specialized-slot h5 <int-slot>)
235 (specialized-slot h6 <int-slot>)
236 (specialized-slot h7 <int-slot>)
237 (unspecialized-slot direct-supers)
238 (unspecialized-slot direct-slots)
239 (unspecialized-slot direct-subclasses)
240 (unspecialized-slot direct-methods)
241 (unspecialized-slot cpl)
242 (unspecialized-slot slots)
243 (unspecialized-slot getters-n-setters)
244 (unspecialized-slot nfields))))
246 (define (build-slots-list dslots cpl)
247 (define (check-cpl slots class-slots)
248 (when (or-map (lambda (slot-def) (assq (car slot-def) slots))
250 (scm-error 'misc-error #f
251 "a predefined <class> inherited field cannot be redefined"
253 (define (remove-duplicate-slots slots)
254 (let lp ((slots (reverse slots)) (res '()) (seen '()))
257 ((memq (caar slots) seen)
258 (lp (cdr slots) res seen))
260 (lp (cdr slots) (cons (car slots) res) (cons (caar slots) seen))))))
261 (let* ((class-slots (and (memq <class> cpl) (slot-ref <class> 'slots))))
263 (check-cpl dslots class-slots))
264 (let lp ((cpl (cdr cpl)) (res dslots) (class-slots '()))
266 (remove-duplicate-slots (append class-slots res))
267 (let* ((head (car cpl))
269 (new-slots (slot-ref head 'direct-slots)))
272 (lp cpl (append new-slots res) class-slots))
274 ;; Move class slots to the head of the list.
275 (lp cpl res new-slots))
277 (check-cpl new-slots class-slots)
278 (lp cpl (append new-slots res) class-slots))))))))
280 (define (%compute-getters-n-setters slots)
281 (define (compute-init-thunk options)
283 ((kw-arg-ref options #:init-value) => (lambda (val) (lambda () val)))
284 ((kw-arg-ref options #:init-thunk))
286 (let lp ((slots slots) (n 0))
289 (((name . options) . slots)
290 (cons (cons name (cons (compute-init-thunk options) n))
291 (lp slots (1+ n)))))))
293 (define (%compute-layout slots getters-n-setters nfields is-class?)
294 (define (instance-allocated? g-n-s)
296 ((name init-thunk . (? exact-integer? index)) #t)
297 ((name init-thunk getter setter index size) #t)
300 (define (allocated-index g-n-s)
302 ((name init-thunk . (? exact-integer? index)) index)
303 ((name init-thunk getter setter index size) index)))
305 (define (allocated-size g-n-s)
307 ((name init-thunk . (? exact-integer? index)) 1)
308 ((name init-thunk getter setter index size) size)))
310 (define (slot-protection-and-kind options)
311 (define (subclass? class parent)
312 (memq parent (class-precedence-list class)))
313 (let ((type (kw-arg-ref options #:class)))
314 (if (and type (subclass? type <foreign-slot>))
316 ((subclass? type <self-slot>) #\s)
317 ((subclass? type <protected-slot>) #\p)
320 ((subclass? type <opaque-slot>) #\o)
321 ((subclass? type <read-only-slot>) #\r)
322 ((subclass? type <hidden-slot>) #\h)
326 (let ((layout (make-string (* nfields 2))))
327 (let lp ((n 0) (slots slots) (getters-n-setters getters-n-setters))
328 (match getters-n-setters
330 (unless (= n nfields) (error "bad nfields"))
331 (unless (null? slots) (error "inconsistent g-n-s/slots"))
333 (let ((class-layout (symbol->string (slot-ref <class> 'layout))))
334 (unless (string-prefix? class-layout layout)
335 (error "bad layout for class"))))
337 ((g-n-s . getters-n-setters)
339 (((name . options) . slots)
341 ((instance-allocated? g-n-s)
342 (unless (< n nfields) (error "bad nfields"))
343 (unless (= n (allocated-index g-n-s)) (error "bad allocation"))
344 (call-with-values (lambda () (slot-protection-and-kind options))
345 (lambda (protection kind)
346 (let init ((n n) (size (allocated-size g-n-s)))
348 ((zero? size) (lp n slots getters-n-setters))
350 (string-set! layout (* n 2) protection)
351 (string-set! layout (1+ (* n 2)) kind)
352 (init (1+ n) (1- size))))))))
354 (lp n slots getters-n-setters))))))))))
356 (define (%prep-layout! class)
357 (let* ((is-class? (and (memq <class> (slot-ref class 'cpl)) #t))
358 (layout (%compute-layout (slot-ref class 'slots)
359 (slot-ref class 'getters-n-setters)
360 (slot-ref class 'nfields)
362 (%init-layout! class layout)))
364 (define (make-standard-class class name dsupers dslots)
365 (let ((z (make-struct/no-tail class)))
366 (slot-set! z 'direct-supers dsupers)
367 (let* ((cpl (compute-cpl z))
368 (dslots (map (lambda (slot)
369 (if (pair? slot) slot (list slot)))
371 (slots (build-slots-list dslots cpl))
372 (nfields (length slots))
373 (g-n-s (%compute-getters-n-setters slots)))
374 (slot-set! z 'name name)
375 (slot-set! z 'direct-slots dslots)
376 (slot-set! z 'direct-subclasses '())
377 (slot-set! z 'direct-methods '())
378 (slot-set! z 'cpl cpl)
379 (slot-set! z 'slots slots)
380 (slot-set! z 'nfields nfields)
381 (slot-set! z 'getters-n-setters g-n-s)
382 (slot-set! z 'redefined #f)
383 (for-each (lambda (super)
384 (let ((subclasses (slot-ref super 'direct-subclasses)))
385 (slot-set! super 'direct-subclasses (cons z subclasses))))
388 (%inherit-magic! z dsupers)
392 (let ((dslots (build-<class>-slots #f)))
393 (%make-root-class '<class> dslots (%compute-getters-n-setters dslots))))
395 (define-syntax define-standard-class
397 ((define-standard-class name (super ...) #:metaclass meta slot ...)
399 (make-standard-class meta 'name (list super ...) '(slot ...))))
400 ((define-standard-class name (super ...) slot ...)
401 (define-standard-class name (super ...) #:metaclass <class> slot ...))))
403 (define-standard-class <top> ())
404 (define-standard-class <object> (<top>))
406 ;; <top>, <object>, and <class> were partially initialized. Correct
408 (slot-set! <object> 'direct-subclasses (list <class>))
409 (slot-set! <class> 'direct-supers (list <object>))
410 (slot-set! <class> 'cpl (list <class> <object> <top>))
412 (define-standard-class <foreign-slot> (<top>))
413 (define-standard-class <protected-slot> (<foreign-slot>))
414 (define-standard-class <hidden-slot> (<foreign-slot>))
415 (define-standard-class <opaque-slot> (<foreign-slot>))
416 (define-standard-class <read-only-slot> (<foreign-slot>))
417 (define-standard-class <self-slot> (<read-only-slot>))
418 (define-standard-class <protected-opaque-slot> (<protected-slot>
420 (define-standard-class <protected-hidden-slot> (<protected-slot>
422 (define-standard-class <protected-read-only-slot> (<protected-slot>
424 (define-standard-class <scm-slot> (<protected-slot>))
425 (define-standard-class <int-slot> (<foreign-slot>))
426 (define-standard-class <float-slot> (<foreign-slot>))
427 (define-standard-class <double-slot> (<foreign-slot>))
429 ;; Finish initialization of <class>.
430 (let ((dslots (build-<class>-slots #t)))
431 (slot-set! <class> 'direct-slots dslots)
432 (slot-set! <class> 'slots dslots)
433 (slot-set! <class> 'getters-n-setters (%compute-getters-n-setters dslots)))
435 ;; Applicables and their classes.
436 (define-standard-class <procedure-class> (<class>))
437 (define-standard-class <applicable-struct-class>
439 (define-standard-class <applicable-struct-with-setter-class>
440 (<applicable-struct-class>))
441 (%bless-applicable-struct-vtables! <applicable-struct-class>
442 <applicable-struct-with-setter-class>)
444 (define-standard-class <applicable> (<top>))
445 (define-standard-class <applicable-struct> (<object> <applicable>)
446 #:metaclass <applicable-struct-class>
448 (define-standard-class <applicable-struct-with-setter> (<applicable-struct>)
449 #:metaclass <applicable-struct-with-setter-class>
451 (define-standard-class <generic> (<applicable-struct>)
452 #:metaclass <applicable-struct-class>
454 (n-specialized #:init-value 0)
455 (extended-by #:init-value ())
457 (%bless-pure-generic-vtable! <generic>)
458 (define-standard-class <extended-generic> (<generic>)
459 #:metaclass <applicable-struct-class>
460 (extends #:init-value ()))
461 (%bless-pure-generic-vtable! <extended-generic>)
462 (define-standard-class <generic-with-setter> (<generic>
463 <applicable-struct-with-setter>)
464 #:metaclass <applicable-struct-with-setter-class>)
465 (%bless-pure-generic-vtable! <generic-with-setter>)
466 (define-standard-class <accessor> (<generic-with-setter>)
467 #:metaclass <applicable-struct-with-setter-class>)
468 (%bless-pure-generic-vtable! <accessor>)
469 (define-standard-class <extended-generic-with-setter> (<extended-generic>
470 <generic-with-setter>)
471 #:metaclass <applicable-struct-with-setter-class>)
472 (%bless-pure-generic-vtable! <extended-generic-with-setter>)
473 (define-standard-class <extended-accessor> (<accessor>
474 <extended-generic-with-setter>)
475 #:metaclass <applicable-struct-with-setter-class>)
476 (%bless-pure-generic-vtable! <extended-accessor>)
479 (define-standard-class <method> (<object>)
486 (define-standard-class <accessor-method> (<method>)
487 (slot-definition #:init-keyword #:slot-definition))
489 ;; Primitive types classes
490 (define-standard-class <boolean> (<top>))
491 (define-standard-class <char> (<top>))
492 (define-standard-class <list> (<top>))
493 (define-standard-class <pair> (<list>))
494 (define-standard-class <null> (<list>))
495 (define-standard-class <string> (<top>))
496 (define-standard-class <symbol> (<top>))
497 (define-standard-class <vector> (<top>))
498 (define-standard-class <foreign> (<top>))
499 (define-standard-class <hashtable> (<top>))
500 (define-standard-class <fluid> (<top>))
501 (define-standard-class <dynamic-state> (<top>))
502 (define-standard-class <frame> (<top>))
503 (define-standard-class <vm-continuation> (<top>))
504 (define-standard-class <bytevector> (<top>))
505 (define-standard-class <uvec> (<bytevector>))
506 (define-standard-class <array> (<top>))
507 (define-standard-class <bitvector> (<top>))
508 (define-standard-class <number> (<top>))
509 (define-standard-class <complex> (<number>))
510 (define-standard-class <real> (<complex>))
511 (define-standard-class <integer> (<real>))
512 (define-standard-class <fraction> (<real>))
513 (define-standard-class <keyword> (<top>))
514 (define-standard-class <unknown> (<top>))
515 (define-standard-class <procedure> (<applicable>)
516 #:metaclass <procedure-class>)
517 (define-standard-class <primitive-generic> (<procedure>)
518 #:metaclass <procedure-class>)
519 (define-standard-class <port> (<top>))
520 (define-standard-class <input-port> (<port>))
521 (define-standard-class <output-port> (<port>))
522 (define-standard-class <input-output-port> (<input-port> <output-port>))
524 (define (%invalidate-method-cache! gf)
525 (slot-set! gf 'procedure (delayed-compile gf))
526 (slot-set! gf 'effective-methods '()))
529 (define (invalidate-method-cache! gf)
530 (%invalidate-method-cache! gf))
532 ;; A simple make which will be redefined later. This version handles
533 ;; only creation of gf, methods and classes (no instances).
535 ;; Since this code will disappear when Goops will be fully booted,
536 ;; no precaution is taken to be efficient.
538 (define (make class . args)
540 ((or (eq? class <generic>) (eq? class <accessor>))
541 (let ((z (make-struct/no-tail class #f '() 0 '())))
542 (set-procedure-property! z 'name (get-keyword #:name args #f))
543 (invalidate-method-cache! z)
544 (when (eq? class <accessor>)
545 (let ((setter (get-keyword #:setter args #f)))
547 (slot-set! z 'setter setter))))
550 (let ((z (%allocate-instance class args)))
552 ((or (eq? class <method>) (eq? class <accessor-method>))
553 (for-each (match-lambda
555 (slot-set! z slot (get-keyword kw args default))))
556 '((#:generic-function generic-function #f)
557 (#:specializers specializers ())
558 (#:procedure procedure #f)
559 (#:formals formals ())
561 (#:make-procedure make-procedure #f))))
562 ((memq <class> (class-precedence-list class))
563 (for-each (match-lambda
565 (slot-set! z slot (get-keyword kw args default))))
567 (#:dsupers direct-supers ())
568 (#:slots direct-slots ())
571 (error "boot `make' does not support this class" class)))
574 (define *dispatch-module* (current-module))
577 ;;; Generic functions have an applicable-methods cache associated with
578 ;;; them. Every distinct set of types that is dispatched through a
579 ;;; generic adds an entry to the cache. This cache gets compiled out to
580 ;;; a dispatch procedure. In steady-state, this dispatch procedure is
581 ;;; never recompiled; but during warm-up there is some churn, both to
582 ;;; the cache and to the dispatch procedure.
584 ;;; So what is the deal if warm-up happens in a multithreaded context?
585 ;;; There is indeed a window between missing the cache for a certain set
586 ;;; of arguments, and then updating the cache with the newly computed
587 ;;; applicable methods. One of the updaters is liable to lose their new
590 ;;; This is actually OK though, because a subsequent cache miss for the
591 ;;; race loser will just cause memoization to try again. The cache will
592 ;;; eventually be consistent. We're not mutating the old part of the
593 ;;; cache, just consing on the new entry.
595 ;;; It doesn't even matter if the dispatch procedure and the cache are
596 ;;; inconsistent -- most likely the type-set that lost the dispatch
597 ;;; procedure race will simply re-trigger a memoization, but since the
598 ;;; winner isn't in the effective-methods cache, it will likely also
599 ;;; re-trigger a memoization, and the cache will finally be consistent.
600 ;;; As you can see there is a possibility for ping-pong effects, but
601 ;;; it's unlikely given the shortness of the window between slot-set!
602 ;;; invocations. We could add a mutex, but it is strictly unnecessary,
603 ;;; and would add runtime cost and complexity.
606 (define (emit-linear-dispatch gf-sym nargs methods free rest?)
607 (define (gen-syms n stem)
608 (let lp ((n (1- n)) (syms '()))
611 (lp (1- n) (cons (gensym stem) syms)))))
612 (let* ((args (gen-syms nargs "a"))
613 (types (gen-syms nargs "t")))
614 (let lp ((methods methods)
616 (exp `(cache-miss ,gf-sym
622 (values `(,(if rest? `(,@args . rest) args)
623 (let ,(map (lambda (t a)
630 (let preddy ((free free)
632 (specs (vector-ref (car methods) 1))
635 (let ((m-sym (gensym "p")))
637 (acons (vector-ref (car methods) 3)
642 `(apply ,m-sym ,@args rest)
645 (let ((var (assq-ref free (car specs))))
650 (cons `(eq? ,(car types) ,var)
652 (let ((var (gensym "c")))
653 (preddy (acons (car specs) var free)
656 (cons `(eq? ,(car types) ,var)
659 (define (compute-dispatch-procedure gf cache)
661 (let lp ((ls cache) (nreq -1) (nrest -1))
664 (collate (make-vector (1+ nreq) '())
665 (make-vector (1+ nrest) '())))
666 ((vector-ref (car ls) 2) ; rest
667 (lp (cdr ls) nreq (max nrest (vector-ref (car ls) 0))))
669 (lp (cdr ls) (max nreq (vector-ref (car ls) 0)) nrest)))))
670 (define (collate req rest)
675 ((vector-ref (car ls) 2) ; rest
676 (let ((n (vector-ref (car ls) 0)))
677 (vector-set! rest n (cons (car ls) (vector-ref rest n)))
680 (let ((n (vector-ref (car ls) 0)))
681 (vector-set! req n (cons (car ls) (vector-ref req n)))
683 (define (emit req rest)
684 (let ((gf-sym (gensym "g")))
685 (define (emit-rest n clauses free)
686 (if (< n (vector-length rest))
687 (let ((methods (vector-ref rest n)))
690 (emit-rest (1+ n) clauses free))
691 ;; FIXME: hash dispatch
695 (emit-linear-dispatch gf-sym n methods free #t))
696 (lambda (clause free)
697 (emit-rest (1+ n) (cons clause clauses) free))))))
698 (emit-req (1- (vector-length req)) clauses free)))
699 (define (emit-req n clauses free)
701 (comp `(lambda ,(map cdr free)
702 (case-lambda ,@clauses))
704 (let ((methods (vector-ref req n)))
707 (emit-req (1- n) clauses free))
708 ;; FIXME: hash dispatch
712 (emit-linear-dispatch gf-sym n methods free #f))
713 (lambda (clause free)
714 (emit-req (1- n) (cons clause clauses) free))))))))
717 (if (or (zero? (vector-length rest))
718 (null? (vector-ref rest 0)))
719 (list `(args (cache-miss ,gf-sym args)))
721 (acons gf gf-sym '()))))
722 (define (comp exp vals)
723 ;; When cross-compiling Guile itself, the native Guile must generate
724 ;; code for the host.
725 (with-target %host-type
727 (let ((p ((@ (system base compile) compile) exp
728 #:env *dispatch-module*
730 #:opts '(#:partial-eval? #f #:cse? #f))))
736 ;; o/~ ten, nine, eight
737 ;; sometimes that's just how it goes
740 ;; get out before it blows o/~
742 (define timer-init 30)
743 (define (delayed-compile gf)
744 (let ((timer timer-init))
746 (set! timer (1- timer))
749 (let ((dispatch (compute-dispatch-procedure
750 gf (slot-ref gf 'effective-methods))))
751 (slot-set! gf 'procedure dispatch)
752 (apply dispatch args)))
754 ;; interestingly, this catches recursive compilation attempts as
755 ;; well; in that case, timer is negative
756 (cache-dispatch gf args))))))
758 (define (cache-dispatch gf args)
759 (define (map-until n f ls)
760 (if (or (zero? n) (null? ls))
762 (cons (f (car ls)) (map-until (1- n) f (cdr ls)))))
763 (define (equal? x y) ; can't use the stock equal? because it's a generic...
764 (cond ((pair? x) (and (pair? y)
765 (eq? (car x) (car y))
766 (equal? (cdr x) (cdr y))))
767 ((null? x) (null? y))
769 (if (slot-ref gf 'n-specialized)
770 (let ((types (map-until (slot-ref gf 'n-specialized) class-of args)))
771 (let lp ((cache (slot-ref gf 'effective-methods)))
773 (cache-miss gf args))
774 ((equal? (vector-ref (car cache) 1) types)
775 (apply (vector-ref (car cache) 3) args))
776 (else (lp (cdr cache))))))
777 (cache-miss gf args)))
779 (define (cache-miss gf args)
780 (apply (memoize-method! gf args) args))
782 (define (memoize-effective-method! gf args applicable)
783 (define (first-n ls n)
784 (if (or (zero? n) (null? ls))
786 (cons (car ls) (first-n (cdr ls) (- n 1)))))
789 (memoize n #f (map class-of args)))
790 ((= n (slot-ref gf 'n-specialized))
791 (memoize n #t (map class-of (first-n args n))))
793 (parse (1+ n) (cdr ls)))))
794 (define (memoize len rest? types)
795 (let* ((cmethod (compute-cmethod applicable types))
796 (cache (cons (vector len types rest? cmethod)
797 (slot-ref gf 'effective-methods))))
798 (slot-set! gf 'effective-methods cache)
799 (slot-set! gf 'procedure (delayed-compile gf))
804 ;;; Compiling next methods into method bodies
807 ;;; So, for the reader: there basic idea is that, given that the
808 ;;; semantics of `next-method' depend on the concrete types being
809 ;;; dispatched, why not compile a specific procedure to handle each type
810 ;;; combination that we see at runtime.
812 ;;; In theory we can do much better than a bytecode compilation, because
813 ;;; we know the *exact* types of the arguments. It's ideal for native
814 ;;; compilation. A task for the future.
816 ;;; I think this whole generic application mess would benefit from a
819 (define (compute-cmethod methods types)
820 (let ((make-procedure (slot-ref (car methods) 'make-procedure)))
823 (if (null? (cdr methods))
825 (no-next-method (method-generic-function (car methods)) args))
826 (compute-cmethod (cdr methods) types)))
827 (method-procedure (car methods)))))
833 (define (memoize-method! gf args)
834 (let ((applicable ((if (eq? gf compute-applicable-methods)
835 %compute-applicable-methods
836 compute-applicable-methods)
839 (memoize-effective-method! gf args applicable))
841 (no-applicable-method gf args)))))
843 (set-procedure-property! memoize-method! 'system-procedure #t)
845 (define no-applicable-method
846 (make <generic> #:name 'no-applicable-method))
850 ;; Then load the rest of GOOPS
854 (define min-fixnum (- (expt 2 29)))
855 (define max-fixnum (- (expt 2 29) 1))
860 (define (goops-error format-string . args)
861 (scm-error 'goops-error #f format-string args '()))
866 (define (is-a? obj class)
867 (and (memq class (class-precedence-list (class-of obj))) #t))
874 (define ensure-metaclass-with-supers
875 (let ((table-of-metas '()))
876 (lambda (meta-supers)
877 (let ((entry (assoc meta-supers table-of-metas)))
879 ;; Found a previously created metaclass
881 ;; Create a new meta-class which inherit from "meta-supers"
882 (let ((new (make <class> #:dsupers meta-supers
884 #:name (gensym "metaclass"))))
885 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
888 (define (ensure-metaclass supers)
891 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
892 (all-cpls (append-map (lambda (m)
893 (cdr (class-precedence-list m)))
896 ;; Find the most specific metaclasses. The new metaclass will be
897 ;; a subclass of these.
900 (if (and (not (member meta all-cpls))
901 (not (member meta needed-metas)))
902 (set! needed-metas (append needed-metas (list meta)))))
904 ;; Now return a subclass of the metaclasses we found.
905 (if (null? (cdr needed-metas))
906 (car needed-metas) ; If there's only one, just use it.
907 (ensure-metaclass-with-supers needed-metas)))))
913 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
915 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
916 ;;; OPTION ::= KEYWORD VALUE
919 (define (make-class supers slots . options)
920 (let* ((name (get-keyword #:name options (make-unbound)))
921 (supers (if (not (or-map (lambda (class)
923 (class-precedence-list class)))
925 (append supers (list <object>))
927 (metaclass (or (get-keyword #:metaclass options #f)
928 (ensure-metaclass supers))))
930 ;; Verify that all direct slots are different and that we don't inherit
931 ;; several time from the same class
932 (let ((tmp1 (find-duplicate supers))
933 (tmp2 (find-duplicate (map slot-definition-name slots))))
935 (goops-error "make-class: super class ~S is duplicate in class ~S"
938 (goops-error "make-class: slot ~S is duplicate in class ~S"
941 ;; Everything seems correct, build the class
942 (apply make metaclass
948 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
950 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
951 ;;; OPTION ::= KEYWORD VALUE
955 (define (parse-options options)
956 (syntax-case options ()
958 ((kw arg . options) (keyword? (syntax->datum #'kw))
959 (with-syntax ((options (parse-options #'options)))
962 #'(kw 'arg #:init-thunk (lambda () arg) . options))
964 #'(kw arg . options)))))))
965 (define (check-valid-kwargs args)
968 ((kw arg . args) (keyword? (syntax->datum #'kw))
969 #`(kw arg . #,(check-valid-kwargs #'args)))))
970 (define (parse-slots-and-kwargs args)
974 ((kw . _) (keyword? (syntax->datum #'kw))
975 #`(() #,(check-valid-kwargs args)))
976 (((name option ...) args ...)
977 (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...)))
978 ((option ...) (parse-options #'(option ...))))
979 #'(((list 'name option ...) . slots) kwargs)))
980 ((name args ...) (symbol? (syntax->datum #'name))
981 (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...))))
982 #'(('(name) . slots) kwargs)))))
984 ((class (super ...) arg ...)
985 (with-syntax ((((slot-def ...) (option ...))
986 (parse-slots-and-kwargs #'(arg ...))))
987 #'(make-class (list super ...)
991 (define-syntax define-class-pre-definition
994 ((_ (k arg rest ...) out ...)
995 (keyword? (syntax->datum #'k))
996 (case (syntax->datum #'k)
998 #'(define-class-pre-definition (rest ...)
1000 (if (or (not (defined? 'arg))
1001 (not (is-a? arg <generic>)))
1004 (ensure-generic (if (defined? 'arg) arg #f) 'arg)))))
1006 #'(define-class-pre-definition (rest ...)
1008 (if (or (not (defined? 'arg))
1009 (not (is-a? arg <accessor>)))
1012 (ensure-accessor (if (defined? 'arg) arg #f) 'arg)))))
1014 #'(define-class-pre-definition (rest ...) out ...))))
1016 #'(begin out ...)))))
1018 ;; Some slot options require extra definitions to be made. In
1019 ;; particular, we want to make sure that the generic function objects
1020 ;; which represent accessors exist before `make-class' tries to add
1022 (define-syntax define-class-pre-definitions
1027 ((_ (slot rest ...) out ...)
1028 (keyword? (syntax->datum #'slot))
1030 ((_ (slot rest ...) out ...)
1031 (identifier? #'slot)
1032 #'(define-class-pre-definitions (rest ...)
1034 ((_ ((slotname slotopt ...) rest ...) out ...)
1035 #'(define-class-pre-definitions (rest ...)
1036 out ... (define-class-pre-definition (slotopt ...)))))))
1038 (define-syntax-rule (define-class name supers slot ...)
1040 (define-class-pre-definitions (slot ...))
1041 (if (and (defined? 'name)
1042 (is-a? name <class>)
1043 (memq <object> (class-precedence-list name)))
1044 (class-redefinition name
1045 (class supers slot ... #:name 'name))
1046 (toplevel-define! 'name (class supers slot ... #:name 'name)))))
1048 (define-syntax-rule (standard-define-class arg ...)
1049 (define-class arg ...))
1052 ;;; {Generic functions and accessors}
1055 ;; Apparently the desired semantics are that we extend previous
1056 ;; procedural definitions, but that if `name' was already a generic, we
1057 ;; overwrite its definition.
1058 (define-syntax define-generic
1061 ((define-generic name) (symbol? (syntax->datum #'name))
1063 (if (and (defined? 'name) (is-a? name <generic>))
1064 (make <generic> #:name 'name)
1065 (ensure-generic (if (defined? 'name) name #f) 'name)))))))
1067 (define-syntax define-extended-generic
1070 ((define-extended-generic name val) (symbol? (syntax->datum #'name))
1071 #'(define name (make-extended-generic val 'name))))))
1073 (define-syntax define-extended-generics
1075 (define (id-append ctx a b)
1076 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
1078 ((define-extended-generic (name ...) #:prefix (prefix ...))
1079 (and (and-map symbol? (syntax->datum #'(name ...)))
1080 (and-map symbol? (syntax->datum #'(prefix ...))))
1081 (with-syntax ((((val ...)) (map (lambda (name)
1082 (map (lambda (prefix)
1083 (id-append name prefix name))
1087 (define-extended-generic name (list val ...))
1090 (define* (make-generic #:optional name)
1091 (make <generic> #:name name))
1093 (define* (make-extended-generic gfs #:optional name)
1094 (let* ((gfs (if (list? gfs) gfs (list gfs)))
1095 (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
1097 (let* ((sname (and name (make-setter-name name)))
1099 (append-map (lambda (gf)
1100 (if (is-a? gf <generic-with-setter>)
1101 (list (ensure-generic (setter gf)
1105 (es (make <extended-generic-with-setter>
1108 #:setter (make <extended-generic>
1110 #:extends setters))))
1111 (extended-by! setters (setter es))
1113 (make <extended-generic>
1116 (extended-by! gfs ans)
1119 (define (extended-by! gfs eg)
1120 (for-each (lambda (gf)
1121 (slot-set! gf 'extended-by
1122 (cons eg (slot-ref gf 'extended-by))))
1124 (invalidate-method-cache! eg))
1126 (define (not-extended-by! gfs eg)
1127 (for-each (lambda (gf)
1128 (slot-set! gf 'extended-by
1129 (delq! eg (slot-ref gf 'extended-by))))
1131 (invalidate-method-cache! eg))
1133 (define* (ensure-generic old-definition #:optional name)
1134 (cond ((is-a? old-definition <generic>) old-definition)
1135 ((procedure-with-setter? old-definition)
1136 (make <generic-with-setter>
1138 #:default (procedure old-definition)
1139 #:setter (setter old-definition)))
1140 ((procedure? old-definition)
1141 (if (generic-capability? old-definition) old-definition
1142 (make <generic> #:name name #:default old-definition)))
1143 (else (make <generic> #:name name))))
1145 ;; same semantics as <generic>
1146 (define-syntax-rule (define-accessor name)
1148 (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
1149 ((is-a? name <accessor>) (make <accessor> #:name 'name))
1150 (else (ensure-accessor name 'name)))))
1152 (define (make-setter-name name)
1153 (string->symbol (string-append "setter:" (symbol->string name))))
1155 (define* (make-accessor #:optional name)
1158 #:setter (make <generic>
1159 #:name (and name (make-setter-name name)))))
1161 (define* (ensure-accessor proc #:optional name)
1162 (cond ((and (is-a? proc <accessor>)
1163 (is-a? (setter proc) <generic>))
1165 ((is-a? proc <generic-with-setter>)
1166 (upgrade-accessor proc (setter proc)))
1167 ((is-a? proc <generic>)
1168 (upgrade-accessor proc (make-generic name)))
1169 ((procedure-with-setter? proc)
1172 #:default (procedure proc)
1173 #:setter (ensure-generic (setter proc) name)))
1175 (ensure-accessor (if (generic-capability? proc)
1176 (make <generic> #:name name #:default proc)
1177 (ensure-generic proc name))
1180 (make-accessor name))))
1182 (define (upgrade-accessor generic setter)
1183 (let ((methods (slot-ref generic 'methods))
1184 (gws (make (if (is-a? generic <extended-generic>)
1185 <extended-generic-with-setter>
1187 #:name (generic-function-name generic)
1188 #:extended-by (slot-ref generic 'extended-by)
1190 (if (is-a? generic <extended-generic>)
1191 (let ((gfs (slot-ref generic 'extends)))
1192 (not-extended-by! gfs generic)
1193 (slot-set! gws 'extends gfs)
1194 (extended-by! gfs gws)))
1195 ;; Steal old methods
1196 (for-each (lambda (method)
1197 (slot-set! method 'generic-function gws))
1199 (slot-set! gws 'methods methods)
1200 (invalidate-method-cache! gws)
1207 ;; Note: `a' and `b' can have unequal lengths (i.e. one can be one
1208 ;; element longer than the other when we have a dotted parameter
1209 ;; list). For instance, with the call
1215 ;; (define-method M (a . l) ....)
1216 ;; (define-method M (a) ....)
1218 ;; we consider that the second method is more specific.
1220 ;; Precondition: `a' and `b' are methods and are applicable to `types'.
1221 (define (%method-more-specific? a b types)
1222 (let lp ((a-specializers (method-specializers a))
1223 (b-specializers (method-specializers b))
1226 ;; (a) less specific than (a b ...) or (a . b)
1227 ((null? a-specializers) #t)
1228 ;; (a b ...) or (a . b) less specific than (a)
1229 ((null? b-specializers) #f)
1230 ;; (a . b) less specific than (a b ...)
1231 ((not (pair? a-specializers)) #f)
1232 ;; (a b ...) more specific than (a . b)
1233 ((not (pair? b-specializers)) #t)
1235 (let ((a-specializer (car a-specializers))
1236 (b-specializer (car b-specializers))
1237 (a-specializers (cdr a-specializers))
1238 (b-specializers (cdr b-specializers))
1240 (types (cdr types)))
1241 (if (eq? a-specializer b-specializer)
1242 (lp a-specializers b-specializers types)
1243 (let lp ((cpl (class-precedence-list type)))
1244 (let ((elt (car cpl)))
1246 ((eq? a-specializer elt) #t)
1247 ((eq? b-specializer elt) #f)
1248 (else (lp (cdr cpl))))))))))))
1250 (define (%sort-applicable-methods methods types)
1251 (sort methods (lambda (a b) (%method-more-specific? a b types))))
1253 (define (%compute-applicable-methods gf args)
1254 (define (method-applicable? m types)
1255 (let lp ((specs (method-specializers m)) (types types))
1257 ((null? specs) (null? types))
1258 ((not (pair? specs)) #t)
1261 (and (memq (car specs) (class-precedence-list (car types)))
1262 (lp (cdr specs) (cdr types)))))))
1263 (let ((n (length args))
1264 (types (map class-of args)))
1265 (let lp ((methods (generic-function-methods gf))
1268 (and (not (null? applicable))
1269 (%sort-applicable-methods applicable types))
1270 (let ((m (car methods)))
1272 (if (method-applicable? m types)
1276 (define compute-applicable-methods %compute-applicable-methods)
1278 (define (toplevel-define! name val)
1279 (module-define! (current-module) name val))
1281 (define-syntax define-method
1282 (syntax-rules (setter)
1283 ((_ ((setter name) . args) body ...)
1285 (if (or (not (defined? 'name))
1286 (not (is-a? name <accessor>)))
1287 (toplevel-define! 'name
1289 (if (defined? 'name) name #f) 'name)))
1290 (add-method! (setter name) (method args body ...))))
1291 ((_ (name . args) body ...)
1293 ;; FIXME: this code is how it always was, but it's quite cracky:
1294 ;; it will only define the generic function if it was undefined
1295 ;; before (ok), or *was defined to #f*. The latter is crack. But
1296 ;; there are bootstrap issues about fixing this -- change it to
1297 ;; (is-a? name <generic>) and see.
1298 (if (or (not (defined? 'name))
1300 (toplevel-define! 'name (make <generic> #:name 'name)))
1301 (add-method! name (method args body ...))))))
1303 (define-syntax method
1305 (define (parse-args args)
1306 (let lp ((ls args) (formals '()) (specializers '()))
1309 (and (identifier? #'f) (identifier? #'s))
1312 (cons #'s specializers)))
1317 (cons #'<top> specializers)))
1319 (list (reverse formals)
1320 (reverse (cons #''() specializers))))
1322 (identifier? #'tail)
1323 (list (append (reverse formals) #'tail)
1324 (reverse (cons #'<top> specializers)))))))
1326 (define (find-free-id exp referent)
1329 (or (find-free-id #'x referent)
1330 (find-free-id #'y referent)))
1333 (let ((id (datum->syntax #'x referent)))
1334 (and (free-identifier=? #'x id) id)))
1337 (define (compute-procedure formals body)
1338 (syntax-case body ()
1340 (with-syntax ((formals formals))
1341 #'(lambda formals body0 ...)))))
1343 (define (->proper args)
1344 (let lp ((ls args) (out '()))
1346 ((x . xs) (lp #'xs (cons #'x out)))
1348 (tail (reverse (cons #'tail out))))))
1350 (define (compute-make-procedure formals body next-method)
1351 (syntax-case body ()
1353 (with-syntax ((next-method next-method))
1354 (syntax-case formals ()
1356 #'(lambda (real-next-method)
1357 (lambda (formal ...)
1358 (let ((next-method (lambda args
1360 (real-next-method formal ...)
1361 (apply real-next-method args)))))
1364 (with-syntax (((formal ...) (->proper #'formals)))
1365 #'(lambda (real-next-method)
1367 (let ((next-method (lambda args
1369 (apply real-next-method formal ...)
1370 (apply real-next-method args)))))
1373 (define (compute-procedures formals body)
1374 ;; So, our use of this is broken, because it operates on the
1375 ;; pre-expansion source code. It's equivalent to just searching
1376 ;; for referent in the datums. Ah well.
1377 (let ((id (find-free-id body 'next-method)))
1379 ;; return a make-procedure
1381 (compute-make-procedure formals body id))
1382 (values (compute-procedure formals body)
1386 ((_ args) #'(method args (if #f #f)))
1387 ((_ args body0 body1 ...)
1388 (with-syntax (((formals (specializer ...)) (parse-args #'args)))
1391 (compute-procedures #'formals #'(body0 body1 ...)))
1392 (lambda (procedure make-procedure)
1393 (with-syntax ((procedure procedure)
1394 (make-procedure make-procedure))
1396 #:specializers (cons* specializer ...)
1398 #:body '(body0 body1 ...)
1399 #:make-procedure make-procedure
1400 #:procedure procedure)))))))))
1406 (define (add-method-in-classes! m)
1407 ;; Add method in all the classes which appears in its specializers list
1408 (for-each* (lambda (x)
1409 (let ((dm (class-direct-methods x)))
1410 (if (not (memq m dm))
1411 (slot-set! x 'direct-methods (cons m dm)))))
1412 (method-specializers m)))
1414 (define (remove-method-in-classes! m)
1415 ;; Remove method in all the classes which appears in its specializers list
1416 (for-each* (lambda (x)
1419 (delv! m (class-direct-methods x))))
1420 (method-specializers m)))
1422 (define (compute-new-list-of-methods gf new)
1423 (let ((new-spec (method-specializers new))
1424 (methods (slot-ref gf 'methods)))
1425 (let loop ((l methods))
1428 (if (equal? (method-specializers (car l)) new-spec)
1430 ;; This spec. list already exists. Remove old method from dependents
1431 (remove-method-in-classes! (car l))
1436 (define (method-n-specializers m)
1437 (length* (slot-ref m 'specializers)))
1439 (define (calculate-n-specialized gf)
1440 (fold (lambda (m n) (max n (method-n-specializers m)))
1442 (generic-function-methods gf)))
1444 (define (invalidate-method-cache! gf)
1445 (%invalidate-method-cache! gf)
1446 (slot-set! gf 'n-specialized (calculate-n-specialized gf))
1447 (for-each (lambda (gf) (invalidate-method-cache! gf))
1448 (slot-ref gf 'extended-by)))
1450 (define internal-add-method!
1451 (method ((gf <generic>) (m <method>))
1452 (slot-set! m 'generic-function gf)
1453 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
1454 (invalidate-method-cache! gf)
1455 (add-method-in-classes! m)
1458 (define-generic add-method!)
1460 ((method-procedure internal-add-method!) add-method! internal-add-method!)
1462 (define-method (add-method! (proc <procedure>) (m <method>))
1463 (if (generic-capability? proc)
1465 (enable-primitive-generic! proc)
1466 (add-method! proc m))
1469 (define-method (add-method! (pg <primitive-generic>) (m <method>))
1470 (add-method! (primitive-generic-generic pg) m))
1472 (define-method (add-method! obj (m <method>))
1473 (goops-error "~S is not a valid generic function" obj))
1476 ;;; {Access to meta objects}
1482 (define-method (method-source (m <method>))
1483 (let* ((spec (map* class-name (slot-ref m 'specializers)))
1484 (src (procedure-source (slot-ref m 'procedure))))
1486 (let ((args (cadr src))
1489 (cons (map* list args spec)
1492 (define-method (method-formals (m <method>))
1493 (slot-ref m 'formals))
1498 (define slot-definition-name car)
1500 (define slot-definition-options cdr)
1502 (define (slot-definition-allocation s)
1503 (get-keyword #:allocation (cdr s) #:instance))
1505 (define (slot-definition-getter s)
1506 (get-keyword #:getter (cdr s) #f))
1508 (define (slot-definition-setter s)
1509 (get-keyword #:setter (cdr s) #f))
1511 (define (slot-definition-accessor s)
1512 (get-keyword #:accessor (cdr s) #f))
1514 (define (slot-definition-init-value s)
1515 ;; can be #f, so we can't use #f as non-value
1516 (get-keyword #:init-value (cdr s) (make-unbound)))
1518 (define (slot-definition-init-form s)
1519 (get-keyword #:init-form (cdr s) (make-unbound)))
1521 (define (slot-definition-init-thunk s)
1522 (get-keyword #:init-thunk (cdr s) #f))
1524 (define (slot-definition-init-keyword s)
1525 (get-keyword #:init-keyword (cdr s) #f))
1527 (define (class-slot-definition class slot-name)
1528 (assq slot-name (class-slots class)))
1530 (define (slot-init-function class slot-name)
1531 (cadr (assq slot-name (slot-ref class 'getters-n-setters))))
1533 (define (accessor-method-slot-definition obj)
1534 "Return the slot definition of the accessor @var{obj}."
1535 (slot-ref obj 'slot-definition))
1539 ;;; {Standard methods used by the C runtime}
1542 ;;; Methods to compare objects
1545 ;; Have to do this in a strange order because equal? is used in the
1546 ;; add-method! implementation; we need to make sure that when the
1547 ;; primitive is extended, that the generic has a method. =
1548 (define g-equal? (make-generic 'equal?))
1549 ;; When this generic gets called, we will have already checked eq? and
1550 ;; eqv? -- the purpose of this generic is to extend equality. So by
1551 ;; default, there is no extension, thus the #f return.
1552 (add-method! g-equal? (method (x y) #f))
1553 (set-primitive-generic! equal? g-equal?)
1556 ;;; methods to display/write an object
1559 ; Code for writing objects must test that the slots they use are
1560 ; bound. Otherwise a slot-unbound method will be called and will
1561 ; conduct to an infinite loop.
1564 (define (display-address o file)
1565 (display (number->string (object-address o) 16) file))
1567 (define-method (write o file)
1568 (display "#<instance " file)
1569 (display-address o file)
1572 (define write-object (primitive-generic-generic write))
1574 (define-method (write (o <object>) file)
1575 (let ((class (class-of o)))
1576 (if (slot-bound? class 'name)
1579 (display (class-name class) file)
1580 (display #\space file)
1581 (display-address o file)
1585 (define-method (write (class <class>) file)
1586 (let ((meta (class-of class)))
1587 (if (and (slot-bound? class 'name)
1588 (slot-bound? meta 'name))
1591 (display (class-name meta) file)
1592 (display #\space file)
1593 (display (class-name class) file)
1594 (display #\space file)
1595 (display-address class file)
1599 (define-method (write (gf <generic>) file)
1600 (let ((meta (class-of gf)))
1601 (if (and (slot-bound? meta 'name)
1602 (slot-bound? gf 'methods))
1605 (display (class-name meta) file)
1606 (let ((name (generic-function-name gf)))
1609 (display #\space file)
1610 (display name file))))
1612 (display (length (generic-function-methods gf)) file)
1613 (display ")>" file))
1616 (define-method (write (o <method>) file)
1617 (let ((meta (class-of o)))
1618 (if (and (slot-bound? meta 'name)
1619 (slot-bound? o 'specializers))
1622 (display (class-name meta) file)
1623 (display #\space file)
1624 (display (map* (lambda (spec)
1625 (if (slot-bound? spec 'name)
1626 (slot-ref spec 'name)
1628 (method-specializers o))
1630 (display #\space file)
1631 (display-address o file)
1635 ;; Display (do the same thing as write by default)
1636 (define-method (display o file)
1637 (write-object o file))
1640 ;;; Handling of duplicate bindings in the module system
1643 (define (find-subclass super name)
1644 (let lp ((classes (class-direct-subclasses super)))
1647 (error "class not found" name))
1648 ((and (slot-bound? (car classes) 'name)
1649 (eq? (class-name (car classes)) name))
1652 (lp (cdr classes))))))
1655 (define <module> (find-subclass <top> '<module>))
1657 (define-method (merge-generics (module <module>)
1667 (define-method (merge-generics (module <module>)
1675 (and (not (eq? val1 val2))
1676 (make-variable (make-extended-generic (list val2 val1) name))))
1678 (define-method (merge-generics (module <module>)
1685 (gf <extended-generic>))
1686 (and (not (memq val2 (slot-ref gf 'extends)))
1690 (cons val2 (delq! val2 (slot-ref gf 'extends))))
1693 (cons gf (delq! gf (slot-ref val2 'extended-by))))
1694 (invalidate-method-cache! gf)
1697 (module-define! duplicate-handlers 'merge-generics merge-generics)
1699 (define-method (merge-accessors (module <module>)
1709 (define-method (merge-accessors (module <module>)
1717 (merge-generics module name int1 val1 int2 val2 var val))
1719 (module-define! duplicate-handlers 'merge-accessors merge-accessors)
1725 (define (class-slot-g-n-s class slot-name)
1726 (let* ((this-slot (assq slot-name (slot-ref class 'slots)))
1727 (g-n-s (cddr (or (assq slot-name (slot-ref class 'getters-n-setters))
1728 (slot-missing class slot-name)))))
1729 (if (not (memq (slot-definition-allocation this-slot)
1730 '(#:class #:each-subclass)))
1731 (slot-missing class slot-name))
1734 (define (class-slot-ref class slot)
1735 (let ((x ((car (class-slot-g-n-s class slot)) #f)))
1737 (slot-unbound class slot)
1740 (define (class-slot-set! class slot value)
1741 ((cadr (class-slot-g-n-s class slot)) #f value))
1743 (define-method (slot-unbound (c <class>) (o <object>) s)
1744 (goops-error "Slot `~S' is unbound in object ~S" s o))
1746 (define-method (slot-unbound (c <class>) s)
1747 (goops-error "Slot `~S' is unbound in class ~S" s c))
1749 (define-method (slot-unbound (o <object>))
1750 (goops-error "Unbound slot in object ~S" o))
1752 (define-method (slot-missing (c <class>) (o <object>) s)
1753 (goops-error "No slot with name `~S' in object ~S" s o))
1755 (define-method (slot-missing (c <class>) s)
1756 (goops-error "No class slot with name `~S' in class ~S" s c))
1759 (define-method (slot-missing (c <class>) (o <object>) s value)
1760 (slot-missing c o s))
1762 ;;; Methods for the possible error we can encounter when calling a gf
1764 (define-method (no-next-method (gf <generic>) args)
1765 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
1767 (define-method (no-applicable-method (gf <generic>) args)
1768 (goops-error "No applicable method for ~S in call ~S"
1769 gf (cons (generic-function-name gf) args)))
1771 (define-method (no-method (gf <generic>) args)
1772 (goops-error "No method defined for ~S" gf))
1775 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
1778 (define-method (shallow-clone (self <object>))
1779 (let ((clone (%allocate-instance (class-of self) '()))
1780 (slots (map slot-definition-name
1781 (class-slots (class-of self)))))
1782 (for-each (lambda (slot)
1783 (if (slot-bound? self slot)
1784 (slot-set! clone slot (slot-ref self slot))))
1788 (define-method (deep-clone (self <object>))
1789 (let ((clone (%allocate-instance (class-of self) '()))
1790 (slots (map slot-definition-name
1791 (class-slots (class-of self)))))
1792 (for-each (lambda (slot)
1793 (if (slot-bound? self slot)
1794 (slot-set! clone slot
1795 (let ((value (slot-ref self slot)))
1796 (if (instance? value)
1803 ;;; {Class redefinition utilities}
1806 ;;; (class-redefinition OLD NEW)
1809 ;;; Has correct the following conditions:
1813 ;;; 1. New accessor specializers refer to new header
1817 ;;; 1. New class cpl refers to the new class header
1818 ;;; 2. Old class header exists on old super classes direct-subclass lists
1819 ;;; 3. New class header exists on new super classes direct-subclass lists
1821 (define-method (class-redefinition (old <class>) (new <class>))
1822 ;; Work on direct methods:
1823 ;; 1. Remove accessor methods from the old class
1824 ;; 2. Patch the occurences of new in the specializers by old
1825 ;; 3. Displace the methods from old to new
1826 (remove-class-accessors! old) ;; -1-
1827 (let ((methods (class-direct-methods new)))
1828 (for-each (lambda (m)
1829 (update-direct-method! m new old)) ;; -2-
1833 (append methods (class-direct-methods old))))
1835 ;; Substitute old for new in new cpl
1836 (set-car! (slot-ref new 'cpl) old)
1838 ;; Remove the old class from the direct-subclasses list of its super classes
1839 (for-each (lambda (c) (slot-set! c 'direct-subclasses
1840 (delv! old (class-direct-subclasses c))))
1841 (class-direct-supers old))
1843 ;; Replace the new class with the old in the direct-subclasses of the supers
1844 (for-each (lambda (c)
1845 (slot-set! c 'direct-subclasses
1846 (cons old (delv! new (class-direct-subclasses c)))))
1847 (class-direct-supers new))
1849 ;; Swap object headers
1850 (%modify-class old new)
1854 ;; Redefine all the subclasses of old to take into account modification
1857 (update-direct-subclass! c new old))
1858 (class-direct-subclasses new))
1860 ;; Invalidate class so that subsequent instances slot accesses invoke
1861 ;; change-object-class
1862 (slot-set! new 'redefined old)
1863 (%invalidate-class new) ;must come after slot-set!
1868 ;;; remove-class-accessors!
1871 (define-method (remove-class-accessors! (c <class>))
1872 (for-each (lambda (m)
1873 (if (is-a? m <accessor-method>)
1874 (let ((gf (slot-ref m 'generic-function)))
1875 ;; remove the method from its GF
1876 (slot-set! gf 'methods
1877 (delq1! m (slot-ref gf 'methods)))
1878 (invalidate-method-cache! gf)
1879 ;; remove the method from its specializers
1880 (remove-method-in-classes! m))))
1881 (class-direct-methods c)))
1884 ;;; update-direct-method!
1887 (define-method (update-direct-method! (m <method>)
1890 (let loop ((l (method-specializers m)))
1891 ;; Note: the <top> in dotted list is never used.
1892 ;; So we can work as if we had only proper lists.
1895 (if (eqv? (car l) old)
1900 ;;; update-direct-subclass!
1903 (define-method (update-direct-subclass! (c <class>)
1906 (class-redefinition c
1907 (make-class (class-direct-supers c)
1908 (class-direct-slots c)
1909 #:name (class-name c)
1910 #:metaclass (class-of c))))
1913 ;;; {Utilities for INITIALIZE methods}
1916 ;;; compute-slot-accessors
1918 (define (compute-slot-accessors class slots)
1921 (let ((getter-function (slot-definition-getter s))
1922 (setter-function (slot-definition-setter s))
1923 (accessor (slot-definition-accessor s)))
1925 (add-method! getter-function
1926 (compute-getter-method class g-n-s)))
1928 (add-method! setter-function
1929 (compute-setter-method class g-n-s)))
1932 (add-method! accessor
1933 (compute-getter-method class g-n-s))
1934 (add-method! (setter accessor)
1935 (compute-setter-method class g-n-s))))))
1936 slots (slot-ref class 'getters-n-setters)))
1938 (define-method (compute-getter-method (class <class>) slotdef)
1939 (let ((init-thunk (cadr slotdef))
1940 (g-n-s (cddr slotdef)))
1941 (make <accessor-method>
1942 #:specializers (list class)
1943 #:procedure (cond ((pair? g-n-s)
1944 (make-generic-bound-check-getter (car g-n-s)))
1946 (standard-get g-n-s))
1948 (bound-check-get g-n-s)))
1949 #:slot-definition slotdef)))
1951 (define-method (compute-setter-method (class <class>) slotdef)
1952 (let ((g-n-s (cddr slotdef)))
1953 (make <accessor-method>
1954 #:specializers (list class <top>)
1955 #:procedure (if (pair? g-n-s)
1957 (standard-set g-n-s))
1958 #:slot-definition slotdef)))
1960 (define (make-generic-bound-check-getter proc)
1961 (lambda (o) (assert-bound (proc o) o)))
1963 ;;; Pre-generate getters and setters for the first 20 slots.
1964 (define-syntax define-standard-accessor-method
1966 (define num-standard-pre-cache 20)
1968 ((_ ((proc n) arg ...) body)
1970 (let ((cache (vector #,@(map (lambda (n*)
1974 (iota num-standard-pre-cache)))))
1976 (if (< n #,num-standard-pre-cache)
1977 (vector-ref cache n)
1978 (lambda (arg ...) body)))))))))
1980 (define-standard-accessor-method ((bound-check-get n) o)
1981 (let ((x (struct-ref o n)))
1986 (define-standard-accessor-method ((standard-get n) o)
1989 (define-standard-accessor-method ((standard-set n) o v)
1990 (struct-set! o n v))
1992 ;;; compute-getters-n-setters
1994 (define (compute-getters-n-setters class slots)
1996 (define (compute-slot-init-function name s)
1997 (or (let ((thunk (slot-definition-init-thunk s)))
2001 (goops-error "Bad init-thunk for slot `~S' in ~S: ~S"
2002 name class thunk))))
2003 (let ((init (slot-definition-init-value s)))
2004 (and (not (unbound? init))
2005 (lambda () init)))))
2007 (define (verify-accessors slot l)
2008 (cond ((integer? l))
2009 ((not (and (list? l) (= (length l) 2)))
2010 (goops-error "Bad getter and setter for slot `~S' in ~S: ~S"
2015 (if (not (procedure? get))
2016 (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
2018 (if (not (procedure? set))
2019 (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
2020 slot class set))))))
2023 ;; The strange treatment of nfields is due to backward compatibility.
2024 (let* ((index (slot-ref class 'nfields))
2025 (g-n-s (compute-get-n-set class s))
2026 (size (- (slot-ref class 'nfields) index))
2027 (name (slot-definition-name s)))
2028 ;; NOTE: The following is interdependent with C macros
2029 ;; defined above goops.c:scm_sys_prep_layout_x.
2031 ;; For simple instance slots, we have the simplest form
2032 ;; '(name init-function . index)
2033 ;; For other slots we have
2034 ;; '(name init-function getter setter . alloc)
2036 ;; '(index size) for instance allocated slots
2037 ;; '() for other slots
2038 (verify-accessors name g-n-s)
2039 (case (slot-definition-allocation s)
2040 ((#:each-subclass #:class)
2041 (unless (and (zero? size) (pair? g-n-s))
2042 (error "Class-allocated slots should not reserve fields"))
2043 ;; Don't initialize the slot; that's handled when the slot
2044 ;; is allocated, in compute-get-n-set.
2045 (cons name (cons #f g-n-s)))
2048 (cons (compute-slot-init-function name s)
2049 (if (or (integer? g-n-s)
2052 (append g-n-s (list index size)))))))))
2058 ;; Replace the bootstrap compute-cpl with this definition.
2060 (make <generic> #:name 'compute-cpl))
2062 (define-method (compute-cpl (class <class>))
2063 (compute-std-cpl class class-direct-supers))
2065 ;;; compute-get-n-set
2067 (define-method (compute-get-n-set (class <class>) s)
2068 (define (class-slot-init-value)
2069 (let ((thunk (slot-definition-init-thunk s)))
2072 (slot-definition-init-value s))))
2074 (case (slot-definition-allocation s)
2075 ((#:instance) ;; Instance slot
2076 ;; get-n-set is just its offset
2077 (let ((already-allocated (slot-ref class 'nfields)))
2078 (slot-set! class 'nfields (+ already-allocated 1))
2081 ((#:class) ;; Class slot
2082 ;; Class-slots accessors are implemented as 2 closures around
2083 ;; a Scheme variable. As instance slots, class slots must be
2084 ;; unbound at init time.
2085 (let ((name (slot-definition-name s)))
2086 (if (memq name (map slot-definition-name (class-direct-slots class)))
2087 ;; This slot is direct; create a new shared variable
2088 (make-closure-variable class (class-slot-init-value))
2089 ;; Slot is inherited. Find its definition in superclass
2090 (let loop ((l (cdr (class-precedence-list class))))
2091 (let ((r (assoc name (slot-ref (car l) 'getters-n-setters))))
2094 (loop (cdr l))))))))
2096 ((#:each-subclass) ;; slot shared by instances of direct subclass.
2097 ;; (Thomas Buerger, April 1998)
2098 (make-closure-variable class (class-slot-init-value)))
2100 ((#:virtual) ;; No allocation
2101 ;; slot-ref and slot-set! function must be given by the user
2102 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
2103 (set (get-keyword #:slot-set! (slot-definition-options s) #f)))
2104 (if (not (and get set))
2105 (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S"
2108 (else (next-method))))
2110 (define (make-closure-variable class value)
2111 (list (lambda (o) value)
2112 (lambda (o v) (set! value v))))
2114 (define-method (compute-get-n-set (o <object>) s)
2115 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
2117 (define-method (compute-slots (class <class>))
2118 (build-slots-list (class-direct-slots class)
2119 (class-precedence-list class)))
2125 (define-method (initialize (object <object>) initargs)
2126 (%initialize-object object initargs))
2128 (define-method (initialize (class <class>) initargs)
2130 (let ((dslots (get-keyword #:slots initargs '()))
2131 (supers (get-keyword #:dsupers initargs '())))
2132 (slot-set! class 'name (get-keyword #:name initargs '???))
2133 (slot-set! class 'direct-supers supers)
2134 (slot-set! class 'direct-slots dslots)
2135 (slot-set! class 'direct-subclasses '())
2136 (slot-set! class 'direct-methods '())
2137 (slot-set! class 'cpl (compute-cpl class))
2138 (slot-set! class 'redefined #f)
2139 (let ((slots (compute-slots class)))
2140 (slot-set! class 'slots slots)
2141 (slot-set! class 'nfields 0)
2142 (slot-set! class 'getters-n-setters (compute-getters-n-setters class
2144 ;; Build getters - setters - accessors
2145 (compute-slot-accessors class slots))
2147 ;; Update the "direct-subclasses" of each inherited classes
2148 (for-each (lambda (x)
2151 (cons class (slot-ref x 'direct-subclasses))))
2154 ;; Support for the underlying structs:
2156 ;; Set the layout slot
2157 (%prep-layout! class)
2158 ;; Inherit class flags (invisible on scheme level) from supers
2159 (%inherit-magic! class supers)))
2161 (define (initialize-object-procedure object initargs)
2162 (let ((proc (get-keyword #:procedure initargs #f)))
2165 (apply slot-set! object 'procedure proc))
2167 (slot-set! object 'procedure proc)))))
2169 (define-method (initialize (applicable-struct <applicable-struct>) initargs)
2171 (initialize-object-procedure applicable-struct initargs))
2173 (define-method (initialize (applicable-struct <applicable-struct-with-setter>)
2176 (slot-set! applicable-struct 'setter (get-keyword #:setter initargs #f)))
2178 (define-method (initialize (generic <generic>) initargs)
2179 (let ((previous-definition (get-keyword #:default initargs #f))
2180 (name (get-keyword #:name initargs #f)))
2182 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
2184 (apply previous-definition args)))
2187 (set-procedure-property! generic 'name name))
2188 (invalidate-method-cache! generic)))
2190 (define-method (initialize (eg <extended-generic>) initargs)
2192 (slot-set! eg 'extends (get-keyword #:extends initargs '())))
2194 (define dummy-procedure (lambda args *unspecified*))
2196 (define-method (initialize (method <method>) initargs)
2198 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
2199 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
2200 (slot-set! method 'procedure
2201 (get-keyword #:procedure initargs #f))
2202 (slot-set! method 'formals (get-keyword #:formals initargs '()))
2203 (slot-set! method 'body (get-keyword #:body initargs '()))
2204 (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
2211 (define (change-object-class old-instance old-class new-class)
2212 (let ((new-instance (allocate-instance new-class '())))
2213 ;; Initialize the slots of the new instance
2214 (for-each (lambda (slot)
2215 (if (and (slot-exists-using-class? old-class old-instance slot)
2216 (eq? (slot-definition-allocation
2217 (class-slot-definition old-class slot))
2219 (slot-bound-using-class? old-class old-instance slot))
2220 ;; Slot was present and allocated in old instance; copy it
2221 (slot-set-using-class!
2225 (slot-ref-using-class old-class old-instance slot))
2226 ;; slot was absent; initialize it with its default value
2227 (let ((init (slot-init-function new-class slot)))
2229 (slot-set-using-class!
2233 (apply init '()))))))
2234 (map slot-definition-name (class-slots new-class)))
2235 ;; Exchange old and new instance in place to keep pointers valid
2236 (%modify-instance old-instance new-instance)
2237 ;; Allow class specific updates of instances (which now are swapped)
2238 (update-instance-for-different-class new-instance old-instance)
2242 (define-method (update-instance-for-different-class (old-instance <object>)
2245 ;;not really important what we do, we just need a default method
2248 (define-method (change-class (old-instance <object>) (new-class <class>))
2249 (change-object-class old-instance (class-of old-instance) new-class))
2254 ;;; A new definition which overwrites the previous one which was built-in
2257 (define-method (allocate-instance (class <class>) initargs)
2258 (%allocate-instance class initargs))
2260 (define-method (make-instance (class <class>) . initargs)
2261 (let ((instance (allocate-instance class initargs)))
2262 (initialize instance initargs)
2265 (define make make-instance)
2270 ;;; Protocol for calling standard generic functions. This protocol is
2271 ;;; not used for real <generic> functions (in this case we use a
2272 ;;; completely C hard-coded protocol). Apply-generic is used by
2273 ;;; goops for calls to subclasses of <generic> and <generic-with-setter>.
2274 ;;; The code below is similar to the first MOP described in AMOP. In
2275 ;;; particular, it doesn't used the currified approach to gf
2276 ;;; call. There are 2 reasons for that:
2277 ;;; - the protocol below is exposed to mimic completely the one written in C
2278 ;;; - the currified protocol would be imho inefficient in C.
2281 (define-method (apply-generic (gf <generic>) args)
2282 (if (null? (slot-ref gf 'methods))
2283 (no-method gf args))
2284 (let ((methods (compute-applicable-methods gf args)))
2286 (apply-methods gf (sort-applicable-methods gf methods args) args)
2287 (no-applicable-method gf args))))
2289 ;; compute-applicable-methods is bound to %compute-applicable-methods.
2291 (define %%compute-applicable-methods
2292 (make <generic> #:name 'compute-applicable-methods))
2294 (define-method (%%compute-applicable-methods (gf <generic>) args)
2295 (%compute-applicable-methods gf args))
2297 (set! compute-applicable-methods %%compute-applicable-methods)
2299 (define-method (sort-applicable-methods (gf <generic>) methods args)
2300 (%sort-applicable-methods methods (map class-of args)))
2302 (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
2303 (%method-more-specific? m1 m2 targs))
2305 (define-method (apply-method (gf <generic>) methods build-next args)
2306 (apply (method-procedure (car methods))
2307 (build-next (cdr methods) args)
2310 (define-method (apply-methods (gf <generic>) (l <list>) args)
2311 (letrec ((next (lambda (procs args)
2313 (let ((a (if (null? new-args) args new-args)))
2315 (no-next-method gf a)
2316 (apply-method gf procs next a)))))))
2317 (apply-method gf l next args)))
2319 ;; We don't want the following procedure to turn up in backtraces:
2320 (for-each (lambda (proc)
2321 (set-procedure-property! proc 'system-procedure #t))
2325 no-applicable-method
2330 ;;; {<composite-metaclass> and <active-metaclass>}
2333 ;(autoload "active-slot" <active-metaclass>)
2334 ;(autoload "composite-slot" <composite-metaclass>)
2335 ;(export <composite-metaclass> <active-metaclass>)
2343 ;; duplicate the standard list->set function but using eq instead of
2344 ;; eqv which really sucks a lot, uselessly here
2346 (define (list2set l)
2351 ((memq (car l) res) (loop (cdr l) res))
2352 (else (loop (cdr l) (cons (car l) res))))))
2354 (define (class-subclasses c)
2355 (letrec ((allsubs (lambda (c)
2356 (cons c (mapappend allsubs
2357 (class-direct-subclasses c))))))
2358 (list2set (cdr (allsubs c)))))
2360 (define (class-methods c)
2361 (list2set (mapappend class-direct-methods
2362 (cons c (class-subclasses c)))))
2365 ;;; {Final initialization}
2368 ;; Tell C code that the main bulk of Goops has been loaded
2375 ;;; {SMOB and port classes}
2378 (define <arbiter> (find-subclass <top> '<arbiter>))
2379 (define <promise> (find-subclass <top> '<promise>))
2380 (define <thread> (find-subclass <top> '<thread>))
2381 (define <mutex> (find-subclass <top> '<mutex>))
2382 (define <condition-variable> (find-subclass <top> '<condition-variable>))
2383 (define <regexp> (find-subclass <top> '<regexp>))
2384 (define <hook> (find-subclass <top> '<hook>))
2385 (define <bitvector> (find-subclass <top> '<bitvector>))
2386 (define <random-state> (find-subclass <top> '<random-state>))
2387 (define <async> (find-subclass <top> '<async>))
2388 (define <directory> (find-subclass <top> '<directory>))
2389 (define <array> (find-subclass <top> '<array>))
2390 (define <character-set> (find-subclass <top> '<character-set>))
2391 (define <dynamic-object> (find-subclass <top> '<dynamic-object>))
2392 (define <guardian> (find-subclass <applicable> '<guardian>))
2393 (define <macro> (find-subclass <top> '<macro>))
2395 (define (define-class-subtree class)
2396 (define! (class-name class) class)
2397 (for-each define-class-subtree (class-direct-subclasses class)))
2399 (define-class-subtree (find-subclass <port> '<file-port>))