Commit | Line | Data |
---|---|---|
3f4829e0 AW |
1 | ;;;; goops.scm -- The Guile Object-Oriented Programming System |
2 | ;;;; | |
3 | ;;;; Copyright (C) 1998-2003,2006,2009-2011,2013-2015 Free Software Foundation, Inc. | |
4ff2133a LC |
4 | ;;;; Copyright (C) 1993-1998 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr> |
5 | ;;;; | |
73be1d9e MV |
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 | |
53befeb7 | 9 | ;;;; version 3 of the License, or (at your option) any later version. |
4ff2133a | 10 | ;;;; |
73be1d9e | 11 | ;;;; This library is distributed in the hope that it will be useful, |
14f1d9fe | 12 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
73be1d9e MV |
13 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | ;;;; Lesser General Public License for more details. | |
4ff2133a | 15 | ;;;; |
73be1d9e MV |
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 | |
92205699 | 18 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
4ff2133a | 19 | ;;;; |
14f1d9fe MD |
20 | \f |
21 | ||
14f1d9fe | 22 | ;;;; |
4ff2133a LC |
23 | ;;;; This file was based upon stklos.stk from the STk distribution |
24 | ;;;; version 4.0.1 by Erick Gallesio <eg@unice.fr>. | |
14f1d9fe MD |
25 | ;;;; |
26 | ||
27 | (define-module (oop goops) | |
28d0871b | 28 | #:use-module (srfi srfi-1) |
51fd1cd6 | 29 | #:use-module (ice-9 match) |
ac5185c2 AW |
30 | #:use-module ((language tree-il primitives) |
31 | :select (add-interesting-primitive!)) | |
28d0871b AW |
32 | #:export-syntax (define-class class standard-define-class |
33 | define-generic define-accessor define-method | |
34 | define-extended-generic define-extended-generics | |
35 | method) | |
36 | #:export ( ;; The root of everything. | |
37 | <top> | |
38 | <class> <object> | |
39 | ||
40 | ;; Slot types. | |
193e2c52 | 41 | <slot> |
28d0871b AW |
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> | |
46 | ||
47 | ;; Methods are implementations of generic functions. | |
f67f8581 LC |
48 | <method> <accessor-method> |
49 | ||
28d0871b AW |
50 | ;; Applicable objects, either procedures or applicable structs. |
51 | <procedure-class> <applicable> | |
52 | <procedure> <primitive-generic> | |
f67f8581 | 53 | |
28d0871b | 54 | ;; Applicable structs. |
6c7dd9eb AW |
55 | <applicable-struct-class> <applicable-struct-with-setter-class> |
56 | <applicable-struct> <applicable-struct-with-setter> | |
28d0871b AW |
57 | <generic> <extended-generic> |
58 | <generic-with-setter> <extended-generic-with-setter> | |
59 | <accessor> <extended-accessor> | |
60 | ||
61 | ;; Types with their own allocated typecodes. | |
62 | <boolean> <char> <list> <pair> <null> <string> <symbol> | |
63 | <vector> <bytevector> <uvec> <foreign> <hashtable> | |
c0a1a0b1 | 64 | <fluid> <dynamic-state> <frame> <vm> <vm-continuation> |
e2fafeb9 | 65 | <keyword> |
28d0871b AW |
66 | |
67 | ;; Numbers. | |
68 | <number> <complex> <real> <integer> <fraction> | |
69 | ||
70 | ;; Unknown. | |
71 | <unknown> | |
72 | ||
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> | |
e2fafeb9 | 79 | <directory> <array> <character-set> |
02620dd9 | 80 | <dynamic-object> <guardian> <macro> |
28d0871b AW |
81 | |
82 | ;; Modules. | |
83 | <module> | |
84 | ||
85 | ;; Ports. | |
86 | <port> <input-port> <output-port> <input-output-port> | |
87 | ||
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. | |
91 | <file-port> | |
92 | <file-input-port> <file-output-port> <file-input-output-port> | |
93 | ||
94 | is-a? class-of | |
95 | ensure-metaclass ensure-metaclass-with-supers | |
96 | make-class | |
97 | make-generic ensure-generic | |
98 | make-extended-generic | |
99 | make-accessor ensure-accessor | |
100 | add-method! | |
8dfc0ba5 | 101 | class-slot-ref class-slot-set! slot-unbound slot-missing |
28d0871b AW |
102 | slot-definition-name slot-definition-options |
103 | slot-definition-allocation | |
104 | ||
105 | slot-definition-getter slot-definition-setter | |
106 | slot-definition-accessor | |
107 | slot-definition-init-value slot-definition-init-form | |
8dfc0ba5 | 108 | slot-definition-init-thunk slot-definition-init-keyword |
28d0871b AW |
109 | slot-init-function class-slot-definition |
110 | method-source | |
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 | |
117 | class-redefinition | |
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 | |
122 | goops-error | |
123 | min-fixnum max-fixnum | |
8dfc0ba5 | 124 | |
2bcb278a AW |
125 | instance? |
126 | slot-ref slot-set! slot-bound? slot-exists? | |
28d0871b AW |
127 | class-name class-direct-supers class-direct-subclasses |
128 | class-direct-methods class-direct-slots class-precedence-list | |
129 | class-slots | |
130 | generic-function-name | |
131 | generic-function-methods method-generic-function | |
132 | method-specializers method-formals | |
133 | primitive-generic-generic enable-primitive-generic! | |
134 | method-procedure accessor-method-slot-definition | |
3f4829e0 | 135 | make find-method get-keyword)) |
78ec533c | 136 | |
9647d3d3 AW |
137 | |
138 | ;;; | |
139 | ;;; Booting GOOPS is a tortuous process. We begin by loading a small | |
140 | ;;; set of primitives from C. | |
141 | ;;; | |
ac5185c2 | 142 | (eval-when (expand load eval) |
6ab19396 | 143 | (load-extension (string-append "libguile-" (effective-version)) |
ac5185c2 | 144 | "scm_init_goops_builtins") |
4d6a7ac6 | 145 | (add-interesting-primitive! 'class-of)) |
aec4a84a | 146 | |
9647d3d3 AW |
147 | |
148 | \f | |
149 | ||
150 | ;;; | |
151 | ;;; We then define the slots that must appear in all classes (<class> | |
26a6aaef AW |
152 | ;;; objects) and slot definitions (<slot> objects). These slots must |
153 | ;;; appear in order. We'll use this list to statically compute offsets | |
154 | ;;; for the various fields, to compute the struct layout for <class> | |
155 | ;;; instances, and to compute the slot definition lists for <class>. | |
156 | ;;; Because the list is needed at expansion-time, we define it as a | |
157 | ;;; macro. | |
9647d3d3 | 158 | ;;; |
ebca094b AW |
159 | (define-syntax macro-fold-left |
160 | (syntax-rules () | |
161 | ((_ folder seed ()) seed) | |
162 | ((_ folder seed (head . tail)) | |
163 | (macro-fold-left folder (folder head seed) tail)))) | |
164 | ||
affe170e AW |
165 | (define-syntax macro-fold-right |
166 | (syntax-rules () | |
167 | ((_ folder seed ()) seed) | |
168 | ((_ folder seed (head . tail)) | |
169 | (folder head (macro-fold-right folder seed tail))))) | |
170 | ||
26a6aaef AW |
171 | (define-syntax-rule (define-macro-folder macro-folder value ...) |
172 | (define-syntax macro-folder | |
173 | (lambda (x) | |
174 | (syntax-case x () | |
175 | ((_ fold visit seed) | |
176 | ;; The datum->syntax makes it as if each `value' were present | |
177 | ;; in the initial form, which allows them to be used as | |
178 | ;; (components of) introduced identifiers. | |
179 | #`(fold visit seed #,(datum->syntax #'visit '(value ...)))))))) | |
180 | ||
181 | (define-macro-folder fold-class-slots | |
568174d1 AW |
182 | (layout #:class <protected-read-only-slot>) |
183 | (flags #:class <hidden-slot>) | |
184 | (self #:class <self-slot>) | |
185 | (instance-finalizer #:class <hidden-slot>) | |
26a6aaef | 186 | (print) |
568174d1 AW |
187 | (name #:class <protected-hidden-slot>) |
188 | (nfields #:class <hidden-slot>) | |
189 | (%reserved #:class <hidden-slot>) | |
26a6aaef AW |
190 | (redefined) |
191 | (direct-supers) | |
192 | (direct-slots) | |
193 | (direct-subclasses) | |
194 | (direct-methods) | |
195 | (cpl) | |
568174d1 | 196 | (slots)) |
26a6aaef AW |
197 | |
198 | (define-macro-folder fold-slot-slots | |
199 | (name #:init-keyword #:name) | |
200 | (allocation #:init-keyword #:allocation #:init-value #:instance) | |
568174d1 | 201 | (init-keyword #:init-keyword #:init-keyword #:init-value #f) |
26a6aaef | 202 | (init-form #:init-keyword #:init-form) |
568174d1 | 203 | (init-value #:init-keyword #:init-value) |
26a6aaef AW |
204 | (init-thunk #:init-keyword #:init-thunk #:init-value #f) |
205 | (options) | |
568174d1 AW |
206 | (getter #:init-keyword #:getter #:init-value #f) |
207 | (setter #:init-keyword #:setter #:init-value #f) | |
208 | (accessor #:init-keyword #:accessor #:init-value #f) | |
209 | ;; These last don't have #:init-keyword because they are meant to be | |
210 | ;; set by `allocate-slots', not in compute-effective-slot-definition. | |
4bde3f04 | 211 | (slot-ref/raw #:init-value #f) |
568174d1 AW |
212 | (slot-ref #:init-value #f) |
213 | (slot-set! #:init-value #f) | |
214 | (index #:init-value #f) | |
215 | (size #:init-value #f)) | |
ebca094b | 216 | |
9647d3d3 AW |
217 | ;;; |
218 | ;;; Statically define variables for slot offsets: `class-index-layout' | |
26a6aaef AW |
219 | ;;; will be 0, `class-index-flags' will be 1, and so on, and the same |
220 | ;;; for `slot-index-name' and such for <slot>. | |
9647d3d3 | 221 | ;;; |
26a6aaef AW |
222 | (let-syntax ((define-slot-indexer |
223 | (syntax-rules () | |
224 | ((_ define-index prefix) | |
225 | (define-syntax define-index | |
226 | (lambda (x) | |
227 | (define (id-append ctx a b) | |
228 | (datum->syntax ctx (symbol-append (syntax->datum a) | |
229 | (syntax->datum b)))) | |
230 | (define (tail-length tail) | |
231 | (syntax-case tail () | |
232 | ((begin) 0) | |
233 | ((visit head tail) (1+ (tail-length #'tail))))) | |
234 | (syntax-case x () | |
235 | ((_ (name . _) tail) | |
236 | #`(begin | |
237 | (define-syntax #,(id-append #'name #'prefix #'name) | |
238 | (identifier-syntax #,(tail-length #'tail))) | |
239 | tail))))))))) | |
240 | (define-slot-indexer define-class-index class-index-) | |
241 | (define-slot-indexer define-slot-index slot-index-) | |
242 | (fold-class-slots macro-fold-left define-class-index (begin)) | |
243 | (fold-slot-slots macro-fold-left define-slot-index (begin))) | |
9647d3d3 | 244 | |
761338f6 AW |
245 | ;;; |
246 | ;;; Structs that are vtables have a "flags" slot, which corresponds to | |
247 | ;;; class-index-flags. `vtable-flag-vtable' indicates that instances of | |
248 | ;;; a vtable are themselves vtables, and `vtable-flag-validated' | |
249 | ;;; indicates that the struct's layout has been validated. goops.c | |
c6fb41fc AW |
250 | ;;; defines a few additional flags: one to indicate that a vtable is |
251 | ;;; actually a class, one to indicate that the class is "valid" (meaning | |
252 | ;;; that it hasn't been redefined), and one to indicate that instances | |
253 | ;;; of a class are slot definition objects (<slot> instances). | |
761338f6 AW |
254 | ;;; |
255 | (define vtable-flag-goops-metaclass | |
256 | (logior vtable-flag-vtable vtable-flag-goops-class)) | |
257 | ||
258 | (define-inlinable (class-add-flags! class flags) | |
259 | (struct-set! class class-index-flags | |
260 | (logior flags (struct-ref class class-index-flags)))) | |
261 | ||
262 | (define-inlinable (class-clear-flags! class flags) | |
263 | (struct-set! class class-index-flags | |
264 | (logand (lognot flags) (struct-ref class class-index-flags)))) | |
265 | ||
266 | (define-inlinable (class-has-flags? class flags) | |
267 | (eqv? flags | |
268 | (logand (struct-ref class class-index-flags) flags))) | |
269 | ||
270 | (define-inlinable (class? obj) | |
271 | (class-has-flags? (struct-vtable obj) vtable-flag-goops-metaclass)) | |
272 | ||
c6fb41fc AW |
273 | (define-inlinable (slot? obj) |
274 | (and (struct? obj) | |
275 | (class-has-flags? (struct-vtable obj) vtable-flag-goops-slot))) | |
276 | ||
761338f6 AW |
277 | (define-inlinable (instance? obj) |
278 | (class-has-flags? (struct-vtable obj) vtable-flag-goops-class)) | |
279 | ||
26350edc AW |
280 | (define (class-has-statically-allocated-slots? class) |
281 | (class-has-flags? class vtable-flag-goops-static)) | |
282 | ||
9647d3d3 AW |
283 | ;;; |
284 | ;;; Now that we know the slots that must be present in classes, and | |
285 | ;;; their offsets, we can create the root of the class hierarchy. | |
286 | ;;; | |
568174d1 AW |
287 | ;;; Note that the `direct-supers', `direct-slots', `cpl', and `slots' |
288 | ;;; fields will be updated later, once we can create slot definition | |
289 | ;;; objects and once we have definitions for <top> and <object>. | |
9647d3d3 AW |
290 | ;;; |
291 | (define <class> | |
292 | (let-syntax ((cons-layout | |
293 | ;; A simple way to compute class layout for the concrete | |
294 | ;; types used in <class>. | |
295 | (syntax-rules (<protected-read-only-slot> | |
296 | <self-slot> | |
297 | <hidden-slot> | |
298 | <protected-hidden-slot>) | |
299 | ((_ (name) tail) | |
300 | (string-append "pw" tail)) | |
568174d1 | 301 | ((_ (name #:class <protected-read-only-slot>) tail) |
9647d3d3 | 302 | (string-append "pr" tail)) |
568174d1 | 303 | ((_ (name #:class <self-slot>) tail) |
9647d3d3 | 304 | (string-append "sr" tail)) |
568174d1 | 305 | ((_ (name #:class <hidden-slot>) tail) |
9647d3d3 | 306 | (string-append "uh" tail)) |
568174d1 AW |
307 | ((_ (name #:class <protected-hidden-slot>) tail) |
308 | (string-append "ph" tail))))) | |
9647d3d3 | 309 | (let* ((layout (fold-class-slots macro-fold-right cons-layout "")) |
568174d1 | 310 | (nfields (/ (string-length layout) 2)) |
761338f6 AW |
311 | (<class> (%make-vtable-vtable layout))) |
312 | (class-add-flags! <class> (logior vtable-flag-goops-class | |
313 | vtable-flag-goops-valid)) | |
9647d3d3 | 314 | (struct-set! <class> class-index-name '<class>) |
568174d1 | 315 | (struct-set! <class> class-index-nfields nfields) |
9647d3d3 | 316 | (struct-set! <class> class-index-direct-supers '()) |
568174d1 | 317 | (struct-set! <class> class-index-direct-slots '()) |
9647d3d3 AW |
318 | (struct-set! <class> class-index-direct-subclasses '()) |
319 | (struct-set! <class> class-index-direct-methods '()) | |
320 | (struct-set! <class> class-index-cpl '()) | |
568174d1 | 321 | (struct-set! <class> class-index-slots '()) |
9647d3d3 AW |
322 | (struct-set! <class> class-index-redefined #f) |
323 | <class>))) | |
51fd1cd6 | 324 | |
9647d3d3 AW |
325 | ;;; |
326 | ;;; Accessors to fields of <class>. | |
327 | ;;; | |
70dd6000 AW |
328 | (define-syntax-rule (define-class-accessor name docstring field) |
329 | (define (name obj) | |
330 | docstring | |
331 | (let ((val obj)) | |
332 | (unless (class? val) | |
333 | (scm-error 'wrong-type-arg #f "Not a class: ~S" | |
334 | (list val) #f)) | |
335 | (struct-ref val field)))) | |
336 | ||
337 | (define-class-accessor class-name | |
338 | "Return the class name of @var{obj}." | |
339 | class-index-name) | |
340 | (define-class-accessor class-direct-supers | |
341 | "Return the direct superclasses of the class @var{obj}." | |
342 | class-index-direct-supers) | |
343 | (define-class-accessor class-direct-slots | |
344 | "Return the direct slots of the class @var{obj}." | |
345 | class-index-direct-slots) | |
346 | (define-class-accessor class-direct-subclasses | |
347 | "Return the direct subclasses of the class @var{obj}." | |
348 | class-index-direct-subclasses) | |
349 | (define-class-accessor class-direct-methods | |
350 | "Return the direct methods of the class @var{obj}." | |
351 | class-index-direct-methods) | |
352 | (define-class-accessor class-precedence-list | |
353 | "Return the class precedence list of the class @var{obj}." | |
354 | class-index-cpl) | |
355 | (define-class-accessor class-slots | |
356 | "Return the slot list of the class @var{obj}." | |
357 | class-index-slots) | |
358 | ||
0ca49290 | 359 | (define (class-subclasses c) |
9647d3d3 | 360 | "Compute a list of all subclasses of @var{c}, direct and indirect." |
0ca49290 AW |
361 | (define (all-subclasses c) |
362 | (cons c (append-map all-subclasses | |
363 | (class-direct-subclasses c)))) | |
364 | (delete-duplicates (cdr (all-subclasses c)) eq?)) | |
365 | ||
366 | (define (class-methods c) | |
9647d3d3 AW |
367 | "Compute a list of all methods that specialize on @var{c} or |
368 | subclasses of @var{c}." | |
0ca49290 AW |
369 | (delete-duplicates (append-map class-direct-methods |
370 | (cons c (class-subclasses c))) | |
371 | eq?)) | |
372 | ||
568174d1 AW |
373 | (define (is-a? obj class) |
374 | "Return @code{#t} if @var{obj} is an instance of @var{class}, or | |
375 | @code{#f} otherwise." | |
376 | (and (memq class (class-precedence-list (class-of obj))) #t)) | |
377 | ||
9647d3d3 AW |
378 | |
379 | \f | |
380 | ||
381 | ;;; | |
568174d1 AW |
382 | ;;; At this point, <class> is missing slot definitions, but we can't |
383 | ;;; create slot definitions until we have a slot definition class. | |
384 | ;;; Continue with manual object creation until we're able to bootstrap | |
385 | ;;; more of the protocol. Again, the CPL and class hierarchy slots | |
386 | ;;; remain uninitialized. | |
9647d3d3 | 387 | ;;; |
568174d1 AW |
388 | (define* (get-keyword key l #:optional default) |
389 | "Determine an associated value for the keyword @var{key} from the list | |
390 | @var{l}. The list @var{l} has to consist of an even number of elements, | |
391 | where, starting with the first, every second element is a keyword, | |
392 | followed by its associated value. If @var{l} does not hold a value for | |
393 | @var{key}, the value @var{default} is returned." | |
394 | (unless (keyword? key) | |
395 | (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list key) #f)) | |
396 | (let lp ((l l)) | |
397 | (match l | |
398 | (() default) | |
399 | ((kw arg . l) | |
400 | (unless (keyword? kw) | |
401 | (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list kw) #f)) | |
402 | (if (eq? kw key) arg (lp l)))))) | |
403 | ||
404 | (define *unbound* (list 'unbound)) | |
405 | ||
406 | (define-inlinable (unbound? x) | |
407 | (eq? x *unbound*)) | |
408 | ||
409 | (define (%allocate-instance class) | |
410 | (let ((obj (allocate-struct class (struct-ref class class-index-nfields)))) | |
411 | (%clear-fields! obj *unbound*) | |
412 | obj)) | |
413 | ||
414 | (define <slot> | |
415 | (let-syntax ((cons-layout | |
416 | ;; All slots are "pw" in <slot>. | |
417 | (syntax-rules () | |
418 | ((_ _ tail) (string-append "pw" tail))))) | |
419 | (let* ((layout (fold-slot-slots macro-fold-right cons-layout "")) | |
420 | (nfields (/ (string-length layout) 2)) | |
421 | (<slot> (make-struct/no-tail <class> (make-struct-layout layout)))) | |
422 | (class-add-flags! <slot> (logior vtable-flag-goops-class | |
e437c50b | 423 | vtable-flag-goops-slot |
568174d1 AW |
424 | vtable-flag-goops-valid)) |
425 | (struct-set! <slot> class-index-name '<slot>) | |
426 | (struct-set! <slot> class-index-nfields nfields) | |
427 | (struct-set! <slot> class-index-direct-supers '()) | |
428 | (struct-set! <slot> class-index-direct-slots '()) | |
429 | (struct-set! <slot> class-index-direct-subclasses '()) | |
430 | (struct-set! <slot> class-index-direct-methods '()) | |
431 | (struct-set! <slot> class-index-cpl (list <slot>)) | |
432 | (struct-set! <slot> class-index-slots '()) | |
433 | (struct-set! <slot> class-index-redefined #f) | |
434 | <slot>))) | |
435 | ||
3f4829e0 AW |
436 | ;;; Access to slot objects is performance-sensitive for slot-ref, so in |
437 | ;;; addition to the type-checking accessors that we export, we also | |
438 | ;;; define some internal inlined helpers that just do an unchecked | |
439 | ;;; struct-ref in cases where we know the object must be a slot, as | |
440 | ;;; when accessing class-slots. | |
441 | ;;; | |
2a3ef7c4 AW |
442 | (define-syntax-rule (define-slot-accessor name docstring %name field) |
443 | (begin | |
444 | (define-syntax-rule (%name obj) | |
445 | (struct-ref obj field)) | |
446 | (define (name obj) | |
447 | docstring | |
448 | (unless (slot? obj) | |
568174d1 | 449 | (scm-error 'wrong-type-arg #f "Not a slot: ~S" |
2a3ef7c4 AW |
450 | (list obj) #f)) |
451 | (%name obj)))) | |
568174d1 AW |
452 | |
453 | (define-slot-accessor slot-definition-name | |
454 | "Return the name of @var{obj}." | |
2a3ef7c4 | 455 | %slot-definition-name slot-index-name) |
568174d1 AW |
456 | (define-slot-accessor slot-definition-allocation |
457 | "Return the allocation of the slot @var{obj}." | |
2a3ef7c4 | 458 | %slot-definition-allocation slot-index-allocation) |
568174d1 AW |
459 | (define-slot-accessor slot-definition-init-keyword |
460 | "Return the init keyword of the slot @var{obj}, or @code{#f}." | |
2a3ef7c4 | 461 | %slot-definition-init-keyword slot-index-init-keyword) |
568174d1 AW |
462 | (define-slot-accessor slot-definition-init-form |
463 | "Return the init form of the slot @var{obj}, or the unbound value" | |
2a3ef7c4 | 464 | %slot-definition-init-form slot-index-init-form) |
568174d1 AW |
465 | (define-slot-accessor slot-definition-init-value |
466 | "Return the init value of the slot @var{obj}, or the unbound value." | |
2a3ef7c4 | 467 | %slot-definition-init-value slot-index-init-value) |
568174d1 AW |
468 | (define-slot-accessor slot-definition-init-thunk |
469 | "Return the init thunk of the slot @var{obj}, or @code{#f}." | |
2a3ef7c4 | 470 | %slot-definition-init-thunk slot-index-init-thunk) |
568174d1 AW |
471 | (define-slot-accessor slot-definition-options |
472 | "Return the initargs given when creating the slot @var{obj}." | |
2a3ef7c4 | 473 | %slot-definition-options slot-index-options) |
568174d1 AW |
474 | (define-slot-accessor slot-definition-getter |
475 | "Return the getter of the slot @var{obj}, or @code{#f}." | |
2a3ef7c4 | 476 | %slot-definition-getter slot-index-getter) |
568174d1 AW |
477 | (define-slot-accessor slot-definition-setter |
478 | "Return the setter of the slot @var{obj}, or @code{#f}." | |
2a3ef7c4 | 479 | %slot-definition-setter slot-index-setter) |
568174d1 AW |
480 | (define-slot-accessor slot-definition-accessor |
481 | "Return the accessor of the slot @var{obj}, or @code{#f}." | |
2a3ef7c4 | 482 | %slot-definition-accessor slot-index-accessor) |
4bde3f04 AW |
483 | (define-slot-accessor slot-definition-slot-ref/raw |
484 | "Return the raw slot-ref procedure of the slot @var{obj}." | |
485 | %slot-definition-slot-ref/raw slot-index-slot-ref/raw) | |
568174d1 | 486 | (define-slot-accessor slot-definition-slot-ref |
4bde3f04 | 487 | "Return the slot-ref procedure of the slot @var{obj}." |
2a3ef7c4 | 488 | %slot-definition-slot-ref slot-index-slot-ref) |
568174d1 | 489 | (define-slot-accessor slot-definition-slot-set! |
4bde3f04 | 490 | "Return the slot-set! procedure of the slot @var{obj}." |
2a3ef7c4 | 491 | %slot-definition-slot-set! slot-index-slot-set!) |
568174d1 AW |
492 | (define-slot-accessor slot-definition-index |
493 | "Return the allocated struct offset of the slot @var{obj}, or @code{#f}." | |
2a3ef7c4 | 494 | %slot-definition-index slot-index-index) |
568174d1 AW |
495 | (define-slot-accessor slot-definition-size |
496 | "Return the number fields used by the slot @var{obj}, or @code{#f}." | |
2a3ef7c4 | 497 | %slot-definition-size slot-index-size) |
568174d1 | 498 | |
568174d1 AW |
499 | ;; Boot definition. |
500 | (define (direct-slot-definition-class class initargs) | |
501 | (get-keyword #:class initargs <slot>)) | |
502 | ||
503 | ;; Boot definition. | |
504 | (define (make-slot class initargs) | |
505 | (let ((slot (make-struct/no-tail class))) | |
506 | (define-syntax-rule (init-slot offset kw default) | |
507 | (struct-set! slot offset (get-keyword kw initargs default))) | |
508 | (init-slot slot-index-name #:name #f) | |
509 | (init-slot slot-index-allocation #:allocation #:instance) | |
510 | (init-slot slot-index-init-keyword #:init-keyword #f) | |
511 | (init-slot slot-index-init-form #:init-form *unbound*) | |
512 | (init-slot slot-index-init-value #:init-value *unbound*) | |
513 | (struct-set! slot slot-index-init-thunk | |
514 | (or (get-keyword #:init-thunk initargs #f) | |
2a3ef7c4 | 515 | (let ((val (%slot-definition-init-value slot))) |
568174d1 AW |
516 | (if (unbound? val) |
517 | #f | |
518 | (lambda () val))))) | |
519 | (struct-set! slot slot-index-options initargs) | |
520 | (init-slot slot-index-getter #:getter #f) | |
521 | (init-slot slot-index-setter #:setter #f) | |
522 | (init-slot slot-index-accessor #:accessor #f) | |
4bde3f04 AW |
523 | (struct-set! slot slot-index-slot-ref/raw #f) |
524 | (struct-set! slot slot-index-slot-ref #f) | |
525 | (struct-set! slot slot-index-slot-set! #f) | |
526 | (struct-set! slot slot-index-index #f) | |
527 | (struct-set! slot slot-index-size #f) | |
568174d1 AW |
528 | slot)) |
529 | ||
530 | ;; Boot definition. | |
531 | (define (make class . args) | |
532 | (unless (memq <slot> (class-precedence-list class)) | |
533 | (error "Unsupported class: ~S" class)) | |
534 | (make-slot class args)) | |
535 | ||
536 | ;; Boot definition. | |
537 | (define (compute-direct-slot-definition class initargs) | |
538 | (apply make (direct-slot-definition-class class initargs) initargs)) | |
539 | ||
540 | (define (compute-direct-slot-definition-initargs class slot-spec) | |
541 | (match slot-spec | |
542 | ((? symbol? name) (list #:name name)) | |
543 | (((? symbol? name) . initargs) | |
544 | (cons* #:name name | |
545 | ;; If there is an #:init-form, the `class' macro will have | |
546 | ;; already added an #:init-thunk. Still, if there isn't an | |
547 | ;; #:init-thunk already but we do have an #:init-value, | |
548 | ;; synthesize an #:init-thunk initarg. This will ensure | |
549 | ;; that the #:init-thunk gets passed on to the effective | |
550 | ;; slot definition too. | |
551 | (if (get-keyword #:init-thunk initargs) | |
552 | initargs | |
553 | (let ((value (get-keyword #:init-value initargs *unbound*))) | |
554 | (if (unbound? value) | |
555 | initargs | |
556 | (cons* #:init-thunk (lambda () value) initargs)))))))) | |
557 | ||
558 | (let () | |
559 | (define-syntax cons-slot | |
560 | (syntax-rules () | |
561 | ((_ (name #:class class) tail) | |
562 | ;; Special case to avoid referencing specialized <slot> kinds, | |
563 | ;; which are not defined yet. | |
564 | (cons (list 'name) tail)) | |
565 | ((_ (name . initargs) tail) | |
566 | (cons (list 'name . initargs) tail)))) | |
567 | (define-syntax-rule (initialize-direct-slots! class fold-slots) | |
568 | (let ((specs (fold-slots macro-fold-right cons-slot '()))) | |
569 | (define (make-direct-slot-definition spec) | |
570 | (let ((initargs (compute-direct-slot-definition-initargs class spec))) | |
571 | (compute-direct-slot-definition class initargs))) | |
572 | (struct-set! class class-index-direct-slots | |
573 | (map make-direct-slot-definition specs)))) | |
574 | ||
575 | (initialize-direct-slots! <class> fold-class-slots) | |
576 | (initialize-direct-slots! <slot> fold-slot-slots)) | |
9647d3d3 AW |
577 | |
578 | ||
579 | \f | |
580 | ||
581 | ;;; | |
568174d1 AW |
582 | ;;; OK, at this point we have initialized `direct-slots' on both <class> |
583 | ;;; and <slot>. We need to define a standard way to make subclasses: | |
584 | ;;; how to compute the precedence list of subclasses, how to compute the | |
585 | ;;; list of slots in a subclass, and what layout to use for instances of | |
586 | ;;; those classes. | |
9647d3d3 | 587 | ;;; |
70dd6000 | 588 | (define (compute-std-cpl c get-direct-supers) |
9c49d475 | 589 | "The standard class precedence list computation algorithm." |
70dd6000 AW |
590 | (define (only-non-null lst) |
591 | (filter (lambda (l) (not (null? l))) lst)) | |
592 | ||
593 | (define (merge-lists reversed-partial-result inputs) | |
594 | (cond | |
595 | ((every null? inputs) | |
596 | (reverse! reversed-partial-result)) | |
597 | (else | |
598 | (let* ((candidate (lambda (c) | |
599 | (and (not (any (lambda (l) | |
600 | (memq c (cdr l))) | |
601 | inputs)) | |
602 | c))) | |
603 | (candidate-car (lambda (l) | |
604 | (and (not (null? l)) | |
605 | (candidate (car l))))) | |
606 | (next (any candidate-car inputs))) | |
bacc8829 AW |
607 | (unless next |
608 | (goops-error "merge-lists: Inconsistent precedence graph")) | |
70dd6000 AW |
609 | (let ((remove-next (lambda (l) |
610 | (if (eq? (car l) next) | |
611 | (cdr l) | |
612 | l)))) | |
613 | (merge-lists (cons next reversed-partial-result) | |
614 | (only-non-null (map remove-next inputs)))))))) | |
615 | (let ((c-direct-supers (get-direct-supers c))) | |
616 | (merge-lists (list c) | |
617 | (only-non-null (append (map class-precedence-list | |
618 | c-direct-supers) | |
619 | (list c-direct-supers)))))) | |
620 | ||
9c49d475 AW |
621 | ;; This version of compute-cpl is replaced with a generic function once |
622 | ;; GOOPS has booted. | |
70dd6000 AW |
623 | (define (compute-cpl class) |
624 | (compute-std-cpl class class-direct-supers)) | |
625 | ||
568174d1 AW |
626 | (define (effective-slot-definition-class class slot) |
627 | (class-of slot)) | |
628 | ||
629 | (define (compute-effective-slot-definition class slot) | |
630 | ;; FIXME: Support slot being a list of slots, as in CLOS. | |
631 | (apply make | |
632 | (effective-slot-definition-class class slot) | |
633 | (slot-definition-options slot))) | |
634 | ||
e0590e7c | 635 | (define (build-slots-list dslots cpl) |
568174d1 | 636 | (define (slot-memq slot slots) |
2a3ef7c4 | 637 | (let ((name (%slot-definition-name slot))) |
568174d1 AW |
638 | (let lp ((slots slots)) |
639 | (match slots | |
640 | (() #f) | |
641 | ((slot . slots) | |
2a3ef7c4 | 642 | (or (eq? (%slot-definition-name slot) name) (lp slots))))))) |
e437c50b | 643 | (define (check-cpl slots static-slots) |
26350edc AW |
644 | (match static-slots |
645 | (() #t) | |
646 | ((static-slot . static-slots) | |
647 | (when (slot-memq static-slot slots) | |
648 | (scm-error 'misc-error #f | |
649 | "statically allocated inherited field cannot be redefined: ~a" | |
650 | (list (%slot-definition-name static-slot)) '())) | |
651 | (check-cpl slots static-slots)))) | |
e0590e7c AW |
652 | (define (remove-duplicate-slots slots) |
653 | (let lp ((slots (reverse slots)) (res '()) (seen '())) | |
f5c34767 AW |
654 | (match slots |
655 | (() res) | |
568174d1 | 656 | ((slot . slots) |
2a3ef7c4 | 657 | (let ((name (%slot-definition-name slot))) |
568174d1 AW |
658 | (if (memq name seen) |
659 | (lp slots res seen) | |
660 | (lp slots (cons slot res) (cons name seen)))))))) | |
e437c50b AW |
661 | ;; For subclases of <class> and <slot>, we need to ensure that the |
662 | ;; <class> or <slot> slots come first. | |
26350edc AW |
663 | (let ((static-slots |
664 | (match (filter class-has-statically-allocated-slots? (cdr cpl)) | |
665 | (() #f) | |
666 | ((class) (struct-ref class class-index-direct-slots)) | |
667 | (classes | |
668 | (error "can't subtype multiple classes with static slot allocation" | |
669 | classes))))) | |
e437c50b AW |
670 | (when static-slots |
671 | (check-cpl dslots static-slots)) | |
672 | (let lp ((cpl (cdr cpl)) (res dslots) (static-slots '())) | |
f5c34767 | 673 | (match cpl |
e437c50b | 674 | (() (remove-duplicate-slots (append static-slots res))) |
f5c34767 AW |
675 | ((head . cpl) |
676 | (let ((new-slots (struct-ref head class-index-direct-slots))) | |
677 | (cond | |
e437c50b AW |
678 | ((not static-slots) |
679 | (lp cpl (append new-slots res) static-slots)) | |
26350edc | 680 | ((class-has-statically-allocated-slots? head) |
e437c50b | 681 | ;; Move static slots to the head of the list. |
f5c34767 AW |
682 | (lp cpl res new-slots)) |
683 | (else | |
e437c50b AW |
684 | (check-cpl new-slots static-slots) |
685 | (lp cpl (append new-slots res) static-slots))))))))) | |
51fd1cd6 | 686 | |
568174d1 AW |
687 | ;; Boot definition. |
688 | (define (compute-get-n-set class slot) | |
689 | (let ((index (struct-ref class class-index-nfields))) | |
690 | (struct-set! class class-index-nfields (1+ index)) | |
691 | index)) | |
692 | ||
4bde3f04 AW |
693 | ;;; Pre-generate getters and setters for the first 20 slots. |
694 | (define-syntax define-standard-accessor-method | |
695 | (lambda (stx) | |
696 | (define num-standard-pre-cache 20) | |
697 | (syntax-case stx () | |
698 | ((_ ((proc n) arg ...) body) | |
699 | #`(define proc | |
700 | (let ((cache (vector #,@(map (lambda (n*) | |
701 | #`(lambda (arg ...) | |
702 | (let ((n #,n*)) | |
703 | body))) | |
704 | (iota num-standard-pre-cache))))) | |
705 | (lambda (n) | |
706 | (if (< n #,num-standard-pre-cache) | |
707 | (vector-ref cache n) | |
708 | (lambda (arg ...) body))))))))) | |
709 | ||
710 | (define-standard-accessor-method ((bound-check-get n) o) | |
711 | (let ((x (struct-ref o n))) | |
712 | (if (unbound? x) | |
713 | (slot-unbound o) | |
714 | x))) | |
715 | ||
716 | (define-standard-accessor-method ((standard-get n) o) | |
717 | (struct-ref o n)) | |
718 | ||
719 | (define-standard-accessor-method ((standard-set n) o v) | |
720 | (struct-set! o n v)) | |
721 | ||
568174d1 AW |
722 | (define (allocate-slots class slots) |
723 | "Transform the computed list of direct slot definitions @var{slots} | |
724 | into a corresponding list of effective slot definitions, allocating | |
725 | slots as we go." | |
726 | (define (make-effective-slot-definition slot) | |
727 | ;; `compute-get-n-set' is expected to mutate `nfields' if it | |
728 | ;; allocates a field to the object. Pretty strange, but we preserve | |
729 | ;; the behavior for backward compatibility. | |
730 | (let* ((slot (compute-effective-slot-definition class slot)) | |
4bde3f04 | 731 | (name (%slot-definition-name slot)) |
568174d1 AW |
732 | (index (struct-ref class class-index-nfields)) |
733 | (g-n-s (compute-get-n-set class slot)) | |
734 | (size (- (struct-ref class class-index-nfields) index))) | |
735 | (call-with-values | |
736 | (lambda () | |
737 | (match g-n-s | |
738 | ((? integer?) | |
739 | (unless (= size 1) | |
740 | (error "unexpected return from compute-get-n-set")) | |
4bde3f04 AW |
741 | (values (standard-get g-n-s) |
742 | (if (slot-definition-init-thunk slot) | |
743 | (standard-get g-n-s) | |
744 | (bound-check-get g-n-s)) | |
745 | (standard-set g-n-s))) | |
568174d1 | 746 | (((? procedure? get) (? procedure? set)) |
4bde3f04 AW |
747 | (values get |
748 | (lambda (o) | |
749 | (let ((value (get o))) | |
750 | (if (unbound? value) | |
751 | (slot-unbound class o name) | |
752 | value))) | |
753 | set)))) | |
754 | (lambda (get/raw get set) | |
755 | (struct-set! slot slot-index-slot-ref/raw get/raw) | |
568174d1 | 756 | (struct-set! slot slot-index-slot-ref get) |
4bde3f04 AW |
757 | (struct-set! slot slot-index-slot-set! set) |
758 | (struct-set! slot slot-index-index index) | |
759 | (struct-set! slot slot-index-size size))) | |
568174d1 AW |
760 | slot)) |
761 | (struct-set! class class-index-nfields 0) | |
762 | (map-in-order make-effective-slot-definition slots)) | |
763 | ||
764 | (define (%compute-layout slots nfields is-class?) | |
765 | (define (slot-protection-and-kind slot) | |
e0590e7c AW |
766 | (define (subclass? class parent) |
767 | (memq parent (class-precedence-list class))) | |
2a3ef7c4 | 768 | (let ((type (kw-arg-ref (%slot-definition-options slot) #:class))) |
e0590e7c AW |
769 | (if (and type (subclass? type <foreign-slot>)) |
770 | (values (cond | |
771 | ((subclass? type <self-slot>) #\s) | |
772 | ((subclass? type <protected-slot>) #\p) | |
773 | (else #\u)) | |
774 | (cond | |
775 | ((subclass? type <opaque-slot>) #\o) | |
776 | ((subclass? type <read-only-slot>) #\r) | |
777 | ((subclass? type <hidden-slot>) #\h) | |
778 | (else #\w))) | |
779 | (values #\p #\w)))) | |
e0590e7c | 780 | (let ((layout (make-string (* nfields 2)))) |
568174d1 AW |
781 | (let lp ((n 0) (slots slots)) |
782 | (match slots | |
e0590e7c AW |
783 | (() |
784 | (unless (= n nfields) (error "bad nfields")) | |
e0590e7c | 785 | (when is-class? |
8dfc0ba5 AW |
786 | (let ((class-layout (struct-ref <class> class-index-layout))) |
787 | (unless (string-prefix? (symbol->string class-layout) layout) | |
e0590e7c AW |
788 | (error "bad layout for class")))) |
789 | layout) | |
568174d1 | 790 | ((slot . slots) |
2a3ef7c4 | 791 | (unless (= n (%slot-definition-index slot)) (error "bad allocation")) |
568174d1 AW |
792 | (call-with-values (lambda () (slot-protection-and-kind slot)) |
793 | (lambda (protection kind) | |
2a3ef7c4 | 794 | (let init ((n n) (size (%slot-definition-size slot))) |
568174d1 AW |
795 | (cond |
796 | ((zero? size) (lp n slots)) | |
797 | (else | |
798 | (unless (< n nfields) (error "bad nfields")) | |
799 | (string-set! layout (* n 2) protection) | |
800 | (string-set! layout (1+ (* n 2)) kind) | |
801 | (init (1+ n) (1- size)))))))))))) | |
e0590e7c | 802 | |
9647d3d3 AW |
803 | |
804 | \f | |
805 | ||
806 | ;;; | |
807 | ;;; With all of this, we are now able to define subclasses of <class>. | |
808 | ;;; | |
e0590e7c | 809 | (define (%prep-layout! class) |
8dfc0ba5 | 810 | (let* ((is-class? (and (memq <class> (struct-ref class class-index-cpl)) #t)) |
568174d1 AW |
811 | (layout (%compute-layout (struct-ref class class-index-slots) |
812 | (struct-ref class class-index-nfields) | |
813 | is-class?))) | |
e0590e7c AW |
814 | (%init-layout! class layout))) |
815 | ||
816 | (define (make-standard-class class name dsupers dslots) | |
817 | (let ((z (make-struct/no-tail class))) | |
568174d1 AW |
818 | (define (make-direct-slot-definition dslot) |
819 | (let ((initargs (compute-direct-slot-definition-initargs z dslot))) | |
820 | (compute-direct-slot-definition z initargs))) | |
821 | ||
822 | (struct-set! z class-index-name name) | |
823 | (struct-set! z class-index-nfields 0) | |
8dfc0ba5 | 824 | (struct-set! z class-index-direct-supers dsupers) |
568174d1 AW |
825 | (struct-set! z class-index-direct-subclasses '()) |
826 | (struct-set! z class-index-direct-methods '()) | |
827 | (struct-set! z class-index-redefined #f) | |
828 | (let ((cpl (compute-cpl z))) | |
8dfc0ba5 | 829 | (struct-set! z class-index-cpl cpl) |
e437c50b AW |
830 | (when (memq <slot> cpl) |
831 | (class-add-flags! z vtable-flag-goops-slot)) | |
568174d1 AW |
832 | (let* ((dslots (map make-direct-slot-definition dslots)) |
833 | (slots (allocate-slots z (build-slots-list dslots cpl)))) | |
834 | (struct-set! z class-index-direct-slots dslots) | |
835 | (struct-set! z class-index-slots slots))) | |
836 | (for-each | |
837 | (lambda (super) | |
838 | (let ((subclasses (struct-ref super class-index-direct-subclasses))) | |
839 | (struct-set! super class-index-direct-subclasses | |
840 | (cons z subclasses)))) | |
841 | dsupers) | |
842 | (%prep-layout! z) | |
843 | z)) | |
e0590e7c | 844 | |
e0590e7c AW |
845 | (define-syntax define-standard-class |
846 | (syntax-rules () | |
847 | ((define-standard-class name (super ...) #:metaclass meta slot ...) | |
848 | (define name | |
849 | (make-standard-class meta 'name (list super ...) '(slot ...)))) | |
850 | ((define-standard-class name (super ...) slot ...) | |
851 | (define-standard-class name (super ...) #:metaclass <class> slot ...)))) | |
852 | ||
9647d3d3 AW |
853 | |
854 | \f | |
855 | ||
856 | ;;; | |
857 | ;;; Sweet! Now we can define <top> and <object>, and finish | |
858 | ;;; initializing the `direct-subclasses', `direct-supers', and `cpl' | |
859 | ;;; slots of <class>. | |
860 | ;;; | |
e0590e7c AW |
861 | (define-standard-class <top> ()) |
862 | (define-standard-class <object> (<top>)) | |
863 | ||
568174d1 AW |
864 | ;; The inheritance links for <top>, <object>, <class>, and <slot> were |
865 | ;; partially initialized. Correct them here. | |
866 | (struct-set! <object> class-index-direct-subclasses (list <slot> <class>)) | |
8dfc0ba5 | 867 | (struct-set! <class> class-index-direct-supers (list <object>)) |
568174d1 | 868 | (struct-set! <slot> class-index-direct-supers (list <object>)) |
8dfc0ba5 | 869 | (struct-set! <class> class-index-cpl (list <class> <object> <top>)) |
568174d1 | 870 | (struct-set! <slot> class-index-cpl (list <slot> <object> <top>)) |
e0590e7c | 871 | |
9647d3d3 AW |
872 | |
873 | \f | |
874 | ||
875 | ;;; | |
876 | ;;; We can also define the various slot types, and finish initializing | |
568174d1 | 877 | ;;; `direct-slots' and `slots' on <class> and <slot>. |
9647d3d3 | 878 | ;;; |
568174d1 | 879 | (define-standard-class <foreign-slot> (<slot>)) |
e0590e7c AW |
880 | (define-standard-class <protected-slot> (<foreign-slot>)) |
881 | (define-standard-class <hidden-slot> (<foreign-slot>)) | |
882 | (define-standard-class <opaque-slot> (<foreign-slot>)) | |
883 | (define-standard-class <read-only-slot> (<foreign-slot>)) | |
884 | (define-standard-class <self-slot> (<read-only-slot>)) | |
885 | (define-standard-class <protected-opaque-slot> (<protected-slot> | |
886 | <opaque-slot>)) | |
887 | (define-standard-class <protected-hidden-slot> (<protected-slot> | |
888 | <hidden-slot>)) | |
889 | (define-standard-class <protected-read-only-slot> (<protected-slot> | |
890 | <read-only-slot>)) | |
891 | (define-standard-class <scm-slot> (<protected-slot>)) | |
892 | (define-standard-class <int-slot> (<foreign-slot>)) | |
893 | (define-standard-class <float-slot> (<foreign-slot>)) | |
894 | (define-standard-class <double-slot> (<foreign-slot>)) | |
895 | ||
568174d1 AW |
896 | |
897 | \f | |
898 | ||
899 | ;;; | |
900 | ;;; Finally! Initialize `direct-slots' and `slots' on <class>, and | |
901 | ;;; `slots' on <slot>. | |
902 | ;;; | |
903 | (let () | |
904 | (define-syntax-rule (cons-slot (name . initargs) tail) | |
905 | (cons (list 'name . initargs) tail)) | |
906 | (define-syntax-rule (initialize-direct-slots! class fold-slots) | |
907 | (let ((specs (fold-slots macro-fold-right cons-slot '()))) | |
908 | (define (make-direct-slot-definition spec) | |
909 | (let ((initargs (compute-direct-slot-definition-initargs class spec))) | |
910 | (compute-direct-slot-definition class initargs))) | |
911 | (struct-set! class class-index-direct-slots | |
912 | (map make-direct-slot-definition specs)))) | |
913 | (define (initialize-slots! class) | |
914 | (let ((slots (build-slots-list (class-direct-slots class) | |
915 | (class-precedence-list class)))) | |
916 | (struct-set! class class-index-slots (allocate-slots class slots)))) | |
917 | ||
918 | ;; Finish initializing <class> with the specialized slot kinds. | |
919 | (initialize-direct-slots! <class> fold-class-slots) | |
920 | ||
921 | (initialize-slots! <class>) | |
26350edc AW |
922 | (initialize-slots! <slot>) |
923 | ||
924 | ;; Now that we're all done with that, mark <class> and <slot> as | |
925 | ;; static. | |
926 | (class-add-flags! <class> vtable-flag-goops-static) | |
927 | (class-add-flags! <slot> vtable-flag-goops-static)) | |
e0590e7c | 928 | |
9647d3d3 AW |
929 | |
930 | \f | |
931 | ||
932 | ;;; | |
933 | ;;; Now, to build out the class hierarchy. | |
934 | ;;; | |
935 | ||
e0590e7c | 936 | (define-standard-class <procedure-class> (<class>)) |
761338f6 | 937 | |
6c7dd9eb AW |
938 | (define-standard-class <applicable-struct-class> |
939 | (<procedure-class>)) | |
761338f6 AW |
940 | (class-add-flags! <applicable-struct-class> |
941 | vtable-flag-applicable-vtable) | |
942 | ||
6c7dd9eb AW |
943 | (define-standard-class <applicable-struct-with-setter-class> |
944 | (<applicable-struct-class>)) | |
761338f6 AW |
945 | (class-add-flags! <applicable-struct-with-setter-class> |
946 | vtable-flag-setter-vtable) | |
6c7dd9eb | 947 | |
e0590e7c AW |
948 | (define-standard-class <applicable> (<top>)) |
949 | (define-standard-class <applicable-struct> (<object> <applicable>) | |
950 | #:metaclass <applicable-struct-class> | |
951 | procedure) | |
6c7dd9eb AW |
952 | (define-standard-class <applicable-struct-with-setter> (<applicable-struct>) |
953 | #:metaclass <applicable-struct-with-setter-class> | |
954 | setter) | |
e0590e7c AW |
955 | (define-standard-class <generic> (<applicable-struct>) |
956 | #:metaclass <applicable-struct-class> | |
957 | methods | |
958 | (n-specialized #:init-value 0) | |
959 | (extended-by #:init-value ()) | |
960 | effective-methods) | |
e0590e7c AW |
961 | (define-standard-class <extended-generic> (<generic>) |
962 | #:metaclass <applicable-struct-class> | |
963 | (extends #:init-value ())) | |
6c7dd9eb AW |
964 | (define-standard-class <generic-with-setter> (<generic> |
965 | <applicable-struct-with-setter>) | |
966 | #:metaclass <applicable-struct-with-setter-class>) | |
e0590e7c | 967 | (define-standard-class <accessor> (<generic-with-setter>) |
6c7dd9eb | 968 | #:metaclass <applicable-struct-with-setter-class>) |
e0590e7c AW |
969 | (define-standard-class <extended-generic-with-setter> (<extended-generic> |
970 | <generic-with-setter>) | |
6c7dd9eb | 971 | #:metaclass <applicable-struct-with-setter-class>) |
e0590e7c AW |
972 | (define-standard-class <extended-accessor> (<accessor> |
973 | <extended-generic-with-setter>) | |
6c7dd9eb | 974 | #:metaclass <applicable-struct-with-setter-class>) |
e0590e7c | 975 | |
6c7dd9eb AW |
976 | (define-standard-class <method> (<object>) |
977 | generic-function | |
978 | specializers | |
979 | procedure | |
980 | formals | |
981 | body | |
982 | make-procedure) | |
983 | (define-standard-class <accessor-method> (<method>) | |
984 | (slot-definition #:init-keyword #:slot-definition)) | |
985 | ||
e0590e7c AW |
986 | (define-standard-class <boolean> (<top>)) |
987 | (define-standard-class <char> (<top>)) | |
988 | (define-standard-class <list> (<top>)) | |
0d96acac AW |
989 | ;; Not all pairs are lists, but there is code out there that relies on |
990 | ;; (is-a? '(1 2 3) <list>) to work. Terrible. How to fix? | |
e0590e7c AW |
991 | (define-standard-class <pair> (<list>)) |
992 | (define-standard-class <null> (<list>)) | |
993 | (define-standard-class <string> (<top>)) | |
994 | (define-standard-class <symbol> (<top>)) | |
995 | (define-standard-class <vector> (<top>)) | |
996 | (define-standard-class <foreign> (<top>)) | |
997 | (define-standard-class <hashtable> (<top>)) | |
998 | (define-standard-class <fluid> (<top>)) | |
999 | (define-standard-class <dynamic-state> (<top>)) | |
1000 | (define-standard-class <frame> (<top>)) | |
1001 | (define-standard-class <vm-continuation> (<top>)) | |
1002 | (define-standard-class <bytevector> (<top>)) | |
1003 | (define-standard-class <uvec> (<bytevector>)) | |
1004 | (define-standard-class <array> (<top>)) | |
1005 | (define-standard-class <bitvector> (<top>)) | |
1006 | (define-standard-class <number> (<top>)) | |
1007 | (define-standard-class <complex> (<number>)) | |
1008 | (define-standard-class <real> (<complex>)) | |
1009 | (define-standard-class <integer> (<real>)) | |
1010 | (define-standard-class <fraction> (<real>)) | |
1011 | (define-standard-class <keyword> (<top>)) | |
1012 | (define-standard-class <unknown> (<top>)) | |
1013 | (define-standard-class <procedure> (<applicable>) | |
1014 | #:metaclass <procedure-class>) | |
1015 | (define-standard-class <primitive-generic> (<procedure>) | |
1016 | #:metaclass <procedure-class>) | |
1017 | (define-standard-class <port> (<top>)) | |
1018 | (define-standard-class <input-port> (<port>)) | |
1019 | (define-standard-class <output-port> (<port>)) | |
1020 | (define-standard-class <input-output-port> (<input-port> <output-port>)) | |
1021 | ||
f37bece4 AW |
1022 | (define (inherit-applicable! class) |
1023 | "An internal routine to redefine a SMOB class that was added after | |
1024 | GOOPS was loaded, and on which scm_set_smob_apply installed an apply | |
1025 | function." | |
1026 | ;; Why not use class-redefinition? We would, except that loading the | |
1027 | ;; compiler to compile effective methods can happen while GOOPS has | |
1028 | ;; only been partially loaded, and loading the compiler might cause | |
1029 | ;; SMOB types to be defined that need this facility. Instead we make | |
1030 | ;; a very specific hack, not a general solution. Probably the right | |
1031 | ;; solution is to avoid using the compiler, but that is another kettle | |
1032 | ;; of fish. | |
1033 | (unless (memq <applicable> (class-precedence-list class)) | |
1034 | (unless (null? (class-slots class)) | |
1035 | (error "SMOB object has slots?")) | |
1036 | (for-each | |
1037 | (lambda (super) | |
1038 | (let ((subclasses (struct-ref super class-index-direct-subclasses))) | |
1039 | (struct-set! super class-index-direct-subclasses | |
1040 | (delq class subclasses)))) | |
1041 | (struct-ref class class-index-direct-supers)) | |
1042 | (struct-set! class class-index-direct-supers (list <applicable>)) | |
1043 | (struct-set! class class-index-cpl (compute-cpl class)) | |
1044 | (let ((subclasses (struct-ref <applicable> class-index-direct-subclasses))) | |
1045 | (struct-set! <applicable> class-index-direct-subclasses | |
1046 | (cons class subclasses))))) | |
1047 | ||
9ae00706 AW |
1048 | |
1049 | \f | |
1050 | ||
1051 | ;;; | |
1052 | ;;; At this point we have defined the class hierarchy, and it's time to | |
1053 | ;;; move on to instance allocation and generics. Once we have generics, | |
1054 | ;;; we'll fill out the metaobject protocol. | |
1055 | ;;; | |
1056 | ;;; Here we define a limited version of `make', so that we can allocate | |
1057 | ;;; instances of specific classes. This definition will be replaced | |
1058 | ;;; later. | |
1059 | ;;; | |
e0590e7c | 1060 | (define (%invalidate-method-cache! gf) |
0d96acac AW |
1061 | (slot-set! gf 'effective-methods '()) |
1062 | (recompute-generic-function-dispatch-procedure! gf)) | |
e0590e7c AW |
1063 | |
1064 | ;; Boot definition. | |
1065 | (define (invalidate-method-cache! gf) | |
1066 | (%invalidate-method-cache! gf)) | |
1067 | ||
e0590e7c AW |
1068 | (define (make class . args) |
1069 | (cond | |
1070 | ((or (eq? class <generic>) (eq? class <accessor>)) | |
1071 | (let ((z (make-struct/no-tail class #f '() 0 '()))) | |
1072 | (set-procedure-property! z 'name (get-keyword #:name args #f)) | |
1073 | (invalidate-method-cache! z) | |
1074 | (when (eq? class <accessor>) | |
1075 | (let ((setter (get-keyword #:setter args #f))) | |
1076 | (when setter | |
6c7dd9eb | 1077 | (slot-set! z 'setter setter)))) |
e0590e7c AW |
1078 | z)) |
1079 | (else | |
07452c83 | 1080 | (let ((z (%allocate-instance class))) |
e0590e7c AW |
1081 | (cond |
1082 | ((or (eq? class <method>) (eq? class <accessor-method>)) | |
1083 | (for-each (match-lambda | |
1084 | ((kw slot default) | |
1085 | (slot-set! z slot (get-keyword kw args default)))) | |
1086 | '((#:generic-function generic-function #f) | |
1087 | (#:specializers specializers ()) | |
1088 | (#:procedure procedure #f) | |
1089 | (#:formals formals ()) | |
1090 | (#:body body ()) | |
1091 | (#:make-procedure make-procedure #f)))) | |
1092 | ((memq <class> (class-precedence-list class)) | |
761338f6 AW |
1093 | (class-add-flags! z (logior vtable-flag-goops-class |
1094 | vtable-flag-goops-valid)) | |
e0590e7c AW |
1095 | (for-each (match-lambda |
1096 | ((kw slot default) | |
1097 | (slot-set! z slot (get-keyword kw args default)))) | |
1098 | '((#:name name ???) | |
1099 | (#:dsupers direct-supers ()) | |
f5c34767 | 1100 | (#:slots direct-slots ())))) |
e0590e7c AW |
1101 | (else |
1102 | (error "boot `make' does not support this class" class))) | |
1103 | z)))) | |
1104 | ||
9ae00706 AW |
1105 | |
1106 | \f | |
1107 | ||
1108 | ;;; | |
568174d1 | 1109 | ;;; Slot access. |
b89432ff AW |
1110 | ;;; |
1111 | ;;; Before we go on, some notes about class redefinition. In GOOPS, | |
1112 | ;;; classes can be redefined. Redefinition of a class marks the class | |
1113 | ;;; as invalid, and instances will be lazily migrated over to the new | |
1114 | ;;; representation as they are accessed. Migration happens when | |
1115 | ;;; `class-of' is called on an instance. For more technical details on | |
1116 | ;;; object redefinition, see struct.h. | |
1117 | ;;; | |
1118 | ;;; In the following interfaces, class-of handles the redefinition | |
1119 | ;;; protocol. I would think though that there is some thread-unsafety | |
1120 | ;;; here though as the { class, object data } pair needs to be accessed | |
1121 | ;;; atomically, not the { class, object } pair. | |
1122 | ;;; | |
c4974c57 AW |
1123 | (define-inlinable (%class-slot-definition class slot-name kt kf) |
1124 | (let lp ((slots (struct-ref class class-index-slots))) | |
1125 | (match slots | |
1126 | ((slot . slots) | |
2a3ef7c4 | 1127 | (if (eq? (%slot-definition-name slot) slot-name) |
c4974c57 AW |
1128 | (kt slot) |
1129 | (lp slots))) | |
1130 | (_ (kf))))) | |
1131 | ||
1132 | (define (class-slot-definition class slot-name) | |
1133 | (unless (class? class) | |
1134 | (scm-error 'wrong-type-arg #f "Not a class: ~S" (list class) #f)) | |
1135 | (%class-slot-definition class slot-name | |
1136 | (lambda (slot) slot) | |
1137 | (lambda () #f))) | |
ade4cf4c AW |
1138 | |
1139 | (define (slot-ref obj slot-name) | |
1140 | "Return the value from @var{obj}'s slot with the nam var{slot_name}." | |
c4974c57 | 1141 | (let ((class (class-of obj))) |
c4974c57 | 1142 | (define (have-slot slot) |
4bde3f04 | 1143 | ((%slot-definition-slot-ref slot) obj)) |
c4974c57 AW |
1144 | (define (no-slot) |
1145 | (unless (symbol? slot-name) | |
1146 | (scm-error 'wrong-type-arg #f "Not a symbol: ~S" | |
1147 | (list slot-name) #f)) | |
1148 | (let ((val (slot-missing class obj slot-name))) | |
1149 | (if (unbound? val) | |
1150 | (slot-unbound class obj slot-name) | |
1151 | val))) | |
1152 | (%class-slot-definition class slot-name have-slot no-slot))) | |
ade4cf4c AW |
1153 | |
1154 | (define (slot-set! obj slot-name value) | |
1155 | "Set the slot named @var{slot_name} of @var{obj} to @var{value}." | |
c4974c57 AW |
1156 | (let ((class (class-of obj))) |
1157 | (define (have-slot slot) | |
4bde3f04 | 1158 | ((%slot-definition-slot-set! slot) obj value)) |
c4974c57 AW |
1159 | (define (no-slot) |
1160 | (unless (symbol? slot-name) | |
1161 | (scm-error 'wrong-type-arg #f "Not a symbol: ~S" | |
1162 | (list slot-name) #f)) | |
1163 | (slot-missing class obj slot-name value)) | |
1164 | ||
1165 | (%class-slot-definition class slot-name have-slot no-slot))) | |
ade4cf4c AW |
1166 | |
1167 | (define (slot-bound? obj slot-name) | |
1168 | "Return the value from @var{obj}'s slot with the nam var{slot_name}." | |
c4974c57 | 1169 | (let ((class (class-of obj))) |
c4974c57 | 1170 | (define (have-slot slot) |
4bde3f04 | 1171 | (not (unbound? ((%slot-definition-slot-ref/raw slot) obj)))) |
c4974c57 AW |
1172 | (define (no-slot) |
1173 | (unless (symbol? slot-name) | |
1174 | (scm-error 'wrong-type-arg #f "Not a symbol: ~S" | |
1175 | (list slot-name) #f)) | |
4bde3f04 | 1176 | (not (unbound? (slot-missing class obj slot-name)))) |
c4974c57 | 1177 | (%class-slot-definition class slot-name have-slot no-slot))) |
ade4cf4c AW |
1178 | |
1179 | (define (slot-exists? obj slot-name) | |
1180 | "Return @code{#t} if @var{obj} has a slot named @var{slot_name}." | |
c4974c57 AW |
1181 | (define (have-slot slot) #t) |
1182 | (define (no-slot) | |
1183 | (unless (symbol? slot-name) | |
1184 | (scm-error 'wrong-type-arg #f "Not a symbol: ~S" | |
1185 | (list slot-name) #f)) | |
1186 | #f) | |
1187 | (%class-slot-definition (class-of obj) slot-name have-slot no-slot)) | |
ade4cf4c | 1188 | |
2bcb278a AW |
1189 | (begin-deprecated |
1190 | (define (check-slot-args class obj slot-name) | |
1191 | (unless (eq? class (class-of obj)) | |
1192 | (scm-error 'wrong-type-arg #f "~S is not the class of ~S" | |
1193 | (list class obj) #f)) | |
1194 | (unless (symbol? slot-name) | |
1195 | (scm-error 'wrong-type-arg #f "Not a symbol: ~S" | |
1196 | (list slot-name) #f))) | |
1197 | ||
1198 | (define (slot-ref-using-class class obj slot-name) | |
1199 | (issue-deprecation-warning "slot-ref-using-class is deprecated. " | |
1200 | "Use slot-ref instead.") | |
1201 | (check-slot-args class obj slot-name) | |
1202 | (slot-ref obj slot-name)) | |
1203 | ||
1204 | (define (slot-set-using-class! class obj slot-name value) | |
1205 | (issue-deprecation-warning "slot-set-using-class! is deprecated. " | |
1206 | "Use slot-set! instead.") | |
1207 | (check-slot-args class obj slot-name) | |
1208 | (slot-set! obj slot-name value)) | |
1209 | ||
1210 | (define (slot-bound-using-class? class obj slot-name) | |
1211 | (issue-deprecation-warning "slot-bound-using-class? is deprecated. " | |
1212 | "Use slot-bound? instead.") | |
1213 | (check-slot-args class obj slot-name) | |
1214 | (slot-bound? obj slot-name)) | |
1215 | ||
1216 | (define (slot-exists-using-class? class obj slot-name) | |
1217 | (issue-deprecation-warning "slot-exists-using-class? is deprecated. " | |
1218 | "Use slot-exists? instead.") | |
1219 | (check-slot-args class obj slot-name) | |
1220 | (slot-exists? obj slot-name))) | |
1221 | ||
b89432ff AW |
1222 | |
1223 | \f | |
1224 | ||
1225 | ;;; | |
1226 | ;;; Method accessors. | |
1227 | ;;; | |
48c981c9 AW |
1228 | (define (method-generic-function obj) |
1229 | "Return the generic function for the method @var{obj}." | |
1230 | (unless (is-a? obj <method>) | |
1231 | (scm-error 'wrong-type-arg #f "Not a method: ~S" | |
1232 | (list obj) #f)) | |
1233 | (slot-ref obj 'generic-function)) | |
1234 | ||
1235 | (define (method-specializers obj) | |
1236 | "Return specializers of the method @var{obj}." | |
1237 | (unless (is-a? obj <method>) | |
1238 | (scm-error 'wrong-type-arg #f "Not a method: ~S" | |
1239 | (list obj) #f)) | |
1240 | (slot-ref obj 'specializers)) | |
1241 | ||
1242 | (define (method-procedure obj) | |
1243 | "Return the procedure of the method @var{obj}." | |
1244 | (unless (is-a? obj <method>) | |
1245 | (scm-error 'wrong-type-arg #f "Not a method: ~S" | |
1246 | (list obj) #f)) | |
1247 | (slot-ref obj 'procedure)) | |
1248 | ||
b89432ff AW |
1249 | |
1250 | \f | |
1251 | ||
1252 | ;;; | |
1253 | ;;; Generic functions! | |
e0590e7c AW |
1254 | ;;; |
1255 | ;;; Generic functions have an applicable-methods cache associated with | |
1256 | ;;; them. Every distinct set of types that is dispatched through a | |
0d96acac AW |
1257 | ;;; generic adds an entry to the cache. A composite dispatch procedure |
1258 | ;;; is recomputed every time an entry gets added to the cache, or when | |
1259 | ;;; the cache is invalidated. | |
1260 | ;;; | |
1261 | ;;; In steady-state, this dispatch procedure is never regenerated; but | |
1262 | ;;; during warm-up there is some churn. | |
e0590e7c AW |
1263 | ;;; |
1264 | ;;; So what is the deal if warm-up happens in a multithreaded context? | |
1265 | ;;; There is indeed a window between missing the cache for a certain set | |
1266 | ;;; of arguments, and then updating the cache with the newly computed | |
1267 | ;;; applicable methods. One of the updaters is liable to lose their new | |
1268 | ;;; entry. | |
1269 | ;;; | |
1270 | ;;; This is actually OK though, because a subsequent cache miss for the | |
1271 | ;;; race loser will just cause memoization to try again. The cache will | |
0d96acac | 1272 | ;;; eventually be consistent. We're not mutating the old part of the |
e0590e7c AW |
1273 | ;;; cache, just consing on the new entry. |
1274 | ;;; | |
1275 | ;;; It doesn't even matter if the dispatch procedure and the cache are | |
1276 | ;;; inconsistent -- most likely the type-set that lost the dispatch | |
1277 | ;;; procedure race will simply re-trigger a memoization, but since the | |
1278 | ;;; winner isn't in the effective-methods cache, it will likely also | |
1279 | ;;; re-trigger a memoization, and the cache will finally be consistent. | |
1280 | ;;; As you can see there is a possibility for ping-pong effects, but | |
1281 | ;;; it's unlikely given the shortness of the window between slot-set! | |
0d96acac AW |
1282 | ;;; invocations. |
1283 | ;;; | |
1284 | ;;; We probably do need to use atomic access primitives to correctly | |
1285 | ;;; handle concurrency, but that's a more general Guile concern. | |
1286 | ;;; | |
e0590e7c | 1287 | |
0d96acac AW |
1288 | (define-syntax arity-case |
1289 | (lambda (x) | |
1290 | (syntax-case x () | |
1291 | ;; (arity-case n 2 foo bar) | |
1292 | ;; => (case n | |
1293 | ;; ((0) (foo)) | |
1294 | ;; ((1) (foo a)) | |
1295 | ;; ((2) (foo a b)) | |
1296 | ;; (else bar)) | |
1297 | ((arity-case n max form alternate) | |
1298 | (let ((max (syntax->datum #'max))) | |
1299 | #`(case n | |
1300 | #,@(let lp ((n 0)) | |
1301 | (let ((ids (map (lambda (n) | |
1302 | (let* ((n (+ (char->integer #\a) n)) | |
1303 | (c (integer->char n))) | |
1304 | (datum->syntax #'here (symbol c)))) | |
1305 | (iota n)))) | |
1306 | #`(((#,n) (form #,@ids)) | |
1307 | . #,(if (< n max) | |
1308 | (lp (1+ n)) | |
1309 | #'())))) | |
1310 | (else alternate))))))) | |
1311 | ||
1312 | ;;; | |
1313 | ;;; These dispatchers are set as the "procedure" field of <generic> | |
1314 | ;;; instances. Unlike CLOS, in GOOPS a generic function can have | |
1315 | ;;; multiple arities. | |
1316 | ;;; | |
1317 | ;;; We pre-generate fast dispatchers for applications of up to 20 | |
1318 | ;;; arguments. More arguments than that will go through slower generic | |
1319 | ;;; routines that cons arguments into a rest list. | |
1320 | ;;; | |
1321 | (define (multiple-arity-dispatcher fv miss) | |
1322 | (define-syntax dispatch | |
1323 | (lambda (x) | |
1324 | (define (build-clauses args) | |
1325 | (let ((len (length (syntax->datum args)))) | |
1326 | #`((#,args ((vector-ref fv #,len) . #,args)) | |
1327 | . #,(syntax-case args () | |
1328 | (() #'()) | |
1329 | ((arg ... _) (build-clauses #'(arg ...))))))) | |
1330 | (syntax-case x () | |
1331 | ((dispatch arg ...) | |
1332 | #`(case-lambda | |
1333 | #,@(build-clauses #'(arg ...)) | |
1334 | (args (apply miss args))))))) | |
1335 | (arity-case (vector-length fv) 20 dispatch | |
1336 | (lambda args | |
1337 | (let ((nargs (length args))) | |
1338 | (if (< nargs (vector-length fv)) | |
1339 | (apply (vector-ref fv nargs) args) | |
1340 | (apply miss args)))))) | |
1341 | ||
1342 | ;;; | |
1343 | ;;; The above multiple-arity-dispatcher is entirely sufficient, and | |
1344 | ;;; should be fast enough. Still, for no good reason we also have an | |
1345 | ;;; arity dispatcher for generics that are only called with one arity. | |
1346 | ;;; | |
1347 | (define (single-arity-dispatcher f nargs miss) | |
1348 | (define-syntax-rule (dispatch arg ...) | |
1349 | (case-lambda | |
1350 | ((arg ...) (f arg ...)) | |
1351 | (args (apply miss args)))) | |
1352 | (arity-case nargs 20 dispatch | |
1353 | (lambda args | |
1354 | (if (eqv? (length args) nargs) | |
1355 | (apply f args) | |
1356 | (apply miss args))))) | |
1357 | ||
1358 | ;;; | |
1359 | ;;; The guts of generic function dispatch are here. Once we've selected | |
1360 | ;;; an arity, we need to map from arguments to effective method. Until | |
1361 | ;;; we have `eqv?' specializers, this map is entirely a function of the | |
1362 | ;;; types (classes) of the arguments. So, we look in the cache to see | |
1363 | ;;; if we have seen this set of concrete types, and if so we apply the | |
1364 | ;;; previously computed effective method. Otherwise we miss the cache, | |
1365 | ;;; so we'll have to compute the right answer for this set of types, add | |
1366 | ;;; the mapping to the cache, and apply the newly computed method. | |
1367 | ;;; | |
1368 | ;;; The cached mapping is invalidated whenever a new method is defined | |
1369 | ;;; on this generic, or whenever the class hierarchy of any method | |
1370 | ;;; specializer changes. | |
1371 | ;;; | |
1372 | (define (single-arity-cache-dispatch cache nargs cache-miss) | |
1373 | (match cache | |
1374 | (() cache-miss) | |
d21ef268 | 1375 | (((typev . cmethod) . cache) |
0d96acac | 1376 | (cond |
d21ef268 | 1377 | ((eqv? nargs (vector-length typev)) |
0d96acac | 1378 | (let ((cache-miss (single-arity-cache-dispatch cache nargs cache-miss))) |
d21ef268 AW |
1379 | (define (type-ref n) |
1380 | (and (< n nargs) (vector-ref typev n))) | |
0d96acac AW |
1381 | (define-syntax args-match? |
1382 | (syntax-rules () | |
1383 | ((args-match?) #t) | |
1384 | ((args-match? (arg type) (arg* type*) ...) | |
1385 | ;; Check that the arg has the exact type that we saw. It | |
1386 | ;; could be that `type' is #f, which indicates the end of | |
1387 | ;; the specializers list. Once all specializers have been | |
1388 | ;; examined, we don't need to look at any more arguments | |
1389 | ;; to know that this is a cache hit. | |
1390 | (or (not type) | |
1391 | (and (eq? (class-of arg) type) | |
1392 | (args-match? (arg* type*) ...)))))) | |
1393 | (define-syntax dispatch | |
1394 | (lambda (x) | |
1395 | (define (bind-types types k) | |
1396 | (let lp ((types types) (n 0)) | |
1397 | (syntax-case types () | |
1398 | (() (k)) | |
1399 | ((type . types) | |
1400 | #`(let ((type (type-ref #,n))) | |
1401 | #,(lp #'types (1+ n))))))) | |
1402 | (syntax-case x () | |
1403 | ((dispatch arg ...) | |
1404 | (with-syntax (((type ...) (generate-temporaries #'(arg ...)))) | |
1405 | (bind-types | |
1406 | #'(type ...) | |
1407 | (lambda () | |
1408 | #'(lambda (arg ...) | |
1409 | (if (args-match? (arg type) ...) | |
1410 | (cmethod arg ...) | |
1411 | (cache-miss arg ...)))))))))) | |
1412 | (arity-case nargs 20 dispatch | |
1413 | (lambda args | |
1414 | (define (args-match? args) | |
d21ef268 AW |
1415 | (let lp ((args args) (n 0)) |
1416 | (match args | |
1417 | ((arg . args) | |
1418 | (or (not (vector-ref typev n)) | |
1419 | (and (eq? (vector-ref typev n) (class-of arg)) | |
1420 | (lp args (1+ n))))) | |
0d96acac AW |
1421 | (_ #t)))) |
1422 | (if (args-match? args) | |
1423 | (apply cmethod args) | |
1424 | (apply cache-miss args)))))) | |
1425 | (else | |
1426 | (single-arity-cache-dispatch cache nargs cache-miss)))))) | |
1427 | ||
1428 | (define (compute-generic-function-dispatch-procedure gf) | |
1429 | (define (seen-arities cache) | |
1430 | (let lp ((arities 0) (cache cache)) | |
1431 | (match cache | |
1432 | (() arities) | |
d21ef268 AW |
1433 | (((typev . cmethod) . cache) |
1434 | (lp (logior arities (ash 1 (vector-length typev))) | |
1435 | cache))))) | |
0d96acac AW |
1436 | (define (cache-miss . args) |
1437 | (memoize-generic-function-application! gf args) | |
1438 | (apply gf args)) | |
1439 | (let* ((cache (slot-ref gf 'effective-methods)) | |
1440 | (arities (seen-arities cache)) | |
1441 | (max-arity (let lp ((max -1)) | |
1442 | (if (< arities (ash 1 (1+ max))) | |
1443 | max | |
1444 | (lp (1+ max)))))) | |
1445 | (cond | |
1446 | ((= max-arity -1) | |
1447 | ;; Nothing in the cache. | |
1448 | cache-miss) | |
1449 | ((= arities (ash 1 max-arity)) | |
1450 | ;; Only one arity in the cache. | |
d21ef268 AW |
1451 | (let* ((nargs max-arity) |
1452 | (f (single-arity-cache-dispatch cache nargs cache-miss))) | |
1453 | (single-arity-dispatcher f nargs cache-miss))) | |
0d96acac AW |
1454 | (else |
1455 | ;; Multiple arities. | |
1456 | (let ((fv (make-vector (1+ max-arity) #f))) | |
1457 | (let lp ((n 0)) | |
1458 | (when (<= n max-arity) | |
1459 | (let ((f (single-arity-cache-dispatch cache n cache-miss))) | |
1460 | (vector-set! fv n f) | |
1461 | (lp (1+ n))))) | |
1462 | (multiple-arity-dispatcher fv cache-miss)))))) | |
1463 | ||
1464 | (define (recompute-generic-function-dispatch-procedure! gf) | |
1465 | (slot-set! gf 'procedure | |
1466 | (compute-generic-function-dispatch-procedure gf))) | |
e0590e7c AW |
1467 | |
1468 | (define (memoize-effective-method! gf args applicable) | |
d21ef268 AW |
1469 | (define (record-types args) |
1470 | (let ((typev (make-vector (length args) #f))) | |
1471 | (let lp ((n 0) (args args)) | |
1472 | (when (and (< n (slot-ref gf 'n-specialized)) | |
1473 | (pair? args)) | |
1474 | (match args | |
1475 | ((arg . args) | |
1476 | (vector-set! typev n (class-of arg)) | |
1477 | (lp (1+ n) args))))) | |
1478 | typev)) | |
1479 | (let* ((typev (record-types args)) | |
e7097386 AW |
1480 | (compute-effective-method (if (eq? (class-of gf) <generic>) |
1481 | %compute-effective-method | |
1482 | compute-effective-method)) | |
1483 | (cmethod (compute-effective-method gf applicable typev)) | |
d21ef268 AW |
1484 | (cache (acons typev cmethod (slot-ref gf 'effective-methods)))) |
1485 | (slot-set! gf 'effective-methods cache) | |
1486 | (recompute-generic-function-dispatch-procedure! gf) | |
1487 | cmethod)) | |
51fd1cd6 | 1488 | |
e0590e7c | 1489 | ;;; |
0d96acac AW |
1490 | ;;; If a method refers to `next-method' in its body, that method will be |
1491 | ;;; able to dispatch to the next most specific method. The exact | |
1492 | ;;; `next-method' implementation is only known at runtime, as it is a | |
1493 | ;;; function of which precise argument types are being dispatched, which | |
1494 | ;;; might be subclasses of the method's declared specializers. | |
e0590e7c | 1495 | ;;; |
0d96acac AW |
1496 | ;;; Guile implements `next-method' by binding it as a closure variable. |
1497 | ;;; An effective method is bound to a specific `next-method' by the | |
1498 | ;;; `make-procedure' slot of a <method>, which returns the new closure. | |
e0590e7c | 1499 | ;;; |
e7097386 AW |
1500 | (define (%compute-specialized-effective-method gf method types next-method) |
1501 | (match (slot-ref method 'make-procedure) | |
1502 | (#f (method-procedure method)) | |
1503 | (make-procedure (make-procedure next-method)))) | |
1504 | ||
1505 | (define (compute-specialized-effective-method gf method types next-method) | |
1506 | (%compute-specialized-effective-method gf method types next-method)) | |
1507 | ||
1508 | (define (%compute-effective-method gf methods types) | |
0d96acac AW |
1509 | (match methods |
1510 | ((method . methods) | |
e7097386 AW |
1511 | (let ((compute-specialized-effective-method |
1512 | (if (and (eq? (class-of gf) <generic>) | |
1513 | (eq? (class-of method) <method>)) | |
1514 | %compute-specialized-effective-method | |
1515 | compute-specialized-effective-method))) | |
1516 | (compute-specialized-effective-method | |
1517 | gf method types | |
1518 | (match methods | |
1519 | (() | |
1520 | (lambda args | |
1521 | (no-next-method gf args))) | |
1522 | (methods | |
1523 | (let ((compute-effective-method (if (eq? (class-of gf) <generic>) | |
1524 | %compute-effective-method | |
1525 | compute-effective-method))) | |
1526 | (compute-effective-method gf methods types))))))))) | |
1527 | ||
1528 | ;; Boot definition; overrided with a generic later. | |
1529 | (define (compute-effective-method gf methods types) | |
1530 | (%compute-effective-method gf methods types)) | |
e0590e7c AW |
1531 | |
1532 | ;;; | |
1533 | ;;; Memoization | |
1534 | ;;; | |
1535 | ||
0d96acac | 1536 | (define (memoize-generic-function-application! gf args) |
e7097386 | 1537 | (let ((applicable ((if (eq? (class-of gf) <generic>) |
8dfc0ba5 AW |
1538 | %compute-applicable-methods |
1539 | compute-applicable-methods) | |
1540 | gf args))) | |
e0590e7c AW |
1541 | (cond (applicable |
1542 | (memoize-effective-method! gf args applicable)) | |
8dfc0ba5 AW |
1543 | (else |
1544 | (no-applicable-method gf args))))) | |
e0590e7c | 1545 | |
e0590e7c AW |
1546 | (define no-applicable-method |
1547 | (make <generic> #:name 'no-applicable-method)) | |
1548 | ||
1549 | (%goops-early-init) | |
82ab5090 | 1550 | |
78ec533c | 1551 | ;; Then load the rest of GOOPS |
14f1d9fe | 1552 | |
14f1d9fe | 1553 | \f |
51385517 | 1554 | ;; FIXME: deprecate. |
e0590e7c AW |
1555 | (define min-fixnum (- (expt 2 29))) |
1556 | (define max-fixnum (- (expt 2 29) 1)) | |
14f1d9fe MD |
1557 | |
1558 | ;; | |
1559 | ;; goops-error | |
1560 | ;; | |
1561 | (define (goops-error format-string . args) | |
14f1d9fe MD |
1562 | (scm-error 'goops-error #f format-string args '())) |
1563 | ||
14f1d9fe MD |
1564 | ;;; |
1565 | ;;; {Meta classes} | |
1566 | ;;; | |
1567 | ||
1568 | (define ensure-metaclass-with-supers | |
1569 | (let ((table-of-metas '())) | |
1570 | (lambda (meta-supers) | |
1571 | (let ((entry (assoc meta-supers table-of-metas))) | |
8dfc0ba5 AW |
1572 | (if entry |
1573 | ;; Found a previously created metaclass | |
1574 | (cdr entry) | |
1575 | ;; Create a new meta-class which inherit from "meta-supers" | |
1576 | (let ((new (make <class> #:dsupers meta-supers | |
1577 | #:slots '() | |
1578 | #:name (gensym "metaclass")))) | |
1579 | (set! table-of-metas (cons (cons meta-supers new) table-of-metas)) | |
1580 | new)))))) | |
14f1d9fe | 1581 | |
9d019f9b | 1582 | (define (ensure-metaclass supers) |
14f1d9fe MD |
1583 | (if (null? supers) |
1584 | <class> | |
1585 | (let* ((all-metas (map (lambda (x) (class-of x)) supers)) | |
8dfc0ba5 AW |
1586 | (all-cpls (append-map (lambda (m) |
1587 | (cdr (class-precedence-list m))) | |
af35fc20 | 1588 | all-metas)) |
8dfc0ba5 AW |
1589 | (needed-metas '())) |
1590 | ;; Find the most specific metaclasses. The new metaclass will be | |
1591 | ;; a subclass of these. | |
1592 | (for-each | |
1593 | (lambda (meta) | |
bacc8829 | 1594 | (when (and (not (member meta all-cpls)) |
8dfc0ba5 AW |
1595 | (not (member meta needed-metas))) |
1596 | (set! needed-metas (append needed-metas (list meta))))) | |
1597 | all-metas) | |
1598 | ;; Now return a subclass of the metaclasses we found. | |
1599 | (if (null? (cdr needed-metas)) | |
1600 | (car needed-metas) ; If there's only one, just use it. | |
1601 | (ensure-metaclass-with-supers needed-metas))))) | |
14f1d9fe MD |
1602 | |
1603 | ;;; | |
1604 | ;;; {Classes} | |
1605 | ;;; | |
1606 | ||
1607 | ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...) | |
1608 | ;;; | |
568174d1 | 1609 | ;;; SLOT-DEFINITION ::= INSTANCE-OF-<SLOT> | (SLOT-NAME OPTION ...) |
14f1d9fe MD |
1610 | ;;; OPTION ::= KEYWORD VALUE |
1611 | ;;; | |
14f1d9fe | 1612 | |
b3501b80 | 1613 | (define (make-class supers slots . options) |
06d54b3f AW |
1614 | (define (find-duplicate l) |
1615 | (match l | |
1616 | (() #f) | |
1617 | ((head . tail) | |
1618 | (if (memq head tail) | |
1619 | head | |
1620 | (find-duplicate tail))))) | |
568174d1 AW |
1621 | (define (slot-spec->name slot-spec) |
1622 | (match slot-spec | |
1623 | (((? symbol? name) . args) name) | |
1624 | ;; We can get here when redefining classes. | |
2a3ef7c4 | 1625 | ((? slot? slot) (%slot-definition-name slot)))) |
06d54b3f | 1626 | |
567a6d1e | 1627 | (let* ((name (get-keyword #:name options *unbound*)) |
9d019f9b AW |
1628 | (supers (if (not (or-map (lambda (class) |
1629 | (memq <object> | |
1630 | (class-precedence-list class))) | |
1631 | supers)) | |
1632 | (append supers (list <object>)) | |
1633 | supers)) | |
1634 | (metaclass (or (get-keyword #:metaclass options #f) | |
1635 | (ensure-metaclass supers)))) | |
1636 | ||
1637 | ;; Verify that all direct slots are different and that we don't inherit | |
1638 | ;; several time from the same class | |
1639 | (let ((tmp1 (find-duplicate supers)) | |
568174d1 | 1640 | (tmp2 (find-duplicate (map slot-spec->name slots)))) |
9d019f9b AW |
1641 | (if tmp1 |
1642 | (goops-error "make-class: super class ~S is duplicate in class ~S" | |
1643 | tmp1 name)) | |
1644 | (if tmp2 | |
1645 | (goops-error "make-class: slot ~S is duplicate in class ~S" | |
1646 | tmp2 name))) | |
1647 | ||
1648 | ;; Everything seems correct, build the class | |
1649 | (apply make metaclass | |
1650 | #:dsupers supers | |
8dfc0ba5 | 1651 | #:slots slots |
9d019f9b AW |
1652 | #:name name |
1653 | options))) | |
14f1d9fe MD |
1654 | |
1655 | ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...) | |
1656 | ;;; | |
1657 | ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...) | |
1658 | ;;; OPTION ::= KEYWORD VALUE | |
1659 | ;;; | |
f840ed25 AW |
1660 | (define-syntax class |
1661 | (lambda (x) | |
1662 | (define (parse-options options) | |
1663 | (syntax-case options () | |
1664 | (() #'()) | |
1665 | ((kw arg . options) (keyword? (syntax->datum #'kw)) | |
1666 | (with-syntax ((options (parse-options #'options))) | |
1667 | (syntax-case #'kw () | |
1668 | (#:init-form | |
1669 | #'(kw 'arg #:init-thunk (lambda () arg) . options)) | |
1670 | (_ | |
1671 | #'(kw arg . options))))))) | |
1672 | (define (check-valid-kwargs args) | |
1673 | (syntax-case args () | |
1674 | (() #'()) | |
1675 | ((kw arg . args) (keyword? (syntax->datum #'kw)) | |
1676 | #`(kw arg . #,(check-valid-kwargs #'args))))) | |
1677 | (define (parse-slots-and-kwargs args) | |
1678 | (syntax-case args () | |
1679 | (() | |
1680 | #'(() ())) | |
1681 | ((kw . _) (keyword? (syntax->datum #'kw)) | |
1682 | #`(() #,(check-valid-kwargs args))) | |
1683 | (((name option ...) args ...) | |
1684 | (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...))) | |
1685 | ((option ...) (parse-options #'(option ...)))) | |
1686 | #'(((list 'name option ...) . slots) kwargs))) | |
1687 | ((name args ...) (symbol? (syntax->datum #'name)) | |
1688 | (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...)))) | |
1689 | #'(('(name) . slots) kwargs))))) | |
1690 | (syntax-case x () | |
1691 | ((class (super ...) arg ...) | |
1692 | (with-syntax ((((slot-def ...) (option ...)) | |
1693 | (parse-slots-and-kwargs #'(arg ...)))) | |
1694 | #'(make-class (list super ...) | |
1695 | (list slot-def ...) | |
1696 | option ...)))))) | |
14f1d9fe | 1697 | |
b3501b80 AW |
1698 | (define-syntax define-class-pre-definition |
1699 | (lambda (x) | |
1700 | (syntax-case x () | |
1701 | ((_ (k arg rest ...) out ...) | |
0dd8493c AW |
1702 | (keyword? (syntax->datum #'k)) |
1703 | (case (syntax->datum #'k) | |
b3501b80 | 1704 | ((#:getter #:setter) |
0dd8493c AW |
1705 | #'(define-class-pre-definition (rest ...) |
1706 | out ... | |
bacc8829 AW |
1707 | (when (or (not (defined? 'arg)) |
1708 | (not (is-a? arg <generic>))) | |
1709 | (toplevel-define! | |
1710 | 'arg | |
1711 | (ensure-generic (if (defined? 'arg) arg #f) 'arg))))) | |
b3501b80 | 1712 | ((#:accessor) |
0dd8493c AW |
1713 | #'(define-class-pre-definition (rest ...) |
1714 | out ... | |
bacc8829 AW |
1715 | (when (or (not (defined? 'arg)) |
1716 | (not (is-a? arg <accessor>))) | |
1717 | (toplevel-define! | |
1718 | 'arg | |
1719 | (ensure-accessor (if (defined? 'arg) arg #f) 'arg))))) | |
b3501b80 | 1720 | (else |
0dd8493c | 1721 | #'(define-class-pre-definition (rest ...) out ...)))) |
b3501b80 | 1722 | ((_ () out ...) |
0dd8493c | 1723 | #'(begin out ...))))) |
8dfc0ba5 | 1724 | |
b3501b80 AW |
1725 | ;; Some slot options require extra definitions to be made. In |
1726 | ;; particular, we want to make sure that the generic function objects | |
1727 | ;; which represent accessors exist before `make-class' tries to add | |
1728 | ;; methods to them. | |
1729 | (define-syntax define-class-pre-definitions | |
1730 | (lambda (x) | |
1731 | (syntax-case x () | |
1732 | ((_ () out ...) | |
0dd8493c | 1733 | #'(begin out ...)) |
b3501b80 | 1734 | ((_ (slot rest ...) out ...) |
0dd8493c AW |
1735 | (keyword? (syntax->datum #'slot)) |
1736 | #'(begin out ...)) | |
b3501b80 | 1737 | ((_ (slot rest ...) out ...) |
0dd8493c AW |
1738 | (identifier? #'slot) |
1739 | #'(define-class-pre-definitions (rest ...) | |
1740 | out ...)) | |
b3501b80 | 1741 | ((_ ((slotname slotopt ...) rest ...) out ...) |
8dfc0ba5 | 1742 | #'(define-class-pre-definitions (rest ...) |
0dd8493c | 1743 | out ... (define-class-pre-definition (slotopt ...))))))) |
b3501b80 | 1744 | |
0c65f52c AW |
1745 | (define-syntax-rule (define-class name supers slot ...) |
1746 | (begin | |
1747 | (define-class-pre-definitions (slot ...)) | |
1748 | (if (and (defined? 'name) | |
1749 | (is-a? name <class>) | |
1750 | (memq <object> (class-precedence-list name))) | |
1751 | (class-redefinition name | |
1752 | (class supers slot ... #:name 'name)) | |
1753 | (toplevel-define! 'name (class supers slot ... #:name 'name))))) | |
8dfc0ba5 | 1754 | |
0c65f52c AW |
1755 | (define-syntax-rule (standard-define-class arg ...) |
1756 | (define-class arg ...)) | |
14f1d9fe MD |
1757 | |
1758 | ;;; | |
1759 | ;;; {Generic functions and accessors} | |
1760 | ;;; | |
1761 | ||
1d83f47e AW |
1762 | ;; Apparently the desired semantics are that we extend previous |
1763 | ;; procedural definitions, but that if `name' was already a generic, we | |
1764 | ;; overwrite its definition. | |
7cb88cbc AW |
1765 | (define-syntax define-generic |
1766 | (lambda (x) | |
1767 | (syntax-case x () | |
1768 | ((define-generic name) (symbol? (syntax->datum #'name)) | |
1769 | #'(define name | |
1770 | (if (and (defined? 'name) (is-a? name <generic>)) | |
1771 | (make <generic> #:name 'name) | |
1772 | (ensure-generic (if (defined? 'name) name #f) 'name))))))) | |
1773 | ||
1774 | (define-syntax define-extended-generic | |
1775 | (lambda (x) | |
1776 | (syntax-case x () | |
1777 | ((define-extended-generic name val) (symbol? (syntax->datum #'name)) | |
1778 | #'(define name (make-extended-generic val 'name)))))) | |
1779 | ||
1780 | (define-syntax define-extended-generics | |
1781 | (lambda (x) | |
1782 | (define (id-append ctx a b) | |
1783 | (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b)))) | |
1784 | (syntax-case x () | |
1785 | ((define-extended-generic (name ...) #:prefix (prefix ...)) | |
1786 | (and (and-map symbol? (syntax->datum #'(name ...))) | |
1787 | (and-map symbol? (syntax->datum #'(prefix ...)))) | |
1788 | (with-syntax ((((val ...)) (map (lambda (name) | |
1789 | (map (lambda (prefix) | |
1790 | (id-append name prefix name)) | |
1791 | #'(prefix ...))) | |
1792 | #'(name ...)))) | |
1793 | #'(begin | |
1794 | (define-extended-generic name (list val ...)) | |
1795 | ...)))))) | |
bbf8d523 | 1796 | |
a8c10aa1 AW |
1797 | (define* (make-generic #:optional name) |
1798 | (make <generic> #:name name)) | |
14f1d9fe | 1799 | |
a8c10aa1 AW |
1800 | (define* (make-extended-generic gfs #:optional name) |
1801 | (let* ((gfs (if (list? gfs) gfs (list gfs))) | |
8dfc0ba5 | 1802 | (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs))) |
bbf8d523 | 1803 | (let ((ans (if gws? |
8dfc0ba5 AW |
1804 | (let* ((sname (and name (make-setter-name name))) |
1805 | (setters | |
1806 | (append-map (lambda (gf) | |
1807 | (if (is-a? gf <generic-with-setter>) | |
1808 | (list (ensure-generic (setter gf) | |
1809 | sname)) | |
1810 | '())) | |
1811 | gfs)) | |
1812 | (es (make <extended-generic-with-setter> | |
1813 | #:name name | |
1814 | #:extends gfs | |
1815 | #:setter (make <extended-generic> | |
1816 | #:name sname | |
1817 | #:extends setters)))) | |
1818 | (extended-by! setters (setter es)) | |
1819 | es) | |
1820 | (make <extended-generic> | |
1821 | #:name name | |
1822 | #:extends gfs)))) | |
bbf8d523 MD |
1823 | (extended-by! gfs ans) |
1824 | ans))) | |
1825 | ||
1826 | (define (extended-by! gfs eg) | |
1827 | (for-each (lambda (gf) | |
8dfc0ba5 AW |
1828 | (slot-set! gf 'extended-by |
1829 | (cons eg (slot-ref gf 'extended-by)))) | |
1830 | gfs) | |
eddd81f4 | 1831 | (invalidate-method-cache! eg)) |
bbf8d523 MD |
1832 | |
1833 | (define (not-extended-by! gfs eg) | |
1834 | (for-each (lambda (gf) | |
8dfc0ba5 AW |
1835 | (slot-set! gf 'extended-by |
1836 | (delq! eg (slot-ref gf 'extended-by)))) | |
1837 | gfs) | |
eddd81f4 | 1838 | (invalidate-method-cache! eg)) |
bbf8d523 | 1839 | |
a8c10aa1 AW |
1840 | (define* (ensure-generic old-definition #:optional name) |
1841 | (cond ((is-a? old-definition <generic>) old-definition) | |
1842 | ((procedure-with-setter? old-definition) | |
1843 | (make <generic-with-setter> | |
1844 | #:name name | |
1845 | #:default (procedure old-definition) | |
1846 | #:setter (setter old-definition))) | |
1847 | ((procedure? old-definition) | |
1848 | (if (generic-capability? old-definition) old-definition | |
1849 | (make <generic> #:name name #:default old-definition))) | |
1850 | (else (make <generic> #:name name)))) | |
14f1d9fe | 1851 | |
1d83f47e | 1852 | ;; same semantics as <generic> |
0c65f52c AW |
1853 | (define-syntax-rule (define-accessor name) |
1854 | (define name | |
1855 | (cond ((not (defined? 'name)) (ensure-accessor #f 'name)) | |
1856 | ((is-a? name <accessor>) (make <accessor> #:name 'name)) | |
1857 | (else (ensure-accessor name 'name))))) | |
c31142ee | 1858 | |
14f1d9fe MD |
1859 | (define (make-setter-name name) |
1860 | (string->symbol (string-append "setter:" (symbol->string name)))) | |
1861 | ||
a8c10aa1 AW |
1862 | (define* (make-accessor #:optional name) |
1863 | (make <accessor> | |
1864 | #:name name | |
1865 | #:setter (make <generic> | |
1866 | #:name (and name (make-setter-name name))))) | |
1867 | ||
1868 | (define* (ensure-accessor proc #:optional name) | |
1869 | (cond ((and (is-a? proc <accessor>) | |
1870 | (is-a? (setter proc) <generic>)) | |
1871 | proc) | |
1872 | ((is-a? proc <generic-with-setter>) | |
1873 | (upgrade-accessor proc (setter proc))) | |
1874 | ((is-a? proc <generic>) | |
1875 | (upgrade-accessor proc (make-generic name))) | |
1876 | ((procedure-with-setter? proc) | |
1877 | (make <accessor> | |
1878 | #:name name | |
1879 | #:default (procedure proc) | |
1880 | #:setter (ensure-generic (setter proc) name))) | |
1881 | ((procedure? proc) | |
1882 | (ensure-accessor (if (generic-capability? proc) | |
1883 | (make <generic> #:name name #:default proc) | |
1884 | (ensure-generic proc name)) | |
1885 | name)) | |
1886 | (else | |
1887 | (make-accessor name)))) | |
14f1d9fe | 1888 | |
f8af5c6d | 1889 | (define (upgrade-accessor generic setter) |
bbf8d523 | 1890 | (let ((methods (slot-ref generic 'methods)) |
8dfc0ba5 AW |
1891 | (gws (make (if (is-a? generic <extended-generic>) |
1892 | <extended-generic-with-setter> | |
1893 | <accessor>) | |
1894 | #:name (generic-function-name generic) | |
1895 | #:extended-by (slot-ref generic 'extended-by) | |
1896 | #:setter setter))) | |
bacc8829 AW |
1897 | (when (is-a? generic <extended-generic>) |
1898 | (let ((gfs (slot-ref generic 'extends))) | |
1899 | (not-extended-by! gfs generic) | |
1900 | (slot-set! gws 'extends gfs) | |
1901 | (extended-by! gfs gws))) | |
14f1d9fe MD |
1902 | ;; Steal old methods |
1903 | (for-each (lambda (method) | |
8dfc0ba5 AW |
1904 | (slot-set! method 'generic-function gws)) |
1905 | methods) | |
14f1d9fe | 1906 | (slot-set! gws 'methods methods) |
eddd81f4 | 1907 | (invalidate-method-cache! gws) |
14f1d9fe MD |
1908 | gws)) |
1909 | ||
1910 | ;;; | |
1911 | ;;; {Methods} | |
1912 | ;;; | |
1913 | ||
3a0d1412 AW |
1914 | ;; Note: `a' and `b' can have unequal lengths (i.e. one can be one |
1915 | ;; element longer than the other when we have a dotted parameter | |
1916 | ;; list). For instance, with the call | |
1917 | ;; | |
1918 | ;; (M 1) | |
1919 | ;; | |
1920 | ;; with | |
1921 | ;; | |
1922 | ;; (define-method M (a . l) ....) | |
1923 | ;; (define-method M (a) ....) | |
8dfc0ba5 | 1924 | ;; |
3a0d1412 | 1925 | ;; we consider that the second method is more specific. |
8dfc0ba5 | 1926 | ;; |
3a0d1412 AW |
1927 | ;; Precondition: `a' and `b' are methods and are applicable to `types'. |
1928 | (define (%method-more-specific? a b types) | |
1929 | (let lp ((a-specializers (method-specializers a)) | |
1930 | (b-specializers (method-specializers b)) | |
1931 | (types types)) | |
1932 | (cond | |
1933 | ;; (a) less specific than (a b ...) or (a . b) | |
1934 | ((null? a-specializers) #t) | |
1935 | ;; (a b ...) or (a . b) less specific than (a) | |
1936 | ((null? b-specializers) #f) | |
1937 | ;; (a . b) less specific than (a b ...) | |
1938 | ((not (pair? a-specializers)) #f) | |
1939 | ;; (a b ...) more specific than (a . b) | |
1940 | ((not (pair? b-specializers)) #t) | |
1941 | (else | |
1942 | (let ((a-specializer (car a-specializers)) | |
1943 | (b-specializer (car b-specializers)) | |
1944 | (a-specializers (cdr a-specializers)) | |
1945 | (b-specializers (cdr b-specializers)) | |
1946 | (type (car types)) | |
1947 | (types (cdr types))) | |
1948 | (if (eq? a-specializer b-specializer) | |
1949 | (lp a-specializers b-specializers types) | |
1950 | (let lp ((cpl (class-precedence-list type))) | |
1951 | (let ((elt (car cpl))) | |
1952 | (cond | |
1953 | ((eq? a-specializer elt) #t) | |
1954 | ((eq? b-specializer elt) #f) | |
1955 | (else (lp (cdr cpl)))))))))))) | |
1956 | ||
c0a56ec7 AW |
1957 | (define (%sort-applicable-methods methods types) |
1958 | (sort methods (lambda (a b) (%method-more-specific? a b types)))) | |
1959 | ||
48c981c9 AW |
1960 | (define (generic-function-methods obj) |
1961 | "Return the methods of the generic function @var{obj}." | |
1962 | (define (fold-upward method-lists gf) | |
1963 | (cond | |
1964 | ((is-a? gf <extended-generic>) | |
1965 | (let lp ((method-lists method-lists) (gfs (slot-ref gf 'extends))) | |
1966 | (match gfs | |
1967 | (() method-lists) | |
1968 | ((gf . gfs) | |
1969 | (lp (fold-upward (cons (slot-ref gf 'methods) method-lists) gf) | |
1970 | gfs))))) | |
1971 | (else method-lists))) | |
1972 | (define (fold-downward method-lists gf) | |
1973 | (let lp ((method-lists (cons (slot-ref gf 'methods) method-lists)) | |
1974 | (gfs (slot-ref gf 'extended-by))) | |
1975 | (match gfs | |
1976 | (() method-lists) | |
1977 | ((gf . gfs) | |
1978 | (lp (fold-downward method-lists gf) gfs))))) | |
1979 | (unless (is-a? obj <generic>) | |
1980 | (scm-error 'wrong-type-arg #f "Not a generic: ~S" | |
1981 | (list obj) #f)) | |
1982 | (concatenate (fold-downward (fold-upward '() obj) obj))) | |
1983 | ||
c0a56ec7 AW |
1984 | (define (%compute-applicable-methods gf args) |
1985 | (define (method-applicable? m types) | |
7b0a8dfb | 1986 | (let ((specs (method-specializers m))) |
c0a56ec7 | 1987 | (cond |
7b0a8dfb AW |
1988 | ((and (is-a? m <accessor-method>) |
1989 | (or (null? specs) (null? types) | |
1990 | (not (eq? (car specs) (car types))))) | |
1991 | ;; Slot accessor methods are added to each subclass with the | |
1992 | ;; slot. They only apply to that specific concrete class, which | |
1993 | ;; appears as the first argument. | |
1994 | #f) | |
c0a56ec7 | 1995 | (else |
7b0a8dfb AW |
1996 | (let lp ((specs specs) (types types)) |
1997 | (cond | |
1998 | ((null? specs) (null? types)) | |
1999 | ((not (pair? specs)) #t) | |
2000 | ((null? types) #f) | |
2001 | (else | |
2002 | (and (memq (car specs) (class-precedence-list (car types))) | |
2003 | (lp (cdr specs) (cdr types)))))))))) | |
c0a56ec7 AW |
2004 | (let ((n (length args)) |
2005 | (types (map class-of args))) | |
2006 | (let lp ((methods (generic-function-methods gf)) | |
2007 | (applicable '())) | |
2008 | (if (null? methods) | |
2009 | (and (not (null? applicable)) | |
2010 | (%sort-applicable-methods applicable types)) | |
2011 | (let ((m (car methods))) | |
2012 | (lp (cdr methods) | |
2013 | (if (method-applicable? m types) | |
2014 | (cons m applicable) | |
2015 | applicable))))))) | |
2016 | ||
2017 | (define compute-applicable-methods %compute-applicable-methods) | |
2018 | ||
2ce560b9 AW |
2019 | (define (toplevel-define! name val) |
2020 | (module-define! (current-module) name val)) | |
2021 | ||
2022 | (define-syntax define-method | |
2023 | (syntax-rules (setter) | |
2024 | ((_ ((setter name) . args) body ...) | |
2025 | (begin | |
bacc8829 AW |
2026 | (when (or (not (defined? 'name)) |
2027 | (not (is-a? name <accessor>))) | |
2028 | (toplevel-define! 'name | |
2029 | (ensure-accessor | |
2030 | (if (defined? 'name) name #f) 'name))) | |
2ce560b9 AW |
2031 | (add-method! (setter name) (method args body ...)))) |
2032 | ((_ (name . args) body ...) | |
2033 | (begin | |
2034 | ;; FIXME: this code is how it always was, but it's quite cracky: | |
2035 | ;; it will only define the generic function if it was undefined | |
2036 | ;; before (ok), or *was defined to #f*. The latter is crack. But | |
2037 | ;; there are bootstrap issues about fixing this -- change it to | |
2038 | ;; (is-a? name <generic>) and see. | |
bacc8829 AW |
2039 | (when (or (not (defined? 'name)) |
2040 | (not name)) | |
2041 | (toplevel-define! 'name (make <generic> #:name 'name))) | |
2ce560b9 | 2042 | (add-method! name (method args body ...)))))) |
14f1d9fe | 2043 | |
47c8983f AW |
2044 | (define-syntax method |
2045 | (lambda (x) | |
d6392715 AW |
2046 | (define (parse-args args) |
2047 | (let lp ((ls args) (formals '()) (specializers '())) | |
47c8983f | 2048 | (syntax-case ls () |
d6392715 | 2049 | (((f s) . rest) |
0dd8493c AW |
2050 | (and (identifier? #'f) (identifier? #'s)) |
2051 | (lp #'rest | |
2052 | (cons #'f formals) | |
2053 | (cons #'s specializers))) | |
d6392715 | 2054 | ((f . rest) |
0dd8493c AW |
2055 | (identifier? #'f) |
2056 | (lp #'rest | |
2057 | (cons #'f formals) | |
2058 | (cons #'<top> specializers))) | |
d6392715 AW |
2059 | (() |
2060 | (list (reverse formals) | |
0dd8493c | 2061 | (reverse (cons #''() specializers)))) |
d6392715 | 2062 | (tail |
0dd8493c AW |
2063 | (identifier? #'tail) |
2064 | (list (append (reverse formals) #'tail) | |
2065 | (reverse (cons #'<top> specializers))))))) | |
47c8983f AW |
2066 | |
2067 | (define (find-free-id exp referent) | |
2068 | (syntax-case exp () | |
2069 | ((x . y) | |
0dd8493c AW |
2070 | (or (find-free-id #'x referent) |
2071 | (find-free-id #'y referent))) | |
47c8983f | 2072 | (x |
0dd8493c AW |
2073 | (identifier? #'x) |
2074 | (let ((id (datum->syntax #'x referent))) | |
2075 | (and (free-identifier=? #'x id) id))) | |
47c8983f AW |
2076 | (_ #f))) |
2077 | ||
2078 | (define (compute-procedure formals body) | |
2079 | (syntax-case body () | |
2080 | ((body0 ...) | |
2081 | (with-syntax ((formals formals)) | |
0dd8493c | 2082 | #'(lambda formals body0 ...))))) |
47c8983f AW |
2083 | |
2084 | (define (->proper args) | |
2085 | (let lp ((ls args) (out '())) | |
2086 | (syntax-case ls () | |
0dd8493c | 2087 | ((x . xs) (lp #'xs (cons #'x out))) |
47c8983f | 2088 | (() (reverse out)) |
0dd8493c | 2089 | (tail (reverse (cons #'tail out)))))) |
47c8983f AW |
2090 | |
2091 | (define (compute-make-procedure formals body next-method) | |
2092 | (syntax-case body () | |
2093 | ((body ...) | |
2094 | (with-syntax ((next-method next-method)) | |
2095 | (syntax-case formals () | |
2096 | ((formal ...) | |
0dd8493c AW |
2097 | #'(lambda (real-next-method) |
2098 | (lambda (formal ...) | |
2099 | (let ((next-method (lambda args | |
2100 | (if (null? args) | |
2101 | (real-next-method formal ...) | |
2102 | (apply real-next-method args))))) | |
2103 | body ...)))) | |
47c8983f | 2104 | (formals |
0dd8493c AW |
2105 | (with-syntax (((formal ...) (->proper #'formals))) |
2106 | #'(lambda (real-next-method) | |
2107 | (lambda formals | |
2108 | (let ((next-method (lambda args | |
2109 | (if (null? args) | |
2110 | (apply real-next-method formal ...) | |
2111 | (apply real-next-method args))))) | |
2112 | body ...)))))))))) | |
47c8983f AW |
2113 | |
2114 | (define (compute-procedures formals body) | |
2115 | ;; So, our use of this is broken, because it operates on the | |
2116 | ;; pre-expansion source code. It's equivalent to just searching | |
2117 | ;; for referent in the datums. Ah well. | |
2118 | (let ((id (find-free-id body 'next-method))) | |
2119 | (if id | |
2120 | ;; return a make-procedure | |
0dd8493c | 2121 | (values #'#f |
47c8983f AW |
2122 | (compute-make-procedure formals body id)) |
2123 | (values (compute-procedure formals body) | |
0dd8493c | 2124 | #'#f)))) |
47c8983f AW |
2125 | |
2126 | (syntax-case x () | |
0dd8493c | 2127 | ((_ args) #'(method args (if #f #f))) |
47c8983f | 2128 | ((_ args body0 body1 ...) |
0dd8493c | 2129 | (with-syntax (((formals (specializer ...)) (parse-args #'args))) |
47c8983f AW |
2130 | (call-with-values |
2131 | (lambda () | |
0dd8493c | 2132 | (compute-procedures #'formals #'(body0 body1 ...))) |
47c8983f AW |
2133 | (lambda (procedure make-procedure) |
2134 | (with-syntax ((procedure procedure) | |
2135 | (make-procedure make-procedure)) | |
0dd8493c AW |
2136 | #'(make <method> |
2137 | #:specializers (cons* specializer ...) | |
2138 | #:formals 'formals | |
2139 | #:body '(body0 body1 ...) | |
2140 | #:make-procedure make-procedure | |
2141 | #:procedure procedure))))))))) | |
14f1d9fe | 2142 | |
ac5185c2 AW |
2143 | ;;; |
2144 | ;;; {Utilities} | |
2145 | ;;; | |
2146 | ;;; These are useful when dealing with method specializers, which might | |
2147 | ;;; have a rest argument. | |
2148 | ;;; | |
2149 | ||
2150 | (define (map* fn . l) ; A map which accepts dotted lists (arg lists | |
2151 | (cond ; must be "isomorph" | |
2152 | ((null? (car l)) '()) | |
2153 | ((pair? (car l)) (cons (apply fn (map car l)) | |
2154 | (apply map* fn (map cdr l)))) | |
2155 | (else (apply fn l)))) | |
2156 | ||
2157 | (define (for-each* fn . l) ; A for-each which accepts dotted lists (arg lists | |
2158 | (cond ; must be "isomorph" | |
2159 | ((null? (car l)) '()) | |
2160 | ((pair? (car l)) (apply fn (map car l)) (apply for-each* fn (map cdr l))) | |
2161 | (else (apply fn l)))) | |
2162 | ||
2163 | (define (length* ls) | |
2164 | (do ((n 0 (+ 1 n)) | |
2165 | (ls ls (cdr ls))) | |
2166 | ((not (pair? ls)) n))) | |
2167 | ||
14f1d9fe MD |
2168 | ;;; |
2169 | ;;; {add-method!} | |
2170 | ;;; | |
2171 | ||
2172 | (define (add-method-in-classes! m) | |
2173 | ;; Add method in all the classes which appears in its specializers list | |
2174 | (for-each* (lambda (x) | |
8dfc0ba5 AW |
2175 | (let ((dm (class-direct-methods x))) |
2176 | (unless (memq m dm) | |
2177 | (struct-set! x class-index-direct-methods (cons m dm))))) | |
2178 | (method-specializers m))) | |
14f1d9fe MD |
2179 | |
2180 | (define (remove-method-in-classes! m) | |
2181 | ;; Remove method in all the classes which appears in its specializers list | |
2182 | (for-each* (lambda (x) | |
8dfc0ba5 AW |
2183 | (struct-set! x |
2184 | class-index-direct-methods | |
2185 | (delv! m (class-direct-methods x)))) | |
2186 | (method-specializers m))) | |
14f1d9fe MD |
2187 | |
2188 | (define (compute-new-list-of-methods gf new) | |
2189 | (let ((new-spec (method-specializers new)) | |
8dfc0ba5 | 2190 | (methods (slot-ref gf 'methods))) |
14f1d9fe MD |
2191 | (let loop ((l methods)) |
2192 | (if (null? l) | |
8dfc0ba5 AW |
2193 | (cons new methods) |
2194 | (if (equal? (method-specializers (car l)) new-spec) | |
2195 | (begin | |
2196 | ;; This spec. list already exists. Remove old method from dependents | |
2197 | (remove-method-in-classes! (car l)) | |
2198 | (set-car! l new) | |
2199 | methods) | |
2200 | (loop (cdr l))))))) | |
14f1d9fe | 2201 | |
eddd81f4 AW |
2202 | (define (method-n-specializers m) |
2203 | (length* (slot-ref m 'specializers))) | |
2204 | ||
2205 | (define (calculate-n-specialized gf) | |
2206 | (fold (lambda (m n) (max n (method-n-specializers m))) | |
2207 | 0 | |
2208 | (generic-function-methods gf))) | |
2209 | ||
2210 | (define (invalidate-method-cache! gf) | |
eddd81f4 | 2211 | (slot-set! gf 'n-specialized (calculate-n-specialized gf)) |
0d96acac | 2212 | (%invalidate-method-cache! gf) |
eddd81f4 AW |
2213 | (for-each (lambda (gf) (invalidate-method-cache! gf)) |
2214 | (slot-ref gf 'extended-by))) | |
2215 | ||
7d38f3d8 AW |
2216 | (define internal-add-method! |
2217 | (method ((gf <generic>) (m <method>)) | |
2218 | (slot-set! m 'generic-function gf) | |
2219 | (slot-set! gf 'methods (compute-new-list-of-methods gf m)) | |
eddd81f4 | 2220 | (invalidate-method-cache! gf) |
7d38f3d8 AW |
2221 | (add-method-in-classes! m) |
2222 | *unspecified*)) | |
14f1d9fe MD |
2223 | |
2224 | (define-generic add-method!) | |
2225 | ||
7d38f3d8 | 2226 | ((method-procedure internal-add-method!) add-method! internal-add-method!) |
14f1d9fe | 2227 | |
71d540f7 | 2228 | (define-method (add-method! (proc <procedure>) (m <method>)) |
14f1d9fe MD |
2229 | (if (generic-capability? proc) |
2230 | (begin | |
8dfc0ba5 AW |
2231 | (enable-primitive-generic! proc) |
2232 | (add-method! proc m)) | |
14f1d9fe MD |
2233 | (next-method))) |
2234 | ||
71d540f7 | 2235 | (define-method (add-method! (pg <primitive-generic>) (m <method>)) |
14f1d9fe MD |
2236 | (add-method! (primitive-generic-generic pg) m)) |
2237 | ||
71d540f7 | 2238 | (define-method (add-method! obj (m <method>)) |
14f1d9fe MD |
2239 | (goops-error "~S is not a valid generic function" obj)) |
2240 | ||
2241 | ;;; | |
2242 | ;;; {Access to meta objects} | |
2243 | ;;; | |
2244 | ||
2245 | ;;; | |
2246 | ;;; Methods | |
2247 | ;;; | |
71d540f7 | 2248 | (define-method (method-source (m <method>)) |
14f1d9fe | 2249 | (let* ((spec (map* class-name (slot-ref m 'specializers))) |
8dfc0ba5 | 2250 | (src (procedure-source (slot-ref m 'procedure)))) |
4e2f1e9e AW |
2251 | (and src |
2252 | (let ((args (cadr src)) | |
2253 | (body (cddr src))) | |
2254 | (cons 'method | |
2255 | (cons (map* list args spec) | |
2256 | body)))))) | |
2257 | ||
2258 | (define-method (method-formals (m <method>)) | |
2259 | (slot-ref m 'formals)) | |
14f1d9fe MD |
2260 | |
2261 | ;;; | |
2262 | ;;; Slots | |
2263 | ;;; | |
14f1d9fe | 2264 | (define (slot-init-function class slot-name) |
2a3ef7c4 AW |
2265 | (%slot-definition-init-thunk (or (class-slot-definition class slot-name) |
2266 | (error "slot not found" slot-name)))) | |
14f1d9fe | 2267 | |
51f66c91 AW |
2268 | (define (accessor-method-slot-definition obj) |
2269 | "Return the slot definition of the accessor @var{obj}." | |
2270 | (slot-ref obj 'slot-definition)) | |
2271 | ||
14f1d9fe MD |
2272 | |
2273 | ;;; | |
2274 | ;;; {Standard methods used by the C runtime} | |
2275 | ;;; | |
2276 | ||
2277 | ;;; Methods to compare objects | |
2278 | ;;; | |
2279 | ||
9f63ce02 AW |
2280 | ;; Have to do this in a strange order because equal? is used in the |
2281 | ;; add-method! implementation; we need to make sure that when the | |
2282 | ;; primitive is extended, that the generic has a method. = | |
2283 | (define g-equal? (make-generic 'equal?)) | |
2284 | ;; When this generic gets called, we will have already checked eq? and | |
2285 | ;; eqv? -- the purpose of this generic is to extend equality. So by | |
2286 | ;; default, there is no extension, thus the #f return. | |
8dfc0ba5 | 2287 | (add-method! g-equal? (method (x y) #f)) |
9f63ce02 | 2288 | (set-primitive-generic! equal? g-equal?) |
a48d60b1 | 2289 | |
14f1d9fe MD |
2290 | ;;; |
2291 | ;;; methods to display/write an object | |
2292 | ;;; | |
2293 | ||
2294 | ; Code for writing objects must test that the slots they use are | |
8dfc0ba5 | 2295 | ; bound. Otherwise a slot-unbound method will be called and will |
14f1d9fe MD |
2296 | ; conduct to an infinite loop. |
2297 | ||
2298 | ;; Write | |
2299 | (define (display-address o file) | |
2300 | (display (number->string (object-address o) 16) file)) | |
2301 | ||
71d540f7 | 2302 | (define-method (write o file) |
14f1d9fe MD |
2303 | (display "#<instance " file) |
2304 | (display-address o file) | |
2305 | (display #\> file)) | |
2306 | ||
2307 | (define write-object (primitive-generic-generic write)) | |
2308 | ||
71d540f7 | 2309 | (define-method (write (o <object>) file) |
14f1d9fe MD |
2310 | (let ((class (class-of o))) |
2311 | (if (slot-bound? class 'name) | |
8dfc0ba5 AW |
2312 | (begin |
2313 | (display "#<" file) | |
2314 | (display (class-name class) file) | |
2315 | (display #\space file) | |
2316 | (display-address o file) | |
2317 | (display #\> file)) | |
2318 | (next-method)))) | |
14f1d9fe | 2319 | |
568174d1 AW |
2320 | (define-method (write (slot <slot>) file) |
2321 | (let ((class (class-of slot))) | |
2322 | (if (and (slot-bound? class 'name) | |
2323 | (slot-bound? slot 'name)) | |
2324 | (begin | |
2325 | (display "#<" file) | |
2326 | (display (class-name class) file) | |
2327 | (display #\space file) | |
2a3ef7c4 | 2328 | (display (%slot-definition-name slot) file) |
568174d1 AW |
2329 | (display #\space file) |
2330 | (display-address slot file) | |
2331 | (display #\> file)) | |
2332 | (next-method)))) | |
2333 | ||
71d540f7 | 2334 | (define-method (write (class <class>) file) |
14f1d9fe MD |
2335 | (let ((meta (class-of class))) |
2336 | (if (and (slot-bound? class 'name) | |
8dfc0ba5 AW |
2337 | (slot-bound? meta 'name)) |
2338 | (begin | |
2339 | (display "#<" file) | |
2340 | (display (class-name meta) file) | |
2341 | (display #\space file) | |
2342 | (display (class-name class) file) | |
2343 | (display #\space file) | |
2344 | (display-address class file) | |
2345 | (display #\> file)) | |
2346 | (next-method)))) | |
14f1d9fe | 2347 | |
71d540f7 | 2348 | (define-method (write (gf <generic>) file) |
14f1d9fe MD |
2349 | (let ((meta (class-of gf))) |
2350 | (if (and (slot-bound? meta 'name) | |
8dfc0ba5 AW |
2351 | (slot-bound? gf 'methods)) |
2352 | (begin | |
2353 | (display "#<" file) | |
2354 | (display (class-name meta) file) | |
2355 | (let ((name (generic-function-name gf))) | |
2356 | (if name | |
2357 | (begin | |
2358 | (display #\space file) | |
2359 | (display name file)))) | |
2360 | (display " (" file) | |
2361 | (display (length (generic-function-methods gf)) file) | |
2362 | (display ")>" file)) | |
2363 | (next-method)))) | |
14f1d9fe | 2364 | |
71d540f7 | 2365 | (define-method (write (o <method>) file) |
14f1d9fe MD |
2366 | (let ((meta (class-of o))) |
2367 | (if (and (slot-bound? meta 'name) | |
8dfc0ba5 AW |
2368 | (slot-bound? o 'specializers)) |
2369 | (begin | |
2370 | (display "#<" file) | |
2371 | (display (class-name meta) file) | |
2372 | (display #\space file) | |
2373 | (display (map* (lambda (spec) | |
2374 | (if (slot-bound? spec 'name) | |
2375 | (slot-ref spec 'name) | |
2376 | spec)) | |
2377 | (method-specializers o)) | |
2378 | file) | |
2379 | (display #\space file) | |
2380 | (display-address o file) | |
2381 | (display #\> file)) | |
2382 | (next-method)))) | |
14f1d9fe MD |
2383 | |
2384 | ;; Display (do the same thing as write by default) | |
8dfc0ba5 | 2385 | (define-method (display o file) |
14f1d9fe MD |
2386 | (write-object o file)) |
2387 | ||
7b07e5ef MD |
2388 | ;;; |
2389 | ;;; Handling of duplicate bindings in the module system | |
2390 | ;;; | |
2391 | ||
28d0871b AW |
2392 | (define (find-subclass super name) |
2393 | (let lp ((classes (class-direct-subclasses super))) | |
2394 | (cond | |
2395 | ((null? classes) | |
2396 | (error "class not found" name)) | |
2397 | ((and (slot-bound? (car classes) 'name) | |
2398 | (eq? (class-name (car classes)) name)) | |
2399 | (car classes)) | |
2400 | (else | |
2401 | (lp (cdr classes)))))) | |
2402 | ||
2403 | ;; A record type. | |
2404 | (define <module> (find-subclass <top> '<module>)) | |
2405 | ||
7b07e5ef | 2406 | (define-method (merge-generics (module <module>) |
8dfc0ba5 AW |
2407 | (name <symbol>) |
2408 | (int1 <module>) | |
2409 | (val1 <top>) | |
2410 | (int2 <module>) | |
2411 | (val2 <top>) | |
2412 | (var <top>) | |
2413 | (val <top>)) | |
7b07e5ef MD |
2414 | #f) |
2415 | ||
2416 | (define-method (merge-generics (module <module>) | |
8dfc0ba5 AW |
2417 | (name <symbol>) |
2418 | (int1 <module>) | |
2419 | (val1 <generic>) | |
2420 | (int2 <module>) | |
2421 | (val2 <generic>) | |
2422 | (var <top>) | |
2423 | (val <boolean>)) | |
c9fa1748 MD |
2424 | (and (not (eq? val1 val2)) |
2425 | (make-variable (make-extended-generic (list val2 val1) name)))) | |
7b07e5ef MD |
2426 | |
2427 | (define-method (merge-generics (module <module>) | |
8dfc0ba5 AW |
2428 | (name <symbol>) |
2429 | (int1 <module>) | |
2430 | (val1 <generic>) | |
2431 | (int2 <module>) | |
2432 | (val2 <generic>) | |
2433 | (var <top>) | |
2434 | (gf <extended-generic>)) | |
c9fa1748 MD |
2435 | (and (not (memq val2 (slot-ref gf 'extends))) |
2436 | (begin | |
8dfc0ba5 AW |
2437 | (slot-set! gf |
2438 | 'extends | |
2439 | (cons val2 (delq! val2 (slot-ref gf 'extends)))) | |
2440 | (slot-set! val2 | |
2441 | 'extended-by | |
2442 | (cons gf (delq! gf (slot-ref val2 'extended-by)))) | |
eddd81f4 | 2443 | (invalidate-method-cache! gf) |
8dfc0ba5 | 2444 | var))) |
7b07e5ef MD |
2445 | |
2446 | (module-define! duplicate-handlers 'merge-generics merge-generics) | |
2447 | ||
f8af5c6d | 2448 | (define-method (merge-accessors (module <module>) |
8dfc0ba5 AW |
2449 | (name <symbol>) |
2450 | (int1 <module>) | |
2451 | (val1 <top>) | |
2452 | (int2 <module>) | |
2453 | (val2 <top>) | |
2454 | (var <top>) | |
2455 | (val <top>)) | |
f8af5c6d MD |
2456 | #f) |
2457 | ||
2458 | (define-method (merge-accessors (module <module>) | |
8dfc0ba5 AW |
2459 | (name <symbol>) |
2460 | (int1 <module>) | |
2461 | (val1 <accessor>) | |
2462 | (int2 <module>) | |
2463 | (val2 <accessor>) | |
2464 | (var <top>) | |
2465 | (val <top>)) | |
f8af5c6d MD |
2466 | (merge-generics module name int1 val1 int2 val2 var val)) |
2467 | ||
2468 | (module-define! duplicate-handlers 'merge-accessors merge-accessors) | |
2469 | ||
14f1d9fe MD |
2470 | ;;; |
2471 | ;;; slot access | |
2472 | ;;; | |
2473 | ||
568174d1 AW |
2474 | (define (class-slot-ref class slot-name) |
2475 | (let ((slot (class-slot-definition class slot-name))) | |
2a3ef7c4 | 2476 | (unless (memq (%slot-definition-allocation slot) '(#:class #:each-subclass)) |
bacc8829 | 2477 | (slot-missing class slot-name)) |
4bde3f04 | 2478 | (let ((x ((%slot-definition-slot-ref/raw slot) #f))) |
568174d1 AW |
2479 | (if (unbound? x) |
2480 | (slot-unbound class slot-name) | |
2481 | x)))) | |
2482 | ||
2483 | (define (class-slot-set! class slot-name value) | |
2484 | (let ((slot (class-slot-definition class slot-name))) | |
2a3ef7c4 | 2485 | (unless (memq (%slot-definition-allocation slot) '(#:class #:each-subclass)) |
568174d1 | 2486 | (slot-missing class slot-name)) |
2a3ef7c4 | 2487 | ((%slot-definition-slot-set! slot) #f value))) |
14f1d9fe | 2488 | |
71d540f7 | 2489 | (define-method (slot-unbound (c <class>) (o <object>) s) |
14f1d9fe MD |
2490 | (goops-error "Slot `~S' is unbound in object ~S" s o)) |
2491 | ||
71d540f7 | 2492 | (define-method (slot-unbound (c <class>) s) |
14f1d9fe MD |
2493 | (goops-error "Slot `~S' is unbound in class ~S" s c)) |
2494 | ||
71d540f7 | 2495 | (define-method (slot-unbound (o <object>)) |
14f1d9fe MD |
2496 | (goops-error "Unbound slot in object ~S" o)) |
2497 | ||
71d540f7 | 2498 | (define-method (slot-missing (c <class>) (o <object>) s) |
14f1d9fe | 2499 | (goops-error "No slot with name `~S' in object ~S" s o)) |
8dfc0ba5 | 2500 | |
71d540f7 | 2501 | (define-method (slot-missing (c <class>) s) |
14f1d9fe | 2502 | (goops-error "No class slot with name `~S' in class ~S" s c)) |
8dfc0ba5 | 2503 | |
14f1d9fe | 2504 | |
71d540f7 | 2505 | (define-method (slot-missing (c <class>) (o <object>) s value) |
14f1d9fe MD |
2506 | (slot-missing c o s)) |
2507 | ||
2508 | ;;; Methods for the possible error we can encounter when calling a gf | |
2509 | ||
71d540f7 | 2510 | (define-method (no-next-method (gf <generic>) args) |
14f1d9fe MD |
2511 | (goops-error "No next method when calling ~S\nwith arguments ~S" gf args)) |
2512 | ||
71d540f7 | 2513 | (define-method (no-applicable-method (gf <generic>) args) |
14f1d9fe | 2514 | (goops-error "No applicable method for ~S in call ~S" |
8dfc0ba5 | 2515 | gf (cons (generic-function-name gf) args))) |
14f1d9fe | 2516 | |
71d540f7 | 2517 | (define-method (no-method (gf <generic>) args) |
14f1d9fe MD |
2518 | (goops-error "No method defined for ~S" gf)) |
2519 | ||
2520 | ;;; | |
2521 | ;;; {Cloning functions (from rdeline@CS.CMU.EDU)} | |
2522 | ;;; | |
2523 | ||
71d540f7 | 2524 | (define-method (shallow-clone (self <object>)) |
07452c83 AW |
2525 | (let* ((class (class-of self)) |
2526 | (clone (%allocate-instance class)) | |
2527 | (slots (map slot-definition-name (class-slots class)))) | |
14f1d9fe | 2528 | (for-each (lambda (slot) |
bacc8829 AW |
2529 | (when (slot-bound? self slot) |
2530 | (slot-set! clone slot (slot-ref self slot)))) | |
8dfc0ba5 | 2531 | slots) |
14f1d9fe MD |
2532 | clone)) |
2533 | ||
71d540f7 | 2534 | (define-method (deep-clone (self <object>)) |
07452c83 AW |
2535 | (let* ((class (class-of self)) |
2536 | (clone (%allocate-instance class)) | |
2537 | (slots (map slot-definition-name (class-slots class)))) | |
14f1d9fe | 2538 | (for-each (lambda (slot) |
bacc8829 AW |
2539 | (when (slot-bound? self slot) |
2540 | (slot-set! clone slot | |
2541 | (let ((value (slot-ref self slot))) | |
2542 | (if (instance? value) | |
2543 | (deep-clone value) | |
2544 | value))))) | |
8dfc0ba5 | 2545 | slots) |
14f1d9fe MD |
2546 | clone)) |
2547 | ||
2548 | ;;; | |
2549 | ;;; {Class redefinition utilities} | |
2550 | ;;; | |
2551 | ||
2552 | ;;; (class-redefinition OLD NEW) | |
2553 | ;;; | |
2554 | ||
2555 | ;;; Has correct the following conditions: | |
2556 | ||
2557 | ;;; Methods | |
8dfc0ba5 | 2558 | ;;; |
14f1d9fe | 2559 | ;;; 1. New accessor specializers refer to new header |
8dfc0ba5 | 2560 | ;;; |
14f1d9fe | 2561 | ;;; Classes |
8dfc0ba5 | 2562 | ;;; |
14f1d9fe MD |
2563 | ;;; 1. New class cpl refers to the new class header |
2564 | ;;; 2. Old class header exists on old super classes direct-subclass lists | |
2565 | ;;; 3. New class header exists on new super classes direct-subclass lists | |
2566 | ||
71d540f7 | 2567 | (define-method (class-redefinition (old <class>) (new <class>)) |
14f1d9fe | 2568 | ;; Work on direct methods: |
8dfc0ba5 AW |
2569 | ;; 1. Remove accessor methods from the old class |
2570 | ;; 2. Patch the occurences of new in the specializers by old | |
2571 | ;; 3. Displace the methods from old to new | |
2572 | (remove-class-accessors! old) ;; -1- | |
14f1d9fe MD |
2573 | (let ((methods (class-direct-methods new))) |
2574 | (for-each (lambda (m) | |
8dfc0ba5 | 2575 | (update-direct-method! m new old)) ;; -2- |
14f1d9fe | 2576 | methods) |
8dfc0ba5 AW |
2577 | (struct-set! new |
2578 | class-index-direct-methods | |
2579 | (append methods (class-direct-methods old)))) | |
14f1d9fe MD |
2580 | |
2581 | ;; Substitute old for new in new cpl | |
8dfc0ba5 AW |
2582 | (set-car! (struct-ref new class-index-cpl) old) |
2583 | ||
14f1d9fe | 2584 | ;; Remove the old class from the direct-subclasses list of its super classes |
8dfc0ba5 AW |
2585 | (for-each (lambda (c) (struct-set! c class-index-direct-subclasses |
2586 | (delv! old (class-direct-subclasses c)))) | |
2587 | (class-direct-supers old)) | |
14f1d9fe MD |
2588 | |
2589 | ;; Replace the new class with the old in the direct-subclasses of the supers | |
2590 | (for-each (lambda (c) | |
8dfc0ba5 AW |
2591 | (struct-set! c class-index-direct-subclasses |
2592 | (cons old (delv! new (class-direct-subclasses c))))) | |
2593 | (class-direct-supers new)) | |
14f1d9fe MD |
2594 | |
2595 | ;; Swap object headers | |
2596 | (%modify-class old new) | |
2597 | ||
2598 | ;; Now old is NEW! | |
2599 | ||
2600 | ;; Redefine all the subclasses of old to take into account modification | |
8dfc0ba5 AW |
2601 | (for-each |
2602 | (lambda (c) | |
2603 | (update-direct-subclass! c new old)) | |
2604 | (class-direct-subclasses new)) | |
14f1d9fe MD |
2605 | |
2606 | ;; Invalidate class so that subsequent instances slot accesses invoke | |
2607 | ;; change-object-class | |
8dfc0ba5 | 2608 | (struct-set! new class-index-redefined old) |
761338f6 | 2609 | (class-clear-flags! new vtable-flag-goops-valid) ;must come after slot-set! |
14f1d9fe MD |
2610 | |
2611 | old) | |
2612 | ||
2613 | ;;; | |
2614 | ;;; remove-class-accessors! | |
2615 | ;;; | |
2616 | ||
71d540f7 | 2617 | (define-method (remove-class-accessors! (c <class>)) |
14f1d9fe | 2618 | (for-each (lambda (m) |
bacc8829 AW |
2619 | (when (is-a? m <accessor-method>) |
2620 | (let ((gf (slot-ref m 'generic-function))) | |
2621 | ;; remove the method from its GF | |
2622 | (slot-set! gf 'methods | |
2623 | (delq1! m (slot-ref gf 'methods))) | |
2624 | (invalidate-method-cache! gf) | |
2625 | ;; remove the method from its specializers | |
2626 | (remove-method-in-classes! m)))) | |
8dfc0ba5 | 2627 | (class-direct-methods c))) |
14f1d9fe MD |
2628 | |
2629 | ;;; | |
2630 | ;;; update-direct-method! | |
2631 | ;;; | |
2632 | ||
71d540f7 | 2633 | (define-method (update-direct-method! (m <method>) |
8dfc0ba5 AW |
2634 | (old <class>) |
2635 | (new <class>)) | |
14f1d9fe | 2636 | (let loop ((l (method-specializers m))) |
8dfc0ba5 | 2637 | ;; Note: the <top> in dotted list is never used. |
14f1d9fe | 2638 | ;; So we can work as if we had only proper lists. |
bacc8829 AW |
2639 | (when (pair? l) |
2640 | (when (eqv? (car l) old) | |
2641 | (set-car! l new)) | |
2642 | (loop (cdr l))))) | |
14f1d9fe MD |
2643 | |
2644 | ;;; | |
2645 | ;;; update-direct-subclass! | |
2646 | ;;; | |
2647 | ||
71d540f7 | 2648 | (define-method (update-direct-subclass! (c <class>) |
8dfc0ba5 AW |
2649 | (old <class>) |
2650 | (new <class>)) | |
14f1d9fe | 2651 | (class-redefinition c |
8dfc0ba5 AW |
2652 | (make-class (class-direct-supers c) |
2653 | (class-direct-slots c) | |
2654 | #:name (class-name c) | |
2655 | #:metaclass (class-of c)))) | |
14f1d9fe MD |
2656 | |
2657 | ;;; | |
2658 | ;;; {Utilities for INITIALIZE methods} | |
2659 | ;;; | |
2660 | ||
2661 | ;;; compute-slot-accessors | |
2662 | ;;; | |
9d019f9b | 2663 | (define (compute-slot-accessors class slots) |
14f1d9fe | 2664 | (for-each |
568174d1 | 2665 | (lambda (slot) |
2a3ef7c4 AW |
2666 | (let ((getter (%slot-definition-getter slot)) |
2667 | (setter (%slot-definition-setter slot)) | |
568174d1 | 2668 | (accessor-setter setter) |
2a3ef7c4 | 2669 | (accessor (%slot-definition-accessor slot))) |
568174d1 AW |
2670 | (when getter |
2671 | (add-method! getter (compute-getter-method class slot))) | |
2672 | (when setter | |
2673 | (add-method! setter (compute-setter-method class slot))) | |
2674 | (when accessor | |
2675 | (add-method! accessor (compute-getter-method class slot)) | |
2676 | (add-method! (accessor-setter accessor) | |
2677 | (compute-setter-method class slot))))) | |
2678 | slots)) | |
2679 | ||
2680 | (define-method (compute-getter-method (class <class>) slot) | |
7b0a8dfb AW |
2681 | (make <accessor-method> |
2682 | #:specializers (list class) | |
2683 | #:procedure (slot-definition-slot-ref slot) | |
2684 | #:slot-definition slot)) | |
568174d1 AW |
2685 | |
2686 | (define-method (compute-setter-method (class <class>) slot) | |
7b0a8dfb AW |
2687 | (make <accessor-method> |
2688 | #:specializers (list class <top>) | |
2689 | #:procedure (slot-definition-slot-set! slot) | |
2690 | #:slot-definition slot)) | |
14f1d9fe MD |
2691 | |
2692 | (define (make-generic-bound-check-getter proc) | |
984d43b6 AW |
2693 | (lambda (o) |
2694 | (let ((val (proc o))) | |
2695 | (if (unbound? val) | |
2696 | (slot-unbound o) | |
2697 | val)))) | |
14f1d9fe | 2698 | |
14f1d9fe MD |
2699 | ;;; compute-cpl |
2700 | ;;; | |
9167e0b8 AW |
2701 | |
2702 | ;; Replace the bootstrap compute-cpl with this definition. | |
2703 | (define compute-cpl | |
2704 | (make <generic> #:name 'compute-cpl)) | |
14f1d9fe | 2705 | |
71d540f7 | 2706 | (define-method (compute-cpl (class <class>)) |
14f1d9fe MD |
2707 | (compute-std-cpl class class-direct-supers)) |
2708 | ||
14f1d9fe MD |
2709 | ;;; compute-get-n-set |
2710 | ;;; | |
568174d1 AW |
2711 | (define compute-get-n-set |
2712 | (make <generic> #:name 'compute-get-n-set)) | |
2713 | ||
71d540f7 | 2714 | (define-method (compute-get-n-set (class <class>) s) |
5b763233 AW |
2715 | (define (class-slot-init-value) |
2716 | (let ((thunk (slot-definition-init-thunk s))) | |
2717 | (if thunk | |
2718 | (thunk) | |
2719 | (slot-definition-init-value s)))) | |
2720 | ||
568174d1 AW |
2721 | (define (make-closure-variable class value) |
2722 | (list (lambda (o) value) | |
2723 | (lambda (o v) (set! value v)))) | |
2724 | ||
14f1d9fe MD |
2725 | (case (slot-definition-allocation s) |
2726 | ((#:instance) ;; Instance slot | |
2727 | ;; get-n-set is just its offset | |
8dfc0ba5 AW |
2728 | (let ((already-allocated (struct-ref class class-index-nfields))) |
2729 | (struct-set! class class-index-nfields (+ already-allocated 1)) | |
14f1d9fe MD |
2730 | already-allocated)) |
2731 | ||
568174d1 | 2732 | ((#:class) ;; Class slot |
8dfc0ba5 | 2733 | ;; Class-slots accessors are implemented as 2 closures around |
14f1d9fe MD |
2734 | ;; a Scheme variable. As instance slots, class slots must be |
2735 | ;; unbound at init time. | |
2736 | (let ((name (slot-definition-name s))) | |
2737 | (if (memq name (map slot-definition-name (class-direct-slots class))) | |
8dfc0ba5 AW |
2738 | ;; This slot is direct; create a new shared variable |
2739 | (make-closure-variable class (class-slot-init-value)) | |
2740 | ;; Slot is inherited. Find its definition in superclass | |
568174d1 AW |
2741 | (let lp ((cpl (cdr (class-precedence-list class)))) |
2742 | (match cpl | |
2743 | ((super . cpl) | |
2744 | (let ((s (class-slot-definition super name))) | |
2745 | (if s | |
2746 | (list (slot-definition-slot-ref s) | |
2747 | (slot-definition-slot-set! s)) | |
2748 | ;; Multiple inheritance means that we might have | |
2749 | ;; to look deeper in the CPL. | |
2750 | (lp cpl))))))))) | |
14f1d9fe MD |
2751 | |
2752 | ((#:each-subclass) ;; slot shared by instances of direct subclass. | |
2753 | ;; (Thomas Buerger, April 1998) | |
5b763233 | 2754 | (make-closure-variable class (class-slot-init-value))) |
14f1d9fe MD |
2755 | |
2756 | ((#:virtual) ;; No allocation | |
2757 | ;; slot-ref and slot-set! function must be given by the user | |
2758 | (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f)) | |
8dfc0ba5 | 2759 | (set (get-keyword #:slot-set! (slot-definition-options s) #f))) |
bacc8829 AW |
2760 | (unless (and get set) |
2761 | (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S" s)) | |
14f1d9fe MD |
2762 | (list get set))) |
2763 | (else (next-method)))) | |
2764 | ||
71d540f7 | 2765 | (define-method (compute-get-n-set (o <object>) s) |
14f1d9fe MD |
2766 | (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s))) |
2767 | ||
71d540f7 | 2768 | (define-method (compute-slots (class <class>)) |
51fd1cd6 AW |
2769 | (build-slots-list (class-direct-slots class) |
2770 | (class-precedence-list class))) | |
14f1d9fe MD |
2771 | |
2772 | ;;; | |
2773 | ;;; {Initialize} | |
2774 | ;;; | |
2775 | ||
4a28ef10 AW |
2776 | ;; FIXME: This could be much more efficient. |
2777 | (define (%initialize-object obj initargs) | |
2778 | "Initialize the object @var{obj} with the given arguments | |
2779 | var{initargs}." | |
79c2ca26 AW |
2780 | (define (valid-initargs? initargs) |
2781 | (match initargs | |
2782 | (() #t) | |
2783 | (((? keyword?) _ . initargs) (valid-initargs? initargs)) | |
2784 | (_ #f))) | |
4a28ef10 AW |
2785 | (unless (instance? obj) |
2786 | (scm-error 'wrong-type-arg #f "Not an object: ~S" | |
2787 | (list obj) #f)) | |
79c2ca26 AW |
2788 | (unless (valid-initargs? initargs) |
2789 | (scm-error 'wrong-type-arg #f "Invalid initargs: ~S" | |
4a28ef10 AW |
2790 | (list initargs) #f)) |
2791 | (let ((class (class-of obj))) | |
2792 | (define (get-initarg kw) | |
2793 | (if kw | |
79c2ca26 AW |
2794 | ;; Inlined get-keyword to avoid checking initargs for validity |
2795 | ;; each time. | |
2796 | (let lp ((initargs initargs)) | |
2797 | (match initargs | |
2798 | ((kw* val . initargs) | |
2799 | (if (eq? kw* kw) | |
2800 | val | |
2801 | (lp initargs))) | |
2802 | (_ *unbound*))) | |
4a28ef10 | 2803 | *unbound*)) |
568174d1 | 2804 | (let lp ((slots (struct-ref class class-index-slots))) |
4a28ef10 AW |
2805 | (match slots |
2806 | (() obj) | |
568174d1 | 2807 | ((slot . slots) |
79c2ca26 | 2808 | (define (initialize-slot! value) |
4bde3f04 | 2809 | ((%slot-definition-slot-set! slot) obj value)) |
2a3ef7c4 | 2810 | (let ((initarg (get-initarg (%slot-definition-init-keyword slot)))) |
568174d1 AW |
2811 | (cond |
2812 | ((not (unbound? initarg)) | |
79c2ca26 | 2813 | (initialize-slot! initarg)) |
2a3ef7c4 | 2814 | ((%slot-definition-init-thunk slot) |
568174d1 AW |
2815 | => (lambda (init-thunk) |
2816 | (unless (memq (slot-definition-allocation slot) | |
2817 | '(#:class #:each-subclass)) | |
79c2ca26 | 2818 | (initialize-slot! (init-thunk))))))) |
568174d1 | 2819 | (lp slots)))))) |
4a28ef10 | 2820 | |
71d540f7 | 2821 | (define-method (initialize (object <object>) initargs) |
14f1d9fe MD |
2822 | (%initialize-object object initargs)) |
2823 | ||
568174d1 AW |
2824 | (define-method (initialize (slot <slot>) initargs) |
2825 | (next-method) | |
2826 | (struct-set! slot slot-index-options initargs) | |
2a3ef7c4 | 2827 | (let ((init-thunk (%slot-definition-init-thunk slot))) |
568174d1 AW |
2828 | (when init-thunk |
2829 | (unless (thunk? init-thunk) | |
2830 | (goops-error "Bad init-thunk for slot `~S': ~S" | |
2a3ef7c4 | 2831 | (%slot-definition-name slot) init-thunk))))) |
568174d1 | 2832 | |
71d540f7 | 2833 | (define-method (initialize (class <class>) initargs) |
568174d1 AW |
2834 | (define (make-direct-slot-definition dslot) |
2835 | (let ((initargs (compute-direct-slot-definition-initargs class dslot))) | |
2836 | (compute-direct-slot-definition class initargs))) | |
2837 | ||
14f1d9fe | 2838 | (next-method) |
568174d1 AW |
2839 | (class-add-flags! class (logior vtable-flag-goops-class |
2840 | vtable-flag-goops-valid)) | |
2841 | (struct-set! class class-index-name (get-keyword #:name initargs '???)) | |
2842 | (struct-set! class class-index-nfields 0) | |
2843 | (struct-set! class class-index-direct-supers | |
2844 | (get-keyword #:dsupers initargs '())) | |
2845 | (struct-set! class class-index-direct-subclasses '()) | |
2846 | (struct-set! class class-index-direct-methods '()) | |
2847 | (struct-set! class class-index-redefined #f) | |
2848 | (struct-set! class class-index-cpl (compute-cpl class)) | |
26350edc AW |
2849 | (when (get-keyword #:static-slot-allocation? initargs #f) |
2850 | (match (filter class-has-statically-allocated-slots? | |
2851 | (class-precedence-list class)) | |
2852 | (() | |
2853 | (class-add-flags! class vtable-flag-goops-static)) | |
2854 | (classes | |
2855 | (error "Class has superclasses with static slot allocation" classes)))) | |
568174d1 AW |
2856 | (struct-set! class class-index-direct-slots |
2857 | (map (lambda (slot) | |
2858 | (if (slot? slot) | |
2859 | slot | |
2860 | (make-direct-slot-definition slot))) | |
2861 | (get-keyword #:slots initargs '()))) | |
2862 | (struct-set! class class-index-slots | |
2863 | (allocate-slots class (compute-slots class))) | |
2864 | ||
e437c50b AW |
2865 | ;; This is a hack. |
2866 | (when (memq <slot> (struct-ref class class-index-cpl)) | |
2867 | (class-add-flags! class vtable-flag-goops-slot)) | |
2868 | ||
568174d1 AW |
2869 | ;; Build getters - setters - accessors |
2870 | (compute-slot-accessors class (struct-ref class class-index-slots)) | |
2871 | ||
2872 | ;; Update the "direct-subclasses" of each inherited classes | |
2873 | (for-each (lambda (x) | |
2874 | (let ((dsubs (struct-ref x class-index-direct-subclasses))) | |
2875 | (struct-set! x class-index-direct-subclasses | |
2876 | (cons class dsubs)))) | |
2877 | (struct-ref class class-index-direct-supers)) | |
2878 | ||
2879 | ;; Compute struct layout of instances, set the `layout' slot, and | |
2880 | ;; update class flags. | |
2881 | (%prep-layout! class)) | |
14f1d9fe | 2882 | |
14f1d9fe MD |
2883 | (define (initialize-object-procedure object initargs) |
2884 | (let ((proc (get-keyword #:procedure initargs #f))) | |
2885 | (cond ((not proc)) | |
8dfc0ba5 AW |
2886 | ((pair? proc) |
2887 | (apply slot-set! object 'procedure proc)) | |
2888 | (else | |
962b9103 | 2889 | (slot-set! object 'procedure proc))))) |
14f1d9fe | 2890 | |
51f66c91 | 2891 | (define-method (initialize (applicable-struct <applicable-struct>) initargs) |
14f1d9fe | 2892 | (next-method) |
51f66c91 | 2893 | (initialize-object-procedure applicable-struct initargs)) |
14f1d9fe | 2894 | |
6c7dd9eb AW |
2895 | (define-method (initialize (applicable-struct <applicable-struct-with-setter>) |
2896 | initargs) | |
2897 | (next-method) | |
2898 | (slot-set! applicable-struct 'setter (get-keyword #:setter initargs #f))) | |
2899 | ||
71d540f7 | 2900 | (define-method (initialize (generic <generic>) initargs) |
14f1d9fe | 2901 | (let ((previous-definition (get-keyword #:default initargs #f)) |
8dfc0ba5 | 2902 | (name (get-keyword #:name initargs #f))) |
14f1d9fe MD |
2903 | (next-method) |
2904 | (slot-set! generic 'methods (if (is-a? previous-definition <procedure>) | |
8dfc0ba5 | 2905 | (list (method args |
7d38f3d8 | 2906 | (apply previous-definition args))) |
8dfc0ba5 | 2907 | '())) |
14f1d9fe | 2908 | (if name |
8dfc0ba5 | 2909 | (set-procedure-property! generic 'name name)) |
e0590e7c | 2910 | (invalidate-method-cache! generic))) |
14f1d9fe | 2911 | |
bbf8d523 MD |
2912 | (define-method (initialize (eg <extended-generic>) initargs) |
2913 | (next-method) | |
2914 | (slot-set! eg 'extends (get-keyword #:extends initargs '()))) | |
2915 | ||
b432fb4b MD |
2916 | (define dummy-procedure (lambda args *unspecified*)) |
2917 | ||
71d540f7 | 2918 | (define-method (initialize (method <method>) initargs) |
14f1d9fe MD |
2919 | (next-method) |
2920 | (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f)) | |
2921 | (slot-set! method 'specializers (get-keyword #:specializers initargs '())) | |
b432fb4b | 2922 | (slot-set! method 'procedure |
8dfc0ba5 | 2923 | (get-keyword #:procedure initargs #f)) |
21497600 AW |
2924 | (slot-set! method 'formals (get-keyword #:formals initargs '())) |
2925 | (slot-set! method 'body (get-keyword #:body initargs '())) | |
e177058b | 2926 | (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f))) |
8dfc0ba5 | 2927 | |
14f1d9fe | 2928 | |
14f1d9fe MD |
2929 | ;;; |
2930 | ;;; {Change-class} | |
2931 | ;;; | |
2932 | ||
2933 | (define (change-object-class old-instance old-class new-class) | |
cc6c7fee | 2934 | (let ((new-instance (allocate-instance new-class '()))) |
8378b269 | 2935 | ;; Initialize the slots of the new instance |
9539b20b AW |
2936 | (for-each |
2937 | (lambda (slot) | |
2938 | (if (and (slot-exists? old-instance slot) | |
2a3ef7c4 | 2939 | (eq? (%slot-definition-allocation |
9539b20b AW |
2940 | (class-slot-definition old-class slot)) |
2941 | #:instance) | |
2942 | (slot-bound? old-instance slot)) | |
2943 | ;; Slot was present and allocated in old instance; copy it | |
2944 | (slot-set! new-instance slot (slot-ref old-instance slot)) | |
2945 | ;; slot was absent; initialize it with its default value | |
2946 | (let ((init (slot-init-function new-class slot))) | |
2947 | (when init | |
2948 | (slot-set! new-instance slot (init)))))) | |
2949 | (map slot-definition-name (class-slots new-class))) | |
14f1d9fe MD |
2950 | ;; Exchange old and new instance in place to keep pointers valid |
2951 | (%modify-instance old-instance new-instance) | |
2952 | ;; Allow class specific updates of instances (which now are swapped) | |
2953 | (update-instance-for-different-class new-instance old-instance) | |
2954 | old-instance)) | |
2955 | ||
2956 | ||
71d540f7 | 2957 | (define-method (update-instance-for-different-class (old-instance <object>) |
8dfc0ba5 AW |
2958 | (new-instance |
2959 | <object>)) | |
14f1d9fe MD |
2960 | ;;not really important what we do, we just need a default method |
2961 | new-instance) | |
2962 | ||
71d540f7 | 2963 | (define-method (change-class (old-instance <object>) (new-class <class>)) |
14f1d9fe MD |
2964 | (change-object-class old-instance (class-of old-instance) new-class)) |
2965 | ||
2966 | ;;; | |
2967 | ;;; {make} | |
2968 | ;;; | |
2969 | ;;; A new definition which overwrites the previous one which was built-in | |
2970 | ;;; | |
2971 | ||
71d540f7 | 2972 | (define-method (allocate-instance (class <class>) initargs) |
07452c83 | 2973 | (%allocate-instance class)) |
14f1d9fe | 2974 | |
71d540f7 | 2975 | (define-method (make-instance (class <class>) . initargs) |
14f1d9fe MD |
2976 | (let ((instance (allocate-instance class initargs))) |
2977 | (initialize instance initargs) | |
2978 | instance)) | |
2979 | ||
2980 | (define make make-instance) | |
2981 | ||
2982 | ;;; | |
2983 | ;;; {apply-generic} | |
2984 | ;;; | |
c2aa5d9b AW |
2985 | ;;; Protocol for calling generic functions, intended to be used when |
2986 | ;;; applying subclasses of <generic> and <generic-with-setter>. The | |
2987 | ;;; code below is similar to the first MOP described in AMOP. | |
2988 | ;;; | |
2989 | ;;; Note that standard generic functions dispatch only on the classes of | |
2990 | ;;; the arguments, and the result of such dispatch can be memoized. The | |
0d96acac AW |
2991 | ;;; `dispatch-generic-function-application-from-cache' routine |
2992 | ;;; implements this. `apply-generic' isn't called currently; the | |
2993 | ;;; generic function MOP was never fully implemented in GOOPS. However | |
2994 | ;;; now that GOOPS is implemented entirely in Scheme (2015) it's much | |
2995 | ;;; easier to complete this work. Contributions gladly accepted! | |
2996 | ;;; Please read the AMOP first though :) | |
c2aa5d9b AW |
2997 | ;;; |
2998 | ;;; The protocol is: | |
2999 | ;;; | |
3000 | ;;; + apply-generic (gf args) | |
3001 | ;;; + compute-applicable-methods (gf args ...) | |
3002 | ;;; + sort-applicable-methods (gf methods args) | |
3003 | ;;; + apply-methods (gf methods args) | |
3004 | ;;; | |
3005 | ;;; apply-methods calls make-next-method to build the "continuation" of | |
3006 | ;;; a method. Applying a next-method will call apply-next-method which | |
3007 | ;;; in turn will call apply again to call effectively the following | |
3008 | ;;; method. (This paragraph is out of date but is kept so that maybe it | |
3009 | ;;; illuminates some future hack.) | |
14f1d9fe MD |
3010 | ;;; |
3011 | ||
71d540f7 | 3012 | (define-method (apply-generic (gf <generic>) args) |
bacc8829 AW |
3013 | (when (null? (slot-ref gf 'methods)) |
3014 | (no-method gf args)) | |
14f1d9fe MD |
3015 | (let ((methods (compute-applicable-methods gf args))) |
3016 | (if methods | |
8dfc0ba5 AW |
3017 | (apply-methods gf (sort-applicable-methods gf methods args) args) |
3018 | (no-applicable-method gf args)))) | |
14f1d9fe MD |
3019 | |
3020 | ;; compute-applicable-methods is bound to %compute-applicable-methods. | |
e7097386 AW |
3021 | (define compute-applicable-methods |
3022 | (let ((gf (make <generic> #:name 'compute-applicable-methods))) | |
3023 | (add-method! gf (method ((gf <generic>) args) | |
3024 | (%compute-applicable-methods gf args))) | |
3025 | gf)) | |
14f1d9fe | 3026 | |
71d540f7 | 3027 | (define-method (sort-applicable-methods (gf <generic>) methods args) |
c0a56ec7 | 3028 | (%sort-applicable-methods methods (map class-of args))) |
14f1d9fe | 3029 | |
71d540f7 | 3030 | (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs) |
14f1d9fe MD |
3031 | (%method-more-specific? m1 m2 targs)) |
3032 | ||
e7097386 AW |
3033 | (define compute-effective-method |
3034 | (let ((gf (make <generic> #:name 'compute-effective-method))) | |
3035 | (add-method! gf (method ((gf <generic>) methods typev) | |
3036 | (%compute-effective-method gf methods typev))) | |
3037 | gf)) | |
3038 | ||
3039 | (define compute-specialized-effective-method | |
3040 | (let ((gf (make <generic> #:name 'compute-specialized-effective-method))) | |
3041 | (add-method! | |
3042 | gf | |
3043 | (method ((gf <generic>) (method <method>) typev next) | |
3044 | (%compute-specialized-effective-method gf method typev next))) | |
3045 | gf)) | |
3046 | ||
3047 | (define-method (compute-specialized-effective-method (gf <generic>) | |
3048 | (m <accessor-method>) | |
3049 | typev | |
3050 | next) | |
3051 | (let ((name (slot-definition-name (accessor-method-slot-definition m)))) | |
3052 | (match typev | |
3053 | (#(class) | |
3054 | (slot-definition-slot-ref (class-slot-definition class name))) | |
3055 | (#(class _) | |
3056 | (slot-definition-slot-set! (class-slot-definition class name))) | |
3057 | (_ | |
3058 | (next-method))))) | |
3059 | ||
71d540f7 | 3060 | (define-method (apply-method (gf <generic>) methods build-next args) |
14f1d9fe | 3061 | (apply (method-procedure (car methods)) |
8dfc0ba5 AW |
3062 | (build-next (cdr methods) args) |
3063 | args)) | |
14f1d9fe | 3064 | |
71d540f7 | 3065 | (define-method (apply-methods (gf <generic>) (l <list>) args) |
14f1d9fe | 3066 | (letrec ((next (lambda (procs args) |
8dfc0ba5 AW |
3067 | (lambda new-args |
3068 | (let ((a (if (null? new-args) args new-args))) | |
3069 | (if (null? procs) | |
3070 | (no-next-method gf a) | |
3071 | (apply-method gf procs next a))))))) | |
14f1d9fe MD |
3072 | (apply-method gf l next args))) |
3073 | ||
3074 | ;; We don't want the following procedure to turn up in backtraces: | |
3075 | (for-each (lambda (proc) | |
8dfc0ba5 AW |
3076 | (set-procedure-property! proc 'system-procedure #t)) |
3077 | (list slot-unbound | |
3078 | slot-missing | |
3079 | no-next-method | |
3080 | no-applicable-method | |
3081 | no-method | |
3082 | )) | |
14f1d9fe | 3083 | |
14f1d9fe MD |
3084 | ;;; |
3085 | ;;; {Final initialization} | |
3086 | ;;; | |
3087 | ||
3088 | ;; Tell C code that the main bulk of Goops has been loaded | |
3089 | (%goops-loaded) | |
28d0871b AW |
3090 | |
3091 | ||
3092 | \f | |
3093 | ||
3094 | ;;; | |
3095 | ;;; {SMOB and port classes} | |
3096 | ;;; | |
3097 | ||
3098 | (define <arbiter> (find-subclass <top> '<arbiter>)) | |
3099 | (define <promise> (find-subclass <top> '<promise>)) | |
3100 | (define <thread> (find-subclass <top> '<thread>)) | |
3101 | (define <mutex> (find-subclass <top> '<mutex>)) | |
3102 | (define <condition-variable> (find-subclass <top> '<condition-variable>)) | |
3103 | (define <regexp> (find-subclass <top> '<regexp>)) | |
3104 | (define <hook> (find-subclass <top> '<hook>)) | |
3105 | (define <bitvector> (find-subclass <top> '<bitvector>)) | |
3106 | (define <random-state> (find-subclass <top> '<random-state>)) | |
3107 | (define <async> (find-subclass <top> '<async>)) | |
3108 | (define <directory> (find-subclass <top> '<directory>)) | |
28d0871b AW |
3109 | (define <array> (find-subclass <top> '<array>)) |
3110 | (define <character-set> (find-subclass <top> '<character-set>)) | |
3111 | (define <dynamic-object> (find-subclass <top> '<dynamic-object>)) | |
3112 | (define <guardian> (find-subclass <applicable> '<guardian>)) | |
02620dd9 | 3113 | (define <macro> (find-subclass <top> '<macro>)) |
28d0871b AW |
3114 | |
3115 | (define (define-class-subtree class) | |
3116 | (define! (class-name class) class) | |
3117 | (for-each define-class-subtree (class-direct-subclasses class))) | |
3118 | ||
3119 | (define-class-subtree (find-subclass <port> '<file-port>)) |