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 (system base target)
31 #:use-module ((language tree-il primitives)
32 :select (add-interesting-primitive!))
33 #:export-syntax (define-class class standard-define-class
34 define-generic define-accessor define-method
35 define-extended-generic define-extended-generics
37 #:export ( ;; The root of everything.
42 <foreign-slot> <protected-slot> <hidden-slot> <opaque-slot>
43 <read-only-slot> <self-slot> <protected-opaque-slot>
44 <protected-hidden-slot> <protected-read-only-slot>
45 <scm-slot> <int-slot> <float-slot> <double-slot>
47 ;; Methods are implementations of generic functions.
48 <method> <accessor-method>
50 ;; Applicable objects, either procedures or applicable structs.
51 <procedure-class> <applicable>
52 <procedure> <primitive-generic>
54 ;; Applicable structs.
55 <applicable-struct-class> <applicable-struct-with-setter-class>
56 <applicable-struct> <applicable-struct-with-setter>
57 <generic> <extended-generic>
58 <generic-with-setter> <extended-generic-with-setter>
59 <accessor> <extended-accessor>
61 ;; Types with their own allocated typecodes.
62 <boolean> <char> <list> <pair> <null> <string> <symbol>
63 <vector> <bytevector> <uvec> <foreign> <hashtable>
64 <fluid> <dynamic-state> <frame> <vm> <vm-continuation>
68 <number> <complex> <real> <integer> <fraction>
73 ;; Particular SMOB data types. All SMOB types have
74 ;; corresponding classes, which may be obtained via class-of,
75 ;; once you have an instance. Perhaps FIXME to provide a
76 ;; smob-type-name->class procedure.
77 <arbiter> <promise> <thread> <mutex> <condition-variable>
78 <regexp> <hook> <bitvector> <random-state> <async>
79 <directory> <array> <character-set>
80 <dynamic-object> <guardian> <macro>
86 <port> <input-port> <output-port> <input-output-port>
88 ;; Like SMOB types, all port types have their own classes,
89 ;; which can be accessed via `class-of' once you have an
90 ;; instance. Here we export bindings just for file ports.
92 <file-input-port> <file-output-port> <file-input-output-port>
95 ensure-metaclass ensure-metaclass-with-supers
97 make-generic ensure-generic
99 make-accessor ensure-accessor
101 class-slot-ref class-slot-set! slot-unbound slot-missing
102 slot-definition-name slot-definition-options
103 slot-definition-allocation
105 slot-definition-getter slot-definition-setter
106 slot-definition-accessor
107 slot-definition-init-value slot-definition-init-form
108 slot-definition-init-thunk slot-definition-init-keyword
109 slot-init-function class-slot-definition
111 compute-cpl compute-std-cpl compute-get-n-set compute-slots
112 compute-getter-method compute-setter-method
113 allocate-instance initialize make-instance make
114 no-next-method no-applicable-method no-method
115 change-class update-instance-for-different-class
116 shallow-clone deep-clone
118 apply-generic apply-method apply-methods
119 compute-applicable-methods %compute-applicable-methods
120 method-more-specific? sort-applicable-methods
121 class-subclasses class-methods
123 min-fixnum max-fixnum
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)
141 ;;; Booting GOOPS is a tortuous process. We begin by loading a small
142 ;;; set of primitives from C.
144 (eval-when (expand load eval)
145 (load-extension (string-append "libguile-" (effective-version))
146 "scm_init_goops_builtins")
147 (add-interesting-primitive! 'class-of))
153 ;;; We then define the slots that must appear in all classes (<class>
154 ;;; objects). These slots must appear in order. We'll use this list to
155 ;;; statically compute offsets for the various fields, to compute the
156 ;;; struct layout for <class> instances, and to compute the slot
157 ;;; definition lists for <class>. Because the list is needed at
158 ;;; expansion-time, we define it as a macro.
160 (define-syntax macro-fold-left
162 ((_ folder seed ()) seed)
163 ((_ folder seed (head . tail))
164 (macro-fold-left folder (folder head seed) tail))))
166 (define-syntax macro-fold-right
168 ((_ folder seed ()) seed)
169 ((_ folder seed (head . tail))
170 (folder head (macro-fold-right folder seed tail)))))
172 (define-syntax fold-class-slots
175 '((layout <protected-read-only-slot>)
176 (flags <hidden-slot>)
178 (instance-finalizer <hidden-slot>)
180 (name <protected-hidden-slot>)
181 (nfields <hidden-slot>)
182 (%reserved <hidden-slot>)
190 (getters-n-setters)))
193 ;; The datum->syntax makes it as if the identifiers in `slots'
194 ;; were present in the initial form, which allows them to be used
195 ;; as (components of) introduced identifiers.
196 #`(fold visit seed #,(datum->syntax #'visit slots))))))
199 ;;; Statically define variables for slot offsets: `class-index-layout'
200 ;;; will be 0, `class-index-flags' will be 1, and so on.
202 (let-syntax ((define-class-index
204 (define (id-append ctx a b)
205 (datum->syntax ctx (symbol-append (syntax->datum a)
207 (define (tail-length tail)
210 ((visit head tail) (1+ (tail-length #'tail)))))
214 (define-syntax #,(id-append #'name #'class-index- #'name)
215 (identifier-syntax #,(tail-length #'tail)))
217 (fold-class-slots macro-fold-left define-class-index (begin)))
220 ;;; Structs that are vtables have a "flags" slot, which corresponds to
221 ;;; class-index-flags. `vtable-flag-vtable' indicates that instances of
222 ;;; a vtable are themselves vtables, and `vtable-flag-validated'
223 ;;; indicates that the struct's layout has been validated. goops.c
224 ;;; defines a couple of additional flags: one to indicate that a vtable
225 ;;; is actually a class, and one to indicate that the class is "valid",
226 ;;; meaning that it hasn't been redefined.
228 (define vtable-flag-goops-metaclass
229 (logior vtable-flag-vtable vtable-flag-goops-class))
231 (define-inlinable (class-add-flags! class flags)
232 (struct-set! class class-index-flags
233 (logior flags (struct-ref class class-index-flags))))
235 (define-inlinable (class-clear-flags! class flags)
236 (struct-set! class class-index-flags
237 (logand (lognot flags) (struct-ref class class-index-flags))))
239 (define-inlinable (class-has-flags? class flags)
241 (logand (struct-ref class class-index-flags) flags)))
243 (define-inlinable (class? obj)
244 (class-has-flags? (struct-vtable obj) vtable-flag-goops-metaclass))
246 (define-inlinable (instance? obj)
247 (class-has-flags? (struct-vtable obj) vtable-flag-goops-class))
250 ;;; Now that we know the slots that must be present in classes, and
251 ;;; their offsets, we can create the root of the class hierarchy.
253 ;;; Note that the `direct-supers', `direct-slots', `cpl', `slots', and
254 ;;; `getters-n-setters' fields will be updated later, once we have
255 ;;; definitions for the specialized slot types like <read-only-slot> and
256 ;;; once we have definitions for <top> and <object>.
259 (let-syntax ((cons-layout
260 ;; A simple way to compute class layout for the concrete
261 ;; types used in <class>.
262 (syntax-rules (<protected-read-only-slot>
265 <protected-hidden-slot>)
267 (string-append "pw" tail))
268 ((_ (name <protected-read-only-slot>) tail)
269 (string-append "pr" tail))
270 ((_ (name <self-slot>) tail)
271 (string-append "sr" tail))
272 ((_ (name <hidden-slot>) tail)
273 (string-append "uh" tail))
274 ((_ (name <protected-hidden-slot>) tail)
275 (string-append "ph" tail))))
278 ((_ (name) tail) (cons (list 'name) tail))
279 ((_ (name class) tail) (cons (list 'name) tail)))))
280 (let* ((layout (fold-class-slots macro-fold-right cons-layout ""))
281 (slots (fold-class-slots macro-fold-right cons-slot '()))
282 (<class> (%make-vtable-vtable layout)))
283 (class-add-flags! <class> (logior vtable-flag-goops-class
284 vtable-flag-goops-valid))
285 (struct-set! <class> class-index-name '<class>)
286 (struct-set! <class> class-index-nfields (length slots))
287 (struct-set! <class> class-index-direct-supers '())
288 (struct-set! <class> class-index-direct-slots slots)
289 (struct-set! <class> class-index-direct-subclasses '())
290 (struct-set! <class> class-index-direct-methods '())
291 (struct-set! <class> class-index-cpl '())
292 (struct-set! <class> class-index-slots slots)
293 (struct-set! <class> class-index-getters-n-setters '())
294 (struct-set! <class> class-index-redefined #f)
298 ;;; Accessors to fields of <class>.
300 (define-syntax-rule (define-class-accessor name docstring field)
305 (scm-error 'wrong-type-arg #f "Not a class: ~S"
307 (struct-ref val field))))
309 (define-class-accessor class-name
310 "Return the class name of @var{obj}."
312 (define-class-accessor class-direct-supers
313 "Return the direct superclasses of the class @var{obj}."
314 class-index-direct-supers)
315 (define-class-accessor class-direct-slots
316 "Return the direct slots of the class @var{obj}."
317 class-index-direct-slots)
318 (define-class-accessor class-direct-subclasses
319 "Return the direct subclasses of the class @var{obj}."
320 class-index-direct-subclasses)
321 (define-class-accessor class-direct-methods
322 "Return the direct methods of the class @var{obj}."
323 class-index-direct-methods)
324 (define-class-accessor class-precedence-list
325 "Return the class precedence list of the class @var{obj}."
327 (define-class-accessor class-slots
328 "Return the slot list of the class @var{obj}."
331 (define (class-subclasses c)
332 "Compute a list of all subclasses of @var{c}, direct and indirect."
333 (define (all-subclasses c)
334 (cons c (append-map all-subclasses
335 (class-direct-subclasses c))))
336 (delete-duplicates (cdr (all-subclasses c)) eq?))
338 (define (class-methods c)
339 "Compute a list of all methods that specialize on @var{c} or
340 subclasses of @var{c}."
341 (delete-duplicates (append-map class-direct-methods
342 (cons c (class-subclasses c)))
349 ;;; The "getters-n-setters" define how to access slot values for a
350 ;;; particular class. In general, there are many ways to access slot
351 ;;; values, but for standard classes it's pretty easy: each slot is
352 ;;; associated with a field in the object.
354 (define (%compute-getters-n-setters slots)
355 (define (compute-init-thunk options)
357 ((kw-arg-ref options #:init-value) => (lambda (val) (lambda () val)))
358 ((kw-arg-ref options #:init-thunk))
360 (let lp ((slots slots) (n 0))
363 (((name . options) . slots)
364 (let ((init-thunk (compute-init-thunk options)))
365 (cons `(,name ,init-thunk . ,n)
366 (lp slots (1+ n))))))))
368 (struct-set! <class> class-index-getters-n-setters
369 (%compute-getters-n-setters (class-slots <class>)))
375 ;;; At this point, we have <class> but no other objects. We need to
376 ;;; define a standard way to make subclasses: how to compute the
377 ;;; precedence list of subclasses, how to compute the list of slots in a
378 ;;; subclass, and what layout to use for instances of those classes.
381 (define (compute-std-cpl c get-direct-supers)
382 "The standard class precedence list computation algorithm."
383 (define (only-non-null lst)
384 (filter (lambda (l) (not (null? l))) lst))
386 (define (merge-lists reversed-partial-result inputs)
388 ((every null? inputs)
389 (reverse! reversed-partial-result))
391 (let* ((candidate (lambda (c)
392 (and (not (any (lambda (l)
396 (candidate-car (lambda (l)
398 (candidate (car l)))))
399 (next (any candidate-car inputs)))
401 (goops-error "merge-lists: Inconsistent precedence graph"))
402 (let ((remove-next (lambda (l)
403 (if (eq? (car l) next)
406 (merge-lists (cons next reversed-partial-result)
407 (only-non-null (map remove-next inputs))))))))
408 (let ((c-direct-supers (get-direct-supers c)))
409 (merge-lists (list c)
410 (only-non-null (append (map class-precedence-list
412 (list c-direct-supers))))))
414 ;; This version of compute-cpl is replaced with a generic function once
416 (define (compute-cpl class)
417 (compute-std-cpl class class-direct-supers))
419 (define (build-slots-list dslots cpl)
420 (define (check-cpl slots class-slots)
421 (when (or-map (match-lambda ((name . options) (assq name slots)))
423 (scm-error 'misc-error #f
424 "a predefined <class> inherited field cannot be redefined"
426 (define (remove-duplicate-slots slots)
427 (let lp ((slots (reverse slots)) (res '()) (seen '()))
430 (((and slot (name . options)) . slots)
433 (lp slots (cons slot res) (cons name seen)))))))
434 (let* ((class-slots (and (memq <class> cpl)
435 (struct-ref <class> class-index-slots))))
437 (check-cpl dslots class-slots))
438 (let lp ((cpl (cdr cpl)) (res dslots) (class-slots '()))
440 (() (remove-duplicate-slots (append class-slots res)))
442 (let ((new-slots (struct-ref head class-index-direct-slots)))
445 (lp cpl (append new-slots res) class-slots))
447 ;; Move class slots to the head of the list.
448 (lp cpl res new-slots))
450 (check-cpl new-slots class-slots)
451 (lp cpl (append new-slots res) class-slots)))))))))
453 (define (%compute-layout slots getters-n-setters nfields is-class?)
454 (define (instance-allocated? g-n-s)
456 ((name init-thunk . (? exact-integer? index)) #t)
457 ((name init-thunk getter setter index size) #t)
460 (define (allocated-index g-n-s)
462 ((name init-thunk . (? exact-integer? index)) index)
463 ((name init-thunk getter setter index size) index)))
465 (define (allocated-size g-n-s)
467 ((name init-thunk . (? exact-integer? index)) 1)
468 ((name init-thunk getter setter index size) size)))
470 (define (slot-protection-and-kind options)
471 (define (subclass? class parent)
472 (memq parent (class-precedence-list class)))
473 (let ((type (kw-arg-ref options #:class)))
474 (if (and type (subclass? type <foreign-slot>))
476 ((subclass? type <self-slot>) #\s)
477 ((subclass? type <protected-slot>) #\p)
480 ((subclass? type <opaque-slot>) #\o)
481 ((subclass? type <read-only-slot>) #\r)
482 ((subclass? type <hidden-slot>) #\h)
486 (let ((layout (make-string (* nfields 2))))
487 (let lp ((n 0) (slots slots) (getters-n-setters getters-n-setters))
488 (match getters-n-setters
490 (unless (= n nfields) (error "bad nfields"))
491 (unless (null? slots) (error "inconsistent g-n-s/slots"))
493 (let ((class-layout (struct-ref <class> class-index-layout)))
494 (unless (string-prefix? (symbol->string class-layout) layout)
495 (error "bad layout for class"))))
497 ((g-n-s . getters-n-setters)
499 (((name . options) . slots)
501 ((instance-allocated? g-n-s)
502 (unless (< n nfields) (error "bad nfields"))
503 (unless (= n (allocated-index g-n-s)) (error "bad allocation"))
504 (call-with-values (lambda () (slot-protection-and-kind options))
505 (lambda (protection kind)
506 (let init ((n n) (size (allocated-size g-n-s)))
508 ((zero? size) (lp n slots getters-n-setters))
510 (string-set! layout (* n 2) protection)
511 (string-set! layout (1+ (* n 2)) kind)
512 (init (1+ n) (1- size))))))))
514 (lp n slots getters-n-setters))))))))))
520 ;;; With all of this, we are now able to define subclasses of <class>.
522 (define (%prep-layout! class)
523 (let* ((is-class? (and (memq <class> (struct-ref class class-index-cpl)) #t))
524 (layout (%compute-layout
525 (struct-ref class class-index-slots)
526 (struct-ref class class-index-getters-n-setters)
527 (struct-ref class class-index-nfields)
529 (%init-layout! class layout)))
531 (define (make-standard-class class name dsupers dslots)
532 (let ((z (make-struct/no-tail class)))
533 (struct-set! z class-index-direct-supers dsupers)
534 (let* ((cpl (compute-cpl z))
535 (dslots (map (lambda (slot)
536 (if (pair? slot) slot (list slot)))
538 (slots (build-slots-list dslots cpl))
539 (nfields (length slots))
540 (g-n-s (%compute-getters-n-setters slots)))
541 (struct-set! z class-index-name name)
542 (struct-set! z class-index-nfields nfields)
543 (struct-set! z class-index-direct-slots dslots)
544 (struct-set! z class-index-direct-subclasses '())
545 (struct-set! z class-index-direct-methods '())
546 (struct-set! z class-index-cpl cpl)
547 (struct-set! z class-index-slots slots)
548 (struct-set! z class-index-getters-n-setters g-n-s)
549 (struct-set! z class-index-redefined #f)
552 (let ((subclasses (struct-ref super class-index-direct-subclasses)))
553 (struct-set! super class-index-direct-subclasses
554 (cons z subclasses))))
559 (define-syntax define-standard-class
561 ((define-standard-class name (super ...) #:metaclass meta slot ...)
563 (make-standard-class meta 'name (list super ...) '(slot ...))))
564 ((define-standard-class name (super ...) slot ...)
565 (define-standard-class name (super ...) #:metaclass <class> slot ...))))
571 ;;; Sweet! Now we can define <top> and <object>, and finish
572 ;;; initializing the `direct-subclasses', `direct-supers', and `cpl'
573 ;;; slots of <class>.
575 (define-standard-class <top> ())
576 (define-standard-class <object> (<top>))
578 ;; <top>, <object>, and <class> were partially initialized. Correct
580 (struct-set! <object> class-index-direct-subclasses (list <class>))
581 (struct-set! <class> class-index-direct-supers (list <object>))
582 (struct-set! <class> class-index-cpl (list <class> <object> <top>))
588 ;;; We can also define the various slot types, and finish initializing
589 ;;; `direct-slots', `slots', and `getters-n-setters' of <class>.
591 (define-standard-class <foreign-slot> (<top>))
592 (define-standard-class <protected-slot> (<foreign-slot>))
593 (define-standard-class <hidden-slot> (<foreign-slot>))
594 (define-standard-class <opaque-slot> (<foreign-slot>))
595 (define-standard-class <read-only-slot> (<foreign-slot>))
596 (define-standard-class <self-slot> (<read-only-slot>))
597 (define-standard-class <protected-opaque-slot> (<protected-slot>
599 (define-standard-class <protected-hidden-slot> (<protected-slot>
601 (define-standard-class <protected-read-only-slot> (<protected-slot>
603 (define-standard-class <scm-slot> (<protected-slot>))
604 (define-standard-class <int-slot> (<foreign-slot>))
605 (define-standard-class <float-slot> (<foreign-slot>))
606 (define-standard-class <double-slot> (<foreign-slot>))
611 (cons (list 'name) tail))
612 ((_ (name class) tail)
613 (cons (list 'name #:class class) tail)))))
614 (let* ((dslots (fold-class-slots macro-fold-right visit '()))
615 (g-n-s (%compute-getters-n-setters dslots)))
616 (struct-set! <class> class-index-direct-slots dslots)
617 (struct-set! <class> class-index-slots dslots)
618 (struct-set! <class> class-index-getters-n-setters g-n-s)))
624 ;;; Now, to build out the class hierarchy.
627 (define-standard-class <procedure-class> (<class>))
629 (define-standard-class <applicable-struct-class>
631 (class-add-flags! <applicable-struct-class>
632 vtable-flag-applicable-vtable)
634 (define-standard-class <applicable-struct-with-setter-class>
635 (<applicable-struct-class>))
636 (class-add-flags! <applicable-struct-with-setter-class>
637 vtable-flag-setter-vtable)
639 (define-standard-class <applicable> (<top>))
640 (define-standard-class <applicable-struct> (<object> <applicable>)
641 #:metaclass <applicable-struct-class>
643 (define-standard-class <applicable-struct-with-setter> (<applicable-struct>)
644 #:metaclass <applicable-struct-with-setter-class>
646 (define-standard-class <generic> (<applicable-struct>)
647 #:metaclass <applicable-struct-class>
649 (n-specialized #:init-value 0)
650 (extended-by #:init-value ())
652 (define-standard-class <extended-generic> (<generic>)
653 #:metaclass <applicable-struct-class>
654 (extends #:init-value ()))
655 (define-standard-class <generic-with-setter> (<generic>
656 <applicable-struct-with-setter>)
657 #:metaclass <applicable-struct-with-setter-class>)
658 (define-standard-class <accessor> (<generic-with-setter>)
659 #:metaclass <applicable-struct-with-setter-class>)
660 (define-standard-class <extended-generic-with-setter> (<extended-generic>
661 <generic-with-setter>)
662 #:metaclass <applicable-struct-with-setter-class>)
663 (define-standard-class <extended-accessor> (<accessor>
664 <extended-generic-with-setter>)
665 #:metaclass <applicable-struct-with-setter-class>)
667 (define-standard-class <method> (<object>)
674 (define-standard-class <accessor-method> (<method>)
675 (slot-definition #:init-keyword #:slot-definition))
677 (define-standard-class <boolean> (<top>))
678 (define-standard-class <char> (<top>))
679 (define-standard-class <list> (<top>))
680 (define-standard-class <pair> (<list>))
681 (define-standard-class <null> (<list>))
682 (define-standard-class <string> (<top>))
683 (define-standard-class <symbol> (<top>))
684 (define-standard-class <vector> (<top>))
685 (define-standard-class <foreign> (<top>))
686 (define-standard-class <hashtable> (<top>))
687 (define-standard-class <fluid> (<top>))
688 (define-standard-class <dynamic-state> (<top>))
689 (define-standard-class <frame> (<top>))
690 (define-standard-class <vm-continuation> (<top>))
691 (define-standard-class <bytevector> (<top>))
692 (define-standard-class <uvec> (<bytevector>))
693 (define-standard-class <array> (<top>))
694 (define-standard-class <bitvector> (<top>))
695 (define-standard-class <number> (<top>))
696 (define-standard-class <complex> (<number>))
697 (define-standard-class <real> (<complex>))
698 (define-standard-class <integer> (<real>))
699 (define-standard-class <fraction> (<real>))
700 (define-standard-class <keyword> (<top>))
701 (define-standard-class <unknown> (<top>))
702 (define-standard-class <procedure> (<applicable>)
703 #:metaclass <procedure-class>)
704 (define-standard-class <primitive-generic> (<procedure>)
705 #:metaclass <procedure-class>)
706 (define-standard-class <port> (<top>))
707 (define-standard-class <input-port> (<port>))
708 (define-standard-class <output-port> (<port>))
709 (define-standard-class <input-output-port> (<input-port> <output-port>))
711 (define (inherit-applicable! class)
712 "An internal routine to redefine a SMOB class that was added after
713 GOOPS was loaded, and on which scm_set_smob_apply installed an apply
715 ;; Why not use class-redefinition? We would, except that loading the
716 ;; compiler to compile effective methods can happen while GOOPS has
717 ;; only been partially loaded, and loading the compiler might cause
718 ;; SMOB types to be defined that need this facility. Instead we make
719 ;; a very specific hack, not a general solution. Probably the right
720 ;; solution is to avoid using the compiler, but that is another kettle
722 (unless (memq <applicable> (class-precedence-list class))
723 (unless (null? (class-slots class))
724 (error "SMOB object has slots?"))
727 (let ((subclasses (struct-ref super class-index-direct-subclasses)))
728 (struct-set! super class-index-direct-subclasses
729 (delq class subclasses))))
730 (struct-ref class class-index-direct-supers))
731 (struct-set! class class-index-direct-supers (list <applicable>))
732 (struct-set! class class-index-cpl (compute-cpl class))
733 (let ((subclasses (struct-ref <applicable> class-index-direct-subclasses)))
734 (struct-set! <applicable> class-index-direct-subclasses
735 (cons class subclasses)))))
741 ;;; At this point we have defined the class hierarchy, and it's time to
742 ;;; move on to instance allocation and generics. Once we have generics,
743 ;;; we'll fill out the metaobject protocol.
745 ;;; Here we define a limited version of `make', so that we can allocate
746 ;;; instances of specific classes. This definition will be replaced
749 (define (%invalidate-method-cache! gf)
750 (slot-set! gf 'procedure (delayed-compile gf))
751 (slot-set! gf 'effective-methods '()))
754 (define (invalidate-method-cache! gf)
755 (%invalidate-method-cache! gf))
757 (define* (get-keyword key l #:optional default)
758 "Determine an associated value for the keyword @var{key} from the list
759 @var{l}. The list @var{l} has to consist of an even number of elements,
760 where, starting with the first, every second element is a keyword,
761 followed by its associated value. If @var{l} does not hold a value for
762 @var{key}, the value @var{default} is returned."
763 (unless (keyword? key)
764 (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list key) #f))
769 (unless (keyword? kw)
770 (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list kw) #f))
771 (if (eq? kw key) arg (lp l))))))
773 (define (%allocate-instance class)
774 (let ((obj (allocate-struct class (struct-ref class class-index-nfields))))
778 (define (make class . args)
780 ((or (eq? class <generic>) (eq? class <accessor>))
781 (let ((z (make-struct/no-tail class #f '() 0 '())))
782 (set-procedure-property! z 'name (get-keyword #:name args #f))
783 (invalidate-method-cache! z)
784 (when (eq? class <accessor>)
785 (let ((setter (get-keyword #:setter args #f)))
787 (slot-set! z 'setter setter))))
790 (let ((z (%allocate-instance class)))
792 ((or (eq? class <method>) (eq? class <accessor-method>))
793 (for-each (match-lambda
795 (slot-set! z slot (get-keyword kw args default))))
796 '((#:generic-function generic-function #f)
797 (#:specializers specializers ())
798 (#:procedure procedure #f)
799 (#:formals formals ())
801 (#:make-procedure make-procedure #f))))
802 ((memq <class> (class-precedence-list class))
803 (class-add-flags! z (logior vtable-flag-goops-class
804 vtable-flag-goops-valid))
805 (for-each (match-lambda
807 (slot-set! z slot (get-keyword kw args default))))
809 (#:dsupers direct-supers ())
810 (#:slots direct-slots ()))))
812 (error "boot `make' does not support this class" class)))
815 (define (is-a? obj class)
816 "Return @code{#t} if @var{obj} is an instance of @var{class}, or
817 @code{#f} otherwise."
818 (and (memq class (class-precedence-list (class-of obj))) #t))
824 ;;; Slot access. This protocol is a bit of a mess: there's the `slots'
825 ;;; slot, which ostensibly holds "slot definitions" but really just has
826 ;;; specially formatted lists. And then there's the `getters-n-setters'
827 ;;; slot, which mirrors `slots' but should in theory indicates how to
828 ;;; get at slots for a particular instance -- never mind that `slots'
829 ;;; was also computed for a particular instance, and that
830 ;;; `getters-n-setters' is a strangely structured chain of pairs.
831 ;;; Perhaps we can fix this in the future, following the CLOS MOP, to
832 ;;; have proper <effective-slot-definition> objects.
834 (define (get-slot-value-using-name class obj slot-name)
835 (match (assq slot-name (struct-ref class class-index-getters-n-setters))
836 (#f (slot-missing class obj slot-name))
837 ((name init-thunk . (? exact-integer? index))
838 (struct-ref obj index))
839 ((name init-thunk getter setter . _)
842 (define (set-slot-value-using-name! class obj slot-name value)
843 (match (assq slot-name (struct-ref class class-index-getters-n-setters))
844 (#f (slot-missing class obj slot-name value))
845 ((name init-thunk . (? exact-integer? index))
846 (struct-set! obj index value))
847 ((name init-thunk getter setter . _)
848 (setter obj value))))
850 (define (test-slot-existence class obj slot-name)
851 (and (assq slot-name (struct-ref class class-index-getters-n-setters))
854 (define (check-slot-args class obj slot-name)
855 (unless (class? class)
856 (scm-error 'wrong-type-arg #f "Not a class: ~S"
858 (unless (instance? obj)
859 (scm-error 'wrong-type-arg #f "Not an instance: ~S"
861 (unless (symbol? slot-name)
862 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
863 (list slot-name) #f)))
865 (define (slot-ref-using-class class obj slot-name)
866 (check-slot-args class obj slot-name)
867 (let ((val (get-slot-value-using-name class obj slot-name)))
869 (slot-unbound class obj slot-name)
872 (define (slot-set-using-class! class obj slot-name value)
873 (check-slot-args class obj slot-name)
874 (set-slot-value-using-name! class obj slot-name value))
876 (define (slot-bound-using-class? class obj slot-name)
877 (check-slot-args class obj slot-name)
878 (not (unbound? (get-slot-value-using-name class obj slot-name))))
880 (define (slot-exists-using-class? class obj slot-name)
881 (check-slot-args class obj slot-name)
882 (test-slot-existence class obj slot-name))
885 ;;; Before we go on, some notes about class redefinition. In GOOPS,
886 ;;; classes can be redefined. Redefinition of a class marks the class
887 ;;; as invalid, and instances will be lazily migrated over to the new
888 ;;; representation as they are accessed. Migration happens when
889 ;;; `class-of' is called on an instance. For more technical details on
890 ;;; object redefinition, see struct.h.
892 ;;; In the following interfaces, class-of handles the redefinition
893 ;;; protocol. I would think though that there is some thread-unsafety
894 ;;; here though as the { class, object data } pair needs to be accessed
895 ;;; atomically, not the { class, object } pair.
898 (define (slot-ref obj slot-name)
899 "Return the value from @var{obj}'s slot with the nam var{slot_name}."
900 (unless (symbol? slot-name)
901 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
902 (list slot-name) #f))
903 (let* ((class (class-of obj))
904 (val (get-slot-value-using-name class obj slot-name)))
906 (slot-unbound class obj slot-name)
909 (define (slot-set! obj slot-name value)
910 "Set the slot named @var{slot_name} of @var{obj} to @var{value}."
911 (unless (symbol? slot-name)
912 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
913 (list slot-name) #f))
914 (set-slot-value-using-name! (class-of obj) obj slot-name value))
916 (define (slot-bound? obj slot-name)
917 "Return the value from @var{obj}'s slot with the nam var{slot_name}."
918 (unless (symbol? slot-name)
919 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
920 (list slot-name) #f))
921 (not (unbound? (get-slot-value-using-name (class-of obj) obj slot-name))))
923 (define (slot-exists? obj slot-name)
924 "Return @code{#t} if @var{obj} has a slot named @var{slot_name}."
925 (unless (symbol? slot-name)
926 (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
927 (list slot-name) #f))
928 (test-slot-existence (class-of obj) obj slot-name))
934 ;;; Method accessors.
936 (define (method-generic-function obj)
937 "Return the generic function for the method @var{obj}."
938 (unless (is-a? obj <method>)
939 (scm-error 'wrong-type-arg #f "Not a method: ~S"
941 (slot-ref obj 'generic-function))
943 (define (method-specializers obj)
944 "Return specializers of the method @var{obj}."
945 (unless (is-a? obj <method>)
946 (scm-error 'wrong-type-arg #f "Not a method: ~S"
948 (slot-ref obj 'specializers))
950 (define (method-procedure obj)
951 "Return the procedure of the method @var{obj}."
952 (unless (is-a? obj <method>)
953 (scm-error 'wrong-type-arg #f "Not a method: ~S"
955 (slot-ref obj 'procedure))
961 ;;; Generic functions!
963 (define *dispatch-module* (current-module))
966 ;;; Generic functions have an applicable-methods cache associated with
967 ;;; them. Every distinct set of types that is dispatched through a
968 ;;; generic adds an entry to the cache. This cache gets compiled out to
969 ;;; a dispatch procedure. In steady-state, this dispatch procedure is
970 ;;; never recompiled; but during warm-up there is some churn, both to
971 ;;; the cache and to the dispatch procedure.
973 ;;; So what is the deal if warm-up happens in a multithreaded context?
974 ;;; There is indeed a window between missing the cache for a certain set
975 ;;; of arguments, and then updating the cache with the newly computed
976 ;;; applicable methods. One of the updaters is liable to lose their new
979 ;;; This is actually OK though, because a subsequent cache miss for the
980 ;;; race loser will just cause memoization to try again. The cache will
981 ;;; eventually be consistent. We're not mutating the old part of the
982 ;;; cache, just consing on the new entry.
984 ;;; It doesn't even matter if the dispatch procedure and the cache are
985 ;;; inconsistent -- most likely the type-set that lost the dispatch
986 ;;; procedure race will simply re-trigger a memoization, but since the
987 ;;; winner isn't in the effective-methods cache, it will likely also
988 ;;; re-trigger a memoization, and the cache will finally be consistent.
989 ;;; As you can see there is a possibility for ping-pong effects, but
990 ;;; it's unlikely given the shortness of the window between slot-set!
991 ;;; invocations. We could add a mutex, but it is strictly unnecessary,
992 ;;; and would add runtime cost and complexity.
995 (define (emit-linear-dispatch gf-sym nargs methods free rest?)
996 (define (gen-syms n stem)
997 (let lp ((n (1- n)) (syms '()))
1000 (lp (1- n) (cons (gensym stem) syms)))))
1001 (let* ((args (gen-syms nargs "a"))
1002 (types (gen-syms nargs "t")))
1003 (let lp ((methods methods)
1005 (exp `(cache-miss ,gf-sym
1007 `(cons* ,@args rest)
1011 (values `(,(if rest? `(,@args . rest) args)
1012 (let ,(map (lambda (t a)
1013 `(,t (class-of ,a)))
1017 ((#(_ specs _ cmethod) . methods)
1018 (let build-dispatch ((free free)
1024 (let ((m-sym (gensym "p")))
1026 (acons cmethod m-sym free)
1027 `(if (and . ,checks)
1029 `(apply ,m-sym ,@args rest)
1035 (let ((var (assq-ref free spec)))
1037 (build-dispatch free
1040 (cons `(eq? ,type ,var)
1042 (let ((var (gensym "c")))
1043 (build-dispatch (acons spec var free)
1046 (cons `(eq? ,type ,var)
1047 checks)))))))))))))))
1049 (define (compute-dispatch-procedure gf cache)
1051 (let lp ((ls cache) (nreq -1) (nrest -1))
1054 (collate (make-vector (1+ nreq) '())
1055 (make-vector (1+ nrest) '())))
1056 ((#(len specs rest? cmethod) . ls)
1058 (lp ls nreq (max nrest len))
1059 (lp ls (max nreq len) nrest))))))
1060 (define (collate req rest)
1061 (let lp ((ls cache))
1063 (() (emit req rest))
1064 (((and entry #(len specs rest? cmethod)) . ls)
1066 (vector-set! rest len (cons entry (vector-ref rest len)))
1067 (vector-set! req len (cons entry (vector-ref req len))))
1069 (define (emit req rest)
1070 (let ((gf-sym (gensym "g")))
1071 (define (emit-rest n clauses free)
1072 (if (< n (vector-length rest))
1073 (match (vector-ref rest n)
1074 (() (emit-rest (1+ n) clauses free))
1075 ;; FIXME: hash dispatch
1079 (emit-linear-dispatch gf-sym n methods free #t))
1080 (lambda (clause free)
1081 (emit-rest (1+ n) (cons clause clauses) free)))))
1082 (emit-req (1- (vector-length req)) clauses free)))
1083 (define (emit-req n clauses free)
1085 (comp `(lambda ,(map cdr free)
1086 (case-lambda ,@clauses))
1088 (match (vector-ref req n)
1089 (() (emit-req (1- n) clauses free))
1090 ;; FIXME: hash dispatch
1094 (emit-linear-dispatch gf-sym n methods free #f))
1095 (lambda (clause free)
1096 (emit-req (1- n) (cons clause clauses) free)))))))
1099 (if (or (zero? (vector-length rest))
1100 (null? (vector-ref rest 0)))
1101 (list `(args (cache-miss ,gf-sym args)))
1103 (acons gf gf-sym '()))))
1104 (define (comp exp vals)
1105 ;; When cross-compiling Guile itself, the native Guile must generate
1106 ;; code for the host.
1107 (with-target %host-type
1109 (let ((p ((@ (system base compile) compile) exp
1110 #:env *dispatch-module*
1112 #:opts '(#:partial-eval? #f #:cse? #f))))
1118 ;; o/~ ten, nine, eight
1119 ;; sometimes that's just how it goes
1122 ;; get out before it blows o/~
1124 (define timer-init 30)
1125 (define (delayed-compile gf)
1126 (let ((timer timer-init))
1128 (set! timer (1- timer))
1131 (let ((dispatch (compute-dispatch-procedure
1132 gf (slot-ref gf 'effective-methods))))
1133 (slot-set! gf 'procedure dispatch)
1134 (apply dispatch args)))
1136 ;; interestingly, this catches recursive compilation attempts as
1137 ;; well; in that case, timer is negative
1138 (cache-dispatch gf args))))))
1140 (define (cache-dispatch gf args)
1141 (define (map-until n f ls)
1142 (if (or (zero? n) (null? ls))
1144 (cons (f (car ls)) (map-until (1- n) f (cdr ls)))))
1145 (define (equal? x y) ; can't use the stock equal? because it's a generic...
1146 (cond ((pair? x) (and (pair? y)
1147 (eq? (car x) (car y))
1148 (equal? (cdr x) (cdr y))))
1149 ((null? x) (null? y))
1151 (if (slot-ref gf 'n-specialized)
1152 (let ((types (map-until (slot-ref gf 'n-specialized) class-of args)))
1153 (let lp ((cache (slot-ref gf 'effective-methods)))
1154 (cond ((null? cache)
1155 (cache-miss gf args))
1156 ((equal? (vector-ref (car cache) 1) types)
1157 (apply (vector-ref (car cache) 3) args))
1158 (else (lp (cdr cache))))))
1159 (cache-miss gf args)))
1161 (define (cache-miss gf args)
1162 (apply (memoize-method! gf args) args))
1164 (define (memoize-effective-method! gf args applicable)
1165 (define (first-n ls n)
1166 (if (or (zero? n) (null? ls))
1168 (cons (car ls) (first-n (cdr ls) (- n 1)))))
1169 (define (parse n ls)
1171 (memoize n #f (map class-of args)))
1172 ((= n (slot-ref gf 'n-specialized))
1173 (memoize n #t (map class-of (first-n args n))))
1175 (parse (1+ n) (cdr ls)))))
1176 (define (memoize len rest? types)
1177 (let* ((cmethod (compute-cmethod applicable types))
1178 (cache (cons (vector len types rest? cmethod)
1179 (slot-ref gf 'effective-methods))))
1180 (slot-set! gf 'effective-methods cache)
1181 (slot-set! gf 'procedure (delayed-compile gf))
1186 ;;; Compiling next methods into method bodies
1189 ;;; So, for the reader: there basic idea is that, given that the
1190 ;;; semantics of `next-method' depend on the concrete types being
1191 ;;; dispatched, why not compile a specific procedure to handle each type
1192 ;;; combination that we see at runtime.
1194 ;;; In theory we can do much better than a bytecode compilation, because
1195 ;;; we know the *exact* types of the arguments. It's ideal for native
1196 ;;; compilation. A task for the future.
1198 ;;; I think this whole generic application mess would benefit from a
1201 (define (compute-cmethod methods types)
1202 (let ((make-procedure (slot-ref (car methods) 'make-procedure)))
1205 (if (null? (cdr methods))
1207 (no-next-method (method-generic-function (car methods)) args))
1208 (compute-cmethod (cdr methods) types)))
1209 (method-procedure (car methods)))))
1215 (define (memoize-method! gf args)
1216 (let ((applicable ((if (eq? gf compute-applicable-methods)
1217 %compute-applicable-methods
1218 compute-applicable-methods)
1221 (memoize-effective-method! gf args applicable))
1223 (no-applicable-method gf args)))))
1225 (set-procedure-property! memoize-method! 'system-procedure #t)
1227 (define no-applicable-method
1228 (make <generic> #:name 'no-applicable-method))
1232 ;; Then load the rest of GOOPS
1235 ;; FIXME: deprecate.
1236 (define min-fixnum (- (expt 2 29)))
1237 (define max-fixnum (- (expt 2 29) 1))
1242 (define (goops-error format-string . args)
1243 (scm-error 'goops-error #f format-string args '()))
1249 (define ensure-metaclass-with-supers
1250 (let ((table-of-metas '()))
1251 (lambda (meta-supers)
1252 (let ((entry (assoc meta-supers table-of-metas)))
1254 ;; Found a previously created metaclass
1256 ;; Create a new meta-class which inherit from "meta-supers"
1257 (let ((new (make <class> #:dsupers meta-supers
1259 #:name (gensym "metaclass"))))
1260 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
1263 (define (ensure-metaclass supers)
1266 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
1267 (all-cpls (append-map (lambda (m)
1268 (cdr (class-precedence-list m)))
1271 ;; Find the most specific metaclasses. The new metaclass will be
1272 ;; a subclass of these.
1275 (when (and (not (member meta all-cpls))
1276 (not (member meta needed-metas)))
1277 (set! needed-metas (append needed-metas (list meta)))))
1279 ;; Now return a subclass of the metaclasses we found.
1280 (if (null? (cdr needed-metas))
1281 (car needed-metas) ; If there's only one, just use it.
1282 (ensure-metaclass-with-supers needed-metas)))))
1288 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
1290 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
1291 ;;; OPTION ::= KEYWORD VALUE
1294 (define (make-class supers slots . options)
1295 (define (find-duplicate l)
1299 (if (memq head tail)
1301 (find-duplicate tail)))))
1303 (let* ((name (get-keyword #:name options (make-unbound)))
1304 (supers (if (not (or-map (lambda (class)
1306 (class-precedence-list class)))
1308 (append supers (list <object>))
1310 (metaclass (or (get-keyword #:metaclass options #f)
1311 (ensure-metaclass supers))))
1313 ;; Verify that all direct slots are different and that we don't inherit
1314 ;; several time from the same class
1315 (let ((tmp1 (find-duplicate supers))
1316 (tmp2 (find-duplicate (map slot-definition-name slots))))
1318 (goops-error "make-class: super class ~S is duplicate in class ~S"
1321 (goops-error "make-class: slot ~S is duplicate in class ~S"
1324 ;; Everything seems correct, build the class
1325 (apply make metaclass
1331 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
1333 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
1334 ;;; OPTION ::= KEYWORD VALUE
1336 (define-syntax class
1338 (define (parse-options options)
1339 (syntax-case options ()
1341 ((kw arg . options) (keyword? (syntax->datum #'kw))
1342 (with-syntax ((options (parse-options #'options)))
1343 (syntax-case #'kw ()
1345 #'(kw 'arg #:init-thunk (lambda () arg) . options))
1347 #'(kw arg . options)))))))
1348 (define (check-valid-kwargs args)
1349 (syntax-case args ()
1351 ((kw arg . args) (keyword? (syntax->datum #'kw))
1352 #`(kw arg . #,(check-valid-kwargs #'args)))))
1353 (define (parse-slots-and-kwargs args)
1354 (syntax-case args ()
1357 ((kw . _) (keyword? (syntax->datum #'kw))
1358 #`(() #,(check-valid-kwargs args)))
1359 (((name option ...) args ...)
1360 (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...)))
1361 ((option ...) (parse-options #'(option ...))))
1362 #'(((list 'name option ...) . slots) kwargs)))
1363 ((name args ...) (symbol? (syntax->datum #'name))
1364 (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...))))
1365 #'(('(name) . slots) kwargs)))))
1367 ((class (super ...) arg ...)
1368 (with-syntax ((((slot-def ...) (option ...))
1369 (parse-slots-and-kwargs #'(arg ...))))
1370 #'(make-class (list super ...)
1374 (define-syntax define-class-pre-definition
1377 ((_ (k arg rest ...) out ...)
1378 (keyword? (syntax->datum #'k))
1379 (case (syntax->datum #'k)
1380 ((#:getter #:setter)
1381 #'(define-class-pre-definition (rest ...)
1383 (when (or (not (defined? 'arg))
1384 (not (is-a? arg <generic>)))
1387 (ensure-generic (if (defined? 'arg) arg #f) 'arg)))))
1389 #'(define-class-pre-definition (rest ...)
1391 (when (or (not (defined? 'arg))
1392 (not (is-a? arg <accessor>)))
1395 (ensure-accessor (if (defined? 'arg) arg #f) 'arg)))))
1397 #'(define-class-pre-definition (rest ...) out ...))))
1399 #'(begin out ...)))))
1401 ;; Some slot options require extra definitions to be made. In
1402 ;; particular, we want to make sure that the generic function objects
1403 ;; which represent accessors exist before `make-class' tries to add
1405 (define-syntax define-class-pre-definitions
1410 ((_ (slot rest ...) out ...)
1411 (keyword? (syntax->datum #'slot))
1413 ((_ (slot rest ...) out ...)
1414 (identifier? #'slot)
1415 #'(define-class-pre-definitions (rest ...)
1417 ((_ ((slotname slotopt ...) rest ...) out ...)
1418 #'(define-class-pre-definitions (rest ...)
1419 out ... (define-class-pre-definition (slotopt ...)))))))
1421 (define-syntax-rule (define-class name supers slot ...)
1423 (define-class-pre-definitions (slot ...))
1424 (if (and (defined? 'name)
1425 (is-a? name <class>)
1426 (memq <object> (class-precedence-list name)))
1427 (class-redefinition name
1428 (class supers slot ... #:name 'name))
1429 (toplevel-define! 'name (class supers slot ... #:name 'name)))))
1431 (define-syntax-rule (standard-define-class arg ...)
1432 (define-class arg ...))
1435 ;;; {Generic functions and accessors}
1438 ;; Apparently the desired semantics are that we extend previous
1439 ;; procedural definitions, but that if `name' was already a generic, we
1440 ;; overwrite its definition.
1441 (define-syntax define-generic
1444 ((define-generic name) (symbol? (syntax->datum #'name))
1446 (if (and (defined? 'name) (is-a? name <generic>))
1447 (make <generic> #:name 'name)
1448 (ensure-generic (if (defined? 'name) name #f) 'name)))))))
1450 (define-syntax define-extended-generic
1453 ((define-extended-generic name val) (symbol? (syntax->datum #'name))
1454 #'(define name (make-extended-generic val 'name))))))
1456 (define-syntax define-extended-generics
1458 (define (id-append ctx a b)
1459 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
1461 ((define-extended-generic (name ...) #:prefix (prefix ...))
1462 (and (and-map symbol? (syntax->datum #'(name ...)))
1463 (and-map symbol? (syntax->datum #'(prefix ...))))
1464 (with-syntax ((((val ...)) (map (lambda (name)
1465 (map (lambda (prefix)
1466 (id-append name prefix name))
1470 (define-extended-generic name (list val ...))
1473 (define* (make-generic #:optional name)
1474 (make <generic> #:name name))
1476 (define* (make-extended-generic gfs #:optional name)
1477 (let* ((gfs (if (list? gfs) gfs (list gfs)))
1478 (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
1480 (let* ((sname (and name (make-setter-name name)))
1482 (append-map (lambda (gf)
1483 (if (is-a? gf <generic-with-setter>)
1484 (list (ensure-generic (setter gf)
1488 (es (make <extended-generic-with-setter>
1491 #:setter (make <extended-generic>
1493 #:extends setters))))
1494 (extended-by! setters (setter es))
1496 (make <extended-generic>
1499 (extended-by! gfs ans)
1502 (define (extended-by! gfs eg)
1503 (for-each (lambda (gf)
1504 (slot-set! gf 'extended-by
1505 (cons eg (slot-ref gf 'extended-by))))
1507 (invalidate-method-cache! eg))
1509 (define (not-extended-by! gfs eg)
1510 (for-each (lambda (gf)
1511 (slot-set! gf 'extended-by
1512 (delq! eg (slot-ref gf 'extended-by))))
1514 (invalidate-method-cache! eg))
1516 (define* (ensure-generic old-definition #:optional name)
1517 (cond ((is-a? old-definition <generic>) old-definition)
1518 ((procedure-with-setter? old-definition)
1519 (make <generic-with-setter>
1521 #:default (procedure old-definition)
1522 #:setter (setter old-definition)))
1523 ((procedure? old-definition)
1524 (if (generic-capability? old-definition) old-definition
1525 (make <generic> #:name name #:default old-definition)))
1526 (else (make <generic> #:name name))))
1528 ;; same semantics as <generic>
1529 (define-syntax-rule (define-accessor name)
1531 (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
1532 ((is-a? name <accessor>) (make <accessor> #:name 'name))
1533 (else (ensure-accessor name 'name)))))
1535 (define (make-setter-name name)
1536 (string->symbol (string-append "setter:" (symbol->string name))))
1538 (define* (make-accessor #:optional name)
1541 #:setter (make <generic>
1542 #:name (and name (make-setter-name name)))))
1544 (define* (ensure-accessor proc #:optional name)
1545 (cond ((and (is-a? proc <accessor>)
1546 (is-a? (setter proc) <generic>))
1548 ((is-a? proc <generic-with-setter>)
1549 (upgrade-accessor proc (setter proc)))
1550 ((is-a? proc <generic>)
1551 (upgrade-accessor proc (make-generic name)))
1552 ((procedure-with-setter? proc)
1555 #:default (procedure proc)
1556 #:setter (ensure-generic (setter proc) name)))
1558 (ensure-accessor (if (generic-capability? proc)
1559 (make <generic> #:name name #:default proc)
1560 (ensure-generic proc name))
1563 (make-accessor name))))
1565 (define (upgrade-accessor generic setter)
1566 (let ((methods (slot-ref generic 'methods))
1567 (gws (make (if (is-a? generic <extended-generic>)
1568 <extended-generic-with-setter>
1570 #:name (generic-function-name generic)
1571 #:extended-by (slot-ref generic 'extended-by)
1573 (when (is-a? generic <extended-generic>)
1574 (let ((gfs (slot-ref generic 'extends)))
1575 (not-extended-by! gfs generic)
1576 (slot-set! gws 'extends gfs)
1577 (extended-by! gfs gws)))
1578 ;; Steal old methods
1579 (for-each (lambda (method)
1580 (slot-set! method 'generic-function gws))
1582 (slot-set! gws 'methods methods)
1583 (invalidate-method-cache! gws)
1590 ;; Note: `a' and `b' can have unequal lengths (i.e. one can be one
1591 ;; element longer than the other when we have a dotted parameter
1592 ;; list). For instance, with the call
1598 ;; (define-method M (a . l) ....)
1599 ;; (define-method M (a) ....)
1601 ;; we consider that the second method is more specific.
1603 ;; Precondition: `a' and `b' are methods and are applicable to `types'.
1604 (define (%method-more-specific? a b types)
1605 (let lp ((a-specializers (method-specializers a))
1606 (b-specializers (method-specializers b))
1609 ;; (a) less specific than (a b ...) or (a . b)
1610 ((null? a-specializers) #t)
1611 ;; (a b ...) or (a . b) less specific than (a)
1612 ((null? b-specializers) #f)
1613 ;; (a . b) less specific than (a b ...)
1614 ((not (pair? a-specializers)) #f)
1615 ;; (a b ...) more specific than (a . b)
1616 ((not (pair? b-specializers)) #t)
1618 (let ((a-specializer (car a-specializers))
1619 (b-specializer (car b-specializers))
1620 (a-specializers (cdr a-specializers))
1621 (b-specializers (cdr b-specializers))
1623 (types (cdr types)))
1624 (if (eq? a-specializer b-specializer)
1625 (lp a-specializers b-specializers types)
1626 (let lp ((cpl (class-precedence-list type)))
1627 (let ((elt (car cpl)))
1629 ((eq? a-specializer elt) #t)
1630 ((eq? b-specializer elt) #f)
1631 (else (lp (cdr cpl))))))))))))
1633 (define (%sort-applicable-methods methods types)
1634 (sort methods (lambda (a b) (%method-more-specific? a b types))))
1636 (define (generic-function-methods obj)
1637 "Return the methods of the generic function @var{obj}."
1638 (define (fold-upward method-lists gf)
1640 ((is-a? gf <extended-generic>)
1641 (let lp ((method-lists method-lists) (gfs (slot-ref gf 'extends)))
1645 (lp (fold-upward (cons (slot-ref gf 'methods) method-lists) gf)
1647 (else method-lists)))
1648 (define (fold-downward method-lists gf)
1649 (let lp ((method-lists (cons (slot-ref gf 'methods) method-lists))
1650 (gfs (slot-ref gf 'extended-by)))
1654 (lp (fold-downward method-lists gf) gfs)))))
1655 (unless (is-a? obj <generic>)
1656 (scm-error 'wrong-type-arg #f "Not a generic: ~S"
1658 (concatenate (fold-downward (fold-upward '() obj) obj)))
1660 (define (%compute-applicable-methods gf args)
1661 (define (method-applicable? m types)
1662 (let lp ((specs (method-specializers m)) (types types))
1664 ((null? specs) (null? types))
1665 ((not (pair? specs)) #t)
1668 (and (memq (car specs) (class-precedence-list (car types)))
1669 (lp (cdr specs) (cdr types)))))))
1670 (let ((n (length args))
1671 (types (map class-of args)))
1672 (let lp ((methods (generic-function-methods gf))
1675 (and (not (null? applicable))
1676 (%sort-applicable-methods applicable types))
1677 (let ((m (car methods)))
1679 (if (method-applicable? m types)
1683 (define compute-applicable-methods %compute-applicable-methods)
1685 (define (toplevel-define! name val)
1686 (module-define! (current-module) name val))
1688 (define-syntax define-method
1689 (syntax-rules (setter)
1690 ((_ ((setter name) . args) body ...)
1692 (when (or (not (defined? 'name))
1693 (not (is-a? name <accessor>)))
1694 (toplevel-define! 'name
1696 (if (defined? 'name) name #f) 'name)))
1697 (add-method! (setter name) (method args body ...))))
1698 ((_ (name . args) body ...)
1700 ;; FIXME: this code is how it always was, but it's quite cracky:
1701 ;; it will only define the generic function if it was undefined
1702 ;; before (ok), or *was defined to #f*. The latter is crack. But
1703 ;; there are bootstrap issues about fixing this -- change it to
1704 ;; (is-a? name <generic>) and see.
1705 (when (or (not (defined? 'name))
1707 (toplevel-define! 'name (make <generic> #:name 'name)))
1708 (add-method! name (method args body ...))))))
1710 (define-syntax method
1712 (define (parse-args args)
1713 (let lp ((ls args) (formals '()) (specializers '()))
1716 (and (identifier? #'f) (identifier? #'s))
1719 (cons #'s specializers)))
1724 (cons #'<top> specializers)))
1726 (list (reverse formals)
1727 (reverse (cons #''() specializers))))
1729 (identifier? #'tail)
1730 (list (append (reverse formals) #'tail)
1731 (reverse (cons #'<top> specializers)))))))
1733 (define (find-free-id exp referent)
1736 (or (find-free-id #'x referent)
1737 (find-free-id #'y referent)))
1740 (let ((id (datum->syntax #'x referent)))
1741 (and (free-identifier=? #'x id) id)))
1744 (define (compute-procedure formals body)
1745 (syntax-case body ()
1747 (with-syntax ((formals formals))
1748 #'(lambda formals body0 ...)))))
1750 (define (->proper args)
1751 (let lp ((ls args) (out '()))
1753 ((x . xs) (lp #'xs (cons #'x out)))
1755 (tail (reverse (cons #'tail out))))))
1757 (define (compute-make-procedure formals body next-method)
1758 (syntax-case body ()
1760 (with-syntax ((next-method next-method))
1761 (syntax-case formals ()
1763 #'(lambda (real-next-method)
1764 (lambda (formal ...)
1765 (let ((next-method (lambda args
1767 (real-next-method formal ...)
1768 (apply real-next-method args)))))
1771 (with-syntax (((formal ...) (->proper #'formals)))
1772 #'(lambda (real-next-method)
1774 (let ((next-method (lambda args
1776 (apply real-next-method formal ...)
1777 (apply real-next-method args)))))
1780 (define (compute-procedures formals body)
1781 ;; So, our use of this is broken, because it operates on the
1782 ;; pre-expansion source code. It's equivalent to just searching
1783 ;; for referent in the datums. Ah well.
1784 (let ((id (find-free-id body 'next-method)))
1786 ;; return a make-procedure
1788 (compute-make-procedure formals body id))
1789 (values (compute-procedure formals body)
1793 ((_ args) #'(method args (if #f #f)))
1794 ((_ args body0 body1 ...)
1795 (with-syntax (((formals (specializer ...)) (parse-args #'args)))
1798 (compute-procedures #'formals #'(body0 body1 ...)))
1799 (lambda (procedure make-procedure)
1800 (with-syntax ((procedure procedure)
1801 (make-procedure make-procedure))
1803 #:specializers (cons* specializer ...)
1805 #:body '(body0 body1 ...)
1806 #:make-procedure make-procedure
1807 #:procedure procedure)))))))))
1812 ;;; These are useful when dealing with method specializers, which might
1813 ;;; have a rest argument.
1816 (define (map* fn . l) ; A map which accepts dotted lists (arg lists
1817 (cond ; must be "isomorph"
1818 ((null? (car l)) '())
1819 ((pair? (car l)) (cons (apply fn (map car l))
1820 (apply map* fn (map cdr l))))
1821 (else (apply fn l))))
1823 (define (for-each* fn . l) ; A for-each which accepts dotted lists (arg lists
1824 (cond ; must be "isomorph"
1825 ((null? (car l)) '())
1826 ((pair? (car l)) (apply fn (map car l)) (apply for-each* fn (map cdr l)))
1827 (else (apply fn l))))
1829 (define (length* ls)
1832 ((not (pair? ls)) n)))
1838 (define (add-method-in-classes! m)
1839 ;; Add method in all the classes which appears in its specializers list
1840 (for-each* (lambda (x)
1841 (let ((dm (class-direct-methods x)))
1843 (struct-set! x class-index-direct-methods (cons m dm)))))
1844 (method-specializers m)))
1846 (define (remove-method-in-classes! m)
1847 ;; Remove method in all the classes which appears in its specializers list
1848 (for-each* (lambda (x)
1850 class-index-direct-methods
1851 (delv! m (class-direct-methods x))))
1852 (method-specializers m)))
1854 (define (compute-new-list-of-methods gf new)
1855 (let ((new-spec (method-specializers new))
1856 (methods (slot-ref gf 'methods)))
1857 (let loop ((l methods))
1860 (if (equal? (method-specializers (car l)) new-spec)
1862 ;; This spec. list already exists. Remove old method from dependents
1863 (remove-method-in-classes! (car l))
1868 (define (method-n-specializers m)
1869 (length* (slot-ref m 'specializers)))
1871 (define (calculate-n-specialized gf)
1872 (fold (lambda (m n) (max n (method-n-specializers m)))
1874 (generic-function-methods gf)))
1876 (define (invalidate-method-cache! gf)
1877 (%invalidate-method-cache! gf)
1878 (slot-set! gf 'n-specialized (calculate-n-specialized gf))
1879 (for-each (lambda (gf) (invalidate-method-cache! gf))
1880 (slot-ref gf 'extended-by)))
1882 (define internal-add-method!
1883 (method ((gf <generic>) (m <method>))
1884 (slot-set! m 'generic-function gf)
1885 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
1886 (invalidate-method-cache! gf)
1887 (add-method-in-classes! m)
1890 (define-generic add-method!)
1892 ((method-procedure internal-add-method!) add-method! internal-add-method!)
1894 (define-method (add-method! (proc <procedure>) (m <method>))
1895 (if (generic-capability? proc)
1897 (enable-primitive-generic! proc)
1898 (add-method! proc m))
1901 (define-method (add-method! (pg <primitive-generic>) (m <method>))
1902 (add-method! (primitive-generic-generic pg) m))
1904 (define-method (add-method! obj (m <method>))
1905 (goops-error "~S is not a valid generic function" obj))
1908 ;;; {Access to meta objects}
1914 (define-method (method-source (m <method>))
1915 (let* ((spec (map* class-name (slot-ref m 'specializers)))
1916 (src (procedure-source (slot-ref m 'procedure))))
1918 (let ((args (cadr src))
1921 (cons (map* list args spec)
1924 (define-method (method-formals (m <method>))
1925 (slot-ref m 'formals))
1930 (define slot-definition-name car)
1932 (define slot-definition-options cdr)
1934 (define (slot-definition-allocation s)
1935 (get-keyword #:allocation (cdr s) #:instance))
1937 (define (slot-definition-getter s)
1938 (get-keyword #:getter (cdr s) #f))
1940 (define (slot-definition-setter s)
1941 (get-keyword #:setter (cdr s) #f))
1943 (define (slot-definition-accessor s)
1944 (get-keyword #:accessor (cdr s) #f))
1946 (define (slot-definition-init-value s)
1947 ;; can be #f, so we can't use #f as non-value
1948 (get-keyword #:init-value (cdr s) (make-unbound)))
1950 (define (slot-definition-init-form s)
1951 (get-keyword #:init-form (cdr s) (make-unbound)))
1953 (define (slot-definition-init-thunk s)
1954 (get-keyword #:init-thunk (cdr s) #f))
1956 (define (slot-definition-init-keyword s)
1957 (get-keyword #:init-keyword (cdr s) #f))
1959 (define (class-slot-definition class slot-name)
1960 (assq slot-name (class-slots class)))
1962 (define (slot-init-function class slot-name)
1963 (cadr (assq slot-name (struct-ref class class-index-getters-n-setters))))
1965 (define (accessor-method-slot-definition obj)
1966 "Return the slot definition of the accessor @var{obj}."
1967 (slot-ref obj 'slot-definition))
1971 ;;; {Standard methods used by the C runtime}
1974 ;;; Methods to compare objects
1977 ;; Have to do this in a strange order because equal? is used in the
1978 ;; add-method! implementation; we need to make sure that when the
1979 ;; primitive is extended, that the generic has a method. =
1980 (define g-equal? (make-generic 'equal?))
1981 ;; When this generic gets called, we will have already checked eq? and
1982 ;; eqv? -- the purpose of this generic is to extend equality. So by
1983 ;; default, there is no extension, thus the #f return.
1984 (add-method! g-equal? (method (x y) #f))
1985 (set-primitive-generic! equal? g-equal?)
1988 ;;; methods to display/write an object
1991 ; Code for writing objects must test that the slots they use are
1992 ; bound. Otherwise a slot-unbound method will be called and will
1993 ; conduct to an infinite loop.
1996 (define (display-address o file)
1997 (display (number->string (object-address o) 16) file))
1999 (define-method (write o file)
2000 (display "#<instance " file)
2001 (display-address o file)
2004 (define write-object (primitive-generic-generic write))
2006 (define-method (write (o <object>) file)
2007 (let ((class (class-of o)))
2008 (if (slot-bound? class 'name)
2011 (display (class-name class) file)
2012 (display #\space file)
2013 (display-address o file)
2017 (define-method (write (class <class>) file)
2018 (let ((meta (class-of class)))
2019 (if (and (slot-bound? class 'name)
2020 (slot-bound? meta 'name))
2023 (display (class-name meta) file)
2024 (display #\space file)
2025 (display (class-name class) file)
2026 (display #\space file)
2027 (display-address class file)
2031 (define-method (write (gf <generic>) file)
2032 (let ((meta (class-of gf)))
2033 (if (and (slot-bound? meta 'name)
2034 (slot-bound? gf 'methods))
2037 (display (class-name meta) file)
2038 (let ((name (generic-function-name gf)))
2041 (display #\space file)
2042 (display name file))))
2044 (display (length (generic-function-methods gf)) file)
2045 (display ")>" file))
2048 (define-method (write (o <method>) file)
2049 (let ((meta (class-of o)))
2050 (if (and (slot-bound? meta 'name)
2051 (slot-bound? o 'specializers))
2054 (display (class-name meta) file)
2055 (display #\space file)
2056 (display (map* (lambda (spec)
2057 (if (slot-bound? spec 'name)
2058 (slot-ref spec 'name)
2060 (method-specializers o))
2062 (display #\space file)
2063 (display-address o file)
2067 ;; Display (do the same thing as write by default)
2068 (define-method (display o file)
2069 (write-object o file))
2072 ;;; Handling of duplicate bindings in the module system
2075 (define (find-subclass super name)
2076 (let lp ((classes (class-direct-subclasses super)))
2079 (error "class not found" name))
2080 ((and (slot-bound? (car classes) 'name)
2081 (eq? (class-name (car classes)) name))
2084 (lp (cdr classes))))))
2087 (define <module> (find-subclass <top> '<module>))
2089 (define-method (merge-generics (module <module>)
2099 (define-method (merge-generics (module <module>)
2107 (and (not (eq? val1 val2))
2108 (make-variable (make-extended-generic (list val2 val1) name))))
2110 (define-method (merge-generics (module <module>)
2117 (gf <extended-generic>))
2118 (and (not (memq val2 (slot-ref gf 'extends)))
2122 (cons val2 (delq! val2 (slot-ref gf 'extends))))
2125 (cons gf (delq! gf (slot-ref val2 'extended-by))))
2126 (invalidate-method-cache! gf)
2129 (module-define! duplicate-handlers 'merge-generics merge-generics)
2131 (define-method (merge-accessors (module <module>)
2141 (define-method (merge-accessors (module <module>)
2149 (merge-generics module name int1 val1 int2 val2 var val))
2151 (module-define! duplicate-handlers 'merge-accessors merge-accessors)
2157 (define (class-slot-g-n-s class slot-name)
2158 (let* ((this-slot (assq slot-name (struct-ref class class-index-slots)))
2159 (getters-n-setters (struct-ref class class-index-getters-n-setters))
2160 (g-n-s (cddr (or (assq slot-name getters-n-setters)
2161 (slot-missing class slot-name)))))
2162 (unless (memq (slot-definition-allocation this-slot)
2163 '(#:class #:each-subclass))
2164 (slot-missing class slot-name))
2167 (define (class-slot-ref class slot)
2168 (let ((x ((car (class-slot-g-n-s class slot)) #f)))
2170 (slot-unbound class slot)
2173 (define (class-slot-set! class slot value)
2174 ((cadr (class-slot-g-n-s class slot)) #f value))
2176 (define-method (slot-unbound (c <class>) (o <object>) s)
2177 (goops-error "Slot `~S' is unbound in object ~S" s o))
2179 (define-method (slot-unbound (c <class>) s)
2180 (goops-error "Slot `~S' is unbound in class ~S" s c))
2182 (define-method (slot-unbound (o <object>))
2183 (goops-error "Unbound slot in object ~S" o))
2185 (define-method (slot-missing (c <class>) (o <object>) s)
2186 (goops-error "No slot with name `~S' in object ~S" s o))
2188 (define-method (slot-missing (c <class>) s)
2189 (goops-error "No class slot with name `~S' in class ~S" s c))
2192 (define-method (slot-missing (c <class>) (o <object>) s value)
2193 (slot-missing c o s))
2195 ;;; Methods for the possible error we can encounter when calling a gf
2197 (define-method (no-next-method (gf <generic>) args)
2198 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
2200 (define-method (no-applicable-method (gf <generic>) args)
2201 (goops-error "No applicable method for ~S in call ~S"
2202 gf (cons (generic-function-name gf) args)))
2204 (define-method (no-method (gf <generic>) args)
2205 (goops-error "No method defined for ~S" gf))
2208 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
2211 (define-method (shallow-clone (self <object>))
2212 (let* ((class (class-of self))
2213 (clone (%allocate-instance class))
2214 (slots (map slot-definition-name (class-slots class))))
2215 (for-each (lambda (slot)
2216 (when (slot-bound? self slot)
2217 (slot-set! clone slot (slot-ref self slot))))
2221 (define-method (deep-clone (self <object>))
2222 (let* ((class (class-of self))
2223 (clone (%allocate-instance class))
2224 (slots (map slot-definition-name (class-slots class))))
2225 (for-each (lambda (slot)
2226 (when (slot-bound? self slot)
2227 (slot-set! clone slot
2228 (let ((value (slot-ref self slot)))
2229 (if (instance? value)
2236 ;;; {Class redefinition utilities}
2239 ;;; (class-redefinition OLD NEW)
2242 ;;; Has correct the following conditions:
2246 ;;; 1. New accessor specializers refer to new header
2250 ;;; 1. New class cpl refers to the new class header
2251 ;;; 2. Old class header exists on old super classes direct-subclass lists
2252 ;;; 3. New class header exists on new super classes direct-subclass lists
2254 (define-method (class-redefinition (old <class>) (new <class>))
2255 ;; Work on direct methods:
2256 ;; 1. Remove accessor methods from the old class
2257 ;; 2. Patch the occurences of new in the specializers by old
2258 ;; 3. Displace the methods from old to new
2259 (remove-class-accessors! old) ;; -1-
2260 (let ((methods (class-direct-methods new)))
2261 (for-each (lambda (m)
2262 (update-direct-method! m new old)) ;; -2-
2265 class-index-direct-methods
2266 (append methods (class-direct-methods old))))
2268 ;; Substitute old for new in new cpl
2269 (set-car! (struct-ref new class-index-cpl) old)
2271 ;; Remove the old class from the direct-subclasses list of its super classes
2272 (for-each (lambda (c) (struct-set! c class-index-direct-subclasses
2273 (delv! old (class-direct-subclasses c))))
2274 (class-direct-supers old))
2276 ;; Replace the new class with the old in the direct-subclasses of the supers
2277 (for-each (lambda (c)
2278 (struct-set! c class-index-direct-subclasses
2279 (cons old (delv! new (class-direct-subclasses c)))))
2280 (class-direct-supers new))
2282 ;; Swap object headers
2283 (%modify-class old new)
2287 ;; Redefine all the subclasses of old to take into account modification
2290 (update-direct-subclass! c new old))
2291 (class-direct-subclasses new))
2293 ;; Invalidate class so that subsequent instances slot accesses invoke
2294 ;; change-object-class
2295 (struct-set! new class-index-redefined old)
2296 (class-clear-flags! new vtable-flag-goops-valid) ;must come after slot-set!
2301 ;;; remove-class-accessors!
2304 (define-method (remove-class-accessors! (c <class>))
2305 (for-each (lambda (m)
2306 (when (is-a? m <accessor-method>)
2307 (let ((gf (slot-ref m 'generic-function)))
2308 ;; remove the method from its GF
2309 (slot-set! gf 'methods
2310 (delq1! m (slot-ref gf 'methods)))
2311 (invalidate-method-cache! gf)
2312 ;; remove the method from its specializers
2313 (remove-method-in-classes! m))))
2314 (class-direct-methods c)))
2317 ;;; update-direct-method!
2320 (define-method (update-direct-method! (m <method>)
2323 (let loop ((l (method-specializers m)))
2324 ;; Note: the <top> in dotted list is never used.
2325 ;; So we can work as if we had only proper lists.
2327 (when (eqv? (car l) old)
2332 ;;; update-direct-subclass!
2335 (define-method (update-direct-subclass! (c <class>)
2338 (class-redefinition c
2339 (make-class (class-direct-supers c)
2340 (class-direct-slots c)
2341 #:name (class-name c)
2342 #:metaclass (class-of c))))
2345 ;;; {Utilities for INITIALIZE methods}
2348 ;;; compute-slot-accessors
2350 (define (compute-slot-accessors class slots)
2353 (let ((getter-function (slot-definition-getter s))
2354 (setter-function (slot-definition-setter s))
2355 (accessor (slot-definition-accessor s)))
2357 (add-method! getter-function
2358 (compute-getter-method class g-n-s)))
2360 (add-method! setter-function
2361 (compute-setter-method class g-n-s)))
2364 (add-method! accessor
2365 (compute-getter-method class g-n-s))
2366 (add-method! (setter accessor)
2367 (compute-setter-method class g-n-s))))))
2368 slots (struct-ref class class-index-getters-n-setters)))
2370 (define-method (compute-getter-method (class <class>) slotdef)
2371 (let ((init-thunk (cadr slotdef))
2372 (g-n-s (cddr slotdef)))
2373 (make <accessor-method>
2374 #:specializers (list class)
2375 #:procedure (cond ((pair? g-n-s)
2376 (make-generic-bound-check-getter (car g-n-s)))
2378 (standard-get g-n-s))
2380 (bound-check-get g-n-s)))
2381 #:slot-definition slotdef)))
2383 (define-method (compute-setter-method (class <class>) slotdef)
2384 (let ((g-n-s (cddr slotdef)))
2385 (make <accessor-method>
2386 #:specializers (list class <top>)
2387 #:procedure (if (pair? g-n-s)
2389 (standard-set g-n-s))
2390 #:slot-definition slotdef)))
2392 (define (make-generic-bound-check-getter proc)
2394 (let ((val (proc o)))
2399 ;;; Pre-generate getters and setters for the first 20 slots.
2400 (define-syntax define-standard-accessor-method
2402 (define num-standard-pre-cache 20)
2404 ((_ ((proc n) arg ...) body)
2406 (let ((cache (vector #,@(map (lambda (n*)
2410 (iota num-standard-pre-cache)))))
2412 (if (< n #,num-standard-pre-cache)
2413 (vector-ref cache n)
2414 (lambda (arg ...) body)))))))))
2416 (define-standard-accessor-method ((bound-check-get n) o)
2417 (let ((x (struct-ref o n)))
2422 (define-standard-accessor-method ((standard-get n) o)
2425 (define-standard-accessor-method ((standard-set n) o v)
2426 (struct-set! o n v))
2428 ;;; compute-getters-n-setters
2430 (define (compute-getters-n-setters class slots)
2432 (define (compute-slot-init-function name s)
2433 (or (let ((thunk (slot-definition-init-thunk s)))
2437 (goops-error "Bad init-thunk for slot `~S' in ~S: ~S"
2438 name class thunk))))
2439 (let ((init (slot-definition-init-value s)))
2440 (and (not (unbound? init))
2441 (lambda () init)))))
2443 (define (verify-accessors slot l)
2444 (cond ((integer? l))
2445 ((not (and (list? l) (= (length l) 2)))
2446 (goops-error "Bad getter and setter for slot `~S' in ~S: ~S"
2451 (unless (procedure? get)
2452 (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
2454 (unless (procedure? set)
2455 (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
2456 slot class set))))))
2459 ;; The strange treatment of nfields is due to backward compatibility.
2460 (let* ((index (slot-ref class 'nfields))
2461 (g-n-s (compute-get-n-set class s))
2462 (size (- (slot-ref class 'nfields) index))
2463 (name (slot-definition-name s)))
2464 ;; NOTE: The following is interdependent with C macros
2465 ;; defined above goops.c:scm_sys_prep_layout_x.
2467 ;; For simple instance slots, we have the simplest form
2468 ;; '(name init-function . index)
2469 ;; For other slots we have
2470 ;; '(name init-function getter setter . alloc)
2472 ;; '(index size) for instance allocated slots
2473 ;; '() for other slots
2474 (verify-accessors name g-n-s)
2475 (case (slot-definition-allocation s)
2476 ((#:each-subclass #:class)
2477 (unless (and (zero? size) (pair? g-n-s))
2478 (error "Class-allocated slots should not reserve fields"))
2479 ;; Don't initialize the slot; that's handled when the slot
2480 ;; is allocated, in compute-get-n-set.
2481 (cons name (cons #f g-n-s)))
2484 (cons (compute-slot-init-function name s)
2485 (if (or (integer? g-n-s)
2488 (append g-n-s (list index size)))))))))
2494 ;; Replace the bootstrap compute-cpl with this definition.
2496 (make <generic> #:name 'compute-cpl))
2498 (define-method (compute-cpl (class <class>))
2499 (compute-std-cpl class class-direct-supers))
2501 ;;; compute-get-n-set
2503 (define-method (compute-get-n-set (class <class>) s)
2504 (define (class-slot-init-value)
2505 (let ((thunk (slot-definition-init-thunk s)))
2508 (slot-definition-init-value s))))
2510 (case (slot-definition-allocation s)
2511 ((#:instance) ;; Instance slot
2512 ;; get-n-set is just its offset
2513 (let ((already-allocated (struct-ref class class-index-nfields)))
2514 (struct-set! class class-index-nfields (+ already-allocated 1))
2517 ((#:class) ;; Class slot
2518 ;; Class-slots accessors are implemented as 2 closures around
2519 ;; a Scheme variable. As instance slots, class slots must be
2520 ;; unbound at init time.
2521 (let ((name (slot-definition-name s)))
2522 (if (memq name (map slot-definition-name (class-direct-slots class)))
2523 ;; This slot is direct; create a new shared variable
2524 (make-closure-variable class (class-slot-init-value))
2525 ;; Slot is inherited. Find its definition in superclass
2526 (let loop ((l (cdr (class-precedence-list class))))
2527 (let ((r (assoc name
2529 class-index-getters-n-setters))))
2532 (loop (cdr l))))))))
2534 ((#:each-subclass) ;; slot shared by instances of direct subclass.
2535 ;; (Thomas Buerger, April 1998)
2536 (make-closure-variable class (class-slot-init-value)))
2538 ((#:virtual) ;; No allocation
2539 ;; slot-ref and slot-set! function must be given by the user
2540 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
2541 (set (get-keyword #:slot-set! (slot-definition-options s) #f)))
2542 (unless (and get set)
2543 (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S" s))
2545 (else (next-method))))
2547 (define (make-closure-variable class value)
2548 (list (lambda (o) value)
2549 (lambda (o v) (set! value v))))
2551 (define-method (compute-get-n-set (o <object>) s)
2552 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
2554 (define-method (compute-slots (class <class>))
2555 (build-slots-list (class-direct-slots class)
2556 (class-precedence-list class)))
2562 (define *unbound* (make-unbound))
2564 ;; FIXME: This could be much more efficient.
2565 (define (%initialize-object obj initargs)
2566 "Initialize the object @var{obj} with the given arguments
2568 (unless (instance? obj)
2569 (scm-error 'wrong-type-arg #f "Not an object: ~S"
2571 (unless (even? (length initargs))
2572 (scm-error 'wrong-type-arg #f "Initargs has odd length: ~S"
2573 (list initargs) #f))
2574 (let ((class (class-of obj)))
2575 (define (get-initarg kw)
2577 (get-keyword kw initargs *unbound*)
2579 (let lp ((get-n-set (struct-ref class class-index-getters-n-setters))
2580 (slots (struct-ref class class-index-slots)))
2583 (((name . options) . slots)
2585 (((_ init-thunk . _) . get-n-set)
2586 (let ((initarg (get-initarg (get-keyword #:init-keyword options))))
2588 ((not (unbound? initarg))
2589 (slot-set! obj name initarg))
2591 (slot-set! obj name (init-thunk)))))
2592 (lp get-n-set slots))))))))
2594 (define-method (initialize (object <object>) initargs)
2595 (%initialize-object object initargs))
2597 (define-method (initialize (class <class>) initargs)
2599 (let ((dslots (get-keyword #:slots initargs '()))
2600 (supers (get-keyword #:dsupers initargs '())))
2601 (class-add-flags! class (logior vtable-flag-goops-class
2602 vtable-flag-goops-valid))
2603 (let ((name (get-keyword #:name initargs '???)))
2604 (struct-set! class class-index-name name))
2605 (struct-set! class class-index-nfields 0)
2606 (struct-set! class class-index-direct-supers supers)
2607 (struct-set! class class-index-direct-slots dslots)
2608 (struct-set! class class-index-direct-subclasses '())
2609 (struct-set! class class-index-direct-methods '())
2610 (struct-set! class class-index-cpl (compute-cpl class))
2611 (struct-set! class class-index-redefined #f)
2612 (let ((slots (compute-slots class)))
2613 (struct-set! class class-index-slots slots)
2614 (let ((getters-n-setters (compute-getters-n-setters class slots)))
2615 (struct-set! class class-index-getters-n-setters getters-n-setters))
2616 ;; Build getters - setters - accessors
2617 (compute-slot-accessors class slots))
2619 ;; Update the "direct-subclasses" of each inherited classes
2620 (for-each (lambda (x)
2621 (let ((dsubs (struct-ref x class-index-direct-subclasses)))
2622 (struct-set! x class-index-direct-subclasses
2623 (cons class dsubs))))
2626 ;; Compute struct layout of instances, set the `layout' slot, and
2627 ;; update class flags.
2628 (%prep-layout! class)))
2630 (define (initialize-object-procedure object initargs)
2631 (let ((proc (get-keyword #:procedure initargs #f)))
2634 (apply slot-set! object 'procedure proc))
2636 (slot-set! object 'procedure proc)))))
2638 (define-method (initialize (applicable-struct <applicable-struct>) initargs)
2640 (initialize-object-procedure applicable-struct initargs))
2642 (define-method (initialize (applicable-struct <applicable-struct-with-setter>)
2645 (slot-set! applicable-struct 'setter (get-keyword #:setter initargs #f)))
2647 (define-method (initialize (generic <generic>) initargs)
2648 (let ((previous-definition (get-keyword #:default initargs #f))
2649 (name (get-keyword #:name initargs #f)))
2651 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
2653 (apply previous-definition args)))
2656 (set-procedure-property! generic 'name name))
2657 (invalidate-method-cache! generic)))
2659 (define-method (initialize (eg <extended-generic>) initargs)
2661 (slot-set! eg 'extends (get-keyword #:extends initargs '())))
2663 (define dummy-procedure (lambda args *unspecified*))
2665 (define-method (initialize (method <method>) initargs)
2667 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
2668 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
2669 (slot-set! method 'procedure
2670 (get-keyword #:procedure initargs #f))
2671 (slot-set! method 'formals (get-keyword #:formals initargs '()))
2672 (slot-set! method 'body (get-keyword #:body initargs '()))
2673 (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
2680 (define (change-object-class old-instance old-class new-class)
2681 (let ((new-instance (allocate-instance new-class '())))
2682 ;; Initialize the slots of the new instance
2683 (for-each (lambda (slot)
2684 (if (and (slot-exists-using-class? old-class old-instance slot)
2685 (eq? (slot-definition-allocation
2686 (class-slot-definition old-class slot))
2688 (slot-bound-using-class? old-class old-instance slot))
2689 ;; Slot was present and allocated in old instance; copy it
2690 (slot-set-using-class!
2694 (slot-ref-using-class old-class old-instance slot))
2695 ;; slot was absent; initialize it with its default value
2696 (let ((init (slot-init-function new-class slot)))
2698 (slot-set-using-class!
2702 (apply init '()))))))
2703 (map slot-definition-name (class-slots new-class)))
2704 ;; Exchange old and new instance in place to keep pointers valid
2705 (%modify-instance old-instance new-instance)
2706 ;; Allow class specific updates of instances (which now are swapped)
2707 (update-instance-for-different-class new-instance old-instance)
2711 (define-method (update-instance-for-different-class (old-instance <object>)
2714 ;;not really important what we do, we just need a default method
2717 (define-method (change-class (old-instance <object>) (new-class <class>))
2718 (change-object-class old-instance (class-of old-instance) new-class))
2723 ;;; A new definition which overwrites the previous one which was built-in
2726 (define-method (allocate-instance (class <class>) initargs)
2727 (%allocate-instance class))
2729 (define-method (make-instance (class <class>) . initargs)
2730 (let ((instance (allocate-instance class initargs)))
2731 (initialize instance initargs)
2734 (define make make-instance)
2739 ;;; Protocol for calling generic functions, intended to be used when
2740 ;;; applying subclasses of <generic> and <generic-with-setter>. The
2741 ;;; code below is similar to the first MOP described in AMOP.
2743 ;;; Note that standard generic functions dispatch only on the classes of
2744 ;;; the arguments, and the result of such dispatch can be memoized. The
2745 ;;; `cache-dispatch' routine implements this. `apply-generic' isn't
2746 ;;; called currently; the generic function MOP was never fully
2747 ;;; implemented in GOOPS. However now that GOOPS is implemented
2748 ;;; entirely in Scheme (2015) it's much easier to complete this work.
2749 ;;; Contributions gladly accepted! Please read the AMOP first though :)
2751 ;;; The protocol is:
2753 ;;; + apply-generic (gf args)
2754 ;;; + compute-applicable-methods (gf args ...)
2755 ;;; + sort-applicable-methods (gf methods args)
2756 ;;; + apply-methods (gf methods args)
2758 ;;; apply-methods calls make-next-method to build the "continuation" of
2759 ;;; a method. Applying a next-method will call apply-next-method which
2760 ;;; in turn will call apply again to call effectively the following
2761 ;;; method. (This paragraph is out of date but is kept so that maybe it
2762 ;;; illuminates some future hack.)
2765 (define-method (apply-generic (gf <generic>) args)
2766 (when (null? (slot-ref gf 'methods))
2767 (no-method gf args))
2768 (let ((methods (compute-applicable-methods gf args)))
2770 (apply-methods gf (sort-applicable-methods gf methods args) args)
2771 (no-applicable-method gf args))))
2773 ;; compute-applicable-methods is bound to %compute-applicable-methods.
2775 (define %%compute-applicable-methods
2776 (make <generic> #:name 'compute-applicable-methods))
2778 (define-method (%%compute-applicable-methods (gf <generic>) args)
2779 (%compute-applicable-methods gf args))
2781 (set! compute-applicable-methods %%compute-applicable-methods)
2783 (define-method (sort-applicable-methods (gf <generic>) methods args)
2784 (%sort-applicable-methods methods (map class-of args)))
2786 (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
2787 (%method-more-specific? m1 m2 targs))
2789 (define-method (apply-method (gf <generic>) methods build-next args)
2790 (apply (method-procedure (car methods))
2791 (build-next (cdr methods) args)
2794 (define-method (apply-methods (gf <generic>) (l <list>) args)
2795 (letrec ((next (lambda (procs args)
2797 (let ((a (if (null? new-args) args new-args)))
2799 (no-next-method gf a)
2800 (apply-method gf procs next a)))))))
2801 (apply-method gf l next args)))
2803 ;; We don't want the following procedure to turn up in backtraces:
2804 (for-each (lambda (proc)
2805 (set-procedure-property! proc 'system-procedure #t))
2809 no-applicable-method
2814 ;;; {Final initialization}
2817 ;; Tell C code that the main bulk of Goops has been loaded
2824 ;;; {SMOB and port classes}
2827 (define <arbiter> (find-subclass <top> '<arbiter>))
2828 (define <promise> (find-subclass <top> '<promise>))
2829 (define <thread> (find-subclass <top> '<thread>))
2830 (define <mutex> (find-subclass <top> '<mutex>))
2831 (define <condition-variable> (find-subclass <top> '<condition-variable>))
2832 (define <regexp> (find-subclass <top> '<regexp>))
2833 (define <hook> (find-subclass <top> '<hook>))
2834 (define <bitvector> (find-subclass <top> '<bitvector>))
2835 (define <random-state> (find-subclass <top> '<random-state>))
2836 (define <async> (find-subclass <top> '<async>))
2837 (define <directory> (find-subclass <top> '<directory>))
2838 (define <array> (find-subclass <top> '<array>))
2839 (define <character-set> (find-subclass <top> '<character-set>))
2840 (define <dynamic-object> (find-subclass <top> '<dynamic-object>))
2841 (define <guardian> (find-subclass <applicable> '<guardian>))
2842 (define <macro> (find-subclass <top> '<macro>))
2844 (define (define-class-subtree class)
2845 (define! (class-name class) class)
2846 (for-each define-class-subtree (class-direct-subclasses class)))
2848 (define-class-subtree (find-subclass <port> '<file-port>))