/
boot.lisp
2943 lines (2729 loc) · 133 KB
/
boot.lisp
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
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;; This software is derived from software originally released by Xerox
;;;; Corporation. Copyright and release statements follow. Later modifications
;;;; to the software are in the public domain and are provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for more
;;;; information.
;;;; copyright information from original PCL sources:
;;;;
;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
;;;; All rights reserved.
;;;;
;;;; Use and copying of this software and preparation of derivative works based
;;;; upon this software are permitted. Any distribution of this software or
;;;; derivative works must comply with all applicable United States export
;;;; control laws.
;;;;
;;;; This software is made available AS IS, and Xerox Corporation makes no
;;;; warranty about the software, its performance or its conformity to any
;;;; specification.
(in-package "SB-PCL")
#|
The CommonLoops evaluator is meta-circular.
Most of the code in PCL is methods on generic functions, including
most of the code that actually implements generic functions and method
lookup.
So, we have a classic bootstrapping problem. The solution to this is
to first get a cheap implementation of generic functions running,
these are called early generic functions. These early generic
functions and the corresponding early methods and early method lookup
are used to get enough of the system running that it is possible to
create real generic functions and methods and implement real method
lookup. At that point (done in the file FIXUP) the function
!FIX-EARLY-GENERIC-FUNCTIONS is called to convert all the early generic
functions to real generic functions.
The cheap generic functions are built using the same
FUNCALLABLE-INSTANCE objects that real generic functions are made out of.
This means that as PCL is being bootstrapped, the cheap generic
function objects which are being created are the same objects which
will later be real generic functions. This is good because:
- we don't cons garbage structure, and
- we can keep pointers to the cheap generic function objects
during booting because those pointers will still point to
the right object after the generic functions are all fixed up.
This file defines the DEFMETHOD macro and the mechanism used to expand
it. This includes the mechanism for processing the body of a method.
DEFMETHOD basically expands into a call to LOAD-DEFMETHOD, which
basically calls ADD-METHOD to add the method to the generic function.
These expansions can be loaded either during bootstrapping or when PCL
is fully up and running.
An important effect of this arrangement is it means we can compile
files with DEFMETHOD forms in them in a completely running PCL, but
then load those files back in during bootstrapping. This makes
development easier. It also means there is only one set of code for
processing DEFMETHOD. Bootstrapping works by being sure to have
LOAD-METHOD be careful to call only primitives which work during
bootstrapping.
|#
(declaim (notinline make-a-method add-named-method
ensure-generic-function-using-class
add-method remove-method))
(defvar *!early-functions*
'((make-a-method !early-make-a-method real-make-a-method)
(add-named-method !early-add-named-method real-add-named-method)))
;;; For each of the early functions, arrange to have it point to its
;;; early definition. Do this in a way that makes sure that if we
;;; redefine one of the early definitions the redefinition will take
;;; effect. This makes development easier.
(loop for (name early-name) in *!early-functions*
do (let ((early-name early-name))
(setf (gdefinition name)
(set-fun-name
(lambda (&rest args)
(apply (fdefinition early-name) args))
name))))
;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
;;; to convert the few functions in the bootstrap which are supposed
;;; to be generic functions but can't be early on.
;;;
;;; each entry is a list of the form
;;;
;;; (GENERIC-FUNCTION-NAME METHOD-COMBINATION-NAME METHODS)
;;;
;;; where methods is a list of lists of the form
;;;
;;; (LAMBDA-LIST SPECIALIZERS QUALIFIERS METHOD-BODY-FUNCTION-NAME)
;;;
;;;,where SPECIALIZERS is a list of class names.
(defvar *!generic-function-fixups*
'((add-method
standard
((generic-function method)
(standard-generic-function method)
()
real-add-method))
(remove-method
standard
((generic-function method)
(standard-generic-function method)
()
real-remove-method))
(get-method
standard
((generic-function qualifiers specializers &optional (errorp t))
(standard-generic-function t t)
()
real-get-method))
(ensure-generic-function-using-class
standard
((generic-function fun-name
&key generic-function-class environment
&allow-other-keys)
(generic-function t)
()
real-ensure-gf-using-class--generic-function)
((generic-function fun-name
&key generic-function-class environment
&allow-other-keys)
(null t)
()
real-ensure-gf-using-class--null))
(make-method-lambda
standard
((proto-generic-function proto-method lambda-expression environment)
(standard-generic-function standard-method t t)
()
real-make-method-lambda))
(make-method-lambda-using-specializers
standard
((proto-generic-function proto-method qualifiers specializers
lambda-expression environment)
(standard-generic-function standard-method t t t t)
()
real-make-method-lambda-using-specializers))
(make-method-specializers-form
standard
((proto-generic-function proto-method specializer-names environment)
(standard-generic-function standard-method t t)
()
real-make-method-specializers-form))
(make-specializer-form-using-class
or
((proto-generic-function proto-method specializer-name environment)
(standard-generic-function standard-method t t)
(or)
real-make-specializer-form-using-class/t)
((proto-generic-function proto-method specializer-name environment)
(standard-generic-function standard-method specializer t)
(or)
real-make-specializer-form-using-class/specializer)
((proto-generic-function proto-method specializer-name environment)
(standard-generic-function standard-method symbol t)
(or)
real-make-specializer-form-using-class/symbol)
((proto-generic-function proto-method specializer-name environment)
(standard-generic-function standard-method cons t)
(or)
real-make-specializer-form-using-class/cons))
(specializer-type-specifier
standard
((proto-generic-function proto-method specializer)
(standard-generic-function standard-method specializer)
()
real-specializer-type-specifier/specializer)
((proto-generic-function proto-method specializer)
(standard-generic-function standard-method symbol)
()
real-specializer-type-specifier/symbol)
((proto-generic-function proto-method specializer)
(standard-generic-function standard-method t)
()
real-specializer-type-specifier/t)
((proto-generic-function proto-method specializer)
(standard-generic-function standard-method class-eq-specializer)
()
real-specializer-type-specifier/class-eq-specializer)
((proto-generic-function proto-method specializer)
(standard-generic-function standard-method eql-specializer)
()
real-specializer-type-specifier/eql-specializer)
((proto-generic-function proto-method specializer)
(standard-generic-function standard-method structure-class)
()
real-specializer-type-specifier/structure-class)
((proto-generic-function proto-method specializer)
(standard-generic-function standard-method system-class)
()
real-specializer-type-specifier/system-class)
((proto-generic-function proto-method specializer)
(standard-generic-function standard-method class)
()
real-specializer-type-specifier/class))
(parse-specializer-using-class
standard
((generic-function specializer)
(standard-generic-function t)
()
real-parse-specializer-using-class))
(unparse-specializer-using-class
standard
((generic-function specializer)
(standard-generic-function t)
()
real-unparse-specializer-using-class))
(make-method-initargs-form
standard
((proto-generic-function proto-method
lambda-expression
lambda-list environment)
(standard-generic-function standard-method t t t)
()
real-make-method-initargs-form))
(compute-effective-method
standard
((generic-function combin applicable-methods)
(generic-function standard-method-combination t)
()
standard-compute-effective-method)
((generic-function combin applicable-methods)
(generic-function short-method-combination t)
()
short-compute-effective-method))))
(defmacro defgeneric (fun-name lambda-list &body options)
(declare (type list lambda-list))
(check-designator fun-name 'defgeneric #'legal-fun-name-p "function name")
(with-current-source-form (lambda-list)
(check-gf-lambda-list lambda-list))
(let ((initargs ())
(methods ()))
(flet ((duplicate-option (name)
(%program-error "The option ~S appears more than once." name))
(expand-method-definition (qab) ; QAB = qualifiers, arglist, body
(let* ((arglist-pos (position-if #'listp qab))
(arglist (elt qab arglist-pos))
(qualifiers (subseq qab 0 arglist-pos))
(body (nthcdr (1+ arglist-pos) qab)))
`(defmethod ,fun-name ,@qualifiers ,arglist ,@body))))
(macrolet ((initarg (key) `(getf initargs ,key)))
(dolist (option options)
(let ((car-option (car option)))
(case car-option
(declare
(dolist (spec (cdr option))
(unless (consp spec)
(%program-error "~@<Invalid declaration specifier in ~
DEFGENERIC: ~S~:@>"
spec))
(when (member (first spec)
;; FIXME: this list is slightly weird.
;; ANSI (on the DEFGENERIC page) in one
;; place allows only OPTIMIZE; in
;; another place gives this list of
;; disallowed declaration specifiers.
;; This seems to be the only place where
;; the FUNCTION declaration is
;; mentioned; TYPE seems to be missing.
;; Very strange. -- CSR, 2002-10-21
'(declaration ftype function
inline notinline special))
(%program-error "The declaration specifier ~S is ~
not allowed inside DEFGENERIC."
spec))
(if (or (eq 'optimize (first spec))
(info :declaration :known (first spec)))
(push spec (initarg :declarations))
(warn "Ignoring unrecognized declaration in DEFGENERIC: ~S"
spec))))
(:method-combination
(when (initarg car-option)
(duplicate-option car-option))
(unless (symbolp (cadr option))
(%program-error "METHOD-COMBINATION name not a symbol: ~
~S"
(cadr option)))
(setf (initarg car-option)
`',(cdr option)))
(:argument-precedence-order
(let* ((required (nth-value 1 (parse-lambda-list lambda-list)))
(supplied (cdr option)))
(unless (= (length required) (length supplied))
(%program-error "argument count discrepancy in ~
:ARGUMENT-PRECEDENCE-ORDER clause."))
(when (set-difference required supplied)
(%program-error "unequal sets for ~
:ARGUMENT-PRECEDENCE-ORDER clause: ~
~S and ~S"
required supplied))
(setf (initarg car-option)
`',(cdr option))))
((:documentation :generic-function-class :method-class)
(unless (proper-list-of-length-p option 2)
(error "bad list length for ~S" option))
(if (initarg car-option)
(duplicate-option car-option)
(setf (initarg car-option) `',(cadr option))))
(:method
(push (cdr option) methods))
(t
;; ANSI requires that unsupported things must get a
;; PROGRAM-ERROR.
(%program-error "unsupported option ~S" option)))))
(when (initarg :declarations)
(setf (initarg :declarations)
`',(initarg :declarations))))
`(progn
(eval-when (:compile-toplevel :load-toplevel :execute)
(compile-or-load-defgeneric ',fun-name))
(load-defgeneric ',fun-name ',lambda-list
(sb-c:source-location) ,@initargs)
,@(when methods
`((set-initial-methods (list ,@(mapcar #'expand-method-definition methods))
(fdefinition ',fun-name))))
(fdefinition ',fun-name)))))
(defun set-initial-methods (methods gf)
(sb-thread::with-recursive-system-lock ((gf-lock gf))
(setf (generic-function-initial-methods gf) methods)))
(defun compile-or-load-defgeneric (fun-name)
(proclaim-as-fun-name fun-name)
(when (typep fun-name '(cons (eql setf)))
(sb-c::warn-if-setf-macro fun-name))
(note-name-defined fun-name :function)
(unless (eq (info :function :where-from fun-name) :declared)
;; Hmm. This is similar to BECOME-DEFINED-FUN-NAME
;; except that it doesn't clear an :ASSUMED-TYPE. Should it?
(setf (info :function :where-from fun-name) :defined)
(setf (info :function :type fun-name)
(if (eq **boot-state** 'complete)
:generic-function
(specifier-type 'function)))))
(defun load-defgeneric (fun-name lambda-list source-location &rest initargs)
(when (fboundp fun-name)
(warn 'sb-kernel:redefinition-with-defgeneric
:name fun-name
:new-location source-location)
(let ((fun (fdefinition fun-name)))
(when (generic-function-p fun)
(sb-thread::with-recursive-system-lock ((gf-lock fun))
(loop for method in (generic-function-initial-methods fun)
do (remove-method fun method))
(setf (generic-function-initial-methods fun) '())))))
(apply #'ensure-generic-function
fun-name
:lambda-list lambda-list
'source source-location
initargs))
(define-condition generic-function-lambda-list-error
(reference-condition simple-program-error)
()
(:default-initargs :references '((:ansi-cl :section (3 4 2)))))
(defun generic-function-lambda-list-error (format-control &rest format-arguments)
(error 'generic-function-lambda-list-error
:format-control format-control
:format-arguments format-arguments))
(defun check-gf-lambda-list (lambda-list)
(declare (muffle-conditions compiler-note))
(binding* ((context "a generic function lambda list")
((nil nil optional nil keys)
(multiple-value-call #'check-lambda-list-names
(parse-lambda-list
lambda-list
:accept (lambda-list-keyword-mask
'(&optional &rest &key &allow-other-keys))
:condition-class 'generic-function-lambda-list-error
:context context)
:context context
:signal-via #'generic-function-lambda-list-error)))
;; PARSE-LAMBDA-LIST validates the skeleton, so just check for
;; incorrect use of defaults.
(labels ((lose (kind arg)
(generic-function-lambda-list-error
(sb-format:tokens
"~@<Invalid ~A argument specifier ~S ~_in ~A ~
~/sb-impl:print-lambda-list/~:>")
kind arg context lambda-list))
(verify-optional (spec)
(when (nth-value 3 (parse-optional-arg-spec spec))
(lose '&optional spec)))
(verify-key (spec)
(when (nth-value 4 (parse-key-arg-spec spec))
(lose '&key spec))))
;; no defaults or supplied-p vars allowed for &OPTIONAL or &KEY
(mapc #'verify-optional optional)
(mapc #'verify-key keys))))
(defun check-method-lambda (method-lambda context)
(unless (typep method-lambda '(cons (eql lambda)))
(error "~@<The METHOD-LAMBDA argument to ~
~/sb-ext:print-symbol-with-prefix/, ~S, is not a lambda ~
form.~@:>"
context method-lambda))
method-lambda)
(eval-when (:compile-toplevel :load-toplevel :execute)
(fmakunbound 'defmethod))
;;; As per CLHS -
;;; "defmethod is not required to perform any compile-time side effects."
;;; and we don't do much other than to make the function known to be defined,
;;; which means that checking of callers' arglists can only occur after called
;;; methods are actually loaded.
(defmacro defmethod (name &rest args)
(check-designator name 'defmethod #'legal-fun-name-p "function name")
(multiple-value-bind (qualifiers lambda-list body)
(parse-defmethod args)
`(progn
(eval-when (:compile-toplevel :execute)
;; :compile-toplevel is needed for subsequent forms
;; :execute is needed for references to itself inside the body
(compile-or-load-defgeneric ',name))
;; KLUDGE: this double expansion is quite a monumental
;; workaround: it comes about because of a fantastic interaction
;; between the processing rules of CLHS 3.2.3.1 and the
;; bizarreness of MAKE-METHOD-LAMBDA.
;;
;; MAKE-METHOD-LAMBDA can be called by the user, and if the
;; lambda itself doesn't refer to outside bindings the return
;; value must be compileable in the null lexical environment.
;; However, the function must also refer somehow to the
;; associated method object, so that it can call NO-NEXT-METHOD
;; with the appropriate arguments if there is no next method --
;; but when the function is generated, the method object doesn't
;; exist yet.
;;
;; In order to resolve this issue, we insert a literal cons cell
;; into the body of the method lambda, return the same cons cell
;; as part of the second (initargs) return value of
;; MAKE-METHOD-LAMBDA, and a method on INITIALIZE-INSTANCE fills
;; in the cell when the method is created. However, this
;; strategy depends on having a fresh cons cell for every method
;; lambda, which (without the workaround below) is skewered by
;; the processing in CLHS 3.2.3.1, which permits implementations
;; to macroexpand the bodies of EVAL-WHEN forms with both
;; :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL only once. The
;; expansion below forces the double expansion in those cases,
;; while expanding only once in the common case.
(eval-when (:load-toplevel)
(%defmethod-expander ,name ,qualifiers ,lambda-list ,body))
(eval-when (:execute)
(%defmethod-expander ,name ,qualifiers ,lambda-list ,body)))))
(defmacro %defmethod-expander
(name qualifiers lambda-list body &environment env)
(multiple-value-bind (proto-gf proto-method)
(prototypes-for-make-method-lambda name)
(expand-defmethod name proto-gf proto-method qualifiers
lambda-list body env)))
(defun prototypes-for-make-method-lambda (name)
(if (not (eq **boot-state** 'complete))
(values nil nil)
(let ((gf? (and (fboundp name)
(gdefinition name))))
(if (or (null gf?)
(not (generic-function-p gf?)))
(values (class-prototype (find-class 'standard-generic-function))
(class-prototype (find-class 'standard-method)))
(values gf?
(class-prototype (or (generic-function-method-class gf?)
(find-class 'standard-method))))))))
;;; Take a name which is either a generic function name or a list specifying
;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
;;; the prototype instance of the method-class for that generic function.
;;;
;;; If there is no generic function by that name, this returns the
;;; default value, the prototype instance of the class
;;; STANDARD-METHOD. This default value is also returned if the spec
;;; names an ordinary function or even a macro. In effect, this leaves
;;; the signalling of the appropriate error until load time.
;;;
;;; Note: During bootstrapping, this function is allowed to return NIL.
(defun method-prototype-for-gf (name)
(let ((gf? (and (fboundp name)
(gdefinition name))))
(cond ((neq **boot-state** 'complete) nil)
((or (null gf?)
(not (generic-function-p gf?))) ; Someone else MIGHT
; error at load time.
(class-prototype (find-class 'standard-method)))
(t
(class-prototype (or (generic-function-method-class gf?)
(find-class 'standard-method)))))))
;;; These are used to communicate the method name and lambda-list to
;;; MAKE-METHOD-LAMBDA-INTERNAL.
(defvar *method-name* nil)
(defvar *method-lambda-list* nil)
(defun expand-defmethod (name proto-gf proto-method qualifiers lambda-list
body env)
(binding* (;; ENV could be of type SB-INTERPRETER:BASIC-ENV but I
;; don't care to figure out what parts of PCL would have
;; to change to accept that, so coerce.
(env (sb-kernel:coerce-to-lexenv env))
((nil unspecialized-lambda-list specializers)
(with-current-source-form (lambda-list)
(parse-specialized-lambda-list lambda-list)))
(*method-name* `(,name ,@qualifiers ,specializers))
(method-lambda `(lambda ,unspecialized-lambda-list
(declare (sb-c::source-form
(lambda ,unspecialized-lambda-list
,@body))
(sb-c::current-defmethod ,name ,qualifiers ,specializers
,unspecialized-lambda-list))
,@body))
((method-function-lambda initargs new-lambda-list)
(make-method-lambda-using-specializers
proto-gf proto-method qualifiers specializers method-lambda env))
(initargs-form
(make-method-initargs-form
proto-gf proto-method method-function-lambda initargs env))
(specializers-form
(make-method-specializers-form
proto-gf proto-method specializers env)))
(mapc (lambda (specializer parameter)
(when (typep specializer 'type-specifier)
(with-current-source-form (parameter)
(check-deprecated-type specializer))))
specializers lambda-list)
;; Note: We could DECLAIM the ftype of the generic function here,
;; since ANSI specifies that we create it if it does not
;; exist. However, I chose not to, because I think it's more
;; useful to support a style of programming where every generic
;; function has an explicit DEFGENERIC and any typos in DEFMETHODs
;; are warned about. Otherwise
;;
;; (DEFGENERIC FOO-BAR-BLETCH (X))
;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
;;
;; compiles without raising an error and runs without raising an
;; error (since SIMPLE-VECTOR cases fall through to VECTOR) but
;; still doesn't do what was intended. I hate that kind of bug
;; (code which silently gives the wrong answer), so we don't do a
;; DECLAIM here. -- WHN 20000229
(make-defmethod-form name qualifiers specializers-form
(or new-lambda-list unspecialized-lambda-list)
(if proto-method
(class-name (class-of proto-method))
'standard-method)
initargs-form)))
(defun make-defmethod-form
(name qualifiers specializers unspecialized-lambda-list
method-class-name initargs-form)
(declare (sb-ext:muffle-conditions sb-ext:code-deletion-note))
(let (fn
fn-lambda)
(if (and (interned-symbol-p (fun-name-block-name name))
(every #'interned-symbol-p qualifiers)
(every (lambda (s)
(if (consp s)
(and (eq (car s) 'eql)
(constantp (cadr s))
(let ((sv (constant-form-value (cadr s))))
(or (interned-symbol-p sv)
(integerp sv)
(and (characterp sv)
(standard-char-p sv)))))
(interned-symbol-p s)))
specializers)
(consp initargs-form)
(eq (car initargs-form) 'list*)
(memq (cadr initargs-form) '(:function))
(consp (setq fn (caddr initargs-form)))
(eq (car fn) 'function)
(consp (setq fn-lambda (cadr fn)))
(eq (car fn-lambda) 'lambda)
(sb-impl::unreachable))
(let* ((specls (mapcar (lambda (specl)
(if (consp specl)
;; CONSTANT-FORM-VALUE? What I
;; kind of want to know, though,
;; is what happens if we don't do
;; this for some slow-method
;; function because of a hairy
;; lexenv -- is the only bad
;; effect that the method
;; function ends up unnamed? If
;; so, couldn't we arrange to
;; name it later?
`(,(car specl) ,(eval (cadr specl)))
specl))
specializers))
(mname `(,(if (eq (cadr initargs-form) :function)
'slow-method 'fast-method)
,name ,@qualifiers ,specls)))
`(progn
(defun ,mname ,(cadr fn-lambda)
,@(cddr fn-lambda))
,(make-defmethod-form-internal
name qualifiers `',specls
unspecialized-lambda-list method-class-name
`(list* ,(cadr initargs-form)
#',mname
,@(cdddr initargs-form)))))
(make-defmethod-form-internal
name qualifiers
specializers
#+nil
`(list ,@(mapcar (lambda (specializer)
(if (consp specializer)
``(,',(car specializer)
,,(cadr specializer))
`',specializer))
specializers))
unspecialized-lambda-list
method-class-name
initargs-form))))
(defun make-defmethod-form-internal
(name qualifiers specializers-form unspecialized-lambda-list
method-class-name initargs-form)
`(load-defmethod
',method-class-name
',name
',qualifiers
,specializers-form
',unspecialized-lambda-list
,initargs-form
(sb-c:source-location)))
(defmacro make-method-function (method-lambda &environment env)
(binding* (((proto-gf proto-method)
(prototypes-for-make-method-lambda nil))
((method-function-lambda initargs)
(make-method-lambda proto-gf proto-method method-lambda env))) ; FIXME: coerce-to-lexenv?
(make-method-initargs-form
proto-gf proto-method method-function-lambda initargs env)))
(defun real-make-method-initargs-form (proto-gf proto-method
method-lambda initargs env)
(declare (ignore proto-gf proto-method))
(check-method-lambda method-lambda 'make-method-initargs)
(make-method-initargs-form-internal method-lambda initargs env))
(unless (fboundp 'make-method-initargs-form)
(setf (gdefinition 'make-method-initargs-form)
(symbol-function 'real-make-method-initargs-form)))
(defun real-make-method-lambda-using-specializers
(proto-gf proto-method qualifiers specializers method-lambda env)
(declare (ignore qualifiers))
(check-method-lambda method-lambda 'make-method-lambda) ; TODO remove check in make-method-lambda
;; Default behavior: delegate to MAKE-METHOD-LAMBDA.
(let* ((lambda-list (second method-lambda))
(*method-lambda-list*
(append
(mapcar #'list (subseq lambda-list 0 (length specializers)) specializers)
(subseq lambda-list (length specializers)))))
(make-method-lambda proto-gf proto-method method-lambda env)))
(unless (fboundp 'make-method-lambda-using-specializers)
(setf (gdefinition 'make-method-lambda-using-specializers)
(symbol-function 'real-make-method-lambda-using-specializers)))
;;; When bootstrapping PCL MAKE-METHOD-LAMBDA starts out as a regular
;;; function: REAL-MAKE-METHOD-LAMBDA set to the fdefinition of
;;; MAKE-METHOD-LAMBDA. Once generic functions are born,
;;; REAL-MAKE-METHOD-LAMBDA is used to implement the default method.
;;; MAKE-METHOD-LAMBDA-INTERNAL is split out into a separate function
;;; so that changing it in a live image is easy, and changes actually
;;; take effect.
(defun real-make-method-lambda (proto-gf proto-method method-lambda env)
(make-method-lambda-internal proto-gf proto-method method-lambda env))
(unless (fboundp 'make-method-lambda)
(setf (gdefinition 'make-method-lambda)
(symbol-function 'real-make-method-lambda)))
(defun declared-specials (declarations)
(loop for (declare . specifiers) in declarations
append (loop for specifier in specifiers
when (eq 'special (car specifier))
append (cdr specifier))))
;;; A helper function for creating Python-friendly type declarations
;;; in DEFMETHOD forms.
;;;
;;; This function operates on
;;; * non-parsed specializers, i.e. class names and extended
;;; specializer syntaxes
;;; * parsed specializers, i.e. CLASSes, EQL-SPECIALIZERs,
;;; CLASS-EQ-SPECIALIZERs and generic SPECIALIZERs
;;;
;;; We're too lazy to cons up a new environment for this, so we just
;;; pass in the list of locally declared specials in addition to the
;;; old environment.
(defun parameter-specializer-declaration-in-defmethod
(proto-generic-function proto-method parameter specializer specials env)
(flet ((declare-type (type)
(return-from parameter-specializer-declaration-in-defmethod
(case type
((nil) '(ignorable))
(t `(type ,type ,parameter))))))
(cond
((not (eq **boot-state** 'complete))
;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
;; types which don't match their specializers. (Specifically,
;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
;; second argument.) Hopefully it only does this kind of
;; weirdness when bootstrapping.. -- WHN 20000610
(declare-type nil))
;; Independent of SPECIALIZER, bail out if the PARAMETER is
;; known to be a special variable. Our rebinding magic for SETQ
;; cases doesn't work right there as SET, (SETF SYMBOL-VALUE),
;; etc. make things undecidable.
((or (var-special-p parameter env) (member parameter specials))
(declare-type nil))
;; Bail out on SLOT-OBJECT special case.
;;
;; KLUDGE: For some low-level implementation classes, perhaps
;; because of some problems related to the incomplete
;; integration of PCL into SBCL's type system, some specializer
;; classes can't be declared as argument types. E.g.
;; (DEFMETHOD FOO ((X SLOT-OBJECT))
;; (DECLARE (TYPE SLOT-OBJECT X))
;; ..)
;; loses when
;; (DEFSTRUCT BAR A B)
;; (FOO (MAKE-BAR))
;; perhaps because of the way that STRUCTURE-OBJECT inherits
;; both from SLOT-OBJECT and from SB-KERNEL:INSTANCE. In an
;; effort to sweep such problems under the rug, we exclude these
;; problem cases here. -- WHN 2001-01-19
((eq specializer 'slot-object)
(declare-type nil))
;; Bail out on unparsed EQL-specializers.
;;
;; KLUDGE: ANSI, in its wisdom, says that EQL-SPECIALIZER-FORMs
;; in EQL specializers are evaluated at DEFMETHOD expansion
;; time. Thus, although one might think that in
;; (DEFMETHOD FOO ((X PACKAGE)
;; (Y (EQL 12))
;; ..))
;; the PACKAGE and (EQL 12) forms are both parallel type names,
;; they're not, as is made clear when you do
;; (DEFMETHOD FOO ((X PACKAGE)
;; (Y (EQL 'BAR)))
;; ..)
;; where Y needs to be a symbol named "BAR", not some cons made
;; by (CONS 'QUOTE 'BAR). I.e. when the EQL-SPECIALIZER-FORM is
;; (EQL 'X), it requires an argument to be of type (EQL X). It'd
;; be easy to transform one to the other, but it'd be somewhat
;; messier to do so while ensuring that the EQL-SPECIALIZER-FORM
;; is only EVAL'd once. (The new code wouldn't be messy, but
;; it'd require a big transformation of the old code.) So
;; instead we punt. -- WHN 20000610
((typep specializer '(cons (eql eql)))
(declare-type nil))
;; Parsed specializer objects, i.e. CLASS, EQL-SPECIALIZER,
;; CLASS-EQ-SPECIALIZER and generic SPECIALIZER.
;;
;; Also unparsed specializers other than EQL: these have to be
;; either class names or extended specializers.
;;
;; For these, we can usually make Python very happy.
;;
;; KLUDGE: Since INFO doesn't work right for class objects here,
;; and they are valid specializers, see if the specializer is
;; a named class, and use the name in that case -- otherwise
;; the class instance is ok, since info will just return NIL, NIL.
;;
;; We still need to deal with the class case too, but at
;; least #.(find-class 'integer) and integer as equivalent
;; specializers with this.
(t
(declare-type (specializer-type-specifier
proto-generic-function proto-method specializer))))))
(defun make-method-lambda-internal (proto-gf proto-method method-lambda env)
(check-method-lambda method-lambda 'make-method-lambda)
(binding* (((real-body declarations documentation)
(parse-body (cddr method-lambda) t))
;; We have the %METHOD-NAME declaration in the place
;; where we expect it only if there is are no
;; non-standard prior MAKE-METHOD-LAMBDA methods -- or
;; unless they're fantastically unintrusive.
(method-name *method-name*)
(method-lambda-list *method-lambda-list*)
;; Macroexpansion caused by code-walking may call
;; make-method-lambda and end up with wrong values
(*method-name* nil)
(*method-lambda-list* nil)
(generic-function-name (when method-name (car method-name)))
;; the method-cell is a way of communicating what method
;; a method-function implements, for the purpose of
;; NO-NEXT-METHOD. We need something that can be shared
;; between function and initargs, but not something that
;; will be coalesced as a constant (because we are
;; naughty, oh yes) with the expansion of any other
;; methods in the same file. -- CSR, 2007-05-30
(method-cell (list (make-symbol "METHOD-CELL")))
((parameters lambda-list specializers)
(parse-specialized-lambda-list
(or method-lambda-list
(ecase (car method-lambda)
(lambda (second method-lambda))
(named-lambda (third method-lambda))))))
(required-parameters (subseq parameters 0 (length specializers)))
(slots (mapcar #'list required-parameters))
(class-declarations
`(declare
;; These declarations seem to be used by PCL to pass
;; information to itself; when I tried to delete 'em
;; ca. 0.6.10 it didn't work. I'm not sure how they
;; work, but note the (VAR-DECLARATION '%CLASS ..)
;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN
;; 2000-12-30
,@(mapcan (lambda (parameter specializer)
(when (typep specializer '(and symbol (not (eql t))))
(list `(%class ,parameter ,specializer))))
parameters specializers)
;; These TYPE declarations weren't in the original PCL
;; code, but the Python compiler likes them a
;; lot. (We're telling the compiler about our
;; knowledge of specialized argument types so that it
;; can avoid run-time type dispatch overhead, which
;; can be a huge win for Python.)
,@(let ((specials (declared-specials declarations)))
(mapcar (lambda (par spec)
(parameter-specializer-declaration-in-defmethod
proto-gf proto-method par spec specials env))
parameters specializers))))
(parameter-declarations
`(declare
,@(mapcan (lambda (parameter)
(list `(%parameter ,parameter)))
required-parameters)))
(method-lambda
;; Remove the documentation string and insert the
;; appropriate class declarations. The documentation
;; string is removed to make it easy for us to insert
;; new declarations later, they will just go after the
;; CADR of the method lambda. The class declarations
;; are inserted to communicate the class of the method's
;; arguments to the code walk.
`(lambda ,lambda-list
;; The default ignorability of method parameters
;; doesn't seem to be specified by ANSI. PCL had
;; them basically ignorable but was a little
;; inconsistent. E.g. even though the two
;; method definitions
;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
;; (DEFMETHOD FOO ((X T) Y) "Z")
;; are otherwise equivalent, PCL treated Y as
;; ignorable in the first definition but not in the
;; second definition. We make all required
;; parameters ignorable as a way of systematizing
;; the old PCL behavior. -- WHN 2000-11-24
(declare (ignorable ,@required-parameters))
,class-declarations
,parameter-declarations
,@declarations
(block ,(fun-name-block-name generic-function-name)
,@real-body)))
(constant-value-p (and (null (cdr real-body))
(constantp (car real-body))))
(constant-value (when constant-value-p
(constant-form-value (car real-body))))
(plist (when (and constant-value-p
(or (typep constant-value '(or number character))
(and (symbolp constant-value)
(symbol-package constant-value))))
(list :constant-value constant-value)))
(applyp (dolist (p lambda-list nil)
(cond ((memq p '(&optional &rest &key))
(return t))
((eq p '&aux)
(return nil)))))
((walked-lambda call-next-method-p setq-p parameters-setqd)
(walk-method-lambda
method-lambda required-parameters env slots))
((walked-lambda-body walked-declarations)
(parse-body (cddr walked-lambda) t)))
(when (some #'cdr slots)
(let ((slot-name-lists (slot-name-lists-from-slots slots)))
(setf plist
`(,@(when slot-name-lists
`(:slot-name-lists ,slot-name-lists))
,@plist)
walked-lambda-body
`((pv-binding (,required-parameters
,slot-name-lists
(load-time-value
(intern-pv-table
:slot-name-lists ',slot-name-lists)))
,@walked-lambda-body)))))
(when (and (memq '&key lambda-list)
(not (memq '&allow-other-keys lambda-list)))
(let ((aux (memq '&aux lambda-list)))
(setq lambda-list (nconc (ldiff lambda-list aux)
(list '&allow-other-keys)
aux))))
(values `(lambda (.method-args. .next-methods.)
(simple-lexical-method-functions
(,lambda-list .method-args. .next-methods.
:call-next-method-p
,(when call-next-method-p t)
:setq-p ,setq-p
:parameters-setqd ,parameters-setqd
:method-cell ,method-cell
:applyp ,applyp)
,@walked-declarations
(locally (declare (disable-package-locks
%parameter-binding-modified))
(symbol-macrolet ((%parameter-binding-modified
',@parameters-setqd))
(declare (enable-package-locks
%parameter-binding-modified))
,@walked-lambda-body))))
`(,@(when call-next-method-p `(method-cell ,method-cell))
,@(when (member call-next-method-p '(:simple nil))
'(simple-next-method-call t))
,@(when plist `(plist ,plist))
,@(when documentation `(:documentation ,documentation))))))
(define-condition specializer-name-syntax-error (error
reference-condition)
((generic-function :initarg :generic-function
:reader specializer-name-syntax-error-generic-function)
(specializer-name :initarg :specializer-name
:reader specializer-name-syntax-error-specializer-name))
(:default-initargs
:references '((:ansi-cl :macro defmethod)
(:ansi-cl :glossary "parameter specializer name")))
(:report
(lambda (condition stream)
(format stream "~@<~S is not a valid parameter specializer name ~
for ~S.~@:>"
(specializer-name-syntax-error-specializer-name condition)
(specializer-name-syntax-error-generic-function condition)))))
(defun specializer-name-syntax-error (specializer-name generic-function)
(error 'specializer-name-syntax-error :generic-function generic-function
:specializer-name specializer-name))
(defun real-make-method-specializers-form
(proto-generic-function proto-method specializer-names environment)
(flet ((make-parse-form (name)
(make-specializer-form-using-class
proto-generic-function proto-method name environment)))
`(list ,@(mapcar #'make-parse-form specializer-names))))
(unless (fboundp 'make-method-specializers-form)
(setf (gdefinition 'make-method-specializers-form)
(symbol-function 'real-make-method-specializers-form)))
(defun real-make-specializer-form-using-class/t
(proto-generic-function proto-method specializer-name environment)
(declare (ignore proto-method environment))
(specializer-name-syntax-error specializer-name proto-generic-function))
(defun real-make-specializer-form-using-class/specializer
(proto-generic-function proto-method specializer-name environment)
(declare (ignore proto-generic-function proto-method environment))
(when (eq **boot-state** 'complete)