1 ;;;; goops.scm -- The Guile Object-Oriented Programming System
3 ;;;; Copyright (C) 1998-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 ((language tree-il primitives)
31 :select (add-interesting-primitive!))
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
125 slot-ref slot-set! slot-bound? slot-exists?
126 class-name class-direct-supers class-direct-subclasses
127 class-direct-methods class-direct-slots class-precedence-list
129 generic-function-name
130 generic-function-methods method-generic-function
131 method-specializers method-formals
132 primitive-generic-generic enable-primitive-generic!
133 method-procedure accessor-method-slot-definition
134 make find-method get-keyword))
138 ;;; Booting GOOPS is a tortuous process. We begin by loading a small
139 ;;; set of primitives from C.
141 (eval-when (expand load eval)
142 (load-extension (string-append "libguile-" (effective-version))
143 "scm_init_goops_builtins")
144 (add-interesting-primitive! 'class-of))
150 ;;; We then define the slots that must appear in all classes (<class>
151 ;;; objects) and slot definitions (<slot> objects). These slots must
152 ;;; appear in order. We'll use this list to statically compute offsets
153 ;;; for the various fields, to compute the struct layout for <class>
154 ;;; instances, and to compute the slot definition lists for <class>.
155 ;;; Because the list is needed at expansion-time, we define it as a
158 (define-syntax macro-fold-left
160 ((_ folder seed ()) seed)
161 ((_ folder seed (head . tail))
162 (macro-fold-left folder (folder head seed) tail))))
164 (define-syntax macro-fold-right
166 ((_ folder seed ()) seed)
167 ((_ folder seed (head . tail))
168 (folder head (macro-fold-right folder seed tail)))))
170 (define-syntax-rule (define-macro-folder macro-folder value ...)
171 (define-syntax macro-folder
175 ;; The datum->syntax makes it as if each `value' were present
176 ;; in the initial form, which allows them to be used as
177 ;; (components of) introduced identifiers.
178 #`(fold visit seed #,(datum->syntax #'visit '(value ...))))))))
180 (define-macro-folder fold-class-slots
181 (layout #:class <protected-read-only-slot>)
182 (flags #:class <hidden-slot>)
183 (self #:class <self-slot>)
184 (instance-finalizer #:class <hidden-slot>)
186 (name #:class <protected-hidden-slot>)
187 (nfields #:class <hidden-slot>)
188 (%reserved #:class <hidden-slot>)
197 (define-macro-folder fold-slot-slots
198 (name #:init-keyword #:name)
199 (allocation #:init-keyword #:allocation #:init-value #:instance)
200 (init-keyword #:init-keyword #:init-keyword #:init-value #f)
201 (init-form #:init-keyword #:init-form)
202 (init-value #:init-keyword #:init-value)
203 (init-thunk #:init-keyword #:init-thunk #:init-value #f)
205 (getter #:init-keyword #:getter #:init-value #f)
206 (setter #:init-keyword #:setter #:init-value #f)
207 (accessor #:init-keyword #:accessor #:init-value #f)
208 ;; These last don't have #:init-keyword because they are meant to be
209 ;; set by `allocate-slots', not in compute-effective-slot-definition.
210 (slot-ref #:init-value #f)
211 (slot-set! #:init-value #f)
212 (index #:init-value #f)
213 (size #:init-value #f))
216 ;;; Statically define variables for slot offsets: `class-index-layout'
217 ;;; will be 0, `class-index-flags' will be 1, and so on, and the same
218 ;;; for `slot-index-name' and such for <slot>.
220 (let-syntax ((define-slot-indexer
222 ((_ define-index prefix)
223 (define-syntax define-index
225 (define (id-append ctx a b)
226 (datum->syntax ctx (symbol-append (syntax->datum a)
228 (define (tail-length tail)
231 ((visit head tail) (1+ (tail-length #'tail)))))
235 (define-syntax #,(id-append #'name #'prefix #'name)
236 (identifier-syntax #,(tail-length #'tail)))
238 (define-slot-indexer define-class-index class-index-)
239 (define-slot-indexer define-slot-index slot-index-)
240 (fold-class-slots macro-fold-left define-class-index (begin))
241 (fold-slot-slots macro-fold-left define-slot-index (begin)))
244 ;;; Structs that are vtables have a "flags" slot, which corresponds to
245 ;;; class-index-flags. `vtable-flag-vtable' indicates that instances of
246 ;;; a vtable are themselves vtables, and `vtable-flag-validated'
247 ;;; indicates that the struct's layout has been validated. goops.c
248 ;;; defines a few additional flags: one to indicate that a vtable is
249 ;;; actually a class, one to indicate that the class is "valid" (meaning
250 ;;; that it hasn't been redefined), and one to indicate that instances
251 ;;; of a class are slot definition objects (<slot> instances).
253 (define vtable-flag-goops-metaclass
254 (logior vtable-flag-vtable vtable-flag-goops-class))
256 (define-inlinable (class-add-flags! class flags)
257 (struct-set! class class-index-flags
258 (logior flags (struct-ref class class-index-flags))))
260 (define-inlinable (class-clear-flags! class flags)
261 (struct-set! class class-index-flags
262 (logand (lognot flags) (struct-ref class class-index-flags))))
264 (define-inlinable (class-has-flags? class flags)
266 (logand (struct-ref class class-index-flags) flags)))
268 (define-inlinable (class? obj)
269 (class-has-flags? (struct-vtable obj) vtable-flag-goops-metaclass))
271 (define-inlinable (slot? obj)
273 (class-has-flags? (struct-vtable obj) vtable-flag-goops-slot)))
275 (define-inlinable (instance? obj)
276 (class-has-flags? (struct-vtable obj) vtable-flag-goops-class))
279 ;;; Now that we know the slots that must be present in classes, and
280 ;;; their offsets, we can create the root of the class hierarchy.
282 ;;; Note that the `direct-supers', `direct-slots', `cpl', and `slots'
283 ;;; fields will be updated later, once we can create slot definition
284 ;;; objects and once we have definitions for <top> and <object>.
287 (let-syntax ((cons-layout
288 ;; A simple way to compute class layout for the concrete
289 ;; types used in <class>.
290 (syntax-rules (<protected-read-only-slot>
293 <protected-hidden-slot>)
295 (string-append "pw" tail))
296 ((_ (name #:class <protected-read-only-slot>) tail)
297 (string-append "pr" tail))
298 ((_ (name #:class <self-slot>) tail)
299 (string-append "sr" tail))
300 ((_ (name #:class <hidden-slot>) tail)
301 (string-append "uh" tail))
302 ((_ (name #:class <protected-hidden-slot>) tail)
303 (string-append "ph" tail)))))
304 (let* ((layout (fold-class-slots macro-fold-right cons-layout ""))
305 (nfields (/ (string-length layout) 2))
306 (<class> (%make-vtable-vtable layout)))
307 (class-add-flags! <class> (logior vtable-flag-goops-class
308 vtable-flag-goops-valid))
309 (struct-set! <class> class-index-name '<class>)
310 (struct-set! <class> class-index-nfields nfields)
311 (struct-set! <class> class-index-direct-supers '())
312 (struct-set! <class> class-index-direct-slots '())
313 (struct-set! <class> class-index-direct-subclasses '())
314 (struct-set! <class> class-index-direct-methods '())
315 (struct-set! <class> class-index-cpl '())
316 (struct-set! <class> class-index-slots '())
317 (struct-set! <class> class-index-redefined #f)
321 ;;; Accessors to fields of <class>.
323 (define-syntax-rule (define-class-accessor name docstring field)
328 (scm-error 'wrong-type-arg #f "Not a class: ~S"
330 (struct-ref val field))))
332 (define-class-accessor class-name
333 "Return the class name of @var{obj}."
335 (define-class-accessor class-direct-supers
336 "Return the direct superclasses of the class @var{obj}."
337 class-index-direct-supers)
338 (define-class-accessor class-direct-slots
339 "Return the direct slots of the class @var{obj}."
340 class-index-direct-slots)
341 (define-class-accessor class-direct-subclasses
342 "Return the direct subclasses of the class @var{obj}."
343 class-index-direct-subclasses)
344 (define-class-accessor class-direct-methods
345 "Return the direct methods of the class @var{obj}."
346 class-index-direct-methods)
347 (define-class-accessor class-precedence-list
348 "Return the class precedence list of the class @var{obj}."
350 (define-class-accessor class-slots
351 "Return the slot list of the class @var{obj}."
354 (define (class-subclasses c)
355 "Compute a list of all subclasses of @var{c}, direct and indirect."
356 (define (all-subclasses c)
357 (cons c (append-map all-subclasses
358 (class-direct-subclasses c))))
359 (delete-duplicates (cdr (all-subclasses c)) eq?))
361 (define (class-methods c)
362 "Compute a list of all methods that specialize on @var{c} or
363 subclasses of @var{c}."
364 (delete-duplicates (append-map class-direct-methods
365 (cons c (class-subclasses c)))
368 (define (is-a? obj class)
369 "Return @code{#t} if @var{obj} is an instance of @var{class}, or
370 @code{#f} otherwise."
371 (and (memq class (class-precedence-list (class-of obj))) #t))
377 ;;; At this point, <class> is missing slot definitions, but we can't
378 ;;; create slot definitions until we have a slot definition class.
379 ;;; Continue with manual object creation until we're able to bootstrap
380 ;;; more of the protocol. Again, the CPL and class hierarchy slots
381 ;;; remain uninitialized.
383 (define* (get-keyword key l #:optional default)
384 "Determine an associated value for the keyword @var{key} from the list
385 @var{l}. The list @var{l} has to consist of an even number of elements,
386 where, starting with the first, every second element is a keyword,
387 followed by its associated value. If @var{l} does not hold a value for
388 @var{key}, the value @var{default} is returned."
389 (unless (keyword? key)
390 (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list key) #f))
395 (unless (keyword? kw)
396 (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list kw) #f))
397 (if (eq? kw key) arg (lp l))))))
399 (define *unbound* (list 'unbound))
401 (define-inlinable (unbound? x)
404 (define (%allocate-instance class)
405 (let ((obj (allocate-struct class (struct-ref class class-index-nfields))))
406 (%clear-fields! obj *unbound*)
410 (let-syntax ((cons-layout
411 ;; All slots are "pw" in <slot>.
413 ((_ _ tail) (string-append "pw" tail)))))
414 (let* ((layout (fold-slot-slots macro-fold-right cons-layout ""))
415 (nfields (/ (string-length layout) 2))
416 (<slot> (make-struct/no-tail <class> (make-struct-layout layout))))
417 (class-add-flags! <slot> (logior vtable-flag-goops-class
418 vtable-flag-goops-slot
419 vtable-flag-goops-valid))
420 (struct-set! <slot> class-index-name '<slot>)
421 (struct-set! <slot> class-index-nfields nfields)
422 (struct-set! <slot> class-index-direct-supers '())
423 (struct-set! <slot> class-index-direct-slots '())
424 (struct-set! <slot> class-index-direct-subclasses '())
425 (struct-set! <slot> class-index-direct-methods '())
426 (struct-set! <slot> class-index-cpl (list <slot>))
427 (struct-set! <slot> class-index-slots '())
428 (struct-set! <slot> class-index-redefined #f)
431 ;;; Access to slot objects is performance-sensitive for slot-ref, so in
432 ;;; addition to the type-checking accessors that we export, we also
433 ;;; define some internal inlined helpers that just do an unchecked
434 ;;; struct-ref in cases where we know the object must be a slot, as
435 ;;; when accessing class-slots.
437 (define-syntax-rule (define-slot-accessor name docstring %name field)
439 (define-syntax-rule (%name obj)
440 (struct-ref obj field))
444 (scm-error 'wrong-type-arg #f "Not a slot: ~S"
448 (define-slot-accessor slot-definition-name
449 "Return the name of @var{obj}."
450 %slot-definition-name slot-index-name)
451 (define-slot-accessor slot-definition-allocation
452 "Return the allocation of the slot @var{obj}."
453 %slot-definition-allocation slot-index-allocation)
454 (define-slot-accessor slot-definition-init-keyword
455 "Return the init keyword of the slot @var{obj}, or @code{#f}."
456 %slot-definition-init-keyword slot-index-init-keyword)
457 (define-slot-accessor slot-definition-init-form
458 "Return the init form of the slot @var{obj}, or the unbound value"
459 %slot-definition-init-form slot-index-init-form)
460 (define-slot-accessor slot-definition-init-value
461 "Return the init value of the slot @var{obj}, or the unbound value."
462 %slot-definition-init-value slot-index-init-value)
463 (define-slot-accessor slot-definition-init-thunk
464 "Return the init thunk of the slot @var{obj}, or @code{#f}."
465 %slot-definition-init-thunk slot-index-init-thunk)
466 (define-slot-accessor slot-definition-options
467 "Return the initargs given when creating the slot @var{obj}."
468 %slot-definition-options slot-index-options)
469 (define-slot-accessor slot-definition-getter
470 "Return the getter of the slot @var{obj}, or @code{#f}."
471 %slot-definition-getter slot-index-getter)
472 (define-slot-accessor slot-definition-setter
473 "Return the setter of the slot @var{obj}, or @code{#f}."
474 %slot-definition-setter slot-index-setter)
475 (define-slot-accessor slot-definition-accessor
476 "Return the accessor of the slot @var{obj}, or @code{#f}."
477 %slot-definition-accessor slot-index-accessor)
478 (define-slot-accessor slot-definition-slot-ref
479 "Return the slot-ref procedure of the slot @var{obj}, or @code{#f}."
480 %slot-definition-slot-ref slot-index-slot-ref)
481 (define-slot-accessor slot-definition-slot-set!
482 "Return the slot-set! procedure of the slot @var{obj}, or @code{#f}."
483 %slot-definition-slot-set! slot-index-slot-set!)
484 (define-slot-accessor slot-definition-index
485 "Return the allocated struct offset of the slot @var{obj}, or @code{#f}."
486 %slot-definition-index slot-index-index)
487 (define-slot-accessor slot-definition-size
488 "Return the number fields used by the slot @var{obj}, or @code{#f}."
489 %slot-definition-size slot-index-size)
492 (define (direct-slot-definition-class class initargs)
493 (get-keyword #:class initargs <slot>))
496 (define (make-slot class initargs)
497 (let ((slot (make-struct/no-tail class)))
498 (define-syntax-rule (init-slot offset kw default)
499 (struct-set! slot offset (get-keyword kw initargs default)))
500 (init-slot slot-index-name #:name #f)
501 (init-slot slot-index-allocation #:allocation #:instance)
502 (init-slot slot-index-init-keyword #:init-keyword #f)
503 (init-slot slot-index-init-form #:init-form *unbound*)
504 (init-slot slot-index-init-value #:init-value *unbound*)
505 (struct-set! slot slot-index-init-thunk
506 (or (get-keyword #:init-thunk initargs #f)
507 (let ((val (%slot-definition-init-value slot)))
511 (struct-set! slot slot-index-options initargs)
512 (init-slot slot-index-getter #:getter #f)
513 (init-slot slot-index-setter #:setter #f)
514 (init-slot slot-index-accessor #:accessor #f)
515 (init-slot slot-index-slot-ref #:slot-ref #f)
516 (init-slot slot-index-slot-set! #:slot-set! #f)
517 (init-slot slot-index-index #:index #f)
518 (init-slot slot-index-size #:size #f)
522 (define (make class . args)
523 (unless (memq <slot> (class-precedence-list class))
524 (error "Unsupported class: ~S" class))
525 (make-slot class args))
528 (define (compute-direct-slot-definition class initargs)
529 (apply make (direct-slot-definition-class class initargs) initargs))
531 (define (compute-direct-slot-definition-initargs class slot-spec)
533 ((? symbol? name) (list #:name name))
534 (((? symbol? name) . initargs)
536 ;; If there is an #:init-form, the `class' macro will have
537 ;; already added an #:init-thunk. Still, if there isn't an
538 ;; #:init-thunk already but we do have an #:init-value,
539 ;; synthesize an #:init-thunk initarg. This will ensure
540 ;; that the #:init-thunk gets passed on to the effective
541 ;; slot definition too.
542 (if (get-keyword #:init-thunk initargs)
544 (let ((value (get-keyword #:init-value initargs *unbound*)))
547 (cons* #:init-thunk (lambda () value) initargs))))))))
550 (define-syntax cons-slot
552 ((_ (name #:class class) tail)
553 ;; Special case to avoid referencing specialized <slot> kinds,
554 ;; which are not defined yet.
555 (cons (list 'name) tail))
556 ((_ (name . initargs) tail)
557 (cons (list 'name . initargs) tail))))
558 (define-syntax-rule (initialize-direct-slots! class fold-slots)
559 (let ((specs (fold-slots macro-fold-right cons-slot '())))
560 (define (make-direct-slot-definition spec)
561 (let ((initargs (compute-direct-slot-definition-initargs class spec)))
562 (compute-direct-slot-definition class initargs)))
563 (struct-set! class class-index-direct-slots
564 (map make-direct-slot-definition specs))))
566 (initialize-direct-slots! <class> fold-class-slots)
567 (initialize-direct-slots! <slot> fold-slot-slots))
573 ;;; OK, at this point we have initialized `direct-slots' on both <class>
574 ;;; and <slot>. We need to define a standard way to make subclasses:
575 ;;; how to compute the precedence list of subclasses, how to compute the
576 ;;; list of slots in a subclass, and what layout to use for instances of
579 (define (compute-std-cpl c get-direct-supers)
580 "The standard class precedence list computation algorithm."
581 (define (only-non-null lst)
582 (filter (lambda (l) (not (null? l))) lst))
584 (define (merge-lists reversed-partial-result inputs)
586 ((every null? inputs)
587 (reverse! reversed-partial-result))
589 (let* ((candidate (lambda (c)
590 (and (not (any (lambda (l)
594 (candidate-car (lambda (l)
596 (candidate (car l)))))
597 (next (any candidate-car inputs)))
599 (goops-error "merge-lists: Inconsistent precedence graph"))
600 (let ((remove-next (lambda (l)
601 (if (eq? (car l) next)
604 (merge-lists (cons next reversed-partial-result)
605 (only-non-null (map remove-next inputs))))))))
606 (let ((c-direct-supers (get-direct-supers c)))
607 (merge-lists (list c)
608 (only-non-null (append (map class-precedence-list
610 (list c-direct-supers))))))
612 ;; This version of compute-cpl is replaced with a generic function once
614 (define (compute-cpl class)
615 (compute-std-cpl class class-direct-supers))
617 (define (effective-slot-definition-class class slot)
620 (define (compute-effective-slot-definition class slot)
621 ;; FIXME: Support slot being a list of slots, as in CLOS.
623 (effective-slot-definition-class class slot)
624 (slot-definition-options slot)))
626 (define (build-slots-list dslots cpl)
627 (define (slot-memq slot slots)
628 (let ((name (%slot-definition-name slot)))
629 (let lp ((slots slots))
633 (or (eq? (%slot-definition-name slot) name) (lp slots)))))))
634 (define (check-cpl slots static-slots)
635 (when (or-map (lambda (slot) (slot-memq slot slots)) static-slots)
636 (scm-error 'misc-error #f
637 "a predefined static inherited field cannot be redefined"
639 (define (remove-duplicate-slots slots)
640 (let lp ((slots (reverse slots)) (res '()) (seen '()))
644 (let ((name (%slot-definition-name slot)))
647 (lp slots (cons slot res) (cons name seen))))))))
648 ;; For subclases of <class> and <slot>, we need to ensure that the
649 ;; <class> or <slot> slots come first.
650 (let* ((static-slots (cond
652 (when (memq <slot> cpl) (error "invalid class"))
653 (struct-ref <class> class-index-slots))
655 (struct-ref <slot> class-index-slots))
658 (check-cpl dslots static-slots))
659 (let lp ((cpl (cdr cpl)) (res dslots) (static-slots '()))
661 (() (remove-duplicate-slots (append static-slots res)))
663 (let ((new-slots (struct-ref head class-index-direct-slots)))
666 (lp cpl (append new-slots res) static-slots))
667 ((or (eq? head <class>) (eq? head <slot>))
668 ;; Move static slots to the head of the list.
669 (lp cpl res new-slots))
671 (check-cpl new-slots static-slots)
672 (lp cpl (append new-slots res) static-slots)))))))))
675 (define (compute-get-n-set class slot)
676 (let ((index (struct-ref class class-index-nfields)))
677 (struct-set! class class-index-nfields (1+ index))
680 (define (allocate-slots class slots)
681 "Transform the computed list of direct slot definitions @var{slots}
682 into a corresponding list of effective slot definitions, allocating
684 (define (make-effective-slot-definition slot)
685 ;; `compute-get-n-set' is expected to mutate `nfields' if it
686 ;; allocates a field to the object. Pretty strange, but we preserve
687 ;; the behavior for backward compatibility.
688 (let* ((slot (compute-effective-slot-definition class slot))
689 (index (struct-ref class class-index-nfields))
690 (g-n-s (compute-get-n-set class slot))
691 (size (- (struct-ref class class-index-nfields) index)))
697 (error "unexpected return from compute-get-n-set"))
699 (((? procedure? get) (? procedure? set))
702 (struct-set! slot slot-index-index index)
703 (struct-set! slot slot-index-size size)
704 (struct-set! slot slot-index-slot-ref get)
705 (struct-set! slot slot-index-slot-set! set)))
707 (struct-set! class class-index-nfields 0)
708 (map-in-order make-effective-slot-definition slots))
710 (define (%compute-layout slots nfields is-class?)
711 (define (slot-protection-and-kind slot)
712 (define (subclass? class parent)
713 (memq parent (class-precedence-list class)))
714 (let ((type (kw-arg-ref (%slot-definition-options slot) #:class)))
715 (if (and type (subclass? type <foreign-slot>))
717 ((subclass? type <self-slot>) #\s)
718 ((subclass? type <protected-slot>) #\p)
721 ((subclass? type <opaque-slot>) #\o)
722 ((subclass? type <read-only-slot>) #\r)
723 ((subclass? type <hidden-slot>) #\h)
726 (let ((layout (make-string (* nfields 2))))
727 (let lp ((n 0) (slots slots))
730 (unless (= n nfields) (error "bad nfields"))
732 (let ((class-layout (struct-ref <class> class-index-layout)))
733 (unless (string-prefix? (symbol->string class-layout) layout)
734 (error "bad layout for class"))))
737 (unless (= n (%slot-definition-index slot)) (error "bad allocation"))
738 (call-with-values (lambda () (slot-protection-and-kind slot))
739 (lambda (protection kind)
740 (let init ((n n) (size (%slot-definition-size slot)))
742 ((zero? size) (lp n slots))
744 (unless (< n nfields) (error "bad nfields"))
745 (string-set! layout (* n 2) protection)
746 (string-set! layout (1+ (* n 2)) kind)
747 (init (1+ n) (1- size))))))))))))
753 ;;; With all of this, we are now able to define subclasses of <class>.
755 (define (%prep-layout! class)
756 (let* ((is-class? (and (memq <class> (struct-ref class class-index-cpl)) #t))
757 (layout (%compute-layout (struct-ref class class-index-slots)
758 (struct-ref class class-index-nfields)
760 (%init-layout! class layout)))
762 (define (make-standard-class class name dsupers dslots)
763 (let ((z (make-struct/no-tail class)))
764 (define (make-direct-slot-definition dslot)
765 (let ((initargs (compute-direct-slot-definition-initargs z dslot)))
766 (compute-direct-slot-definition z initargs)))
768 (struct-set! z class-index-name name)
769 (struct-set! z class-index-nfields 0)
770 (struct-set! z class-index-direct-supers dsupers)
771 (struct-set! z class-index-direct-subclasses '())
772 (struct-set! z class-index-direct-methods '())
773 (struct-set! z class-index-redefined #f)
774 (let ((cpl (compute-cpl z)))
775 (struct-set! z class-index-cpl cpl)
776 (when (memq <slot> cpl)
777 (class-add-flags! z vtable-flag-goops-slot))
778 (let* ((dslots (map make-direct-slot-definition dslots))
779 (slots (allocate-slots z (build-slots-list dslots cpl))))
780 (struct-set! z class-index-direct-slots dslots)
781 (struct-set! z class-index-slots slots)))
784 (let ((subclasses (struct-ref super class-index-direct-subclasses)))
785 (struct-set! super class-index-direct-subclasses
786 (cons z subclasses))))
791 (define-syntax define-standard-class
793 ((define-standard-class name (super ...) #:metaclass meta slot ...)
795 (make-standard-class meta 'name (list super ...) '(slot ...))))
796 ((define-standard-class name (super ...) slot ...)
797 (define-standard-class name (super ...) #:metaclass <class> slot ...))))
803 ;;; Sweet! Now we can define <top> and <object>, and finish
804 ;;; initializing the `direct-subclasses', `direct-supers', and `cpl'
805 ;;; slots of <class>.
807 (define-standard-class <top> ())
808 (define-standard-class <object> (<top>))
810 ;; The inheritance links for <top>, <object>, <class>, and <slot> were
811 ;; partially initialized. Correct them here.
812 (struct-set! <object> class-index-direct-subclasses (list <slot> <class>))
813 (struct-set! <class> class-index-direct-supers (list <object>))
814 (struct-set! <slot> class-index-direct-supers (list <object>))
815 (struct-set! <class> class-index-cpl (list <class> <object> <top>))
816 (struct-set! <slot> class-index-cpl (list <slot> <object> <top>))
822 ;;; We can also define the various slot types, and finish initializing
823 ;;; `direct-slots' and `slots' on <class> and <slot>.
825 (define-standard-class <foreign-slot> (<slot>))
826 (define-standard-class <protected-slot> (<foreign-slot>))
827 (define-standard-class <hidden-slot> (<foreign-slot>))
828 (define-standard-class <opaque-slot> (<foreign-slot>))
829 (define-standard-class <read-only-slot> (<foreign-slot>))
830 (define-standard-class <self-slot> (<read-only-slot>))
831 (define-standard-class <protected-opaque-slot> (<protected-slot>
833 (define-standard-class <protected-hidden-slot> (<protected-slot>
835 (define-standard-class <protected-read-only-slot> (<protected-slot>
837 (define-standard-class <scm-slot> (<protected-slot>))
838 (define-standard-class <int-slot> (<foreign-slot>))
839 (define-standard-class <float-slot> (<foreign-slot>))
840 (define-standard-class <double-slot> (<foreign-slot>))
846 ;;; Finally! Initialize `direct-slots' and `slots' on <class>, and
847 ;;; `slots' on <slot>.
850 (define-syntax-rule (cons-slot (name . initargs) tail)
851 (cons (list 'name . initargs) tail))
852 (define-syntax-rule (initialize-direct-slots! class fold-slots)
853 (let ((specs (fold-slots macro-fold-right cons-slot '())))
854 (define (make-direct-slot-definition spec)
855 (let ((initargs (compute-direct-slot-definition-initargs class spec)))
856 (compute-direct-slot-definition class initargs)))
857 (struct-set! class class-index-direct-slots
858 (map make-direct-slot-definition specs))))
859 (define (initialize-slots! class)
860 (let ((slots (build-slots-list (class-direct-slots class)
861 (class-precedence-list class))))
862 (struct-set! class class-index-slots (allocate-slots class slots))))
864 ;; Finish initializing <class> with the specialized slot kinds.
865 (initialize-direct-slots! <class> fold-class-slots)
867 (initialize-slots! <class>)
868 (initialize-slots! <slot>))
874 ;;; Now, to build out the class hierarchy.
877 (define-standard-class <procedure-class> (<class>))
879 (define-standard-class <applicable-struct-class>
881 (class-add-flags! <applicable-struct-class>
882 vtable-flag-applicable-vtable)
884 (define-standard-class <applicable-struct-with-setter-class>
885 (<applicable-struct-class>))
886 (class-add-flags! <applicable-struct-with-setter-class>
887 vtable-flag-setter-vtable)
889 (define-standard-class <applicable> (<top>))
890 (define-standard-class <applicable-struct> (<object> <applicable>)
891 #:metaclass <applicable-struct-class>
893 (define-standard-class <applicable-struct-with-setter> (<applicable-struct>)
894 #:metaclass <applicable-struct-with-setter-class>
896 (define-standard-class <generic> (<applicable-struct>)
897 #:metaclass <applicable-struct-class>
899 (n-specialized #:init-value 0)
900 (extended-by #:init-value ())
902 (define-standard-class <extended-generic> (<generic>)
903 #:metaclass <applicable-struct-class>
904 (extends #:init-value ()))
905 (define-standard-class <generic-with-setter> (<generic>
906 <applicable-struct-with-setter>)
907 #:metaclass <applicable-struct-with-setter-class>)
908 (define-standard-class <accessor> (<generic-with-setter>)
909 #:metaclass <applicable-struct-with-setter-class>)
910 (define-standard-class <extended-generic-with-setter> (<extended-generic>
911 <generic-with-setter>)
912 #:metaclass <applicable-struct-with-setter-class>)
913 (define-standard-class <extended-accessor> (<accessor>
914 <extended-generic-with-setter>)
915 #:metaclass <applicable-struct-with-setter-class>)
917 (define-standard-class <method> (<object>)
924 (define-standard-class <accessor-method> (<method>)
925 (slot-definition #:init-keyword #:slot-definition))
927 (define-standard-class <boolean> (<top>))
928 (define-standard-class <char> (<top>))
929 (define-standard-class <list> (<top>))
930 ;; Not all pairs are lists, but there is code out there that relies on
931 ;; (is-a? '(1 2 3) <list>) to work. Terrible. How to fix?
932 (define-standard-class <pair> (<list>))
933 (define-standard-class <null> (<list>))
934 (define-standard-class <string> (<top>))
935 (define-standard-class <symbol> (<top>))
936 (define-standard-class <vector> (<top>))
937 (define-standard-class <foreign> (<top>))
938 (define-standard-class <hashtable> (<top>))
939 (define-standard-class <fluid> (<top>))
940 (define-standard-class <dynamic-state> (<top>))
941 (define-standard-class <frame> (<top>))
942 (define-standard-class <vm-continuation> (<top>))
943 (define-standard-class <bytevector> (<top>))
944 (define-standard-class <uvec> (<bytevector>))
945 (define-standard-class <array> (<top>))
946 (define-standard-class <bitvector> (<top>))
947 (define-standard-class <number> (<top>))
948 (define-standard-class <complex> (<number>))
949 (define-standard-class <real> (<complex>))
950 (define-standard-class <integer> (<real>))
951 (define-standard-class <fraction> (<real>))
952 (define-standard-class <keyword> (<top>))
953 (define-standard-class <unknown> (<top>))
954 (define-standard-class <procedure> (<applicable>)
955 #:metaclass <procedure-class>)
956 (define-standard-class <primitive-generic> (<procedure>)
957 #:metaclass <procedure-class>)
958 (define-standard-class <port> (<top>))
959 (define-standard-class <input-port> (<port>))
960 (define-standard-class <output-port> (<port>))
961 (define-standard-class <input-output-port> (<input-port> <output-port>))
963 (define (inherit-applicable! class)
964 "An internal routine to redefine a SMOB class that was added after
965 GOOPS was loaded, and on which scm_set_smob_apply installed an apply
967 ;; Why not use class-redefinition? We would, except that loading the
968 ;; compiler to compile effective methods can happen while GOOPS has
969 ;; only been partially loaded, and loading the compiler might cause
970 ;; SMOB types to be defined that need this facility. Instead we make
971 ;; a very specific hack, not a general solution. Probably the right
972 ;; solution is to avoid using the compiler, but that is another kettle
974 (unless (memq <applicable> (class-precedence-list class))
975 (unless (null? (class-slots class))
976 (error "SMOB object has slots?"))
979 (let ((subclasses (struct-ref super class-index-direct-subclasses)))
980 (struct-set! super class-index-direct-subclasses
981 (delq class subclasses))))
982 (struct-ref class class-index-direct-supers))
983 (struct-set! class class-index-direct-supers (list <applicable>))
984 (struct-set! class class-index-cpl (compute-cpl class))
985 (let ((subclasses (struct-ref <applicable> class-index-direct-subclasses)))
986 (struct-set! <applicable> class-index-direct-subclasses
987 (cons class subclasses)))))
993 ;;; At this point we have defined the class hierarchy, and it's time to
994 ;;; move on to instance allocation and generics. Once we have generics,
995 ;;; we'll fill out the metaobject protocol.
997 ;;; Here we define a limited version of `make', so that we can allocate
998 ;;; instances of specific classes. This definition will be replaced
1001 (define (%invalidate-method-cache! gf)
1002 (slot-set! gf 'effective-methods '())
1003 (recompute-generic-function-dispatch-procedure! gf))
1006 (define (invalidate-method-cache! gf)
1007 (%invalidate-method-cache! gf))
1009 (define (make class . args)
1011 ((or (eq? class <generic>) (eq? class <accessor>))
1012 (let ((z (make-struct/no-tail class #f '() 0 '())))
1013 (set-procedure-property! z 'name (get-keyword #:name args #f))
1014 (invalidate-method-cache! z)
1015 (when (eq? class <accessor>)
1016 (let ((setter (get-keyword #:setter args #f)))
1018 (slot-set! z 'setter setter))))
1021 (let ((z (%allocate-instance class)))
1023 ((or (eq? class <method>) (eq? class <accessor-method>))
1024 (for-each (match-lambda
1026 (slot-set! z slot (get-keyword kw args default))))
1027 '((#:generic-function generic-function #f)
1028 (#:specializers specializers ())
1029 (#:procedure procedure #f)
1030 (#:formals formals ())
1032 (#:make-procedure make-procedure #f))))
1033 ((memq <class> (class-precedence-list class))
1034 (class-add-flags! z (logior vtable-flag-goops-class
1035 vtable-flag-goops-valid))
1036 (for-each (match-lambda
1038 (slot-set! z slot (get-keyword kw args default))))
1040 (#:dsupers direct-supers ())
1041 (#:slots direct-slots ()))))
1043 (error "boot `make' does not support this class" class)))
1052 ;;; Before we go on, some notes about class redefinition. In GOOPS,
1053 ;;; classes can be redefined. Redefinition of a class marks the class
1054 ;;; as invalid, and instances will be lazily migrated over to the new
1055 ;;; representation as they are accessed. Migration happens when
1056 ;;; `class-of' is called on an instance. For more technical details on
1057 ;;; object redefinition, see struct.h.
1059 ;;; In the following interfaces, class-of handles the redefinition
1060 ;;; protocol. I would think though that there is some thread-unsafety
1061 ;;; here though as the { class, object data } pair needs to be accessed
1062 ;;; atomically, not the { class, object } pair.
1064 (define-inlinable (%class-slot-definition class slot-name kt kf)
1065 (let lp ((slots (struct-ref class class-index-slots)))
1068 (if (eq? (%slot-definition-name slot) slot-name)
1073 (define (class-slot-definition class slot-name)
1074 (unless (class? class)
1075 (scm-error 'wrong-type-arg #f "Not a class: ~S" (list class) #f))
1076 (%class-slot-definition class slot-name
1077 (lambda (slot) slot)
1080 (define (slot-ref obj slot-name)
1081 "Return the value from @var{obj}'s slot with the nam var{slot_name}."
1082 (let ((class (class-of obj)))
1083 (define (slot-value slot)
1085 ((%slot-definition-slot-ref slot)
1086 => (lambda (slot-ref) (slot-ref obj)))
1088 (struct-ref obj (%slot-definition-index slot)))))
1089 (define (have-slot slot)
1090 (let ((val (slot-value slot)))
1092 (slot-unbound class obj slot-name)
1095 (unless (symbol? slot-name)
1096 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
1097 (list slot-name) #f))
1098 (let ((val (slot-missing class obj slot-name)))
1100 (slot-unbound class obj slot-name)
1102 (%class-slot-definition class slot-name have-slot no-slot)))
1104 (define (slot-set! obj slot-name value)
1105 "Set the slot named @var{slot_name} of @var{obj} to @var{value}."
1106 (let ((class (class-of obj)))
1107 (define (have-slot slot)
1109 ((%slot-definition-slot-set! slot)
1110 => (lambda (slot-set!) (slot-set! obj value)))
1112 (struct-set! obj (%slot-definition-index slot) value))))
1114 (unless (symbol? slot-name)
1115 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
1116 (list slot-name) #f))
1117 (slot-missing class obj slot-name value))
1119 (%class-slot-definition class slot-name have-slot no-slot)))
1121 (define (slot-bound? obj slot-name)
1122 "Return the value from @var{obj}'s slot with the nam var{slot_name}."
1123 (let ((class (class-of obj)))
1124 (define (slot-value slot)
1126 ((%slot-definition-slot-ref slot)
1127 => (lambda (slot-ref) (slot-ref obj)))
1129 (struct-ref obj (%slot-definition-index slot)))))
1130 (define (have-slot slot)
1131 (not (unbound? (slot-value slot))))
1133 (unless (symbol? slot-name)
1134 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
1135 (list slot-name) #f))
1136 (let ((val (slot-missing class obj slot-name)))
1138 (slot-unbound class obj slot-name)
1140 (%class-slot-definition class slot-name have-slot no-slot)))
1142 (define (slot-exists? obj slot-name)
1143 "Return @code{#t} if @var{obj} has a slot named @var{slot_name}."
1144 (define (have-slot slot) #t)
1146 (unless (symbol? slot-name)
1147 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
1148 (list slot-name) #f))
1150 (%class-slot-definition (class-of obj) slot-name have-slot no-slot))
1153 (define (check-slot-args class obj slot-name)
1154 (unless (eq? class (class-of obj))
1155 (scm-error 'wrong-type-arg #f "~S is not the class of ~S"
1156 (list class obj) #f))
1157 (unless (symbol? slot-name)
1158 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
1159 (list slot-name) #f)))
1161 (define (slot-ref-using-class class obj slot-name)
1162 (issue-deprecation-warning "slot-ref-using-class is deprecated. "
1163 "Use slot-ref instead.")
1164 (check-slot-args class obj slot-name)
1165 (slot-ref obj slot-name))
1167 (define (slot-set-using-class! class obj slot-name value)
1168 (issue-deprecation-warning "slot-set-using-class! is deprecated. "
1169 "Use slot-set! instead.")
1170 (check-slot-args class obj slot-name)
1171 (slot-set! obj slot-name value))
1173 (define (slot-bound-using-class? class obj slot-name)
1174 (issue-deprecation-warning "slot-bound-using-class? is deprecated. "
1175 "Use slot-bound? instead.")
1176 (check-slot-args class obj slot-name)
1177 (slot-bound? obj slot-name))
1179 (define (slot-exists-using-class? class obj slot-name)
1180 (issue-deprecation-warning "slot-exists-using-class? is deprecated. "
1181 "Use slot-exists? instead.")
1182 (check-slot-args class obj slot-name)
1183 (slot-exists? obj slot-name)))
1189 ;;; Method accessors.
1191 (define (method-generic-function obj)
1192 "Return the generic function for the method @var{obj}."
1193 (unless (is-a? obj <method>)
1194 (scm-error 'wrong-type-arg #f "Not a method: ~S"
1196 (slot-ref obj 'generic-function))
1198 (define (method-specializers obj)
1199 "Return specializers of the method @var{obj}."
1200 (unless (is-a? obj <method>)
1201 (scm-error 'wrong-type-arg #f "Not a method: ~S"
1203 (slot-ref obj 'specializers))
1205 (define (method-procedure obj)
1206 "Return the procedure of the method @var{obj}."
1207 (unless (is-a? obj <method>)
1208 (scm-error 'wrong-type-arg #f "Not a method: ~S"
1210 (slot-ref obj 'procedure))
1216 ;;; Generic functions!
1218 ;;; Generic functions have an applicable-methods cache associated with
1219 ;;; them. Every distinct set of types that is dispatched through a
1220 ;;; generic adds an entry to the cache. A composite dispatch procedure
1221 ;;; is recomputed every time an entry gets added to the cache, or when
1222 ;;; the cache is invalidated.
1224 ;;; In steady-state, this dispatch procedure is never regenerated; but
1225 ;;; during warm-up there is some churn.
1227 ;;; So what is the deal if warm-up happens in a multithreaded context?
1228 ;;; There is indeed a window between missing the cache for a certain set
1229 ;;; of arguments, and then updating the cache with the newly computed
1230 ;;; applicable methods. One of the updaters is liable to lose their new
1233 ;;; This is actually OK though, because a subsequent cache miss for the
1234 ;;; race loser will just cause memoization to try again. The cache will
1235 ;;; eventually be consistent. We're not mutating the old part of the
1236 ;;; cache, just consing on the new entry.
1238 ;;; It doesn't even matter if the dispatch procedure and the cache are
1239 ;;; inconsistent -- most likely the type-set that lost the dispatch
1240 ;;; procedure race will simply re-trigger a memoization, but since the
1241 ;;; winner isn't in the effective-methods cache, it will likely also
1242 ;;; re-trigger a memoization, and the cache will finally be consistent.
1243 ;;; As you can see there is a possibility for ping-pong effects, but
1244 ;;; it's unlikely given the shortness of the window between slot-set!
1247 ;;; We probably do need to use atomic access primitives to correctly
1248 ;;; handle concurrency, but that's a more general Guile concern.
1251 (define-syntax arity-case
1254 ;; (arity-case n 2 foo bar)
1260 ((arity-case n max form alternate)
1261 (let ((max (syntax->datum #'max)))
1264 (let ((ids (map (lambda (n)
1265 (let* ((n (+ (char->integer #\a) n))
1266 (c (integer->char n)))
1267 (datum->syntax #'here (symbol c))))
1269 #`(((#,n) (form #,@ids))
1273 (else alternate)))))))
1276 ;;; These dispatchers are set as the "procedure" field of <generic>
1277 ;;; instances. Unlike CLOS, in GOOPS a generic function can have
1278 ;;; multiple arities.
1280 ;;; We pre-generate fast dispatchers for applications of up to 20
1281 ;;; arguments. More arguments than that will go through slower generic
1282 ;;; routines that cons arguments into a rest list.
1284 (define (multiple-arity-dispatcher fv miss)
1285 (define-syntax dispatch
1287 (define (build-clauses args)
1288 (let ((len (length (syntax->datum args))))
1289 #`((#,args ((vector-ref fv #,len) . #,args))
1290 . #,(syntax-case args ()
1292 ((arg ... _) (build-clauses #'(arg ...)))))))
1296 #,@(build-clauses #'(arg ...))
1297 (args (apply miss args)))))))
1298 (arity-case (vector-length fv) 20 dispatch
1300 (let ((nargs (length args)))
1301 (if (< nargs (vector-length fv))
1302 (apply (vector-ref fv nargs) args)
1303 (apply miss args))))))
1306 ;;; The above multiple-arity-dispatcher is entirely sufficient, and
1307 ;;; should be fast enough. Still, for no good reason we also have an
1308 ;;; arity dispatcher for generics that are only called with one arity.
1310 (define (single-arity-dispatcher f nargs miss)
1311 (define-syntax-rule (dispatch arg ...)
1313 ((arg ...) (f arg ...))
1314 (args (apply miss args))))
1315 (arity-case nargs 20 dispatch
1317 (if (eqv? (length args) nargs)
1319 (apply miss args)))))
1322 ;;; The guts of generic function dispatch are here. Once we've selected
1323 ;;; an arity, we need to map from arguments to effective method. Until
1324 ;;; we have `eqv?' specializers, this map is entirely a function of the
1325 ;;; types (classes) of the arguments. So, we look in the cache to see
1326 ;;; if we have seen this set of concrete types, and if so we apply the
1327 ;;; previously computed effective method. Otherwise we miss the cache,
1328 ;;; so we'll have to compute the right answer for this set of types, add
1329 ;;; the mapping to the cache, and apply the newly computed method.
1331 ;;; The cached mapping is invalidated whenever a new method is defined
1332 ;;; on this generic, or whenever the class hierarchy of any method
1333 ;;; specializer changes.
1335 (define (single-arity-cache-dispatch cache nargs cache-miss)
1338 (((typev . cmethod) . cache)
1340 ((eqv? nargs (vector-length typev))
1341 (let ((cache-miss (single-arity-cache-dispatch cache nargs cache-miss)))
1342 (define (type-ref n)
1343 (and (< n nargs) (vector-ref typev n)))
1344 (define-syntax args-match?
1347 ((args-match? (arg type) (arg* type*) ...)
1348 ;; Check that the arg has the exact type that we saw. It
1349 ;; could be that `type' is #f, which indicates the end of
1350 ;; the specializers list. Once all specializers have been
1351 ;; examined, we don't need to look at any more arguments
1352 ;; to know that this is a cache hit.
1354 (and (eq? (class-of arg) type)
1355 (args-match? (arg* type*) ...))))))
1356 (define-syntax dispatch
1358 (define (bind-types types k)
1359 (let lp ((types types) (n 0))
1360 (syntax-case types ()
1363 #`(let ((type (type-ref #,n)))
1364 #,(lp #'types (1+ n)))))))
1367 (with-syntax (((type ...) (generate-temporaries #'(arg ...))))
1372 (if (args-match? (arg type) ...)
1374 (cache-miss arg ...))))))))))
1375 (arity-case nargs 20 dispatch
1377 (define (args-match? args)
1378 (let lp ((args args) (n 0))
1381 (or (not (vector-ref typev n))
1382 (and (eq? (vector-ref typev n) (class-of arg))
1385 (if (args-match? args)
1386 (apply cmethod args)
1387 (apply cache-miss args))))))
1389 (single-arity-cache-dispatch cache nargs cache-miss))))))
1391 (define (compute-generic-function-dispatch-procedure gf)
1392 (define (seen-arities cache)
1393 (let lp ((arities 0) (cache cache))
1396 (((typev . cmethod) . cache)
1397 (lp (logior arities (ash 1 (vector-length typev)))
1399 (define (cache-miss . args)
1400 (memoize-generic-function-application! gf args)
1402 (let* ((cache (slot-ref gf 'effective-methods))
1403 (arities (seen-arities cache))
1404 (max-arity (let lp ((max -1))
1405 (if (< arities (ash 1 (1+ max)))
1410 ;; Nothing in the cache.
1412 ((= arities (ash 1 max-arity))
1413 ;; Only one arity in the cache.
1414 (let* ((nargs max-arity)
1415 (f (single-arity-cache-dispatch cache nargs cache-miss)))
1416 (single-arity-dispatcher f nargs cache-miss)))
1418 ;; Multiple arities.
1419 (let ((fv (make-vector (1+ max-arity) #f)))
1421 (when (<= n max-arity)
1422 (let ((f (single-arity-cache-dispatch cache n cache-miss)))
1423 (vector-set! fv n f)
1425 (multiple-arity-dispatcher fv cache-miss))))))
1427 (define (recompute-generic-function-dispatch-procedure! gf)
1428 (slot-set! gf 'procedure
1429 (compute-generic-function-dispatch-procedure gf)))
1431 (define (memoize-effective-method! gf args applicable)
1432 (define (record-types args)
1433 (let ((typev (make-vector (length args) #f)))
1434 (let lp ((n 0) (args args))
1435 (when (and (< n (slot-ref gf 'n-specialized))
1439 (vector-set! typev n (class-of arg))
1440 (lp (1+ n) args)))))
1442 (let* ((typev (record-types args))
1443 (cmethod (compute-cmethod applicable typev))
1444 (cache (acons typev cmethod (slot-ref gf 'effective-methods))))
1445 (slot-set! gf 'effective-methods cache)
1446 (recompute-generic-function-dispatch-procedure! gf)
1450 ;;; If a method refers to `next-method' in its body, that method will be
1451 ;;; able to dispatch to the next most specific method. The exact
1452 ;;; `next-method' implementation is only known at runtime, as it is a
1453 ;;; function of which precise argument types are being dispatched, which
1454 ;;; might be subclasses of the method's declared specializers.
1456 ;;; Guile implements `next-method' by binding it as a closure variable.
1457 ;;; An effective method is bound to a specific `next-method' by the
1458 ;;; `make-procedure' slot of a <method>, which returns the new closure.
1460 (define (compute-cmethod methods types)
1463 (match (slot-ref method 'make-procedure)
1464 (#f (method-procedure method))
1470 (no-next-method (method-generic-function method) args)))
1472 (compute-cmethod methods types)))))))))
1478 (define (memoize-generic-function-application! gf args)
1479 (let ((applicable ((if (eq? gf compute-applicable-methods)
1480 %compute-applicable-methods
1481 compute-applicable-methods)
1484 (memoize-effective-method! gf args applicable))
1486 (no-applicable-method gf args)))))
1488 (define no-applicable-method
1489 (make <generic> #:name 'no-applicable-method))
1493 ;; Then load the rest of GOOPS
1496 ;; FIXME: deprecate.
1497 (define min-fixnum (- (expt 2 29)))
1498 (define max-fixnum (- (expt 2 29) 1))
1503 (define (goops-error format-string . args)
1504 (scm-error 'goops-error #f format-string args '()))
1510 (define ensure-metaclass-with-supers
1511 (let ((table-of-metas '()))
1512 (lambda (meta-supers)
1513 (let ((entry (assoc meta-supers table-of-metas)))
1515 ;; Found a previously created metaclass
1517 ;; Create a new meta-class which inherit from "meta-supers"
1518 (let ((new (make <class> #:dsupers meta-supers
1520 #:name (gensym "metaclass"))))
1521 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
1524 (define (ensure-metaclass supers)
1527 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
1528 (all-cpls (append-map (lambda (m)
1529 (cdr (class-precedence-list m)))
1532 ;; Find the most specific metaclasses. The new metaclass will be
1533 ;; a subclass of these.
1536 (when (and (not (member meta all-cpls))
1537 (not (member meta needed-metas)))
1538 (set! needed-metas (append needed-metas (list meta)))))
1540 ;; Now return a subclass of the metaclasses we found.
1541 (if (null? (cdr needed-metas))
1542 (car needed-metas) ; If there's only one, just use it.
1543 (ensure-metaclass-with-supers needed-metas)))))
1549 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
1551 ;;; SLOT-DEFINITION ::= INSTANCE-OF-<SLOT> | (SLOT-NAME OPTION ...)
1552 ;;; OPTION ::= KEYWORD VALUE
1555 (define (make-class supers slots . options)
1556 (define (find-duplicate l)
1560 (if (memq head tail)
1562 (find-duplicate tail)))))
1563 (define (slot-spec->name slot-spec)
1565 (((? symbol? name) . args) name)
1566 ;; We can get here when redefining classes.
1567 ((? slot? slot) (%slot-definition-name slot))))
1569 (let* ((name (get-keyword #:name options *unbound*))
1570 (supers (if (not (or-map (lambda (class)
1572 (class-precedence-list class)))
1574 (append supers (list <object>))
1576 (metaclass (or (get-keyword #:metaclass options #f)
1577 (ensure-metaclass supers))))
1579 ;; Verify that all direct slots are different and that we don't inherit
1580 ;; several time from the same class
1581 (let ((tmp1 (find-duplicate supers))
1582 (tmp2 (find-duplicate (map slot-spec->name slots))))
1584 (goops-error "make-class: super class ~S is duplicate in class ~S"
1587 (goops-error "make-class: slot ~S is duplicate in class ~S"
1590 ;; Everything seems correct, build the class
1591 (apply make metaclass
1597 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
1599 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
1600 ;;; OPTION ::= KEYWORD VALUE
1602 (define-syntax class
1604 (define (parse-options options)
1605 (syntax-case options ()
1607 ((kw arg . options) (keyword? (syntax->datum #'kw))
1608 (with-syntax ((options (parse-options #'options)))
1609 (syntax-case #'kw ()
1611 #'(kw 'arg #:init-thunk (lambda () arg) . options))
1613 #'(kw arg . options)))))))
1614 (define (check-valid-kwargs args)
1615 (syntax-case args ()
1617 ((kw arg . args) (keyword? (syntax->datum #'kw))
1618 #`(kw arg . #,(check-valid-kwargs #'args)))))
1619 (define (parse-slots-and-kwargs args)
1620 (syntax-case args ()
1623 ((kw . _) (keyword? (syntax->datum #'kw))
1624 #`(() #,(check-valid-kwargs args)))
1625 (((name option ...) args ...)
1626 (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...)))
1627 ((option ...) (parse-options #'(option ...))))
1628 #'(((list 'name option ...) . slots) kwargs)))
1629 ((name args ...) (symbol? (syntax->datum #'name))
1630 (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...))))
1631 #'(('(name) . slots) kwargs)))))
1633 ((class (super ...) arg ...)
1634 (with-syntax ((((slot-def ...) (option ...))
1635 (parse-slots-and-kwargs #'(arg ...))))
1636 #'(make-class (list super ...)
1640 (define-syntax define-class-pre-definition
1643 ((_ (k arg rest ...) out ...)
1644 (keyword? (syntax->datum #'k))
1645 (case (syntax->datum #'k)
1646 ((#:getter #:setter)
1647 #'(define-class-pre-definition (rest ...)
1649 (when (or (not (defined? 'arg))
1650 (not (is-a? arg <generic>)))
1653 (ensure-generic (if (defined? 'arg) arg #f) 'arg)))))
1655 #'(define-class-pre-definition (rest ...)
1657 (when (or (not (defined? 'arg))
1658 (not (is-a? arg <accessor>)))
1661 (ensure-accessor (if (defined? 'arg) arg #f) 'arg)))))
1663 #'(define-class-pre-definition (rest ...) out ...))))
1665 #'(begin out ...)))))
1667 ;; Some slot options require extra definitions to be made. In
1668 ;; particular, we want to make sure that the generic function objects
1669 ;; which represent accessors exist before `make-class' tries to add
1671 (define-syntax define-class-pre-definitions
1676 ((_ (slot rest ...) out ...)
1677 (keyword? (syntax->datum #'slot))
1679 ((_ (slot rest ...) out ...)
1680 (identifier? #'slot)
1681 #'(define-class-pre-definitions (rest ...)
1683 ((_ ((slotname slotopt ...) rest ...) out ...)
1684 #'(define-class-pre-definitions (rest ...)
1685 out ... (define-class-pre-definition (slotopt ...)))))))
1687 (define-syntax-rule (define-class name supers slot ...)
1689 (define-class-pre-definitions (slot ...))
1690 (if (and (defined? 'name)
1691 (is-a? name <class>)
1692 (memq <object> (class-precedence-list name)))
1693 (class-redefinition name
1694 (class supers slot ... #:name 'name))
1695 (toplevel-define! 'name (class supers slot ... #:name 'name)))))
1697 (define-syntax-rule (standard-define-class arg ...)
1698 (define-class arg ...))
1701 ;;; {Generic functions and accessors}
1704 ;; Apparently the desired semantics are that we extend previous
1705 ;; procedural definitions, but that if `name' was already a generic, we
1706 ;; overwrite its definition.
1707 (define-syntax define-generic
1710 ((define-generic name) (symbol? (syntax->datum #'name))
1712 (if (and (defined? 'name) (is-a? name <generic>))
1713 (make <generic> #:name 'name)
1714 (ensure-generic (if (defined? 'name) name #f) 'name)))))))
1716 (define-syntax define-extended-generic
1719 ((define-extended-generic name val) (symbol? (syntax->datum #'name))
1720 #'(define name (make-extended-generic val 'name))))))
1722 (define-syntax define-extended-generics
1724 (define (id-append ctx a b)
1725 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
1727 ((define-extended-generic (name ...) #:prefix (prefix ...))
1728 (and (and-map symbol? (syntax->datum #'(name ...)))
1729 (and-map symbol? (syntax->datum #'(prefix ...))))
1730 (with-syntax ((((val ...)) (map (lambda (name)
1731 (map (lambda (prefix)
1732 (id-append name prefix name))
1736 (define-extended-generic name (list val ...))
1739 (define* (make-generic #:optional name)
1740 (make <generic> #:name name))
1742 (define* (make-extended-generic gfs #:optional name)
1743 (let* ((gfs (if (list? gfs) gfs (list gfs)))
1744 (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
1746 (let* ((sname (and name (make-setter-name name)))
1748 (append-map (lambda (gf)
1749 (if (is-a? gf <generic-with-setter>)
1750 (list (ensure-generic (setter gf)
1754 (es (make <extended-generic-with-setter>
1757 #:setter (make <extended-generic>
1759 #:extends setters))))
1760 (extended-by! setters (setter es))
1762 (make <extended-generic>
1765 (extended-by! gfs ans)
1768 (define (extended-by! gfs eg)
1769 (for-each (lambda (gf)
1770 (slot-set! gf 'extended-by
1771 (cons eg (slot-ref gf 'extended-by))))
1773 (invalidate-method-cache! eg))
1775 (define (not-extended-by! gfs eg)
1776 (for-each (lambda (gf)
1777 (slot-set! gf 'extended-by
1778 (delq! eg (slot-ref gf 'extended-by))))
1780 (invalidate-method-cache! eg))
1782 (define* (ensure-generic old-definition #:optional name)
1783 (cond ((is-a? old-definition <generic>) old-definition)
1784 ((procedure-with-setter? old-definition)
1785 (make <generic-with-setter>
1787 #:default (procedure old-definition)
1788 #:setter (setter old-definition)))
1789 ((procedure? old-definition)
1790 (if (generic-capability? old-definition) old-definition
1791 (make <generic> #:name name #:default old-definition)))
1792 (else (make <generic> #:name name))))
1794 ;; same semantics as <generic>
1795 (define-syntax-rule (define-accessor name)
1797 (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
1798 ((is-a? name <accessor>) (make <accessor> #:name 'name))
1799 (else (ensure-accessor name 'name)))))
1801 (define (make-setter-name name)
1802 (string->symbol (string-append "setter:" (symbol->string name))))
1804 (define* (make-accessor #:optional name)
1807 #:setter (make <generic>
1808 #:name (and name (make-setter-name name)))))
1810 (define* (ensure-accessor proc #:optional name)
1811 (cond ((and (is-a? proc <accessor>)
1812 (is-a? (setter proc) <generic>))
1814 ((is-a? proc <generic-with-setter>)
1815 (upgrade-accessor proc (setter proc)))
1816 ((is-a? proc <generic>)
1817 (upgrade-accessor proc (make-generic name)))
1818 ((procedure-with-setter? proc)
1821 #:default (procedure proc)
1822 #:setter (ensure-generic (setter proc) name)))
1824 (ensure-accessor (if (generic-capability? proc)
1825 (make <generic> #:name name #:default proc)
1826 (ensure-generic proc name))
1829 (make-accessor name))))
1831 (define (upgrade-accessor generic setter)
1832 (let ((methods (slot-ref generic 'methods))
1833 (gws (make (if (is-a? generic <extended-generic>)
1834 <extended-generic-with-setter>
1836 #:name (generic-function-name generic)
1837 #:extended-by (slot-ref generic 'extended-by)
1839 (when (is-a? generic <extended-generic>)
1840 (let ((gfs (slot-ref generic 'extends)))
1841 (not-extended-by! gfs generic)
1842 (slot-set! gws 'extends gfs)
1843 (extended-by! gfs gws)))
1844 ;; Steal old methods
1845 (for-each (lambda (method)
1846 (slot-set! method 'generic-function gws))
1848 (slot-set! gws 'methods methods)
1849 (invalidate-method-cache! gws)
1856 ;; Note: `a' and `b' can have unequal lengths (i.e. one can be one
1857 ;; element longer than the other when we have a dotted parameter
1858 ;; list). For instance, with the call
1864 ;; (define-method M (a . l) ....)
1865 ;; (define-method M (a) ....)
1867 ;; we consider that the second method is more specific.
1869 ;; Precondition: `a' and `b' are methods and are applicable to `types'.
1870 (define (%method-more-specific? a b types)
1871 (let lp ((a-specializers (method-specializers a))
1872 (b-specializers (method-specializers b))
1875 ;; (a) less specific than (a b ...) or (a . b)
1876 ((null? a-specializers) #t)
1877 ;; (a b ...) or (a . b) less specific than (a)
1878 ((null? b-specializers) #f)
1879 ;; (a . b) less specific than (a b ...)
1880 ((not (pair? a-specializers)) #f)
1881 ;; (a b ...) more specific than (a . b)
1882 ((not (pair? b-specializers)) #t)
1884 (let ((a-specializer (car a-specializers))
1885 (b-specializer (car b-specializers))
1886 (a-specializers (cdr a-specializers))
1887 (b-specializers (cdr b-specializers))
1889 (types (cdr types)))
1890 (if (eq? a-specializer b-specializer)
1891 (lp a-specializers b-specializers types)
1892 (let lp ((cpl (class-precedence-list type)))
1893 (let ((elt (car cpl)))
1895 ((eq? a-specializer elt) #t)
1896 ((eq? b-specializer elt) #f)
1897 (else (lp (cdr cpl))))))))))))
1899 (define (%sort-applicable-methods methods types)
1900 (sort methods (lambda (a b) (%method-more-specific? a b types))))
1902 (define (generic-function-methods obj)
1903 "Return the methods of the generic function @var{obj}."
1904 (define (fold-upward method-lists gf)
1906 ((is-a? gf <extended-generic>)
1907 (let lp ((method-lists method-lists) (gfs (slot-ref gf 'extends)))
1911 (lp (fold-upward (cons (slot-ref gf 'methods) method-lists) gf)
1913 (else method-lists)))
1914 (define (fold-downward method-lists gf)
1915 (let lp ((method-lists (cons (slot-ref gf 'methods) method-lists))
1916 (gfs (slot-ref gf 'extended-by)))
1920 (lp (fold-downward method-lists gf) gfs)))))
1921 (unless (is-a? obj <generic>)
1922 (scm-error 'wrong-type-arg #f "Not a generic: ~S"
1924 (concatenate (fold-downward (fold-upward '() obj) obj)))
1926 (define (%compute-applicable-methods gf args)
1927 (define (method-applicable? m types)
1928 (let lp ((specs (method-specializers m)) (types types))
1930 ((null? specs) (null? types))
1931 ((not (pair? specs)) #t)
1934 (and (memq (car specs) (class-precedence-list (car types)))
1935 (lp (cdr specs) (cdr types)))))))
1936 (let ((n (length args))
1937 (types (map class-of args)))
1938 (let lp ((methods (generic-function-methods gf))
1941 (and (not (null? applicable))
1942 (%sort-applicable-methods applicable types))
1943 (let ((m (car methods)))
1945 (if (method-applicable? m types)
1949 (define compute-applicable-methods %compute-applicable-methods)
1951 (define (toplevel-define! name val)
1952 (module-define! (current-module) name val))
1954 (define-syntax define-method
1955 (syntax-rules (setter)
1956 ((_ ((setter name) . args) body ...)
1958 (when (or (not (defined? 'name))
1959 (not (is-a? name <accessor>)))
1960 (toplevel-define! 'name
1962 (if (defined? 'name) name #f) 'name)))
1963 (add-method! (setter name) (method args body ...))))
1964 ((_ (name . args) body ...)
1966 ;; FIXME: this code is how it always was, but it's quite cracky:
1967 ;; it will only define the generic function if it was undefined
1968 ;; before (ok), or *was defined to #f*. The latter is crack. But
1969 ;; there are bootstrap issues about fixing this -- change it to
1970 ;; (is-a? name <generic>) and see.
1971 (when (or (not (defined? 'name))
1973 (toplevel-define! 'name (make <generic> #:name 'name)))
1974 (add-method! name (method args body ...))))))
1976 (define-syntax method
1978 (define (parse-args args)
1979 (let lp ((ls args) (formals '()) (specializers '()))
1982 (and (identifier? #'f) (identifier? #'s))
1985 (cons #'s specializers)))
1990 (cons #'<top> specializers)))
1992 (list (reverse formals)
1993 (reverse (cons #''() specializers))))
1995 (identifier? #'tail)
1996 (list (append (reverse formals) #'tail)
1997 (reverse (cons #'<top> specializers)))))))
1999 (define (find-free-id exp referent)
2002 (or (find-free-id #'x referent)
2003 (find-free-id #'y referent)))
2006 (let ((id (datum->syntax #'x referent)))
2007 (and (free-identifier=? #'x id) id)))
2010 (define (compute-procedure formals body)
2011 (syntax-case body ()
2013 (with-syntax ((formals formals))
2014 #'(lambda formals body0 ...)))))
2016 (define (->proper args)
2017 (let lp ((ls args) (out '()))
2019 ((x . xs) (lp #'xs (cons #'x out)))
2021 (tail (reverse (cons #'tail out))))))
2023 (define (compute-make-procedure formals body next-method)
2024 (syntax-case body ()
2026 (with-syntax ((next-method next-method))
2027 (syntax-case formals ()
2029 #'(lambda (real-next-method)
2030 (lambda (formal ...)
2031 (let ((next-method (lambda args
2033 (real-next-method formal ...)
2034 (apply real-next-method args)))))
2037 (with-syntax (((formal ...) (->proper #'formals)))
2038 #'(lambda (real-next-method)
2040 (let ((next-method (lambda args
2042 (apply real-next-method formal ...)
2043 (apply real-next-method args)))))
2046 (define (compute-procedures formals body)
2047 ;; So, our use of this is broken, because it operates on the
2048 ;; pre-expansion source code. It's equivalent to just searching
2049 ;; for referent in the datums. Ah well.
2050 (let ((id (find-free-id body 'next-method)))
2052 ;; return a make-procedure
2054 (compute-make-procedure formals body id))
2055 (values (compute-procedure formals body)
2059 ((_ args) #'(method args (if #f #f)))
2060 ((_ args body0 body1 ...)
2061 (with-syntax (((formals (specializer ...)) (parse-args #'args)))
2064 (compute-procedures #'formals #'(body0 body1 ...)))
2065 (lambda (procedure make-procedure)
2066 (with-syntax ((procedure procedure)
2067 (make-procedure make-procedure))
2069 #:specializers (cons* specializer ...)
2071 #:body '(body0 body1 ...)
2072 #:make-procedure make-procedure
2073 #:procedure procedure)))))))))
2078 ;;; These are useful when dealing with method specializers, which might
2079 ;;; have a rest argument.
2082 (define (map* fn . l) ; A map which accepts dotted lists (arg lists
2083 (cond ; must be "isomorph"
2084 ((null? (car l)) '())
2085 ((pair? (car l)) (cons (apply fn (map car l))
2086 (apply map* fn (map cdr l))))
2087 (else (apply fn l))))
2089 (define (for-each* fn . l) ; A for-each which accepts dotted lists (arg lists
2090 (cond ; must be "isomorph"
2091 ((null? (car l)) '())
2092 ((pair? (car l)) (apply fn (map car l)) (apply for-each* fn (map cdr l)))
2093 (else (apply fn l))))
2095 (define (length* ls)
2098 ((not (pair? ls)) n)))
2104 (define (add-method-in-classes! m)
2105 ;; Add method in all the classes which appears in its specializers list
2106 (for-each* (lambda (x)
2107 (let ((dm (class-direct-methods x)))
2109 (struct-set! x class-index-direct-methods (cons m dm)))))
2110 (method-specializers m)))
2112 (define (remove-method-in-classes! m)
2113 ;; Remove method in all the classes which appears in its specializers list
2114 (for-each* (lambda (x)
2116 class-index-direct-methods
2117 (delv! m (class-direct-methods x))))
2118 (method-specializers m)))
2120 (define (compute-new-list-of-methods gf new)
2121 (let ((new-spec (method-specializers new))
2122 (methods (slot-ref gf 'methods)))
2123 (let loop ((l methods))
2126 (if (equal? (method-specializers (car l)) new-spec)
2128 ;; This spec. list already exists. Remove old method from dependents
2129 (remove-method-in-classes! (car l))
2134 (define (method-n-specializers m)
2135 (length* (slot-ref m 'specializers)))
2137 (define (calculate-n-specialized gf)
2138 (fold (lambda (m n) (max n (method-n-specializers m)))
2140 (generic-function-methods gf)))
2142 (define (invalidate-method-cache! gf)
2143 (slot-set! gf 'n-specialized (calculate-n-specialized gf))
2144 (%invalidate-method-cache! gf)
2145 (for-each (lambda (gf) (invalidate-method-cache! gf))
2146 (slot-ref gf 'extended-by)))
2148 (define internal-add-method!
2149 (method ((gf <generic>) (m <method>))
2150 (slot-set! m 'generic-function gf)
2151 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
2152 (invalidate-method-cache! gf)
2153 (add-method-in-classes! m)
2156 (define-generic add-method!)
2158 ((method-procedure internal-add-method!) add-method! internal-add-method!)
2160 (define-method (add-method! (proc <procedure>) (m <method>))
2161 (if (generic-capability? proc)
2163 (enable-primitive-generic! proc)
2164 (add-method! proc m))
2167 (define-method (add-method! (pg <primitive-generic>) (m <method>))
2168 (add-method! (primitive-generic-generic pg) m))
2170 (define-method (add-method! obj (m <method>))
2171 (goops-error "~S is not a valid generic function" obj))
2174 ;;; {Access to meta objects}
2180 (define-method (method-source (m <method>))
2181 (let* ((spec (map* class-name (slot-ref m 'specializers)))
2182 (src (procedure-source (slot-ref m 'procedure))))
2184 (let ((args (cadr src))
2187 (cons (map* list args spec)
2190 (define-method (method-formals (m <method>))
2191 (slot-ref m 'formals))
2196 (define (slot-init-function class slot-name)
2197 (%slot-definition-init-thunk (or (class-slot-definition class slot-name)
2198 (error "slot not found" slot-name))))
2200 (define (accessor-method-slot-definition obj)
2201 "Return the slot definition of the accessor @var{obj}."
2202 (slot-ref obj 'slot-definition))
2206 ;;; {Standard methods used by the C runtime}
2209 ;;; Methods to compare objects
2212 ;; Have to do this in a strange order because equal? is used in the
2213 ;; add-method! implementation; we need to make sure that when the
2214 ;; primitive is extended, that the generic has a method. =
2215 (define g-equal? (make-generic 'equal?))
2216 ;; When this generic gets called, we will have already checked eq? and
2217 ;; eqv? -- the purpose of this generic is to extend equality. So by
2218 ;; default, there is no extension, thus the #f return.
2219 (add-method! g-equal? (method (x y) #f))
2220 (set-primitive-generic! equal? g-equal?)
2223 ;;; methods to display/write an object
2226 ; Code for writing objects must test that the slots they use are
2227 ; bound. Otherwise a slot-unbound method will be called and will
2228 ; conduct to an infinite loop.
2231 (define (display-address o file)
2232 (display (number->string (object-address o) 16) file))
2234 (define-method (write o file)
2235 (display "#<instance " file)
2236 (display-address o file)
2239 (define write-object (primitive-generic-generic write))
2241 (define-method (write (o <object>) file)
2242 (let ((class (class-of o)))
2243 (if (slot-bound? class 'name)
2246 (display (class-name class) file)
2247 (display #\space file)
2248 (display-address o file)
2252 (define-method (write (slot <slot>) file)
2253 (let ((class (class-of slot)))
2254 (if (and (slot-bound? class 'name)
2255 (slot-bound? slot 'name))
2258 (display (class-name class) file)
2259 (display #\space file)
2260 (display (%slot-definition-name slot) file)
2261 (display #\space file)
2262 (display-address slot file)
2266 (define-method (write (class <class>) file)
2267 (let ((meta (class-of class)))
2268 (if (and (slot-bound? class 'name)
2269 (slot-bound? meta 'name))
2272 (display (class-name meta) file)
2273 (display #\space file)
2274 (display (class-name class) file)
2275 (display #\space file)
2276 (display-address class file)
2280 (define-method (write (gf <generic>) file)
2281 (let ((meta (class-of gf)))
2282 (if (and (slot-bound? meta 'name)
2283 (slot-bound? gf 'methods))
2286 (display (class-name meta) file)
2287 (let ((name (generic-function-name gf)))
2290 (display #\space file)
2291 (display name file))))
2293 (display (length (generic-function-methods gf)) file)
2294 (display ")>" file))
2297 (define-method (write (o <method>) file)
2298 (let ((meta (class-of o)))
2299 (if (and (slot-bound? meta 'name)
2300 (slot-bound? o 'specializers))
2303 (display (class-name meta) file)
2304 (display #\space file)
2305 (display (map* (lambda (spec)
2306 (if (slot-bound? spec 'name)
2307 (slot-ref spec 'name)
2309 (method-specializers o))
2311 (display #\space file)
2312 (display-address o file)
2316 ;; Display (do the same thing as write by default)
2317 (define-method (display o file)
2318 (write-object o file))
2321 ;;; Handling of duplicate bindings in the module system
2324 (define (find-subclass super name)
2325 (let lp ((classes (class-direct-subclasses super)))
2328 (error "class not found" name))
2329 ((and (slot-bound? (car classes) 'name)
2330 (eq? (class-name (car classes)) name))
2333 (lp (cdr classes))))))
2336 (define <module> (find-subclass <top> '<module>))
2338 (define-method (merge-generics (module <module>)
2348 (define-method (merge-generics (module <module>)
2356 (and (not (eq? val1 val2))
2357 (make-variable (make-extended-generic (list val2 val1) name))))
2359 (define-method (merge-generics (module <module>)
2366 (gf <extended-generic>))
2367 (and (not (memq val2 (slot-ref gf 'extends)))
2371 (cons val2 (delq! val2 (slot-ref gf 'extends))))
2374 (cons gf (delq! gf (slot-ref val2 'extended-by))))
2375 (invalidate-method-cache! gf)
2378 (module-define! duplicate-handlers 'merge-generics merge-generics)
2380 (define-method (merge-accessors (module <module>)
2390 (define-method (merge-accessors (module <module>)
2398 (merge-generics module name int1 val1 int2 val2 var val))
2400 (module-define! duplicate-handlers 'merge-accessors merge-accessors)
2406 (define (class-slot-ref class slot-name)
2407 (let ((slot (class-slot-definition class slot-name)))
2408 (unless (memq (%slot-definition-allocation slot) '(#:class #:each-subclass))
2409 (slot-missing class slot-name))
2410 (let ((x ((%slot-definition-slot-ref slot) #f)))
2412 (slot-unbound class slot-name)
2415 (define (class-slot-set! class slot-name value)
2416 (let ((slot (class-slot-definition class slot-name)))
2417 (unless (memq (%slot-definition-allocation slot) '(#:class #:each-subclass))
2418 (slot-missing class slot-name))
2419 ((%slot-definition-slot-set! slot) #f value)))
2421 (define-method (slot-unbound (c <class>) (o <object>) s)
2422 (goops-error "Slot `~S' is unbound in object ~S" s o))
2424 (define-method (slot-unbound (c <class>) s)
2425 (goops-error "Slot `~S' is unbound in class ~S" s c))
2427 (define-method (slot-unbound (o <object>))
2428 (goops-error "Unbound slot in object ~S" o))
2430 (define-method (slot-missing (c <class>) (o <object>) s)
2431 (goops-error "No slot with name `~S' in object ~S" s o))
2433 (define-method (slot-missing (c <class>) s)
2434 (goops-error "No class slot with name `~S' in class ~S" s c))
2437 (define-method (slot-missing (c <class>) (o <object>) s value)
2438 (slot-missing c o s))
2440 ;;; Methods for the possible error we can encounter when calling a gf
2442 (define-method (no-next-method (gf <generic>) args)
2443 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
2445 (define-method (no-applicable-method (gf <generic>) args)
2446 (goops-error "No applicable method for ~S in call ~S"
2447 gf (cons (generic-function-name gf) args)))
2449 (define-method (no-method (gf <generic>) args)
2450 (goops-error "No method defined for ~S" gf))
2453 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
2456 (define-method (shallow-clone (self <object>))
2457 (let* ((class (class-of self))
2458 (clone (%allocate-instance class))
2459 (slots (map slot-definition-name (class-slots class))))
2460 (for-each (lambda (slot)
2461 (when (slot-bound? self slot)
2462 (slot-set! clone slot (slot-ref self slot))))
2466 (define-method (deep-clone (self <object>))
2467 (let* ((class (class-of self))
2468 (clone (%allocate-instance class))
2469 (slots (map slot-definition-name (class-slots class))))
2470 (for-each (lambda (slot)
2471 (when (slot-bound? self slot)
2472 (slot-set! clone slot
2473 (let ((value (slot-ref self slot)))
2474 (if (instance? value)
2481 ;;; {Class redefinition utilities}
2484 ;;; (class-redefinition OLD NEW)
2487 ;;; Has correct the following conditions:
2491 ;;; 1. New accessor specializers refer to new header
2495 ;;; 1. New class cpl refers to the new class header
2496 ;;; 2. Old class header exists on old super classes direct-subclass lists
2497 ;;; 3. New class header exists on new super classes direct-subclass lists
2499 (define-method (class-redefinition (old <class>) (new <class>))
2500 ;; Work on direct methods:
2501 ;; 1. Remove accessor methods from the old class
2502 ;; 2. Patch the occurences of new in the specializers by old
2503 ;; 3. Displace the methods from old to new
2504 (remove-class-accessors! old) ;; -1-
2505 (let ((methods (class-direct-methods new)))
2506 (for-each (lambda (m)
2507 (update-direct-method! m new old)) ;; -2-
2510 class-index-direct-methods
2511 (append methods (class-direct-methods old))))
2513 ;; Substitute old for new in new cpl
2514 (set-car! (struct-ref new class-index-cpl) old)
2516 ;; Remove the old class from the direct-subclasses list of its super classes
2517 (for-each (lambda (c) (struct-set! c class-index-direct-subclasses
2518 (delv! old (class-direct-subclasses c))))
2519 (class-direct-supers old))
2521 ;; Replace the new class with the old in the direct-subclasses of the supers
2522 (for-each (lambda (c)
2523 (struct-set! c class-index-direct-subclasses
2524 (cons old (delv! new (class-direct-subclasses c)))))
2525 (class-direct-supers new))
2527 ;; Swap object headers
2528 (%modify-class old new)
2532 ;; Redefine all the subclasses of old to take into account modification
2535 (update-direct-subclass! c new old))
2536 (class-direct-subclasses new))
2538 ;; Invalidate class so that subsequent instances slot accesses invoke
2539 ;; change-object-class
2540 (struct-set! new class-index-redefined old)
2541 (class-clear-flags! new vtable-flag-goops-valid) ;must come after slot-set!
2546 ;;; remove-class-accessors!
2549 (define-method (remove-class-accessors! (c <class>))
2550 (for-each (lambda (m)
2551 (when (is-a? m <accessor-method>)
2552 (let ((gf (slot-ref m 'generic-function)))
2553 ;; remove the method from its GF
2554 (slot-set! gf 'methods
2555 (delq1! m (slot-ref gf 'methods)))
2556 (invalidate-method-cache! gf)
2557 ;; remove the method from its specializers
2558 (remove-method-in-classes! m))))
2559 (class-direct-methods c)))
2562 ;;; update-direct-method!
2565 (define-method (update-direct-method! (m <method>)
2568 (let loop ((l (method-specializers m)))
2569 ;; Note: the <top> in dotted list is never used.
2570 ;; So we can work as if we had only proper lists.
2572 (when (eqv? (car l) old)
2577 ;;; update-direct-subclass!
2580 (define-method (update-direct-subclass! (c <class>)
2583 (class-redefinition c
2584 (make-class (class-direct-supers c)
2585 (class-direct-slots c)
2586 #:name (class-name c)
2587 #:metaclass (class-of c))))
2590 ;;; {Utilities for INITIALIZE methods}
2593 ;;; compute-slot-accessors
2595 (define (compute-slot-accessors class slots)
2598 (let ((getter (%slot-definition-getter slot))
2599 (setter (%slot-definition-setter slot))
2600 (accessor-setter setter)
2601 (accessor (%slot-definition-accessor slot)))
2603 (add-method! getter (compute-getter-method class slot)))
2605 (add-method! setter (compute-setter-method class slot)))
2607 (add-method! accessor (compute-getter-method class slot))
2608 (add-method! (accessor-setter accessor)
2609 (compute-setter-method class slot)))))
2612 (define-method (compute-getter-method (class <class>) slot)
2613 (let ((init-thunk (slot-definition-init-thunk slot))
2614 (slot-ref (slot-definition-slot-ref slot))
2615 (index (slot-definition-index slot)))
2616 (make <accessor-method>
2617 #:specializers (list class)
2619 (slot-ref (make-generic-bound-check-getter slot-ref))
2620 (init-thunk (standard-get index))
2621 (else (bound-check-get index)))
2622 #:slot-definition slot)))
2624 (define-method (compute-setter-method (class <class>) slot)
2625 (let ((slot-set! (slot-definition-slot-set! slot))
2626 (index (slot-definition-index slot)))
2627 (make <accessor-method>
2628 #:specializers (list class <top>)
2630 (slot-set! slot-set!)
2631 (else (standard-set index)))
2632 #:slot-definition slot)))
2634 (define (make-generic-bound-check-getter proc)
2636 (let ((val (proc o)))
2641 ;;; Pre-generate getters and setters for the first 20 slots.
2642 (define-syntax define-standard-accessor-method
2644 (define num-standard-pre-cache 20)
2646 ((_ ((proc n) arg ...) body)
2648 (let ((cache (vector #,@(map (lambda (n*)
2652 (iota num-standard-pre-cache)))))
2654 (if (< n #,num-standard-pre-cache)
2655 (vector-ref cache n)
2656 (lambda (arg ...) body)))))))))
2658 (define-standard-accessor-method ((bound-check-get n) o)
2659 (let ((x (struct-ref o n)))
2664 (define-standard-accessor-method ((standard-get n) o)
2667 (define-standard-accessor-method ((standard-set n) o v)
2668 (struct-set! o n v))
2673 ;; Replace the bootstrap compute-cpl with this definition.
2675 (make <generic> #:name 'compute-cpl))
2677 (define-method (compute-cpl (class <class>))
2678 (compute-std-cpl class class-direct-supers))
2680 ;;; compute-get-n-set
2682 (define compute-get-n-set
2683 (make <generic> #:name 'compute-get-n-set))
2685 (define-method (compute-get-n-set (class <class>) s)
2686 (define (class-slot-init-value)
2687 (let ((thunk (slot-definition-init-thunk s)))
2690 (slot-definition-init-value s))))
2692 (define (make-closure-variable class value)
2693 (list (lambda (o) value)
2694 (lambda (o v) (set! value v))))
2696 (case (slot-definition-allocation s)
2697 ((#:instance) ;; Instance slot
2698 ;; get-n-set is just its offset
2699 (let ((already-allocated (struct-ref class class-index-nfields)))
2700 (struct-set! class class-index-nfields (+ already-allocated 1))
2703 ((#:class) ;; Class slot
2704 ;; Class-slots accessors are implemented as 2 closures around
2705 ;; a Scheme variable. As instance slots, class slots must be
2706 ;; unbound at init time.
2707 (let ((name (slot-definition-name s)))
2708 (if (memq name (map slot-definition-name (class-direct-slots class)))
2709 ;; This slot is direct; create a new shared variable
2710 (make-closure-variable class (class-slot-init-value))
2711 ;; Slot is inherited. Find its definition in superclass
2712 (let lp ((cpl (cdr (class-precedence-list class))))
2715 (let ((s (class-slot-definition super name)))
2717 (list (slot-definition-slot-ref s)
2718 (slot-definition-slot-set! s))
2719 ;; Multiple inheritance means that we might have
2720 ;; to look deeper in the CPL.
2723 ((#:each-subclass) ;; slot shared by instances of direct subclass.
2724 ;; (Thomas Buerger, April 1998)
2725 (make-closure-variable class (class-slot-init-value)))
2727 ((#:virtual) ;; No allocation
2728 ;; slot-ref and slot-set! function must be given by the user
2729 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
2730 (set (get-keyword #:slot-set! (slot-definition-options s) #f)))
2731 (unless (and get set)
2732 (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S" s))
2734 (else (next-method))))
2736 (define-method (compute-get-n-set (o <object>) s)
2737 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
2739 (define-method (compute-slots (class <class>))
2740 (build-slots-list (class-direct-slots class)
2741 (class-precedence-list class)))
2747 ;; FIXME: This could be much more efficient.
2748 (define (%initialize-object obj initargs)
2749 "Initialize the object @var{obj} with the given arguments
2751 (define (valid-initargs? initargs)
2754 (((? keyword?) _ . initargs) (valid-initargs? initargs))
2756 (unless (instance? obj)
2757 (scm-error 'wrong-type-arg #f "Not an object: ~S"
2759 (unless (valid-initargs? initargs)
2760 (scm-error 'wrong-type-arg #f "Invalid initargs: ~S"
2761 (list initargs) #f))
2762 (let ((class (class-of obj)))
2763 (define (get-initarg kw)
2765 ;; Inlined get-keyword to avoid checking initargs for validity
2767 (let lp ((initargs initargs))
2769 ((kw* val . initargs)
2775 (let lp ((slots (struct-ref class class-index-slots)))
2779 (define (initialize-slot! value)
2781 ((%slot-definition-slot-set! slot)
2782 => (lambda (slot-set!) (slot-set! obj value)))
2784 (struct-set! obj (%slot-definition-index slot) value))))
2785 (let ((initarg (get-initarg (%slot-definition-init-keyword slot))))
2787 ((not (unbound? initarg))
2788 (initialize-slot! initarg))
2789 ((%slot-definition-init-thunk slot)
2790 => (lambda (init-thunk)
2791 (unless (memq (slot-definition-allocation slot)
2792 '(#:class #:each-subclass))
2793 (initialize-slot! (init-thunk)))))))
2796 (define-method (initialize (object <object>) initargs)
2797 (%initialize-object object initargs))
2799 (define-method (initialize (slot <slot>) initargs)
2801 (struct-set! slot slot-index-options initargs)
2802 (let ((init-thunk (%slot-definition-init-thunk slot)))
2804 (unless (thunk? init-thunk)
2805 (goops-error "Bad init-thunk for slot `~S': ~S"
2806 (%slot-definition-name slot) init-thunk)))))
2808 (define-method (initialize (class <class>) initargs)
2809 (define (make-direct-slot-definition dslot)
2810 (let ((initargs (compute-direct-slot-definition-initargs class dslot)))
2811 (compute-direct-slot-definition class initargs)))
2814 (class-add-flags! class (logior vtable-flag-goops-class
2815 vtable-flag-goops-valid))
2816 (struct-set! class class-index-name (get-keyword #:name initargs '???))
2817 (struct-set! class class-index-nfields 0)
2818 (struct-set! class class-index-direct-supers
2819 (get-keyword #:dsupers initargs '()))
2820 (struct-set! class class-index-direct-subclasses '())
2821 (struct-set! class class-index-direct-methods '())
2822 (struct-set! class class-index-redefined #f)
2823 (struct-set! class class-index-cpl (compute-cpl class))
2824 (struct-set! class class-index-direct-slots
2828 (make-direct-slot-definition slot)))
2829 (get-keyword #:slots initargs '())))
2830 (struct-set! class class-index-slots
2831 (allocate-slots class (compute-slots class)))
2834 (when (memq <slot> (struct-ref class class-index-cpl))
2835 (class-add-flags! class vtable-flag-goops-slot))
2837 ;; Build getters - setters - accessors
2838 (compute-slot-accessors class (struct-ref class class-index-slots))
2840 ;; Update the "direct-subclasses" of each inherited classes
2841 (for-each (lambda (x)
2842 (let ((dsubs (struct-ref x class-index-direct-subclasses)))
2843 (struct-set! x class-index-direct-subclasses
2844 (cons class dsubs))))
2845 (struct-ref class class-index-direct-supers))
2847 ;; Compute struct layout of instances, set the `layout' slot, and
2848 ;; update class flags.
2849 (%prep-layout! class))
2851 (define (initialize-object-procedure object initargs)
2852 (let ((proc (get-keyword #:procedure initargs #f)))
2855 (apply slot-set! object 'procedure proc))
2857 (slot-set! object 'procedure proc)))))
2859 (define-method (initialize (applicable-struct <applicable-struct>) initargs)
2861 (initialize-object-procedure applicable-struct initargs))
2863 (define-method (initialize (applicable-struct <applicable-struct-with-setter>)
2866 (slot-set! applicable-struct 'setter (get-keyword #:setter initargs #f)))
2868 (define-method (initialize (generic <generic>) initargs)
2869 (let ((previous-definition (get-keyword #:default initargs #f))
2870 (name (get-keyword #:name initargs #f)))
2872 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
2874 (apply previous-definition args)))
2877 (set-procedure-property! generic 'name name))
2878 (invalidate-method-cache! generic)))
2880 (define-method (initialize (eg <extended-generic>) initargs)
2882 (slot-set! eg 'extends (get-keyword #:extends initargs '())))
2884 (define dummy-procedure (lambda args *unspecified*))
2886 (define-method (initialize (method <method>) initargs)
2888 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
2889 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
2890 (slot-set! method 'procedure
2891 (get-keyword #:procedure initargs #f))
2892 (slot-set! method 'formals (get-keyword #:formals initargs '()))
2893 (slot-set! method 'body (get-keyword #:body initargs '()))
2894 (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
2901 (define (change-object-class old-instance old-class new-class)
2902 (let ((new-instance (allocate-instance new-class '())))
2903 ;; Initialize the slots of the new instance
2906 (if (and (slot-exists? old-instance slot)
2907 (eq? (%slot-definition-allocation
2908 (class-slot-definition old-class slot))
2910 (slot-bound? old-instance slot))
2911 ;; Slot was present and allocated in old instance; copy it
2912 (slot-set! new-instance slot (slot-ref old-instance slot))
2913 ;; slot was absent; initialize it with its default value
2914 (let ((init (slot-init-function new-class slot)))
2916 (slot-set! new-instance slot (init))))))
2917 (map slot-definition-name (class-slots new-class)))
2918 ;; Exchange old and new instance in place to keep pointers valid
2919 (%modify-instance old-instance new-instance)
2920 ;; Allow class specific updates of instances (which now are swapped)
2921 (update-instance-for-different-class new-instance old-instance)
2925 (define-method (update-instance-for-different-class (old-instance <object>)
2928 ;;not really important what we do, we just need a default method
2931 (define-method (change-class (old-instance <object>) (new-class <class>))
2932 (change-object-class old-instance (class-of old-instance) new-class))
2937 ;;; A new definition which overwrites the previous one which was built-in
2940 (define-method (allocate-instance (class <class>) initargs)
2941 (%allocate-instance class))
2943 (define-method (make-instance (class <class>) . initargs)
2944 (let ((instance (allocate-instance class initargs)))
2945 (initialize instance initargs)
2948 (define make make-instance)
2953 ;;; Protocol for calling generic functions, intended to be used when
2954 ;;; applying subclasses of <generic> and <generic-with-setter>. The
2955 ;;; code below is similar to the first MOP described in AMOP.
2957 ;;; Note that standard generic functions dispatch only on the classes of
2958 ;;; the arguments, and the result of such dispatch can be memoized. The
2959 ;;; `dispatch-generic-function-application-from-cache' routine
2960 ;;; implements this. `apply-generic' isn't called currently; the
2961 ;;; generic function MOP was never fully implemented in GOOPS. However
2962 ;;; now that GOOPS is implemented entirely in Scheme (2015) it's much
2963 ;;; easier to complete this work. Contributions gladly accepted!
2964 ;;; Please read the AMOP first though :)
2966 ;;; The protocol is:
2968 ;;; + apply-generic (gf args)
2969 ;;; + compute-applicable-methods (gf args ...)
2970 ;;; + sort-applicable-methods (gf methods args)
2971 ;;; + apply-methods (gf methods args)
2973 ;;; apply-methods calls make-next-method to build the "continuation" of
2974 ;;; a method. Applying a next-method will call apply-next-method which
2975 ;;; in turn will call apply again to call effectively the following
2976 ;;; method. (This paragraph is out of date but is kept so that maybe it
2977 ;;; illuminates some future hack.)
2980 (define-method (apply-generic (gf <generic>) args)
2981 (when (null? (slot-ref gf 'methods))
2982 (no-method gf args))
2983 (let ((methods (compute-applicable-methods gf args)))
2985 (apply-methods gf (sort-applicable-methods gf methods args) args)
2986 (no-applicable-method gf args))))
2988 ;; compute-applicable-methods is bound to %compute-applicable-methods.
2990 (define %%compute-applicable-methods
2991 (make <generic> #:name 'compute-applicable-methods))
2993 (define-method (%%compute-applicable-methods (gf <generic>) args)
2994 (%compute-applicable-methods gf args))
2996 (set! compute-applicable-methods %%compute-applicable-methods)
2998 (define-method (sort-applicable-methods (gf <generic>) methods args)
2999 (%sort-applicable-methods methods (map class-of args)))
3001 (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
3002 (%method-more-specific? m1 m2 targs))
3004 (define-method (apply-method (gf <generic>) methods build-next args)
3005 (apply (method-procedure (car methods))
3006 (build-next (cdr methods) args)
3009 (define-method (apply-methods (gf <generic>) (l <list>) args)
3010 (letrec ((next (lambda (procs args)
3012 (let ((a (if (null? new-args) args new-args)))
3014 (no-next-method gf a)
3015 (apply-method gf procs next a)))))))
3016 (apply-method gf l next args)))
3018 ;; We don't want the following procedure to turn up in backtraces:
3019 (for-each (lambda (proc)
3020 (set-procedure-property! proc 'system-procedure #t))
3024 no-applicable-method
3029 ;;; {Final initialization}
3032 ;; Tell C code that the main bulk of Goops has been loaded
3039 ;;; {SMOB and port classes}
3042 (define <arbiter> (find-subclass <top> '<arbiter>))
3043 (define <promise> (find-subclass <top> '<promise>))
3044 (define <thread> (find-subclass <top> '<thread>))
3045 (define <mutex> (find-subclass <top> '<mutex>))
3046 (define <condition-variable> (find-subclass <top> '<condition-variable>))
3047 (define <regexp> (find-subclass <top> '<regexp>))
3048 (define <hook> (find-subclass <top> '<hook>))
3049 (define <bitvector> (find-subclass <top> '<bitvector>))
3050 (define <random-state> (find-subclass <top> '<random-state>))
3051 (define <async> (find-subclass <top> '<async>))
3052 (define <directory> (find-subclass <top> '<directory>))
3053 (define <array> (find-subclass <top> '<array>))
3054 (define <character-set> (find-subclass <top> '<character-set>))
3055 (define <dynamic-object> (find-subclass <top> '<dynamic-object>))
3056 (define <guardian> (find-subclass <applicable> '<guardian>))
3057 (define <macro> (find-subclass <top> '<macro>))
3059 (define (define-class-subtree class)
3060 (define! (class-name class) class)
3061 (for-each define-class-subtree (class-direct-subclasses class)))
3063 (define-class-subtree (find-subclass <port> '<file-port>))