3 ;;;; Copyright (C) 1998,1999,2000-2003,2006,2009-2011,2013-2015 Free Software Foundation, Inc.
4 ;;;; Copyright (C) 1993-1998 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 ;;;; This file was based upon stklos.stk from the STk distribution
24 ;;;; version 4.0.1 by Erick Gallesio <eg@unice.fr>.
27 (define-module (oop goops)
28 #:use-module (srfi srfi-1)
29 #:use-module (ice-9 match)
30 #:use-module (oop goops util)
31 #:use-module (system base target)
32 #:export-syntax (define-class class standard-define-class
33 define-generic define-accessor define-method
34 define-extended-generic define-extended-generics
36 #:export ( ;; The root of everything.
41 <foreign-slot> <protected-slot> <hidden-slot> <opaque-slot>
42 <read-only-slot> <self-slot> <protected-opaque-slot>
43 <protected-hidden-slot> <protected-read-only-slot>
44 <scm-slot> <int-slot> <float-slot> <double-slot>
46 ;; Methods are implementations of generic functions.
47 <method> <accessor-method>
49 ;; Applicable objects, either procedures or applicable structs.
50 <procedure-class> <applicable>
51 <procedure> <primitive-generic>
53 ;; Applicable structs.
54 <applicable-struct-class> <applicable-struct-with-setter-class>
55 <applicable-struct> <applicable-struct-with-setter>
56 <generic> <extended-generic>
57 <generic-with-setter> <extended-generic-with-setter>
58 <accessor> <extended-accessor>
60 ;; Types with their own allocated typecodes.
61 <boolean> <char> <list> <pair> <null> <string> <symbol>
62 <vector> <bytevector> <uvec> <foreign> <hashtable>
63 <fluid> <dynamic-state> <frame> <vm> <vm-continuation>
67 <number> <complex> <real> <integer> <fraction>
72 ;; Particular SMOB data types. All SMOB types have
73 ;; corresponding classes, which may be obtained via class-of,
74 ;; once you have an instance. Perhaps FIXME to provide a
75 ;; smob-type-name->class procedure.
76 <arbiter> <promise> <thread> <mutex> <condition-variable>
77 <regexp> <hook> <bitvector> <random-state> <async>
78 <directory> <array> <character-set>
79 <dynamic-object> <guardian> <macro>
85 <port> <input-port> <output-port> <input-output-port>
87 ;; Like SMOB types, all port types have their own classes,
88 ;; which can be accessed via `class-of' once you have an
89 ;; instance. Here we export bindings just for file ports.
91 <file-input-port> <file-output-port> <file-input-output-port>
94 ensure-metaclass ensure-metaclass-with-supers
96 make-generic ensure-generic
98 make-accessor ensure-accessor
100 class-slot-ref class-slot-set! slot-unbound slot-missing
101 slot-definition-name slot-definition-options
102 slot-definition-allocation
104 slot-definition-getter slot-definition-setter
105 slot-definition-accessor
106 slot-definition-init-value slot-definition-init-form
107 slot-definition-init-thunk slot-definition-init-keyword
108 slot-init-function class-slot-definition
110 compute-cpl compute-std-cpl compute-get-n-set compute-slots
111 compute-getter-method compute-setter-method
112 allocate-instance initialize make-instance make
113 no-next-method no-applicable-method no-method
114 change-class update-instance-for-different-class
115 shallow-clone deep-clone
117 apply-generic apply-method apply-methods
118 compute-applicable-methods %compute-applicable-methods
119 method-more-specific? sort-applicable-methods
120 class-subclasses class-methods
122 min-fixnum max-fixnum
124 ;;; *fixme* Should go into goops.c
125 instance? slot-ref-using-class
126 slot-set-using-class! slot-bound-using-class?
127 slot-exists-using-class? slot-ref slot-set! slot-bound?
128 class-name class-direct-supers class-direct-subclasses
129 class-direct-methods class-direct-slots class-precedence-list
131 generic-function-name
132 generic-function-methods method-generic-function
133 method-specializers method-formals
134 primitive-generic-generic enable-primitive-generic!
135 method-procedure accessor-method-slot-definition
136 slot-exists? make find-method get-keyword)
139 ;; XXX FIXME: figure out why the 'eval-when's in this file must use
140 ;; 'compile' and must avoid 'expand', but only in 2.2, and only when
141 ;; compiling something that imports goops, e.g. (ice-9 occam-channel),
142 ;; before (oop goops) itself has been compiled.
144 ;; First initialize the builtin part of GOOPS
145 (eval-when (compile load eval)
146 (load-extension (string-append "libguile-" (effective-version))
147 "scm_init_goops_builtins"))
149 (eval-when (compile load eval)
150 (use-modules ((language tree-il primitives) :select (add-interesting-primitive!)))
151 (add-interesting-primitive! 'class-of))
153 (define-syntax macro-fold-left
155 ((_ folder seed ()) seed)
156 ((_ folder seed (head . tail))
157 (macro-fold-left folder (folder head seed) tail))))
159 (define-syntax macro-fold-right
161 ((_ folder seed ()) seed)
162 ((_ folder seed (head . tail))
163 (folder head (macro-fold-right folder seed tail)))))
165 (define-syntax fold-<class>-slots
168 '((layout <protected-read-only-slot>)
169 (flags <hidden-slot>)
171 (instance-finalizer <hidden-slot>)
173 (name <protected-hidden-slot>)
174 (nfields <hidden-slot>)
175 (%reserved <hidden-slot>)
183 (getters-n-setters)))
186 ;; The datum->syntax makes it as if the identifiers in `slots'
187 ;; were present in the initial form, which allows them to be used
188 ;; as (components of) introduced identifiers.
189 #`(fold visit seed #,(datum->syntax #'visit slots))))))
191 ;; Define class-index-layout to 0, class-index-flags to 1, and so on.
192 (let-syntax ((define-class-index
194 (define (id-append ctx a b)
195 (datum->syntax ctx (symbol-append (syntax->datum a)
197 (define (tail-length tail)
200 ((visit head tail) (1+ (tail-length #'tail)))))
204 (define #,(id-append #'name #'class-index- #'name)
205 #,(tail-length #'tail))
207 (fold-<class>-slots macro-fold-left define-class-index (begin)))
209 (define-syntax-rule (define-class-accessor name docstring field)
214 (scm-error 'wrong-type-arg #f "Not a class: ~S"
216 (struct-ref val field))))
218 (define-class-accessor class-name
219 "Return the class name of @var{obj}."
221 (define-class-accessor class-direct-supers
222 "Return the direct superclasses of the class @var{obj}."
223 class-index-direct-supers)
224 (define-class-accessor class-direct-slots
225 "Return the direct slots of the class @var{obj}."
226 class-index-direct-slots)
227 (define-class-accessor class-direct-subclasses
228 "Return the direct subclasses of the class @var{obj}."
229 class-index-direct-subclasses)
230 (define-class-accessor class-direct-methods
231 "Return the direct methods of the class @var{obj}."
232 class-index-direct-methods)
233 (define-class-accessor class-precedence-list
234 "Return the class precedence list of the class @var{obj}."
236 (define-class-accessor class-slots
237 "Return the slot list of the class @var{obj}."
243 (define (is-a? obj class)
244 (and (memq class (class-precedence-list (class-of obj))) #t))
247 ;;; The standard class precedence list computation algorithm
249 ;;; Correct behaviour:
251 ;;; (define-class food ())
252 ;;; (define-class fruit (food))
253 ;;; (define-class spice (food))
254 ;;; (define-class apple (fruit))
255 ;;; (define-class cinnamon (spice))
256 ;;; (define-class pie (apple cinnamon))
257 ;;; => cpl (pie) = pie apple fruit cinnamon spice food object top
259 ;;; (define-class d ())
260 ;;; (define-class e ())
261 ;;; (define-class f ())
262 ;;; (define-class b (d e))
263 ;;; (define-class c (e f))
264 ;;; (define-class a (b c))
265 ;;; => cpl (a) = a b d c e f object top
268 (define (compute-std-cpl c get-direct-supers)
269 (define (only-non-null lst)
270 (filter (lambda (l) (not (null? l))) lst))
272 (define (merge-lists reversed-partial-result inputs)
274 ((every null? inputs)
275 (reverse! reversed-partial-result))
277 (let* ((candidate (lambda (c)
278 (and (not (any (lambda (l)
282 (candidate-car (lambda (l)
284 (candidate (car l)))))
285 (next (any candidate-car inputs)))
287 (goops-error "merge-lists: Inconsistent precedence graph"))
288 (let ((remove-next (lambda (l)
289 (if (eq? (car l) next)
292 (merge-lists (cons next reversed-partial-result)
293 (only-non-null (map remove-next inputs))))))))
294 (let ((c-direct-supers (get-direct-supers c)))
295 (merge-lists (list c)
296 (only-non-null (append (map class-precedence-list
298 (list c-direct-supers))))))
300 ;; Bootstrap version.
301 (define (compute-cpl class)
302 (compute-std-cpl class class-direct-supers))
304 (define (build-slots-list dslots cpl)
305 (define (check-cpl slots class-slots)
306 (when (or-map (lambda (slot-def) (assq (car slot-def) slots))
308 (scm-error 'misc-error #f
309 "a predefined <class> inherited field cannot be redefined"
311 (define (remove-duplicate-slots slots)
312 (let lp ((slots (reverse slots)) (res '()) (seen '()))
315 ((memq (caar slots) seen)
316 (lp (cdr slots) res seen))
318 (lp (cdr slots) (cons (car slots) res) (cons (caar slots) seen))))))
319 (let* ((class-slots (and (memq <class> cpl)
320 (struct-ref <class> class-index-slots))))
322 (check-cpl dslots class-slots))
323 (let lp ((cpl (cdr cpl)) (res dslots) (class-slots '()))
325 (remove-duplicate-slots (append class-slots res))
326 (let* ((head (car cpl))
328 (new-slots (struct-ref head class-index-direct-slots)))
331 (lp cpl (append new-slots res) class-slots))
333 ;; Move class slots to the head of the list.
334 (lp cpl res new-slots))
336 (check-cpl new-slots class-slots)
337 (lp cpl (append new-slots res) class-slots))))))))
339 (define (%compute-getters-n-setters slots)
340 (define (compute-init-thunk options)
342 ((kw-arg-ref options #:init-value) => (lambda (val) (lambda () val)))
343 ((kw-arg-ref options #:init-thunk))
345 (let lp ((slots slots) (n 0))
348 (((name . options) . slots)
349 (cons (cons name (cons (compute-init-thunk options) n))
350 (lp slots (1+ n)))))))
352 (define (%compute-layout slots getters-n-setters nfields is-class?)
353 (define (instance-allocated? g-n-s)
355 ((name init-thunk . (? exact-integer? index)) #t)
356 ((name init-thunk getter setter index size) #t)
359 (define (allocated-index g-n-s)
361 ((name init-thunk . (? exact-integer? index)) index)
362 ((name init-thunk getter setter index size) index)))
364 (define (allocated-size g-n-s)
366 ((name init-thunk . (? exact-integer? index)) 1)
367 ((name init-thunk getter setter index size) size)))
369 (define (slot-protection-and-kind options)
370 (define (subclass? class parent)
371 (memq parent (class-precedence-list class)))
372 (let ((type (kw-arg-ref options #:class)))
373 (if (and type (subclass? type <foreign-slot>))
375 ((subclass? type <self-slot>) #\s)
376 ((subclass? type <protected-slot>) #\p)
379 ((subclass? type <opaque-slot>) #\o)
380 ((subclass? type <read-only-slot>) #\r)
381 ((subclass? type <hidden-slot>) #\h)
385 (let ((layout (make-string (* nfields 2))))
386 (let lp ((n 0) (slots slots) (getters-n-setters getters-n-setters))
387 (match getters-n-setters
389 (unless (= n nfields) (error "bad nfields"))
390 (unless (null? slots) (error "inconsistent g-n-s/slots"))
392 (let ((class-layout (struct-ref <class> class-index-layout)))
393 (unless (string-prefix? (symbol->string class-layout) layout)
394 (error "bad layout for class"))))
396 ((g-n-s . getters-n-setters)
398 (((name . options) . slots)
400 ((instance-allocated? g-n-s)
401 (unless (< n nfields) (error "bad nfields"))
402 (unless (= n (allocated-index g-n-s)) (error "bad allocation"))
403 (call-with-values (lambda () (slot-protection-and-kind options))
404 (lambda (protection kind)
405 (let init ((n n) (size (allocated-size g-n-s)))
407 ((zero? size) (lp n slots getters-n-setters))
409 (string-set! layout (* n 2) protection)
410 (string-set! layout (1+ (* n 2)) kind)
411 (init (1+ n) (1- size))))))))
413 (lp n slots getters-n-setters))))))))))
415 (define (%prep-layout! class)
416 (let* ((is-class? (and (memq <class> (struct-ref class class-index-cpl)) #t))
417 (layout (%compute-layout
418 (struct-ref class class-index-slots)
419 (struct-ref class class-index-getters-n-setters)
420 (struct-ref class class-index-nfields)
422 (%init-layout! class layout)))
424 (define (make-standard-class class name dsupers dslots)
425 (let ((z (make-struct/no-tail class)))
426 (struct-set! z class-index-direct-supers dsupers)
427 (let* ((cpl (compute-cpl z))
428 (dslots (map (lambda (slot)
429 (if (pair? slot) slot (list slot)))
431 (slots (build-slots-list dslots cpl))
432 (nfields (length slots))
433 (g-n-s (%compute-getters-n-setters slots)))
434 (struct-set! z class-index-name name)
435 (struct-set! z class-index-nfields nfields)
436 (struct-set! z class-index-direct-slots dslots)
437 (struct-set! z class-index-direct-subclasses '())
438 (struct-set! z class-index-direct-methods '())
439 (struct-set! z class-index-cpl cpl)
440 (struct-set! z class-index-slots slots)
441 (struct-set! z class-index-getters-n-setters g-n-s)
442 (struct-set! z class-index-redefined #f)
443 (for-each (lambda (super)
445 (struct-ref super class-index-direct-subclasses)))
446 (struct-set! super class-index-direct-subclasses
447 (cons z subclasses))))
453 (let-syntax ((cons-dslot
454 ;; The specialized slot classes have not been defined
455 ;; yet; initialize <class> with unspecialized slots.
457 ((_ (name) tail) (cons (list 'name) tail))
458 ((_ (name class) tail) (cons (list 'name) tail))))
460 ;; A simple way to compute class layout for the concrete
461 ;; types used in <class>.
462 (syntax-rules (<protected-read-only-slot> <self-slot>
463 <hidden-slot> <protected-hidden-slot>)
465 (string-append "pw" tail))
466 ((_ (name <protected-read-only-slot>) tail)
467 (string-append "pr" tail))
468 ((_ (name <self-slot>) tail)
469 (string-append "sr" tail))
470 ((_ (name <hidden-slot>) tail)
471 (string-append "uh" tail))
472 ((_ (name <protected-hidden-slot>) tail)
473 (string-append "ph" tail)))))
474 (let* ((dslots (fold-<class>-slots macro-fold-right cons-dslot '()))
475 (layout (fold-<class>-slots macro-fold-right cons-layout ""))
476 (<class> (%make-root-class layout)))
477 ;; The `direct-supers', `direct-slots', `cpl', `slots', and
478 ;; `getters-n-setters' fields will be updated later.
479 (struct-set! <class> class-index-name '<class>)
480 (struct-set! <class> class-index-nfields (length dslots))
481 (struct-set! <class> class-index-direct-supers '())
482 (struct-set! <class> class-index-direct-slots dslots)
483 (struct-set! <class> class-index-direct-subclasses '())
484 (struct-set! <class> class-index-direct-methods '())
485 (struct-set! <class> class-index-cpl '())
486 (struct-set! <class> class-index-slots dslots)
487 (struct-set! <class> class-index-getters-n-setters
488 (%compute-getters-n-setters dslots))
489 (struct-set! <class> class-index-redefined #f)
492 (define-syntax define-standard-class
494 ((define-standard-class name (super ...) #:metaclass meta slot ...)
496 (make-standard-class meta 'name (list super ...) '(slot ...))))
497 ((define-standard-class name (super ...) slot ...)
498 (define-standard-class name (super ...) #:metaclass <class> slot ...))))
500 (define-standard-class <top> ())
501 (define-standard-class <object> (<top>))
503 ;; <top>, <object>, and <class> were partially initialized. Correct
505 (struct-set! <object> class-index-direct-subclasses (list <class>))
506 (struct-set! <class> class-index-direct-supers (list <object>))
507 (struct-set! <class> class-index-cpl (list <class> <object> <top>))
509 (define-standard-class <foreign-slot> (<top>))
510 (define-standard-class <protected-slot> (<foreign-slot>))
511 (define-standard-class <hidden-slot> (<foreign-slot>))
512 (define-standard-class <opaque-slot> (<foreign-slot>))
513 (define-standard-class <read-only-slot> (<foreign-slot>))
514 (define-standard-class <self-slot> (<read-only-slot>))
515 (define-standard-class <protected-opaque-slot> (<protected-slot>
517 (define-standard-class <protected-hidden-slot> (<protected-slot>
519 (define-standard-class <protected-read-only-slot> (<protected-slot>
521 (define-standard-class <scm-slot> (<protected-slot>))
522 (define-standard-class <int-slot> (<foreign-slot>))
523 (define-standard-class <float-slot> (<foreign-slot>))
524 (define-standard-class <double-slot> (<foreign-slot>))
526 ;; Finish initialization of <class> with specialized slots.
530 (cons (list 'name) tail))
531 ((_ (name class) tail)
532 (cons (list 'name #:class class) tail)))))
533 (let* ((dslots (fold-<class>-slots macro-fold-right visit '()))
534 (g-n-s (%compute-getters-n-setters dslots)))
535 (struct-set! <class> class-index-direct-slots dslots)
536 (struct-set! <class> class-index-slots dslots)
537 (struct-set! <class> class-index-getters-n-setters g-n-s)))
539 ;; Applicables and their classes.
540 (define-standard-class <procedure-class> (<class>))
541 (define-standard-class <applicable-struct-class>
543 (define-standard-class <applicable-struct-with-setter-class>
544 (<applicable-struct-class>))
545 (%bless-applicable-struct-vtables! <applicable-struct-class>
546 <applicable-struct-with-setter-class>)
548 (define-standard-class <applicable> (<top>))
549 (define-standard-class <applicable-struct> (<object> <applicable>)
550 #:metaclass <applicable-struct-class>
552 (define-standard-class <applicable-struct-with-setter> (<applicable-struct>)
553 #:metaclass <applicable-struct-with-setter-class>
555 (define-standard-class <generic> (<applicable-struct>)
556 #:metaclass <applicable-struct-class>
558 (n-specialized #:init-value 0)
559 (extended-by #:init-value ())
561 (define-standard-class <extended-generic> (<generic>)
562 #:metaclass <applicable-struct-class>
563 (extends #:init-value ()))
564 (define-standard-class <generic-with-setter> (<generic>
565 <applicable-struct-with-setter>)
566 #:metaclass <applicable-struct-with-setter-class>)
567 (define-standard-class <accessor> (<generic-with-setter>)
568 #:metaclass <applicable-struct-with-setter-class>)
569 (define-standard-class <extended-generic-with-setter> (<extended-generic>
570 <generic-with-setter>)
571 #:metaclass <applicable-struct-with-setter-class>)
572 (define-standard-class <extended-accessor> (<accessor>
573 <extended-generic-with-setter>)
574 #:metaclass <applicable-struct-with-setter-class>)
577 (define-standard-class <method> (<object>)
584 (define-standard-class <accessor-method> (<method>)
585 (slot-definition #:init-keyword #:slot-definition))
587 ;; Primitive types classes
588 (define-standard-class <boolean> (<top>))
589 (define-standard-class <char> (<top>))
590 (define-standard-class <list> (<top>))
591 (define-standard-class <pair> (<list>))
592 (define-standard-class <null> (<list>))
593 (define-standard-class <string> (<top>))
594 (define-standard-class <symbol> (<top>))
595 (define-standard-class <vector> (<top>))
596 (define-standard-class <foreign> (<top>))
597 (define-standard-class <hashtable> (<top>))
598 (define-standard-class <fluid> (<top>))
599 (define-standard-class <dynamic-state> (<top>))
600 (define-standard-class <frame> (<top>))
601 (define-standard-class <vm-continuation> (<top>))
602 (define-standard-class <bytevector> (<top>))
603 (define-standard-class <uvec> (<bytevector>))
604 (define-standard-class <array> (<top>))
605 (define-standard-class <bitvector> (<top>))
606 (define-standard-class <number> (<top>))
607 (define-standard-class <complex> (<number>))
608 (define-standard-class <real> (<complex>))
609 (define-standard-class <integer> (<real>))
610 (define-standard-class <fraction> (<real>))
611 (define-standard-class <keyword> (<top>))
612 (define-standard-class <unknown> (<top>))
613 (define-standard-class <procedure> (<applicable>)
614 #:metaclass <procedure-class>)
615 (define-standard-class <primitive-generic> (<procedure>)
616 #:metaclass <procedure-class>)
617 (define-standard-class <port> (<top>))
618 (define-standard-class <input-port> (<port>))
619 (define-standard-class <output-port> (<port>))
620 (define-standard-class <input-output-port> (<input-port> <output-port>))
622 (define (inherit-applicable! class)
623 "An internal routine to redefine a SMOB class that was added after
624 GOOPS was loaded, and on which scm_set_smob_apply installed an apply
626 ;; Why not use class-redefinition? We would, except that loading the
627 ;; compiler to compile effective methods can happen while GOOPS has
628 ;; only been partially loaded, and loading the compiler might cause
629 ;; SMOB types to be defined that need this facility. Instead we make
630 ;; a very specific hack, not a general solution. Probably the right
631 ;; solution is to avoid using the compiler, but that is another kettle
633 (unless (memq <applicable> (class-precedence-list class))
634 (unless (null? (class-slots class))
635 (error "SMOB object has slots?"))
638 (let ((subclasses (struct-ref super class-index-direct-subclasses)))
639 (struct-set! super class-index-direct-subclasses
640 (delq class subclasses))))
641 (struct-ref class class-index-direct-supers))
642 (struct-set! class class-index-direct-supers (list <applicable>))
643 (struct-set! class class-index-cpl (compute-cpl class))
644 (let ((subclasses (struct-ref <applicable> class-index-direct-subclasses)))
645 (struct-set! <applicable> class-index-direct-subclasses
646 (cons class subclasses)))))
648 (define (%invalidate-method-cache! gf)
649 (slot-set! gf 'procedure (delayed-compile gf))
650 (slot-set! gf 'effective-methods '()))
653 (define (invalidate-method-cache! gf)
654 (%invalidate-method-cache! gf))
656 (define* (get-keyword key l #:optional default)
657 "Determine an associated value for the keyword @var{key} from the list
658 @var{l}. The list @var{l} has to consist of an even number of elements,
659 where, starting with the first, every second element is a keyword,
660 followed by its associated value. If @var{l} does not hold a value for
661 @var{key}, the value @var{default} is returned."
662 (unless (keyword? key)
663 (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list key) #f))
668 (unless (keyword? kw)
669 (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list kw) #f))
670 (if (eq? kw key) arg (lp l))))))
672 ;; A simple make which will be redefined later. This version handles
673 ;; only creation of gf, methods and classes (no instances).
675 ;; Since this code will disappear when Goops will be fully booted,
676 ;; no precaution is taken to be efficient.
678 (define (%allocate-instance class)
679 (let ((obj (allocate-struct class (struct-ref class class-index-nfields))))
683 (define (make class . args)
685 ((or (eq? class <generic>) (eq? class <accessor>))
686 (let ((z (make-struct/no-tail class #f '() 0 '())))
687 (set-procedure-property! z 'name (get-keyword #:name args #f))
688 (invalidate-method-cache! z)
689 (when (eq? class <accessor>)
690 (let ((setter (get-keyword #:setter args #f)))
692 (slot-set! z 'setter setter))))
695 (let ((z (%allocate-instance class)))
697 ((or (eq? class <method>) (eq? class <accessor-method>))
698 (for-each (match-lambda
700 (slot-set! z slot (get-keyword kw args default))))
701 '((#:generic-function generic-function #f)
702 (#:specializers specializers ())
703 (#:procedure procedure #f)
704 (#:formals formals ())
706 (#:make-procedure make-procedure #f))))
707 ((memq <class> (class-precedence-list class))
708 (for-each (match-lambda
710 (slot-set! z slot (get-keyword kw args default))))
712 (#:dsupers direct-supers ())
713 (#:slots direct-slots ())
716 (error "boot `make' does not support this class" class)))
719 ;; In the future, this function will return the effective slot
720 ;; definition associated with SLOT_NAME. Now it just returns some of
721 ;; the information which will be stored in the effective slot
724 (define (get-slot-value-using-name class obj slot-name)
725 (match (assq slot-name (struct-ref class class-index-getters-n-setters))
726 (#f (slot-missing class obj slot-name))
727 ((name init-thunk . (? exact-integer? index))
728 (struct-ref obj index))
729 ((name init-thunk getter setter . _)
732 (define (set-slot-value-using-name! class obj slot-name value)
733 (match (assq slot-name (struct-ref class class-index-getters-n-setters))
734 (#f (slot-missing class obj slot-name value))
735 ((name init-thunk . (? exact-integer? index))
736 (struct-set! obj index value))
737 ((name init-thunk getter setter . _)
738 (setter obj value))))
740 (define (test-slot-existence class obj slot-name)
741 (and (assq slot-name (struct-ref class class-index-getters-n-setters))
744 ;; ========================================
746 (define (check-slot-args class obj slot-name)
747 (unless (class? class)
748 (scm-error 'wrong-type-arg #f "Not a class: ~S"
750 (unless (is-a? obj <object>)
751 (scm-error 'wrong-type-arg #f "Not an instance: ~S"
753 (unless (symbol? slot-name)
754 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
755 (list slot-name) #f)))
757 (define (slot-ref-using-class class obj slot-name)
758 (check-slot-args class obj slot-name)
759 (let ((val (get-slot-value-using-name class obj slot-name)))
761 (slot-unbound class obj slot-name)
764 (define (slot-set-using-class! class obj slot-name value)
765 (check-slot-args class obj slot-name)
766 (set-slot-value-using-name! class obj slot-name value))
768 (define (slot-bound-using-class? class obj slot-name)
769 (check-slot-args class obj slot-name)
770 (not (unbound? (get-slot-value-using-name class obj slot-name))))
772 (define (slot-exists-using-class? class obj slot-name)
773 (check-slot-args class obj slot-name)
774 (test-slot-existence class obj slot-name))
776 ;; Class redefinition protocol:
778 ;; A class is represented by a heap header h1 which points to a
779 ;; malloc:ed memory block m1.
781 ;; When a new version of a class is created, a new header h2 and
782 ;; memory block m2 are allocated. The headers h1 and h2 then switch
783 ;; pointers so that h1 refers to m2 and h2 to m1. In this way, names
784 ;; bound to h1 will point to the new class at the same time as h2 will
785 ;; be a handle which the GC will use to free m1.
787 ;; The `redefined' slot of m1 will be set to point to h1. An old
788 ;; instance will have its class pointer (the CAR of the heap header)
789 ;; pointing to m1. The non-immediate `redefined'-slot in m1 indicates
790 ;; the class modification and the new class pointer can be found via
794 ;; In the following interfaces, class-of handles the redefinition
795 ;; protocol. There would seem to be some thread-unsafety though as the
796 ;; { class, object data } pair needs to be accessed atomically, not the
797 ;; { class, object } pair.
799 (define (slot-ref obj slot-name)
800 "Return the value from @var{obj}'s slot with the nam var{slot_name}."
801 (slot-ref-using-class (class-of obj) obj slot-name))
803 (define (slot-set! obj slot-name value)
804 "Set the slot named @var{slot_name} of @var{obj} to @var{value}."
805 (slot-set-using-class! (class-of obj) obj slot-name value))
807 (define (slot-bound? obj slot-name)
808 "Return the value from @var{obj}'s slot with the nam var{slot_name}."
809 (slot-bound-using-class? (class-of obj) obj slot-name))
811 (define (slot-exists? obj slot-name)
812 "Return @code{#t} if @var{obj} has a slot named @var{slot_name}."
813 (slot-exists-using-class? (class-of obj) obj slot-name))
815 (define (method-generic-function obj)
816 "Return the generic function for the method @var{obj}."
817 (unless (is-a? obj <method>)
818 (scm-error 'wrong-type-arg #f "Not a method: ~S"
820 (slot-ref obj 'generic-function))
822 (define (method-specializers obj)
823 "Return specializers of the method @var{obj}."
824 (unless (is-a? obj <method>)
825 (scm-error 'wrong-type-arg #f "Not a method: ~S"
827 (slot-ref obj 'specializers))
829 (define (method-procedure obj)
830 "Return the procedure of the method @var{obj}."
831 (unless (is-a? obj <method>)
832 (scm-error 'wrong-type-arg #f "Not a method: ~S"
834 (slot-ref obj 'procedure))
836 (define *dispatch-module* (current-module))
839 ;;; Generic functions have an applicable-methods cache associated with
840 ;;; them. Every distinct set of types that is dispatched through a
841 ;;; generic adds an entry to the cache. This cache gets compiled out to
842 ;;; a dispatch procedure. In steady-state, this dispatch procedure is
843 ;;; never recompiled; but during warm-up there is some churn, both to
844 ;;; the cache and to the dispatch procedure.
846 ;;; So what is the deal if warm-up happens in a multithreaded context?
847 ;;; There is indeed a window between missing the cache for a certain set
848 ;;; of arguments, and then updating the cache with the newly computed
849 ;;; applicable methods. One of the updaters is liable to lose their new
852 ;;; This is actually OK though, because a subsequent cache miss for the
853 ;;; race loser will just cause memoization to try again. The cache will
854 ;;; eventually be consistent. We're not mutating the old part of the
855 ;;; cache, just consing on the new entry.
857 ;;; It doesn't even matter if the dispatch procedure and the cache are
858 ;;; inconsistent -- most likely the type-set that lost the dispatch
859 ;;; procedure race will simply re-trigger a memoization, but since the
860 ;;; winner isn't in the effective-methods cache, it will likely also
861 ;;; re-trigger a memoization, and the cache will finally be consistent.
862 ;;; As you can see there is a possibility for ping-pong effects, but
863 ;;; it's unlikely given the shortness of the window between slot-set!
864 ;;; invocations. We could add a mutex, but it is strictly unnecessary,
865 ;;; and would add runtime cost and complexity.
868 (define (emit-linear-dispatch gf-sym nargs methods free rest?)
869 (define (gen-syms n stem)
870 (let lp ((n (1- n)) (syms '()))
873 (lp (1- n) (cons (gensym stem) syms)))))
874 (let* ((args (gen-syms nargs "a"))
875 (types (gen-syms nargs "t")))
876 (let lp ((methods methods)
878 (exp `(cache-miss ,gf-sym
884 (values `(,(if rest? `(,@args . rest) args)
885 (let ,(map (lambda (t a)
892 (let preddy ((free free)
894 (specs (vector-ref (car methods) 1))
897 (let ((m-sym (gensym "p")))
899 (acons (vector-ref (car methods) 3)
904 `(apply ,m-sym ,@args rest)
907 (let ((var (assq-ref free (car specs))))
912 (cons `(eq? ,(car types) ,var)
914 (let ((var (gensym "c")))
915 (preddy (acons (car specs) var free)
918 (cons `(eq? ,(car types) ,var)
921 (define (compute-dispatch-procedure gf cache)
923 (let lp ((ls cache) (nreq -1) (nrest -1))
926 (collate (make-vector (1+ nreq) '())
927 (make-vector (1+ nrest) '())))
928 ((vector-ref (car ls) 2) ; rest
929 (lp (cdr ls) nreq (max nrest (vector-ref (car ls) 0))))
931 (lp (cdr ls) (max nreq (vector-ref (car ls) 0)) nrest)))))
932 (define (collate req rest)
937 ((vector-ref (car ls) 2) ; rest
938 (let ((n (vector-ref (car ls) 0)))
939 (vector-set! rest n (cons (car ls) (vector-ref rest n)))
942 (let ((n (vector-ref (car ls) 0)))
943 (vector-set! req n (cons (car ls) (vector-ref req n)))
945 (define (emit req rest)
946 (let ((gf-sym (gensym "g")))
947 (define (emit-rest n clauses free)
948 (if (< n (vector-length rest))
949 (let ((methods (vector-ref rest n)))
952 (emit-rest (1+ n) clauses free))
953 ;; FIXME: hash dispatch
957 (emit-linear-dispatch gf-sym n methods free #t))
958 (lambda (clause free)
959 (emit-rest (1+ n) (cons clause clauses) free))))))
960 (emit-req (1- (vector-length req)) clauses free)))
961 (define (emit-req n clauses free)
963 (comp `(lambda ,(map cdr free)
964 (case-lambda ,@clauses))
966 (let ((methods (vector-ref req n)))
969 (emit-req (1- n) clauses free))
970 ;; FIXME: hash dispatch
974 (emit-linear-dispatch gf-sym n methods free #f))
975 (lambda (clause free)
976 (emit-req (1- n) (cons clause clauses) free))))))))
979 (if (or (zero? (vector-length rest))
980 (null? (vector-ref rest 0)))
981 (list `(args (cache-miss ,gf-sym args)))
983 (acons gf gf-sym '()))))
984 (define (comp exp vals)
985 ;; When cross-compiling Guile itself, the native Guile must generate
986 ;; code for the host.
987 (with-target %host-type
989 (let ((p ((@ (system base compile) compile) exp
990 #:env *dispatch-module*
992 #:opts '(#:partial-eval? #f #:cse? #f))))
998 ;; o/~ ten, nine, eight
999 ;; sometimes that's just how it goes
1002 ;; get out before it blows o/~
1004 (define timer-init 30)
1005 (define (delayed-compile gf)
1006 (let ((timer timer-init))
1008 (set! timer (1- timer))
1011 (let ((dispatch (compute-dispatch-procedure
1012 gf (slot-ref gf 'effective-methods))))
1013 (slot-set! gf 'procedure dispatch)
1014 (apply dispatch args)))
1016 ;; interestingly, this catches recursive compilation attempts as
1017 ;; well; in that case, timer is negative
1018 (cache-dispatch gf args))))))
1020 (define (cache-dispatch gf args)
1021 (define (map-until n f ls)
1022 (if (or (zero? n) (null? ls))
1024 (cons (f (car ls)) (map-until (1- n) f (cdr ls)))))
1025 (define (equal? x y) ; can't use the stock equal? because it's a generic...
1026 (cond ((pair? x) (and (pair? y)
1027 (eq? (car x) (car y))
1028 (equal? (cdr x) (cdr y))))
1029 ((null? x) (null? y))
1031 (if (slot-ref gf 'n-specialized)
1032 (let ((types (map-until (slot-ref gf 'n-specialized) class-of args)))
1033 (let lp ((cache (slot-ref gf 'effective-methods)))
1034 (cond ((null? cache)
1035 (cache-miss gf args))
1036 ((equal? (vector-ref (car cache) 1) types)
1037 (apply (vector-ref (car cache) 3) args))
1038 (else (lp (cdr cache))))))
1039 (cache-miss gf args)))
1041 (define (cache-miss gf args)
1042 (apply (memoize-method! gf args) args))
1044 (define (memoize-effective-method! gf args applicable)
1045 (define (first-n ls n)
1046 (if (or (zero? n) (null? ls))
1048 (cons (car ls) (first-n (cdr ls) (- n 1)))))
1049 (define (parse n ls)
1051 (memoize n #f (map class-of args)))
1052 ((= n (slot-ref gf 'n-specialized))
1053 (memoize n #t (map class-of (first-n args n))))
1055 (parse (1+ n) (cdr ls)))))
1056 (define (memoize len rest? types)
1057 (let* ((cmethod (compute-cmethod applicable types))
1058 (cache (cons (vector len types rest? cmethod)
1059 (slot-ref gf 'effective-methods))))
1060 (slot-set! gf 'effective-methods cache)
1061 (slot-set! gf 'procedure (delayed-compile gf))
1066 ;;; Compiling next methods into method bodies
1069 ;;; So, for the reader: there basic idea is that, given that the
1070 ;;; semantics of `next-method' depend on the concrete types being
1071 ;;; dispatched, why not compile a specific procedure to handle each type
1072 ;;; combination that we see at runtime.
1074 ;;; In theory we can do much better than a bytecode compilation, because
1075 ;;; we know the *exact* types of the arguments. It's ideal for native
1076 ;;; compilation. A task for the future.
1078 ;;; I think this whole generic application mess would benefit from a
1081 (define (compute-cmethod methods types)
1082 (let ((make-procedure (slot-ref (car methods) 'make-procedure)))
1085 (if (null? (cdr methods))
1087 (no-next-method (method-generic-function (car methods)) args))
1088 (compute-cmethod (cdr methods) types)))
1089 (method-procedure (car methods)))))
1095 (define (memoize-method! gf args)
1096 (let ((applicable ((if (eq? gf compute-applicable-methods)
1097 %compute-applicable-methods
1098 compute-applicable-methods)
1101 (memoize-effective-method! gf args applicable))
1103 (no-applicable-method gf args)))))
1105 (set-procedure-property! memoize-method! 'system-procedure #t)
1107 (define no-applicable-method
1108 (make <generic> #:name 'no-applicable-method))
1112 ;; Then load the rest of GOOPS
1115 ;; FIXME: deprecate.
1116 (define min-fixnum (- (expt 2 29)))
1117 (define max-fixnum (- (expt 2 29) 1))
1122 (define (goops-error format-string . args)
1123 (scm-error 'goops-error #f format-string args '()))
1129 (define ensure-metaclass-with-supers
1130 (let ((table-of-metas '()))
1131 (lambda (meta-supers)
1132 (let ((entry (assoc meta-supers table-of-metas)))
1134 ;; Found a previously created metaclass
1136 ;; Create a new meta-class which inherit from "meta-supers"
1137 (let ((new (make <class> #:dsupers meta-supers
1139 #:name (gensym "metaclass"))))
1140 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
1143 (define (ensure-metaclass supers)
1146 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
1147 (all-cpls (append-map (lambda (m)
1148 (cdr (class-precedence-list m)))
1151 ;; Find the most specific metaclasses. The new metaclass will be
1152 ;; a subclass of these.
1155 (if (and (not (member meta all-cpls))
1156 (not (member meta needed-metas)))
1157 (set! needed-metas (append needed-metas (list meta)))))
1159 ;; Now return a subclass of the metaclasses we found.
1160 (if (null? (cdr needed-metas))
1161 (car needed-metas) ; If there's only one, just use it.
1162 (ensure-metaclass-with-supers needed-metas)))))
1168 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
1170 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
1171 ;;; OPTION ::= KEYWORD VALUE
1174 (define (make-class supers slots . options)
1175 (let* ((name (get-keyword #:name options (make-unbound)))
1176 (supers (if (not (or-map (lambda (class)
1178 (class-precedence-list class)))
1180 (append supers (list <object>))
1182 (metaclass (or (get-keyword #:metaclass options #f)
1183 (ensure-metaclass supers))))
1185 ;; Verify that all direct slots are different and that we don't inherit
1186 ;; several time from the same class
1187 (let ((tmp1 (find-duplicate supers))
1188 (tmp2 (find-duplicate (map slot-definition-name slots))))
1190 (goops-error "make-class: super class ~S is duplicate in class ~S"
1193 (goops-error "make-class: slot ~S is duplicate in class ~S"
1196 ;; Everything seems correct, build the class
1197 (apply make metaclass
1203 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
1205 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
1206 ;;; OPTION ::= KEYWORD VALUE
1208 (define-syntax class
1210 (define (parse-options options)
1211 (syntax-case options ()
1213 ((kw arg . options) (keyword? (syntax->datum #'kw))
1214 (with-syntax ((options (parse-options #'options)))
1215 (syntax-case #'kw ()
1217 #'(kw 'arg #:init-thunk (lambda () arg) . options))
1219 #'(kw arg . options)))))))
1220 (define (check-valid-kwargs args)
1221 (syntax-case args ()
1223 ((kw arg . args) (keyword? (syntax->datum #'kw))
1224 #`(kw arg . #,(check-valid-kwargs #'args)))))
1225 (define (parse-slots-and-kwargs args)
1226 (syntax-case args ()
1229 ((kw . _) (keyword? (syntax->datum #'kw))
1230 #`(() #,(check-valid-kwargs args)))
1231 (((name option ...) args ...)
1232 (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...)))
1233 ((option ...) (parse-options #'(option ...))))
1234 #'(((list 'name option ...) . slots) kwargs)))
1235 ((name args ...) (symbol? (syntax->datum #'name))
1236 (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...))))
1237 #'(('(name) . slots) kwargs)))))
1239 ((class (super ...) arg ...)
1240 (with-syntax ((((slot-def ...) (option ...))
1241 (parse-slots-and-kwargs #'(arg ...))))
1242 #'(make-class (list super ...)
1246 (define-syntax define-class-pre-definition
1249 ((_ (k arg rest ...) out ...)
1250 (keyword? (syntax->datum #'k))
1251 (case (syntax->datum #'k)
1252 ((#:getter #:setter)
1253 #'(define-class-pre-definition (rest ...)
1255 (if (or (not (defined? 'arg))
1256 (not (is-a? arg <generic>)))
1259 (ensure-generic (if (defined? 'arg) arg #f) 'arg)))))
1261 #'(define-class-pre-definition (rest ...)
1263 (if (or (not (defined? 'arg))
1264 (not (is-a? arg <accessor>)))
1267 (ensure-accessor (if (defined? 'arg) arg #f) 'arg)))))
1269 #'(define-class-pre-definition (rest ...) out ...))))
1271 #'(begin out ...)))))
1273 ;; Some slot options require extra definitions to be made. In
1274 ;; particular, we want to make sure that the generic function objects
1275 ;; which represent accessors exist before `make-class' tries to add
1277 (define-syntax define-class-pre-definitions
1282 ((_ (slot rest ...) out ...)
1283 (keyword? (syntax->datum #'slot))
1285 ((_ (slot rest ...) out ...)
1286 (identifier? #'slot)
1287 #'(define-class-pre-definitions (rest ...)
1289 ((_ ((slotname slotopt ...) rest ...) out ...)
1290 #'(define-class-pre-definitions (rest ...)
1291 out ... (define-class-pre-definition (slotopt ...)))))))
1293 (define-syntax-rule (define-class name supers slot ...)
1295 (define-class-pre-definitions (slot ...))
1296 (if (and (defined? 'name)
1297 (is-a? name <class>)
1298 (memq <object> (class-precedence-list name)))
1299 (class-redefinition name
1300 (class supers slot ... #:name 'name))
1301 (toplevel-define! 'name (class supers slot ... #:name 'name)))))
1303 (define-syntax-rule (standard-define-class arg ...)
1304 (define-class arg ...))
1307 ;;; {Generic functions and accessors}
1310 ;; Apparently the desired semantics are that we extend previous
1311 ;; procedural definitions, but that if `name' was already a generic, we
1312 ;; overwrite its definition.
1313 (define-syntax define-generic
1316 ((define-generic name) (symbol? (syntax->datum #'name))
1318 (if (and (defined? 'name) (is-a? name <generic>))
1319 (make <generic> #:name 'name)
1320 (ensure-generic (if (defined? 'name) name #f) 'name)))))))
1322 (define-syntax define-extended-generic
1325 ((define-extended-generic name val) (symbol? (syntax->datum #'name))
1326 #'(define name (make-extended-generic val 'name))))))
1328 (define-syntax define-extended-generics
1330 (define (id-append ctx a b)
1331 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
1333 ((define-extended-generic (name ...) #:prefix (prefix ...))
1334 (and (and-map symbol? (syntax->datum #'(name ...)))
1335 (and-map symbol? (syntax->datum #'(prefix ...))))
1336 (with-syntax ((((val ...)) (map (lambda (name)
1337 (map (lambda (prefix)
1338 (id-append name prefix name))
1342 (define-extended-generic name (list val ...))
1345 (define* (make-generic #:optional name)
1346 (make <generic> #:name name))
1348 (define* (make-extended-generic gfs #:optional name)
1349 (let* ((gfs (if (list? gfs) gfs (list gfs)))
1350 (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
1352 (let* ((sname (and name (make-setter-name name)))
1354 (append-map (lambda (gf)
1355 (if (is-a? gf <generic-with-setter>)
1356 (list (ensure-generic (setter gf)
1360 (es (make <extended-generic-with-setter>
1363 #:setter (make <extended-generic>
1365 #:extends setters))))
1366 (extended-by! setters (setter es))
1368 (make <extended-generic>
1371 (extended-by! gfs ans)
1374 (define (extended-by! gfs eg)
1375 (for-each (lambda (gf)
1376 (slot-set! gf 'extended-by
1377 (cons eg (slot-ref gf 'extended-by))))
1379 (invalidate-method-cache! eg))
1381 (define (not-extended-by! gfs eg)
1382 (for-each (lambda (gf)
1383 (slot-set! gf 'extended-by
1384 (delq! eg (slot-ref gf 'extended-by))))
1386 (invalidate-method-cache! eg))
1388 (define* (ensure-generic old-definition #:optional name)
1389 (cond ((is-a? old-definition <generic>) old-definition)
1390 ((procedure-with-setter? old-definition)
1391 (make <generic-with-setter>
1393 #:default (procedure old-definition)
1394 #:setter (setter old-definition)))
1395 ((procedure? old-definition)
1396 (if (generic-capability? old-definition) old-definition
1397 (make <generic> #:name name #:default old-definition)))
1398 (else (make <generic> #:name name))))
1400 ;; same semantics as <generic>
1401 (define-syntax-rule (define-accessor name)
1403 (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
1404 ((is-a? name <accessor>) (make <accessor> #:name 'name))
1405 (else (ensure-accessor name 'name)))))
1407 (define (make-setter-name name)
1408 (string->symbol (string-append "setter:" (symbol->string name))))
1410 (define* (make-accessor #:optional name)
1413 #:setter (make <generic>
1414 #:name (and name (make-setter-name name)))))
1416 (define* (ensure-accessor proc #:optional name)
1417 (cond ((and (is-a? proc <accessor>)
1418 (is-a? (setter proc) <generic>))
1420 ((is-a? proc <generic-with-setter>)
1421 (upgrade-accessor proc (setter proc)))
1422 ((is-a? proc <generic>)
1423 (upgrade-accessor proc (make-generic name)))
1424 ((procedure-with-setter? proc)
1427 #:default (procedure proc)
1428 #:setter (ensure-generic (setter proc) name)))
1430 (ensure-accessor (if (generic-capability? proc)
1431 (make <generic> #:name name #:default proc)
1432 (ensure-generic proc name))
1435 (make-accessor name))))
1437 (define (upgrade-accessor generic setter)
1438 (let ((methods (slot-ref generic 'methods))
1439 (gws (make (if (is-a? generic <extended-generic>)
1440 <extended-generic-with-setter>
1442 #:name (generic-function-name generic)
1443 #:extended-by (slot-ref generic 'extended-by)
1445 (if (is-a? generic <extended-generic>)
1446 (let ((gfs (slot-ref generic 'extends)))
1447 (not-extended-by! gfs generic)
1448 (slot-set! gws 'extends gfs)
1449 (extended-by! gfs gws)))
1450 ;; Steal old methods
1451 (for-each (lambda (method)
1452 (slot-set! method 'generic-function gws))
1454 (slot-set! gws 'methods methods)
1455 (invalidate-method-cache! gws)
1462 ;; Note: `a' and `b' can have unequal lengths (i.e. one can be one
1463 ;; element longer than the other when we have a dotted parameter
1464 ;; list). For instance, with the call
1470 ;; (define-method M (a . l) ....)
1471 ;; (define-method M (a) ....)
1473 ;; we consider that the second method is more specific.
1475 ;; Precondition: `a' and `b' are methods and are applicable to `types'.
1476 (define (%method-more-specific? a b types)
1477 (let lp ((a-specializers (method-specializers a))
1478 (b-specializers (method-specializers b))
1481 ;; (a) less specific than (a b ...) or (a . b)
1482 ((null? a-specializers) #t)
1483 ;; (a b ...) or (a . b) less specific than (a)
1484 ((null? b-specializers) #f)
1485 ;; (a . b) less specific than (a b ...)
1486 ((not (pair? a-specializers)) #f)
1487 ;; (a b ...) more specific than (a . b)
1488 ((not (pair? b-specializers)) #t)
1490 (let ((a-specializer (car a-specializers))
1491 (b-specializer (car b-specializers))
1492 (a-specializers (cdr a-specializers))
1493 (b-specializers (cdr b-specializers))
1495 (types (cdr types)))
1496 (if (eq? a-specializer b-specializer)
1497 (lp a-specializers b-specializers types)
1498 (let lp ((cpl (class-precedence-list type)))
1499 (let ((elt (car cpl)))
1501 ((eq? a-specializer elt) #t)
1502 ((eq? b-specializer elt) #f)
1503 (else (lp (cdr cpl))))))))))))
1505 (define (%sort-applicable-methods methods types)
1506 (sort methods (lambda (a b) (%method-more-specific? a b types))))
1508 (define (generic-function-methods obj)
1509 "Return the methods of the generic function @var{obj}."
1510 (define (fold-upward method-lists gf)
1512 ((is-a? gf <extended-generic>)
1513 (let lp ((method-lists method-lists) (gfs (slot-ref gf 'extends)))
1517 (lp (fold-upward (cons (slot-ref gf 'methods) method-lists) gf)
1519 (else method-lists)))
1520 (define (fold-downward method-lists gf)
1521 (let lp ((method-lists (cons (slot-ref gf 'methods) method-lists))
1522 (gfs (slot-ref gf 'extended-by)))
1526 (lp (fold-downward method-lists gf) gfs)))))
1527 (unless (is-a? obj <generic>)
1528 (scm-error 'wrong-type-arg #f "Not a generic: ~S"
1530 (concatenate (fold-downward (fold-upward '() obj) obj)))
1532 (define (%compute-applicable-methods gf args)
1533 (define (method-applicable? m types)
1534 (let lp ((specs (method-specializers m)) (types types))
1536 ((null? specs) (null? types))
1537 ((not (pair? specs)) #t)
1540 (and (memq (car specs) (class-precedence-list (car types)))
1541 (lp (cdr specs) (cdr types)))))))
1542 (let ((n (length args))
1543 (types (map class-of args)))
1544 (let lp ((methods (generic-function-methods gf))
1547 (and (not (null? applicable))
1548 (%sort-applicable-methods applicable types))
1549 (let ((m (car methods)))
1551 (if (method-applicable? m types)
1555 (define compute-applicable-methods %compute-applicable-methods)
1557 (define (toplevel-define! name val)
1558 (module-define! (current-module) name val))
1560 (define-syntax define-method
1561 (syntax-rules (setter)
1562 ((_ ((setter name) . args) body ...)
1564 (if (or (not (defined? 'name))
1565 (not (is-a? name <accessor>)))
1566 (toplevel-define! 'name
1568 (if (defined? 'name) name #f) 'name)))
1569 (add-method! (setter name) (method args body ...))))
1570 ((_ (name . args) body ...)
1572 ;; FIXME: this code is how it always was, but it's quite cracky:
1573 ;; it will only define the generic function if it was undefined
1574 ;; before (ok), or *was defined to #f*. The latter is crack. But
1575 ;; there are bootstrap issues about fixing this -- change it to
1576 ;; (is-a? name <generic>) and see.
1577 (if (or (not (defined? 'name))
1579 (toplevel-define! 'name (make <generic> #:name 'name)))
1580 (add-method! name (method args body ...))))))
1582 (define-syntax method
1584 (define (parse-args args)
1585 (let lp ((ls args) (formals '()) (specializers '()))
1588 (and (identifier? #'f) (identifier? #'s))
1591 (cons #'s specializers)))
1596 (cons #'<top> specializers)))
1598 (list (reverse formals)
1599 (reverse (cons #''() specializers))))
1601 (identifier? #'tail)
1602 (list (append (reverse formals) #'tail)
1603 (reverse (cons #'<top> specializers)))))))
1605 (define (find-free-id exp referent)
1608 (or (find-free-id #'x referent)
1609 (find-free-id #'y referent)))
1612 (let ((id (datum->syntax #'x referent)))
1613 (and (free-identifier=? #'x id) id)))
1616 (define (compute-procedure formals body)
1617 (syntax-case body ()
1619 (with-syntax ((formals formals))
1620 #'(lambda formals body0 ...)))))
1622 (define (->proper args)
1623 (let lp ((ls args) (out '()))
1625 ((x . xs) (lp #'xs (cons #'x out)))
1627 (tail (reverse (cons #'tail out))))))
1629 (define (compute-make-procedure formals body next-method)
1630 (syntax-case body ()
1632 (with-syntax ((next-method next-method))
1633 (syntax-case formals ()
1635 #'(lambda (real-next-method)
1636 (lambda (formal ...)
1637 (let ((next-method (lambda args
1639 (real-next-method formal ...)
1640 (apply real-next-method args)))))
1643 (with-syntax (((formal ...) (->proper #'formals)))
1644 #'(lambda (real-next-method)
1646 (let ((next-method (lambda args
1648 (apply real-next-method formal ...)
1649 (apply real-next-method args)))))
1652 (define (compute-procedures formals body)
1653 ;; So, our use of this is broken, because it operates on the
1654 ;; pre-expansion source code. It's equivalent to just searching
1655 ;; for referent in the datums. Ah well.
1656 (let ((id (find-free-id body 'next-method)))
1658 ;; return a make-procedure
1660 (compute-make-procedure formals body id))
1661 (values (compute-procedure formals body)
1665 ((_ args) #'(method args (if #f #f)))
1666 ((_ args body0 body1 ...)
1667 (with-syntax (((formals (specializer ...)) (parse-args #'args)))
1670 (compute-procedures #'formals #'(body0 body1 ...)))
1671 (lambda (procedure make-procedure)
1672 (with-syntax ((procedure procedure)
1673 (make-procedure make-procedure))
1675 #:specializers (cons* specializer ...)
1677 #:body '(body0 body1 ...)
1678 #:make-procedure make-procedure
1679 #:procedure procedure)))))))))
1685 (define (add-method-in-classes! m)
1686 ;; Add method in all the classes which appears in its specializers list
1687 (for-each* (lambda (x)
1688 (let ((dm (class-direct-methods x)))
1690 (struct-set! x class-index-direct-methods (cons m dm)))))
1691 (method-specializers m)))
1693 (define (remove-method-in-classes! m)
1694 ;; Remove method in all the classes which appears in its specializers list
1695 (for-each* (lambda (x)
1697 class-index-direct-methods
1698 (delv! m (class-direct-methods x))))
1699 (method-specializers m)))
1701 (define (compute-new-list-of-methods gf new)
1702 (let ((new-spec (method-specializers new))
1703 (methods (slot-ref gf 'methods)))
1704 (let loop ((l methods))
1707 (if (equal? (method-specializers (car l)) new-spec)
1709 ;; This spec. list already exists. Remove old method from dependents
1710 (remove-method-in-classes! (car l))
1715 (define (method-n-specializers m)
1716 (length* (slot-ref m 'specializers)))
1718 (define (calculate-n-specialized gf)
1719 (fold (lambda (m n) (max n (method-n-specializers m)))
1721 (generic-function-methods gf)))
1723 (define (invalidate-method-cache! gf)
1724 (%invalidate-method-cache! gf)
1725 (slot-set! gf 'n-specialized (calculate-n-specialized gf))
1726 (for-each (lambda (gf) (invalidate-method-cache! gf))
1727 (slot-ref gf 'extended-by)))
1729 (define internal-add-method!
1730 (method ((gf <generic>) (m <method>))
1731 (slot-set! m 'generic-function gf)
1732 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
1733 (invalidate-method-cache! gf)
1734 (add-method-in-classes! m)
1737 (define-generic add-method!)
1739 ((method-procedure internal-add-method!) add-method! internal-add-method!)
1741 (define-method (add-method! (proc <procedure>) (m <method>))
1742 (if (generic-capability? proc)
1744 (enable-primitive-generic! proc)
1745 (add-method! proc m))
1748 (define-method (add-method! (pg <primitive-generic>) (m <method>))
1749 (add-method! (primitive-generic-generic pg) m))
1751 (define-method (add-method! obj (m <method>))
1752 (goops-error "~S is not a valid generic function" obj))
1755 ;;; {Access to meta objects}
1761 (define-method (method-source (m <method>))
1762 (let* ((spec (map* class-name (slot-ref m 'specializers)))
1763 (src (procedure-source (slot-ref m 'procedure))))
1765 (let ((args (cadr src))
1768 (cons (map* list args spec)
1771 (define-method (method-formals (m <method>))
1772 (slot-ref m 'formals))
1777 (define slot-definition-name car)
1779 (define slot-definition-options cdr)
1781 (define (slot-definition-allocation s)
1782 (get-keyword #:allocation (cdr s) #:instance))
1784 (define (slot-definition-getter s)
1785 (get-keyword #:getter (cdr s) #f))
1787 (define (slot-definition-setter s)
1788 (get-keyword #:setter (cdr s) #f))
1790 (define (slot-definition-accessor s)
1791 (get-keyword #:accessor (cdr s) #f))
1793 (define (slot-definition-init-value s)
1794 ;; can be #f, so we can't use #f as non-value
1795 (get-keyword #:init-value (cdr s) (make-unbound)))
1797 (define (slot-definition-init-form s)
1798 (get-keyword #:init-form (cdr s) (make-unbound)))
1800 (define (slot-definition-init-thunk s)
1801 (get-keyword #:init-thunk (cdr s) #f))
1803 (define (slot-definition-init-keyword s)
1804 (get-keyword #:init-keyword (cdr s) #f))
1806 (define (class-slot-definition class slot-name)
1807 (assq slot-name (class-slots class)))
1809 (define (slot-init-function class slot-name)
1810 (cadr (assq slot-name (struct-ref class class-index-getters-n-setters))))
1812 (define (accessor-method-slot-definition obj)
1813 "Return the slot definition of the accessor @var{obj}."
1814 (slot-ref obj 'slot-definition))
1818 ;;; {Standard methods used by the C runtime}
1821 ;;; Methods to compare objects
1824 ;; Have to do this in a strange order because equal? is used in the
1825 ;; add-method! implementation; we need to make sure that when the
1826 ;; primitive is extended, that the generic has a method. =
1827 (define g-equal? (make-generic 'equal?))
1828 ;; When this generic gets called, we will have already checked eq? and
1829 ;; eqv? -- the purpose of this generic is to extend equality. So by
1830 ;; default, there is no extension, thus the #f return.
1831 (add-method! g-equal? (method (x y) #f))
1832 (set-primitive-generic! equal? g-equal?)
1835 ;;; methods to display/write an object
1838 ; Code for writing objects must test that the slots they use are
1839 ; bound. Otherwise a slot-unbound method will be called and will
1840 ; conduct to an infinite loop.
1843 (define (display-address o file)
1844 (display (number->string (object-address o) 16) file))
1846 (define-method (write o file)
1847 (display "#<instance " file)
1848 (display-address o file)
1851 (define write-object (primitive-generic-generic write))
1853 (define-method (write (o <object>) file)
1854 (let ((class (class-of o)))
1855 (if (slot-bound? class 'name)
1858 (display (class-name class) file)
1859 (display #\space file)
1860 (display-address o file)
1864 (define-method (write (class <class>) file)
1865 (let ((meta (class-of class)))
1866 (if (and (slot-bound? class 'name)
1867 (slot-bound? meta 'name))
1870 (display (class-name meta) file)
1871 (display #\space file)
1872 (display (class-name class) file)
1873 (display #\space file)
1874 (display-address class file)
1878 (define-method (write (gf <generic>) file)
1879 (let ((meta (class-of gf)))
1880 (if (and (slot-bound? meta 'name)
1881 (slot-bound? gf 'methods))
1884 (display (class-name meta) file)
1885 (let ((name (generic-function-name gf)))
1888 (display #\space file)
1889 (display name file))))
1891 (display (length (generic-function-methods gf)) file)
1892 (display ")>" file))
1895 (define-method (write (o <method>) file)
1896 (let ((meta (class-of o)))
1897 (if (and (slot-bound? meta 'name)
1898 (slot-bound? o 'specializers))
1901 (display (class-name meta) file)
1902 (display #\space file)
1903 (display (map* (lambda (spec)
1904 (if (slot-bound? spec 'name)
1905 (slot-ref spec 'name)
1907 (method-specializers o))
1909 (display #\space file)
1910 (display-address o file)
1914 ;; Display (do the same thing as write by default)
1915 (define-method (display o file)
1916 (write-object o file))
1919 ;;; Handling of duplicate bindings in the module system
1922 (define (find-subclass super name)
1923 (let lp ((classes (class-direct-subclasses super)))
1926 (error "class not found" name))
1927 ((and (slot-bound? (car classes) 'name)
1928 (eq? (class-name (car classes)) name))
1931 (lp (cdr classes))))))
1934 (define <module> (find-subclass <top> '<module>))
1936 (define-method (merge-generics (module <module>)
1946 (define-method (merge-generics (module <module>)
1954 (and (not (eq? val1 val2))
1955 (make-variable (make-extended-generic (list val2 val1) name))))
1957 (define-method (merge-generics (module <module>)
1964 (gf <extended-generic>))
1965 (and (not (memq val2 (slot-ref gf 'extends)))
1969 (cons val2 (delq! val2 (slot-ref gf 'extends))))
1972 (cons gf (delq! gf (slot-ref val2 'extended-by))))
1973 (invalidate-method-cache! gf)
1976 (module-define! duplicate-handlers 'merge-generics merge-generics)
1978 (define-method (merge-accessors (module <module>)
1988 (define-method (merge-accessors (module <module>)
1996 (merge-generics module name int1 val1 int2 val2 var val))
1998 (module-define! duplicate-handlers 'merge-accessors merge-accessors)
2004 (define (class-slot-g-n-s class slot-name)
2005 (let* ((this-slot (assq slot-name (struct-ref class class-index-slots)))
2006 (getters-n-setters (struct-ref class class-index-getters-n-setters))
2007 (g-n-s (cddr (or (assq slot-name getters-n-setters)
2008 (slot-missing class slot-name)))))
2009 (if (not (memq (slot-definition-allocation this-slot)
2010 '(#:class #:each-subclass)))
2011 (slot-missing class slot-name))
2014 (define (class-slot-ref class slot)
2015 (let ((x ((car (class-slot-g-n-s class slot)) #f)))
2017 (slot-unbound class slot)
2020 (define (class-slot-set! class slot value)
2021 ((cadr (class-slot-g-n-s class slot)) #f value))
2023 (define-method (slot-unbound (c <class>) (o <object>) s)
2024 (goops-error "Slot `~S' is unbound in object ~S" s o))
2026 (define-method (slot-unbound (c <class>) s)
2027 (goops-error "Slot `~S' is unbound in class ~S" s c))
2029 (define-method (slot-unbound (o <object>))
2030 (goops-error "Unbound slot in object ~S" o))
2032 (define-method (slot-missing (c <class>) (o <object>) s)
2033 (goops-error "No slot with name `~S' in object ~S" s o))
2035 (define-method (slot-missing (c <class>) s)
2036 (goops-error "No class slot with name `~S' in class ~S" s c))
2039 (define-method (slot-missing (c <class>) (o <object>) s value)
2040 (slot-missing c o s))
2042 ;;; Methods for the possible error we can encounter when calling a gf
2044 (define-method (no-next-method (gf <generic>) args)
2045 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
2047 (define-method (no-applicable-method (gf <generic>) args)
2048 (goops-error "No applicable method for ~S in call ~S"
2049 gf (cons (generic-function-name gf) args)))
2051 (define-method (no-method (gf <generic>) args)
2052 (goops-error "No method defined for ~S" gf))
2055 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
2058 (define-method (shallow-clone (self <object>))
2059 (let* ((class (class-of self))
2060 (clone (%allocate-instance class))
2061 (slots (map slot-definition-name (class-slots class))))
2062 (for-each (lambda (slot)
2063 (if (slot-bound? self slot)
2064 (slot-set! clone slot (slot-ref self slot))))
2068 (define-method (deep-clone (self <object>))
2069 (let* ((class (class-of self))
2070 (clone (%allocate-instance class))
2071 (slots (map slot-definition-name (class-slots class))))
2072 (for-each (lambda (slot)
2073 (if (slot-bound? self slot)
2074 (slot-set! clone slot
2075 (let ((value (slot-ref self slot)))
2076 (if (instance? value)
2083 ;;; {Class redefinition utilities}
2086 ;;; (class-redefinition OLD NEW)
2089 ;;; Has correct the following conditions:
2093 ;;; 1. New accessor specializers refer to new header
2097 ;;; 1. New class cpl refers to the new class header
2098 ;;; 2. Old class header exists on old super classes direct-subclass lists
2099 ;;; 3. New class header exists on new super classes direct-subclass lists
2101 (define-method (class-redefinition (old <class>) (new <class>))
2102 ;; Work on direct methods:
2103 ;; 1. Remove accessor methods from the old class
2104 ;; 2. Patch the occurences of new in the specializers by old
2105 ;; 3. Displace the methods from old to new
2106 (remove-class-accessors! old) ;; -1-
2107 (let ((methods (class-direct-methods new)))
2108 (for-each (lambda (m)
2109 (update-direct-method! m new old)) ;; -2-
2112 class-index-direct-methods
2113 (append methods (class-direct-methods old))))
2115 ;; Substitute old for new in new cpl
2116 (set-car! (struct-ref new class-index-cpl) old)
2118 ;; Remove the old class from the direct-subclasses list of its super classes
2119 (for-each (lambda (c) (struct-set! c class-index-direct-subclasses
2120 (delv! old (class-direct-subclasses c))))
2121 (class-direct-supers old))
2123 ;; Replace the new class with the old in the direct-subclasses of the supers
2124 (for-each (lambda (c)
2125 (struct-set! c class-index-direct-subclasses
2126 (cons old (delv! new (class-direct-subclasses c)))))
2127 (class-direct-supers new))
2129 ;; Swap object headers
2130 (%modify-class old new)
2134 ;; Redefine all the subclasses of old to take into account modification
2137 (update-direct-subclass! c new old))
2138 (class-direct-subclasses new))
2140 ;; Invalidate class so that subsequent instances slot accesses invoke
2141 ;; change-object-class
2142 (struct-set! new class-index-redefined old)
2143 (%invalidate-class new) ;must come after slot-set!
2148 ;;; remove-class-accessors!
2151 (define-method (remove-class-accessors! (c <class>))
2152 (for-each (lambda (m)
2153 (if (is-a? m <accessor-method>)
2154 (let ((gf (slot-ref m 'generic-function)))
2155 ;; remove the method from its GF
2156 (slot-set! gf 'methods
2157 (delq1! m (slot-ref gf 'methods)))
2158 (invalidate-method-cache! gf)
2159 ;; remove the method from its specializers
2160 (remove-method-in-classes! m))))
2161 (class-direct-methods c)))
2164 ;;; update-direct-method!
2167 (define-method (update-direct-method! (m <method>)
2170 (let loop ((l (method-specializers m)))
2171 ;; Note: the <top> in dotted list is never used.
2172 ;; So we can work as if we had only proper lists.
2175 (if (eqv? (car l) old)
2180 ;;; update-direct-subclass!
2183 (define-method (update-direct-subclass! (c <class>)
2186 (class-redefinition c
2187 (make-class (class-direct-supers c)
2188 (class-direct-slots c)
2189 #:name (class-name c)
2190 #:metaclass (class-of c))))
2193 ;;; {Utilities for INITIALIZE methods}
2196 ;;; compute-slot-accessors
2198 (define (compute-slot-accessors class slots)
2201 (let ((getter-function (slot-definition-getter s))
2202 (setter-function (slot-definition-setter s))
2203 (accessor (slot-definition-accessor s)))
2205 (add-method! getter-function
2206 (compute-getter-method class g-n-s)))
2208 (add-method! setter-function
2209 (compute-setter-method class g-n-s)))
2212 (add-method! accessor
2213 (compute-getter-method class g-n-s))
2214 (add-method! (setter accessor)
2215 (compute-setter-method class g-n-s))))))
2216 slots (struct-ref class class-index-getters-n-setters)))
2218 (define-method (compute-getter-method (class <class>) slotdef)
2219 (let ((init-thunk (cadr slotdef))
2220 (g-n-s (cddr slotdef)))
2221 (make <accessor-method>
2222 #:specializers (list class)
2223 #:procedure (cond ((pair? g-n-s)
2224 (make-generic-bound-check-getter (car g-n-s)))
2226 (standard-get g-n-s))
2228 (bound-check-get g-n-s)))
2229 #:slot-definition slotdef)))
2231 (define-method (compute-setter-method (class <class>) slotdef)
2232 (let ((g-n-s (cddr slotdef)))
2233 (make <accessor-method>
2234 #:specializers (list class <top>)
2235 #:procedure (if (pair? g-n-s)
2237 (standard-set g-n-s))
2238 #:slot-definition slotdef)))
2240 (define (make-generic-bound-check-getter proc)
2242 (let ((val (proc o)))
2247 ;;; Pre-generate getters and setters for the first 20 slots.
2248 (define-syntax define-standard-accessor-method
2250 (define num-standard-pre-cache 20)
2252 ((_ ((proc n) arg ...) body)
2254 (let ((cache (vector #,@(map (lambda (n*)
2258 (iota num-standard-pre-cache)))))
2260 (if (< n #,num-standard-pre-cache)
2261 (vector-ref cache n)
2262 (lambda (arg ...) body)))))))))
2264 (define-standard-accessor-method ((bound-check-get n) o)
2265 (let ((x (struct-ref o n)))
2270 (define-standard-accessor-method ((standard-get n) o)
2273 (define-standard-accessor-method ((standard-set n) o v)
2274 (struct-set! o n v))
2276 ;;; compute-getters-n-setters
2278 (define (compute-getters-n-setters class slots)
2280 (define (compute-slot-init-function name s)
2281 (or (let ((thunk (slot-definition-init-thunk s)))
2285 (goops-error "Bad init-thunk for slot `~S' in ~S: ~S"
2286 name class thunk))))
2287 (let ((init (slot-definition-init-value s)))
2288 (and (not (unbound? init))
2289 (lambda () init)))))
2291 (define (verify-accessors slot l)
2292 (cond ((integer? l))
2293 ((not (and (list? l) (= (length l) 2)))
2294 (goops-error "Bad getter and setter for slot `~S' in ~S: ~S"
2299 (if (not (procedure? get))
2300 (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
2302 (if (not (procedure? set))
2303 (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
2304 slot class set))))))
2307 ;; The strange treatment of nfields is due to backward compatibility.
2308 (let* ((index (slot-ref class 'nfields))
2309 (g-n-s (compute-get-n-set class s))
2310 (size (- (slot-ref class 'nfields) index))
2311 (name (slot-definition-name s)))
2312 ;; NOTE: The following is interdependent with C macros
2313 ;; defined above goops.c:scm_sys_prep_layout_x.
2315 ;; For simple instance slots, we have the simplest form
2316 ;; '(name init-function . index)
2317 ;; For other slots we have
2318 ;; '(name init-function getter setter . alloc)
2320 ;; '(index size) for instance allocated slots
2321 ;; '() for other slots
2322 (verify-accessors name g-n-s)
2323 (case (slot-definition-allocation s)
2324 ((#:each-subclass #:class)
2325 (unless (and (zero? size) (pair? g-n-s))
2326 (error "Class-allocated slots should not reserve fields"))
2327 ;; Don't initialize the slot; that's handled when the slot
2328 ;; is allocated, in compute-get-n-set.
2329 (cons name (cons #f g-n-s)))
2332 (cons (compute-slot-init-function name s)
2333 (if (or (integer? g-n-s)
2336 (append g-n-s (list index size)))))))))
2342 ;; Replace the bootstrap compute-cpl with this definition.
2344 (make <generic> #:name 'compute-cpl))
2346 (define-method (compute-cpl (class <class>))
2347 (compute-std-cpl class class-direct-supers))
2349 ;;; compute-get-n-set
2351 (define-method (compute-get-n-set (class <class>) s)
2352 (define (class-slot-init-value)
2353 (let ((thunk (slot-definition-init-thunk s)))
2356 (slot-definition-init-value s))))
2358 (case (slot-definition-allocation s)
2359 ((#:instance) ;; Instance slot
2360 ;; get-n-set is just its offset
2361 (let ((already-allocated (struct-ref class class-index-nfields)))
2362 (struct-set! class class-index-nfields (+ already-allocated 1))
2365 ((#:class) ;; Class slot
2366 ;; Class-slots accessors are implemented as 2 closures around
2367 ;; a Scheme variable. As instance slots, class slots must be
2368 ;; unbound at init time.
2369 (let ((name (slot-definition-name s)))
2370 (if (memq name (map slot-definition-name (class-direct-slots class)))
2371 ;; This slot is direct; create a new shared variable
2372 (make-closure-variable class (class-slot-init-value))
2373 ;; Slot is inherited. Find its definition in superclass
2374 (let loop ((l (cdr (class-precedence-list class))))
2375 (let ((r (assoc name
2377 class-index-getters-n-setters))))
2380 (loop (cdr l))))))))
2382 ((#:each-subclass) ;; slot shared by instances of direct subclass.
2383 ;; (Thomas Buerger, April 1998)
2384 (make-closure-variable class (class-slot-init-value)))
2386 ((#:virtual) ;; No allocation
2387 ;; slot-ref and slot-set! function must be given by the user
2388 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
2389 (set (get-keyword #:slot-set! (slot-definition-options s) #f)))
2390 (if (not (and get set))
2391 (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S"
2394 (else (next-method))))
2396 (define (make-closure-variable class value)
2397 (list (lambda (o) value)
2398 (lambda (o v) (set! value v))))
2400 (define-method (compute-get-n-set (o <object>) s)
2401 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
2403 (define-method (compute-slots (class <class>))
2404 (build-slots-list (class-direct-slots class)
2405 (class-precedence-list class)))
2411 (define *unbound* (make-unbound))
2413 ;; FIXME: This could be much more efficient.
2414 (define (%initialize-object obj initargs)
2415 "Initialize the object @var{obj} with the given arguments
2417 (unless (instance? obj)
2418 (scm-error 'wrong-type-arg #f "Not an object: ~S"
2420 (unless (even? (length initargs))
2421 (scm-error 'wrong-type-arg #f "Initargs has odd length: ~S"
2422 (list initargs) #f))
2423 (let ((class (class-of obj)))
2424 (define (get-initarg kw)
2426 (get-keyword kw initargs *unbound*)
2428 (let lp ((get-n-set (struct-ref class class-index-getters-n-setters))
2429 (slots (struct-ref class class-index-slots)))
2432 (((name . options) . slots)
2434 (((_ init-thunk . _) . get-n-set)
2435 (let ((initarg (get-initarg (get-keyword #:init-keyword options))))
2437 ((not (unbound? initarg))
2438 (slot-set! obj name initarg))
2440 (slot-set! obj name (init-thunk)))))
2441 (lp get-n-set slots))))))))
2443 (define-method (initialize (object <object>) initargs)
2444 (%initialize-object object initargs))
2446 (define-method (initialize (class <class>) initargs)
2448 (let ((dslots (get-keyword #:slots initargs '()))
2449 (supers (get-keyword #:dsupers initargs '())))
2450 (let ((name (get-keyword #:name initargs '???)))
2451 (struct-set! class class-index-name name))
2452 (struct-set! class class-index-nfields 0)
2453 (struct-set! class class-index-direct-supers supers)
2454 (struct-set! class class-index-direct-slots dslots)
2455 (struct-set! class class-index-direct-subclasses '())
2456 (struct-set! class class-index-direct-methods '())
2457 (struct-set! class class-index-cpl (compute-cpl class))
2458 (struct-set! class class-index-redefined #f)
2459 (let ((slots (compute-slots class)))
2460 (struct-set! class class-index-slots slots)
2461 (let ((getters-n-setters (compute-getters-n-setters class slots)))
2462 (struct-set! class class-index-getters-n-setters getters-n-setters))
2463 ;; Build getters - setters - accessors
2464 (compute-slot-accessors class slots))
2466 ;; Update the "direct-subclasses" of each inherited classes
2467 (for-each (lambda (x)
2468 (let ((dsubs (struct-ref x class-index-direct-subclasses)))
2469 (struct-set! x class-index-direct-subclasses
2470 (cons class dsubs))))
2473 ;; Compute struct layout of instances, set the `layout' slot, and
2474 ;; update class flags.
2475 (%prep-layout! class)))
2477 (define (initialize-object-procedure object initargs)
2478 (let ((proc (get-keyword #:procedure initargs #f)))
2481 (apply slot-set! object 'procedure proc))
2483 (slot-set! object 'procedure proc)))))
2485 (define-method (initialize (applicable-struct <applicable-struct>) initargs)
2487 (initialize-object-procedure applicable-struct initargs))
2489 (define-method (initialize (applicable-struct <applicable-struct-with-setter>)
2492 (slot-set! applicable-struct 'setter (get-keyword #:setter initargs #f)))
2494 (define-method (initialize (generic <generic>) initargs)
2495 (let ((previous-definition (get-keyword #:default initargs #f))
2496 (name (get-keyword #:name initargs #f)))
2498 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
2500 (apply previous-definition args)))
2503 (set-procedure-property! generic 'name name))
2504 (invalidate-method-cache! generic)))
2506 (define-method (initialize (eg <extended-generic>) initargs)
2508 (slot-set! eg 'extends (get-keyword #:extends initargs '())))
2510 (define dummy-procedure (lambda args *unspecified*))
2512 (define-method (initialize (method <method>) initargs)
2514 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
2515 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
2516 (slot-set! method 'procedure
2517 (get-keyword #:procedure initargs #f))
2518 (slot-set! method 'formals (get-keyword #:formals initargs '()))
2519 (slot-set! method 'body (get-keyword #:body initargs '()))
2520 (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
2527 (define (change-object-class old-instance old-class new-class)
2528 (let ((new-instance (allocate-instance new-class '())))
2529 ;; Initialize the slots of the new instance
2530 (for-each (lambda (slot)
2531 (if (and (slot-exists-using-class? old-class old-instance slot)
2532 (eq? (slot-definition-allocation
2533 (class-slot-definition old-class slot))
2535 (slot-bound-using-class? old-class old-instance slot))
2536 ;; Slot was present and allocated in old instance; copy it
2537 (slot-set-using-class!
2541 (slot-ref-using-class old-class old-instance slot))
2542 ;; slot was absent; initialize it with its default value
2543 (let ((init (slot-init-function new-class slot)))
2545 (slot-set-using-class!
2549 (apply init '()))))))
2550 (map slot-definition-name (class-slots new-class)))
2551 ;; Exchange old and new instance in place to keep pointers valid
2552 (%modify-instance old-instance new-instance)
2553 ;; Allow class specific updates of instances (which now are swapped)
2554 (update-instance-for-different-class new-instance old-instance)
2558 (define-method (update-instance-for-different-class (old-instance <object>)
2561 ;;not really important what we do, we just need a default method
2564 (define-method (change-class (old-instance <object>) (new-class <class>))
2565 (change-object-class old-instance (class-of old-instance) new-class))
2570 ;;; A new definition which overwrites the previous one which was built-in
2573 (define-method (allocate-instance (class <class>) initargs)
2574 (%allocate-instance class))
2576 (define-method (make-instance (class <class>) . initargs)
2577 (let ((instance (allocate-instance class initargs)))
2578 (initialize instance initargs)
2581 (define make make-instance)
2586 ;;; Protocol for calling generic functions, intended to be used when
2587 ;;; applying subclasses of <generic> and <generic-with-setter>. The
2588 ;;; code below is similar to the first MOP described in AMOP.
2590 ;;; Note that standard generic functions dispatch only on the classes of
2591 ;;; the arguments, and the result of such dispatch can be memoized. The
2592 ;;; `cache-dispatch' routine implements this. `apply-generic' isn't
2593 ;;; called currently; the generic function MOP was never fully
2594 ;;; implemented in GOOPS. However now that GOOPS is implemented
2595 ;;; entirely in Scheme (2015) it's much easier to complete this work.
2596 ;;; Contributions gladly accepted! Please read the AMOP first though :)
2598 ;;; The protocol is:
2600 ;;; + apply-generic (gf args)
2601 ;;; + compute-applicable-methods (gf args ...)
2602 ;;; + sort-applicable-methods (gf methods args)
2603 ;;; + apply-methods (gf methods args)
2605 ;;; apply-methods calls make-next-method to build the "continuation" of
2606 ;;; a method. Applying a next-method will call apply-next-method which
2607 ;;; in turn will call apply again to call effectively the following
2608 ;;; method. (This paragraph is out of date but is kept so that maybe it
2609 ;;; illuminates some future hack.)
2612 (define-method (apply-generic (gf <generic>) args)
2613 (if (null? (slot-ref gf 'methods))
2614 (no-method gf args))
2615 (let ((methods (compute-applicable-methods gf args)))
2617 (apply-methods gf (sort-applicable-methods gf methods args) args)
2618 (no-applicable-method gf args))))
2620 ;; compute-applicable-methods is bound to %compute-applicable-methods.
2622 (define %%compute-applicable-methods
2623 (make <generic> #:name 'compute-applicable-methods))
2625 (define-method (%%compute-applicable-methods (gf <generic>) args)
2626 (%compute-applicable-methods gf args))
2628 (set! compute-applicable-methods %%compute-applicable-methods)
2630 (define-method (sort-applicable-methods (gf <generic>) methods args)
2631 (%sort-applicable-methods methods (map class-of args)))
2633 (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
2634 (%method-more-specific? m1 m2 targs))
2636 (define-method (apply-method (gf <generic>) methods build-next args)
2637 (apply (method-procedure (car methods))
2638 (build-next (cdr methods) args)
2641 (define-method (apply-methods (gf <generic>) (l <list>) args)
2642 (letrec ((next (lambda (procs args)
2644 (let ((a (if (null? new-args) args new-args)))
2646 (no-next-method gf a)
2647 (apply-method gf procs next a)))))))
2648 (apply-method gf l next args)))
2650 ;; We don't want the following procedure to turn up in backtraces:
2651 (for-each (lambda (proc)
2652 (set-procedure-property! proc 'system-procedure #t))
2656 no-applicable-method
2661 ;;; {<composite-metaclass> and <active-metaclass>}
2664 ;(autoload "active-slot" <active-metaclass>)
2665 ;(autoload "composite-slot" <composite-metaclass>)
2666 ;(export <composite-metaclass> <active-metaclass>)
2674 ;; duplicate the standard list->set function but using eq instead of
2675 ;; eqv which really sucks a lot, uselessly here
2677 (define (list2set l)
2682 ((memq (car l) res) (loop (cdr l) res))
2683 (else (loop (cdr l) (cons (car l) res))))))
2685 (define (class-subclasses c)
2686 (letrec ((allsubs (lambda (c)
2687 (cons c (mapappend allsubs
2688 (class-direct-subclasses c))))))
2689 (list2set (cdr (allsubs c)))))
2691 (define (class-methods c)
2692 (list2set (mapappend class-direct-methods
2693 (cons c (class-subclasses c)))))
2696 ;;; {Final initialization}
2699 ;; Tell C code that the main bulk of Goops has been loaded
2706 ;;; {SMOB and port classes}
2709 (define <arbiter> (find-subclass <top> '<arbiter>))
2710 (define <promise> (find-subclass <top> '<promise>))
2711 (define <thread> (find-subclass <top> '<thread>))
2712 (define <mutex> (find-subclass <top> '<mutex>))
2713 (define <condition-variable> (find-subclass <top> '<condition-variable>))
2714 (define <regexp> (find-subclass <top> '<regexp>))
2715 (define <hook> (find-subclass <top> '<hook>))
2716 (define <bitvector> (find-subclass <top> '<bitvector>))
2717 (define <random-state> (find-subclass <top> '<random-state>))
2718 (define <async> (find-subclass <top> '<async>))
2719 (define <directory> (find-subclass <top> '<directory>))
2720 (define <array> (find-subclass <top> '<array>))
2721 (define <character-set> (find-subclass <top> '<character-set>))
2722 (define <dynamic-object> (find-subclass <top> '<dynamic-object>))
2723 (define <guardian> (find-subclass <applicable> '<guardian>))
2724 (define <macro> (find-subclass <top> '<macro>))
2726 (define (define-class-subtree class)
2727 (define! (class-name class) class)
2728 (for-each define-class-subtree (class-direct-subclasses class)))
2730 (define-class-subtree (find-subclass <port> '<file-port>))