File: object.stk

package info (click to toggle)
stklos 0.55-0.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 15,068 kB
  • ctags: 14,984
  • sloc: ansic: 91,480; sh: 29,217; asm: 25,654; lisp: 1,431; makefile: 1,284; cpp: 1,125; perl: 213; yacc: 150; lex: 78; fortran: 24
file content (1073 lines) | stat: -rw-r--r-- 35,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
;;;;
;;;; object.stk	-- -- A variation of the Gregor Kickzales Tiny CLOS for STklos
;;;; 
;;;; Copyright  1993-2002 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;; 
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;; 
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
;;;; USA.
;;;; 
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 20-Feb-1994 21:09
;;;; Last file update: 27-Dec-2002 23:11 (eg)


#|
//FIXMOI

* decoupage du source
* methods rapides
* Vrification que tous les points du mop sont implments

|#



(define class-redefinition (void))	;; forward declaration

;=============================================================================
;
;			      U t i l i t i e s
;
;=============================================================================

(define (%error-bad-class who obj)   (error who "bad class ~S" obj))
(define (%error-bad-generic who obj) (error who "bad generic function ~S" obj))
(define (%error-bad-method who obj)  (error who "bad method ~S" obj))

(define make-closure eval)

(define (specializers l)
  (cond
   ((null? l) '())
   ((pair? l) (cons (if (pair? (car l)) (cadar l) '<top>)
 		    (specializers (cdr l))))
   (else      '<top>)))
 
(define (formals l)
  (if (pair? l)
      (cons (if (pair? (car l)) (caar l) (car l)) (formals (cdr l)))
      l))
 

(define (declare-slots slots)
  ;; This is a *HUGE* *HACK*. Since STklos compiler displays the
  ;; unknown symbols, slots accessors are not known at compilation
  ;; time and yield a message. So, we pre-declare them here.
  ;; In the abolute this code is false, since the keywords :accessor
  ;; :getter and :setter can have a different meaning than the usual
  ;; one for a peculiar meta-class. Anyway, the only risk is to
  ;; trust that a symbol is defined whereas it is not. 
  (for-each (lambda (s)
	      (let ((getter   (slot-definition-getter   s))
		    (setter   (slot-definition-setter   s))
		    (accessor (slot-definition-accessor s)))
		(when getter   (new-global getter))
		(when setter   (new-global setter))
		(when accessor (new-global accessor))))
	    slots)
  slots)


;--------------------------------------------------
(define (make class . l)	;; A temporary version used for bootstrapping 
  (cond
    ((eq? class <generic>) (%make class
				  'generic
				  (key-get l :name '???)))
    ((eq? class <method>) (%make class
				  'method
				  (list (key-get l :generic-function #f)
					(key-get l :specializers     '())
					(key-get l :procedure        '()))))
    (else       	    (error 'basic-make "cannot make ~S with ~S" class l))))

;=============================================================================
;
; 			      Access to Meta objects
;
;=============================================================================

;// ;;;
;// ;;; Methods
;// ;;;
;// (define-method method-body ((m <method>))
;//   (let* ((spec (map class-name (slot-ref m 'specializers)))
;// 	 (proc (procedure-body (slot-ref m 'procedure)))
;// 	 (args (cdadr proc))
;// 	 (body (cddr proc)))
;//     (list* 'method (map list args spec) body)))
;// 

;;;
;;; Classes
;;;
(define (class-name C)
  (if (class? C) (slot-ref C 'name) (%error-bad-class 'class-name C)))

(define (class-direct-supers C)
  (if (class? C) 
      (slot-ref C 'direct-supers) 
      (%error-bad-class 'class-direct-supers C)))

(define (class-direct-slots C)
  (if (class? C) 
      (slot-ref C 'direct-slots) 
      (%error-bad-class 'class-direct-slots C)))

(define (class-direct-subclasses C)
  (if (class? C) 
      (slot-ref C 'direct-subclasses) 
      (%error-bad-class 'class-direct-subclasses C)))

(define (class-direct-methods C)
  (if (class? C) 
      (slot-ref C 'direct-methods) 
      (%error-bad-class 'class-direct-methods C)))

(define (class-precedence-list C)
  (if (class? C)
      (slot-ref C 'cpl)
      (%error-bad-class 'class-precedence-list C)))

(define (class-slots C)
  (if (class? C)
      (slot-ref C 'slots)
      (%error-bad-class 'class-slots C)))

;;;
;;; Slots
;;;
(define (slot-definition-name s)
  (if (pair? s) (car s) s))

(define (slot-definition-options s)
  (and (pair? s) (cdr s)))

(define (slot-definition-allocation s)
  (if (symbol? s)
      :instance
      (key-get (cdr s) :allocation :instance)))

(define (slot-definition-getter s)
  (and (pair? s) (key-get (cdr s) :getter #f)))

(define (slot-definition-setter s)
  (and (pair? s) (key-get (cdr s) :setter #f)))

(define (slot-definition-accessor s)
  (and (pair? s) (key-get (cdr s) :accessor #f)))

(define (slot-definition-init-form s)
  (if (pair? s)
      (key-get (cdr s) :init-form (void))
      (void)))

(define (slot-definition-init-keyword s)
  (and (pair? s) (key-get (cdr s) :init-keyword #f)))

(define (slot-init-function c s)
  (let ((s (slot-definition-name s)))
    (cadr (assq s (slot-ref c 'getters-n-setters)))))

(define (class-slot-definition class slot-name)
  (assq slot-name (class-slots class)))

;;;
;;; Generic functions
;;;
(define (generic-function-name gf)
  (if (generic? gf)
      (slot-ref gf 'name) 
      (%error-bad-generic 'generic-function-name gf)))

(define (generic-function-methods gf)
  (if (generic? gf)
      (slot-ref gf 'methods)
      (%error-bad-generic 'generic-function-methods gf)))

;;;
;;; Methods
;;;
(define (method-generic-function m)
  (if (method? m)
      (slot-ref m 'generic-function)
      (%error-bad-method 'method-generic-function m)))

(define (method-specializers m)
  (if (method? m)
      (slot-ref m 'specializers)
      (%error-bad-method 'method-specializers m)))

(define (method-procedure m)
  (if (method? m)
      (slot-ref m 'procedure)
      (%error-bad-method 'method-procedure m)))

;=============================================================================

;;
;; is-a?
;;
(define (is-a? obj class)
  (and (memq class (class-precedence-list (class-of obj))) #t))

;;
;; Find-class
;;
(define (find-class name :optional (default #f default?))
  (let ((cls (if (symbol? name)
		 (symbol-value* name (current-module) #f)
		 name)))
    (if (is-a? cls <class>)
	cls
	(if default?
	    default
	    (error 'find-class "bad class ~S" name)))))

;;
;; %compute-slots
;;
(define (%compute-slots C)
  (define (remove-duplicate-slots l res)
    (if (null? l) 
	res
	(let ((s (slot-definition-name (car l))))
	  (unless (symbol? s)
	    (error 'compute-slots "bad slot name ~S" s))
	  (remove-duplicate-slots (cdr l) 
				  (if (assq s res) res (cons (car l) res))))))

  (define (build-slots-list cpl res)
    (if (null? cpl) 
	(remove-duplicate-slots (reverse res) '())
	(build-slots-list (cdr cpl) 
			  (append (class-direct-slots (car cpl)) res))))
  (define (sort-slots l)
    ;; Slots are sorted such that instance slots are always before other slots.
    ;; So, virtual slots are placed fist and, for instance, virtual slots can have
    ;; an initial value which depends of real slots.
    ;; Note that the following "sort" just displace non-instance slots at the
    ;; end keeping initial order (i.e. we don't use the STklos sort procedure
    ;; since it is not stable.
    (let Loop ((l l) (instance '()) (other '()))
      (cond
	((null? l)
	   (append (reverse! instance) (reverse! other)))
	((eq? (slot-definition-allocation (car l)) :instance)
	   (Loop (cdr l) (cons (car l) instance) other))
	(else
	   (Loop (cdr l) instance (cons (car l) other))))))
  
  (sort-slots (build-slots-list (class-precedence-list C) '())))


;=============================================================================
;
; 			M e t a c l a s s e s   s t u f f
;
;=============================================================================
(define ensure-metaclass-with-supers
  (let ((table-of-metas '()))
    (lambda (meta-supers)
      (let ((entry (assoc meta-supers table-of-metas)))
	(if entry
	    ;; Found a previously created metaclass
	    (cdr entry)
	    ;; Create a new meta-class which inherit from "meta-supers"
	    (let ((new (make <class> :dsupers meta-supers
			             :slots   '()
				     :name   (gensym "metaclass"))))
	      (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
	      new))))))

(define (ensure-metaclass supers)
  (if (null? supers)
      (find-class <class>)
      (let* ((all-metas (map (lambda (x)
			       (if (is-a? x <class>)
				   x
				   (class-of (find-class x))))
			     supers))
	     (all-cpls  (apply append
			       (map (lambda (m) (cdr (class-precedence-list m))) 
				    all-metas)))
	     (needed-metas '()))
	;; Find the most specific metaclasses.  The new metaclass will be
	;; a subclass of these.
	(for-each
	 (lambda (meta)
	   (when (and (not (member meta all-cpls)) (not (member meta needed-metas)))
	     (set! needed-metas (append needed-metas (list meta)))))
	 all-metas)
	;; Now return a subclass of the metaclasses we found.
	(if (null? (cdr needed-metas))
	    (car needed-metas)  ; If there's only one, just use it.
	    (ensure-metaclass-with-supers needed-metas)))))

;=============================================================================
;
; 			D e f i n e - c l a s s
;
;=============================================================================
;==== Define-class
(define-macro (define-class name supers slots . options)
  `(define ,name 
      (ensure-class
        ',name					; name
	',supers				; supers
	',(declare-slots slots)		; slots 
	,(or (key-get options :metaclass #f)	; metaclass
	     `(ensure-metaclass ',supers))
	,@options)))

;==== Ensure-class
(define (ensure-class name supers slots metaclass . options)

  (define (find-duplicate l)	; find a duplicate in a list; #f otherwise
    (cond
       ((null? l)		#f)
       ((memq (car l) (cdr l))	(car l))
       (else 			(find-duplicate (cdr l)))))
 
  (let ((supers (if (null? supers) 
		    (list <object>)
		    (map find-class supers))))
    ;; Verify that all direct slots are different and that we don't inherit
    ;; several time from the same class
    (let ((tmp (find-duplicate supers)))
      (when tmp
	(error 'define-class "super class ~S is duplicated in class ~S" tmp name)))
    (let ((tmp (find-duplicate (map slot-definition-name slots))))
      (when tmp
	(error 'define-class "slot ~S is duplicated in class ~S" tmp name)))

    ;; Everything seems correct, build the class
    (let ((old (find-class name #f))
	  (cls (apply make metaclass :dsupers supers :slots slots 
		      		     :name name options)))
      (when old (class-redefinition old cls))
      cls)))

;=============================================================================
;
; 			D e f i n e - g e n e r i c
;
;=============================================================================

; ==== Define-generic
(define-macro (define-generic gf)
  `(define ,gf (ensure-generic-function ',gf)))

;==== Ensure-generic-function
(define (ensure-generic-function name)
  (let ((old (symbol-value* name (current-module) #f)))
    (if (generic? old)
	old
	(%symbol-define name 
			(make <generic> :name name 
			      		:default (and (procedure? old) old))
			(current-module)))))

;//(define (ensure-generic-function name)
;//  (let ((old (symbol-value name (current-module) #f)))
;//    (if (generic? old)
;//	old
;//	(eval `(begin
;//		 (define ,name ,(make <generic>
;//				  :name name 
;//				  :default (and (procedure? old) old)))
;//		 ,name)))))
;//

;=============================================================================
;
; 			D e f i n e - m e t h o d
;
;=============================================================================

;==== Add-method!
(define (add-method-in-classes! m)
  ;; Add method in all the classes which appears in its specializers list
  (for-each* (lambda (x)
	       (let ((dm (class-direct-methods x)))
		 (unless (memq m dm)
		   (slot-set! x 'direct-methods (cons m dm)))))
	     (method-specializers m)))

(define (remove-method-in-classes! m)
  ;; Remove method in all the classes which appears in its specializers list
  (for-each* (lambda (x)
	       (slot-set! x 'direct-methods (delete! m (class-direct-methods x)
						     eq?)))
	      (method-specializers m)))

(define (compute-new-list-of-methods gf new)
  (let ((new-spec (method-specializers new))
	(methods  (generic-function-methods gf)))
    (let Loop ((l methods))
      (if (null? l)
	  (cons new methods)
	  (if (equal? (method-specializers (car l)) new-spec)
	      (begin 
		;; This spec. list already exists. Remove old method from dependents
		(remove-method-in-classes! (car l))
		(set-car! l new) 
		methods)
	      (Loop (cdr l)))))))

;;
;; Add-method!
;;
(define (add-method! gf m)
  (slot-set! gf 'methods (compute-new-list-of-methods gf m))
  (add-method-in-classes! m)
  m)


(define (ensure-method gf args body :optional kind-of-method)
  (let* ((new  (extended-lambda->lambda `(method ,args ,@body)))
	 (args (cadr new))
	 (body (cddr new)))
    (make (or kind-of-method <method>)
          :generic-function gf
	  :specializers     (map* find-class (specializers args))
	  :procedure        (make-closure `(let ((next-method (void)))
					     (lambda ,(formals args)
					       ,@body))))))


;====  Method
(define-macro (method args . body)
  `(ensure-method #f ',args ',body))


; ==== Define-method
(define-macro (define-method name args . body)
  (let ((gf (gensym "gf")))
    `(let ((,gf (ensure-generic-function ',name)))
       (add-method! ,gf (ensure-method ,gf ',args ',body))
       (values (void) ',name))))

;=============================================================================
;
; 			    Standard methods   
; 			used by the C runtime
;
;=============================================================================

;==== Methods to compare objects
(define-method object-eqv?   (x y) #f)
(define-method object-equal? (x y) (eqv? x y))

 
;==== Methods to display/write an object
 
;     Code for writing objects must test that the slots they use are
;     bound. Otherwise a slot-unbound method will be called and will 
;     conduct to an infinite loop.
 
;; Write
(define-method write-object (o file)
  (format file "#[instance ~A]" (number->string (address-of o) 16)))
 
(define-method write-object ((o <object>) file)
  (let ((class (class-of o)))
    (if (slot-bound? class 'name) 
	(format file "#[~A ~A]" (class-name class) 
		(number->string (address-of o) 16))
	(next-method))))

(define-method write-object((class <class>) file)
  (let ((meta (class-of class)))
    (if (and (slot-bound? class 'name) (slot-bound? meta 'name)) 
	(format file "#[~A ~A ~A]" (class-name meta) (class-name class)
		(number->string (address-of class) 16))
	(next-method))))

(define-method write-object((gf <generic>) file)
  (let ((meta (class-of gf)))
    (if (and (slot-bound? gf 'name) (slot-bound? meta 'name)
	     (slot-bound? gf 'methods))
	(format file "#[~A ~A (~A)]" 
		(class-name meta)
		(generic-function-name gf)
		(length (generic-function-methods gf)))
	(next-method))))

;; Display (do the same thing as write by default)
(define-method display-object (o file) 
  (write-object o file))

;==== Slot access

;; Avoid printing object itself in the error message, because it might
;; cause an infinite loop (via write-object method). Problem signaled
;; by Shiro Kawai <shiro@acm.org>.

(define-method slot-unbound ((c <class>) (o <object>) s)
  (error "slot ~S is unbound in #p~A (an object of class ~S)"
	 s (number->string (address-of o) 16) c))

(define-method slot-missing ((c <class>) (o <object>) name :optional value)
  (error "no slot with name `~S' in #p~A (an object of class ~S)"
	 name (number->string (address-of o) 16) c))

; ==== Methods for the possible error we can encounter when calling a gf

(define-method no-next-method ((gf <generic>) method args)
  (error "no next method for ~S in call ~S" 
	 method (cons (generic-function-name gf) args)))

(define-method no-applicable-method ((gf <generic>) args)
  (error "no applicable method for ~S\nin call ~S"
	 gf (cons (generic-function-name gf) args)))

(define-method no-method ((gf <generic>) args)
  (error "no method defined for ~S"  gf))


;// (define-macro (next-method-exists?)
;//  `((with-module STklos %next-method-exists?) next-method))



;=============================================================================
;
;	      Cloning functions (from Rob Deline <rdeline@CS.CMU.EDU>)
;
;=============================================================================
(define-method shallow-clone ((self <object>))
  (let ((clone (%allocate-instance (class-of self)))
	(slots (map slot-definition-name
		    (class-slots (class-of self)))))
    (for-each (lambda (slot)
		(if (slot-bound? self slot)
		    (slot-set! clone slot (slot-ref self slot))))
	      slots)
    clone))

(define-method deep-clone ((self <object>))
  (let ((clone (%allocate-instance (class-of self)))
	(slots (map slot-definition-name
		    (class-slots (class-of self)))))
    (for-each (lambda (slot)
		(if (slot-bound? self slot)
		    (slot-set! clone slot
			       (let ((value (slot-ref self slot)))
				 (if (instance? value)
				     (deep-clone value)
				     value)))))
	      slots)
    clone))

;=============================================================================
;
; 		     	Class redefinition utilities
;;
;=============================================================================

;==== Remove-class-accessors
(define-method remove-class-accessors ((c <class>))
  (for-each 
     (lambda (m) (if (is-a? m <accessor-method>) (remove-method-in-classes! m)))
     (class-direct-methods c)))


;==== Update-direct-method
(define-method update-direct-method ((m  <method>) (old <class>) (new <class>))
  (let Loop ((l (method-specializers m)))
    (when (pair? l)       	; Note: the <top> in dotted list is never used. 
      (if (eq? (car l) old)     ; So we can work if we had only proper lists.
	(set-car! l new))
      (Loop (cdr l)))))

;==== Update-direct-subclass
(define-method update-direct-subclass ((c <class>) (old <Class>) (new <Class>))
  (let ((new-supers (map (lambda (cls) (if (eq? cls old) new cls))
			 (class-direct-supers c))))
    ;; Create a new class with same name as c. This will automagically call 
    ;; class-redefinition on this subclass and redefine all its descent
    (ensure-class (class-name c)
		  new-supers
		  (class-direct-slots c)
		  (class-of c))))

;==== Class-redefinition
(define-method class-redefinition ((old <class>) (new <class>))
  ;; Work on direct methods:
  ;;		1. Remove accessor methods from the old class 
  ;;		2. Patch the occurences of old in the specializers by new
  ;;		3. Displace the methods from old to new
  (remove-class-accessors old)					;; -1-
  (let ((methods (class-direct-methods old)))
    (for-each (lambda (m) (update-direct-method m old new))	;; -2-
	      methods)
    (slot-set! new 'direct-methods methods))			;; -3-

  ;; Remove the old class from the direct-subclasses list of its super classes
  (for-each (lambda (c) (slot-set! c 'direct-subclasses 
				   (delete! old (class-direct-subclasses c) eq?)))
	    (class-direct-supers old))

  ;; Redefine all the subclasses of old to take into account modification
  (for-each 
       (lambda (c) (update-direct-subclass c old new))
       (class-direct-subclasses old))

  ;; Invalidate class so that subsequent instances slot accesses invoke
  ;; change-object-class
  (slot-set! old 'redefined new))


;=============================================================================
;
; 			Utilities for INITIALIZE methods
;
;=============================================================================


;;;
;;; Compute-get-n-set
;;;
(define-method compute-get-n-set ((class <class>) s)
  (case (slot-definition-allocation s)
    ((:instance) ;; Instance slot
     		 ;; get-n-set is just its offset
     		 (let ((already-allocated (slot-ref class 'nfields)))
		   (slot-set! class 'nfields (+ already-allocated 1))
		   already-allocated))

    ((:class)  ;; Class slot
               ;; Class-slots accessors are implemented as 2 closures around 
     	       ;; a Scheme variable. 
               (let ((name (slot-definition-name s)))
		 (if (memq name (map slot-definition-name 
				     (class-direct-slots class)))
		     ;; This slot is direct; create a new shared cell
		     (let ((shared-cell (void)))
		       (list (lambda (o)   shared-cell)
			     (lambda (o v) (set! shared-cell v))))
		     ;; Slot is inherited. Find its definition in superclass
		     (let Loop ((l (cdr (class-precedence-list class))))
		       (let ((r (assq name (slot-ref (car l) 'getters-n-setters))))
			 (if r
			     (cddr r)
			     (Loop (cdr l))))))))

    ((:each-subclass) ;; slot shared by instances of direct subclass.
     		      ;; (Thomas Buerger, April 1998)
     		      (let ((shared-cell (void)))
			(list (lambda (o)   shared-cell)
			      (lambda (o v) (set! shared-cell v)))))

    ((:virtual) ;; No allocation
     		;; slot-ref and slot-set! function must be given by the user
     	     	(let ((get (key-get (slot-definition-options s) :slot-ref  #f))
		      (set (key-get (slot-definition-options s) :slot-set! #f)))
		  (unless (and get set)
		    (error "a :slot-ref and a :slot-set! must be supplied in ~S" s))
		  (list (make-closure get)
			(make-closure set))))
    ((:active) ;; Active slot
     	       ;; active slots admit a daemon before or after slot access
               (let* ((index 	   (slot-ref class 'nfields))
		      (name	   (car s))
		      (s	   (cdr s))
		      (before-ref  (make-closure (key-get s :before-slot-ref  #f)))
		      (after-ref   (make-closure (key-get s :after-slot-ref   #f)))
		      (before-set! (make-closure (key-get s :before-slot-set! #f)))
		      (after-set!  (make-closure (key-get s :after-slot-set!  #f))))
		 (slot-set! class 'nfields (+ index 1))
		 (list (lambda (o)
			 (if before-ref
			     (if (before-ref o)
				 (let ((res (%fast-slot-ref o index)))
				   (and after-ref
					(not (eqv? res (void)))
					(after-ref o))
				   res)
				 (void))
			     (let ((res (%fast-slot-ref o index)))
			       (and after-ref (not (eqv? res (void))) (after-ref o))
			       res)))
		       (lambda (o v) 
			 (if before-set!
			     (when (before-set! o v)
			       (%fast-slot-set! o index v)
			       (and after-set! (after-set! o v)))
			     (begin
			       (%fast-slot-set! o index v)
			       (and after-set! (after-set! o v))))))))
    (else    (next-method))))

(define-method compute-get-n-set ((o <object>) s)
  (error "allocation type \"~S\" is unknown" (slot-definition-allocation s)))


;;;
;;; compute-slot-accessors
;;;
(define (compute-slot-accessors class slots)
  (for-each
      (lambda (s)
	(let ((name     (slot-definition-name     s))
	      (getter   (slot-definition-getter   s))
	      (setter   (slot-definition-setter   s))
	      (accessor (slot-definition-accessor s)))
	  (when getter
	    (let ((gf (ensure-generic-function getter)))
	      (add-method! gf (ensure-method gf '(o) `((slot-ref o ',name))
					     <accessor-method>))))
	  (when setter
	    (let ((gf (ensure-generic-function setter)))
	      (add-method! gf (ensure-method gf '(o v) `((slot-set! o ',name v))
					     <accessor-method>))))
	  (when accessor
	    (let ((gf (ensure-generic-function accessor)))
	      (add-method! gf (ensure-method gf '(o) `((slot-ref o ',name))
					     <accessor-method>))
	      (add-method! gf (ensure-method gf '(o v) `((slot-set! o ',name v))
					     <accessor-method>))))))
      slots))

;;;
;;; compute-getters-n-setters
;;; 

(define (compute-getters-n-setters class slots)

  (define (compute-slot-init-function s)
    (let ((init (slot-definition-init-form s)))
      (cond
	((eq? init (void))
	   #f)
	((eq? (slot-definition-allocation s) :class)
	   (let ((sn (slot-definition-name s)))
	     (make-closure `(lambda (o)
			      (let ((val (%slot-ref o ',sn)))
				(if (eq? val (void))
				    ,init
				    val))))))
	(else
	   (make-closure `(lambda (o) ,init))))))


  (define (verify-accessors slot l)
    (if (pair? l)
	(let ((get (car l)) 
	      (set (cadr l)))
	  (unless (and (closure? get) (= (%procedure-arity get) 1))
	    (error "bad getter closure for slot `~S' in ~S: ~S" slot class get))
	  (unless (and (closure? set) (= (%procedure-arity set) 2))
	    (error "bad setter closure for slot `~S' in ~S: ~S" slot class set)))))

  (map (lambda (s)
	 (let* ((s     (if (pair? s) s (list s)))
		(g-n-s (compute-get-n-set class s))
		(name  (slot-definition-name s)))
	   ;; For each slot we have '(name init-function getter setter)
	   ;; If slot is an instance one, we have the simplest
	   ;; form '(name init-function . index)
	   (verify-accessors name g-n-s)
	   (list* name (compute-slot-init-function s) g-n-s)))
       slots))

;;;
;;; compute-cpl
;;;
;;; The current implementation was provided by Anthony Beuriv
;;; <beurive@labri.u-bordeaux.fr>. This implementation is monotonic and 
;;; works "by level". For more information on monotonic superclass 
;;; linearization  algorithm look at the paper 
;;; 	A Monotonic Superclass Linearization for Dylan, 
;;;     K. Barrett, B. Cassels, P. Haahr, D. A. Moon, K. Playford, 
;;;     P. Tucker Withington, 
;;;     OOPSLA 96.
;;; also available at 
;;;	http://www.webcom.com/~haahr/dylan/linearization-oopsla96.html
;;;
(define (compute-cpl class)

  (define (reduce sequences cpl)
    (define (aux sequence)
      (if (and (pair? sequence) (memq (car sequence) cpl))
	  (aux (cdr sequence))
	  sequence))
    (map aux sequences))

  (define (delete-null-lists sequences)
    (if (pair? sequences)
	(let ((first (car sequences)))
	  (if (pair? first)
	      (cons first (delete-null-lists (cdr sequences)))
	      (delete-null-lists (cdr sequences))))
	'()))

  (define (select-candidate candidate sequences)
    (if (pair? sequences)
	(and (not (memq candidate (cdr (car sequences))))
	     (select-candidate candidate (cdr sequences)))
	candidate))

  (define (filter-candidates candidates sequences)
    (if (pair? candidates)
	(or (select-candidate (car candidates) sequences)
	    (filter-candidates (cdr candidates) sequences))
	#f)) ; No valid candidate (inconsistency).

  (define (next-class sequences)
    (let ((candidates (map car sequences)))
      (or (filter-candidates candidates sequences)
	  (car candidates)))) ; Arbitrarily brake the inconsistency.

  (define (step cpl sequences)
    (if (pair? sequences)
	(let ((new-cpl (cons (next-class sequences) cpl)))
	  (step new-cpl (delete-null-lists (reduce sequences new-cpl))))
	(reverse cpl)))

  (step '() (let ((supers (slot-ref class 'direct-supers)))
	      (cons (cons class supers)
		    (map (lambda (super)
			   (slot-ref super 'cpl))
			 supers)))))


;=============================================================================
;
; 			    I n i t i a l i z e
;
;=============================================================================

(define-method initialize ((object <object>) initargs)
  (%initialize-object object initargs))

(define-method initialize ((class <class>) initargs)
  (next-method)
  (let ((dslots (key-get initargs :slots   '()))
	(supers (key-get initargs :dsupers '())))

    (slot-set! class 'name	  	(key-get initargs :name '???))
    (slot-set! class 'direct-supers 	supers)
    (slot-set! class 'direct-slots  	dslots)
    (slot-set! class 'direct-subclasses '())
    (slot-set! class 'direct-methods    '())
    (slot-set! class 'cpl		(compute-cpl class))
    (slot-set! class 'redefined		#f)
    (let ((slots (%compute-slots class)))
      (slot-set! class 'slots	  	  slots)
      (slot-set! class 'nfields	  	  0)
      (slot-set! class 'getters-n-setters (compute-getters-n-setters class slots)))
    ;; Update the "direct-subclasses" of each inherited classes
    (for-each (lambda (x)
		(slot-set! x 'direct-subclasses
			   (cons class (slot-ref x 'direct-subclasses))))
	      supers)

    ;; Build getters - setters - accessors
    (compute-slot-accessors class dslots)))


(define-method initialize ((generic <generic>) initargs)
  (let ((previous-definition (key-get initargs :default #f)))
    (next-method)
    (slot-set! generic 'name    (key-get initargs :name '???))
    (slot-set! generic 'methods (if (procedure? previous-definition)
				    (list (make <method>	; /// FAST-METHOD
						:specializers <top>
						:procedure
						    (lambda (nm . l)
						      (apply previous-definition 
							     l))))
				    '()))))

(define-method initialize ((method <method>) initargs)
  (next-method)
  (slot-set! method 'generic-function (key-get initargs :generic-function #f))
  (slot-set! method 'specializers     (key-get initargs :specializers '()))
  (slot-set! method 'procedure        (key-get initargs :procedure (lambda l '()))))


;=============================================================================
;
; 				     M a k e 
;
;     A new definition which overwrite the previous one which was built-in
;
;=============================================================================

(define-method allocate-instance ((class <class>) initargs)
  (%allocate-instance class))

(define-method make-instance ((class <class>) . initargs)   ;// VIRER CE SYMBOLE
  (let ((instance (allocate-instance class initargs)))
    (initialize instance initargs)
    instance))

(define make make-instance)

;=============================================================================
;
; 			C h a n g e - c l a s s
;
;=============================================================================

(define (change-object-class old-instance old-class new-class)
  (let ((new-instance (allocate-instance new-class ())))
    ;; Initalize the slot of the new instance
    (for-each (lambda (slot)
		(if (slot-exists-using-class? old-class old-instance slot)
		    ;; Slot was present in old instance; set it 
		    (if (slot-bound-using-class? old-class old-instance slot)
			(slot-set-using-class!
			     new-class 
			     new-instance 
			     slot 
			     (slot-ref-using-class old-class old-instance slot)))
		    ;; slot was absent; initialize it with its default value
		    (let ((init (slot-init-function new-class slot)))
		      (if init
			  (slot-set-using-class!
			       new-class 
			       new-instance 
			       slot
			       (apply init '()))))))
	      (map slot-definition-name (class-slots new-class)))
    ;; Exchange old an new instance in place to keep pointers valids
    (%modify-instance old-instance new-instance)
    old-instance))


(define-method change-class ((old-instance <object>) (new-class <class>))
  (change-object-class old-instance (class-of old-instance) new-class))

;=============================================================================
;
;				a p p l y - g e n e r i c
;
; Protocol for calling standard generic functions.
; This protocol is  not used for real <generic> functions (in this case we use
; a completely C hard-coded protocol). 
; Apply-generic is used by STklos for calls to subclasses of <generic>.
;
; The code below is similar to the first MOP described in AMOP. In particular,
; it doesn't used the currified approach to gf call. There are 3 reasons for 
; that: 
;   - the protocol below is exposed to mimic completely the one written in C
;   - the currified protocol would be imho inefficient in C.
;   - nobody (except persons really interested with playing with a MOP) will 
;     probably use the following code
;=============================================================================
 
(define-method compute-applicable-methods ((gf <generic>) args)
  (apply find-method gf args))

(define-method method-more-specific? ((m1 <method>) (m2 <method>) targs)
  (%method-more-specific? m1 m2 targs))

(define-method sort-applicable-methods ((gf <generic>) methods args)
  (let ((targs (map class-of args)))
    (sort methods (lambda (m1 m2) (method-more-specific? m1 m2 targs)))))
 
(define-method apply-method ((gf <generic>) methods build-next args)
  (let ((proc (method-procedure (car methods))))
    (%set-next-method! proc (build-next methods args))
    (apply proc args)))

(define-method apply-methods ((gf <generic>) (l <list>) args)
  (letrec ((next (lambda (procs args)
		   (lambda new-args
		     (let ((a (if (null? new-args) args new-args)))
		       (if (null? (cdr procs))
			   (no-next-method gf (car procs) a)
			   (apply-method gf (cdr procs) next a)))))))
    (apply-method gf l next args)))


;; ==== apply-generic

(define-method apply-generic ((gf <generic>) args)
  (if (null? (generic-function-methods gf))
      (no-method gf args))
  (let ((methods (compute-applicable-methods gf args)))
    (if methods
	(apply-methods gf (sort-applicable-methods gf methods args) args)
 	(no-applicable-method gf args))))


;// ;=============================================================================
;// ;
;// ;		     <Composite-metaclass> 
;// ;=============================================================================


;// 
;// (export <Composite-metaclass> <Active-metaclass>)
;// 

;=============================================================================
;
; 				T o o l s
;
;=============================================================================
(define class-subclasses #f)
(define class-methods    #f)

(let ()
  (define (list2set l)
    (let Loop ((l l) (res '()))
      (cond
       ((null? l) 	   res)
       ((memq (car l) res) (Loop (cdr l) res))
       (else		   (Loop (cdr l) (cons (car l) res))))))

  (define (mapappend func . args)
    (if (memq '()  args)
	'()
	(append (apply func (map car args))
		(apply mapappend func (map cdr args)))))
 
  ;; ==== class-subclasses  
  (set! class-subclasses 
	(lambda (c)
	  (letrec ((allsubs (lambda (c)
			      (cons c (mapappend allsubs 
						 (class-direct-subclasses c))))))
	    (list2set (cdr (allsubs c))))))
  ;; ==== class-methods
  (set! class-methods 
	(lambda (c)
	  (list2set (mapappend class-direct-methods 
			       (cons c (class-subclasses c)))))))

;==== CLOS like slot-value (but as a gf instead of proc as slot-ref or slot-set!) 
(define-method slot-value ((o <object>) s) 		;; The getter
  (slot-ref o s))
 
(define-method slot-value ((o <object>) s v)		;; The setter
  (slot-set! o s v))


(autoload "describe" describe)



;// FIXME:
(%object-system-initialized)


; LocalWords:  supers metaclass OOPSLA linearization beurive labri fr Cassels
; LocalWords:  Beuriv Playford Withington Haahr Gregor Kickzales init