Christophe Weblog Wiki Code Publications Music
e78457b267fd8c30b8742457b45641c7b299935a
[specializable.git] / src / pcl-patch.lisp
1 ;;;; pcl-patch.lisp --- Hot-patch for SBCL's PCL variant.
2 ;;;;
3 ;;;; Copyright (C) 2014 Jan Moringen
4 ;;;;
5 ;;;; Author: Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
6
7 (cl:in-package #:sb-pcl)
8
9 ;;; `make-method-lambda-using-specializers'
10
11 (export '(make-method-lambda-using-specializers))
12
13 (defgeneric make-method-lambda-using-specializers (gf method qualifiers specializers method-lambda env)
14   (:method ((gf standard-generic-function) (method standard-method) qualifiers specializers method-lambda env)
15     (declare (type (cons (eql lambda) (cons list)) method-lambda))
16     ;; Default behavior: delegate to MAKE-METHOD-LAMBDA.
17     (let* ((lambda-list (second method-lambda))
18            (*method-lambda-list* (append
19                                   (mapcar #'list (subseq lambda-list 0 (length specializers)) specializers)
20                                   (subseq lambda-list (length specializers)))))
21       (make-method-lambda gf method method-lambda env)))
22   #+sb-doc
23   (:documentation
24    "TODO
25 return three values:
26 1. the method lambda
27 2. initargs for the method instance
28 3. a (possibly modified) method lambda-list or nil"))
29
30 (defun expand-defmethod (name
31                          proto-gf
32                          proto-method
33                          qualifiers
34                          lambda-list
35                          body
36                          env)
37   (multiple-value-bind (parameters unspecialized-lambda-list specializers)
38       (parse-specialized-lambda-list lambda-list)
39     (declare (ignore parameters))
40     (let ((*method-name* `(,name ,@qualifiers ,specializers))
41           (method-lambda `(lambda ,unspecialized-lambda-list ,@body)))
42       (multiple-value-bind (method-function-lambda initargs new-lambda-list)
43           (make-method-lambda-using-specializers
44            proto-gf proto-method qualifiers specializers method-lambda env)
45         (let ((initargs-form (make-method-initargs-form
46                               proto-gf proto-method method-function-lambda
47                               initargs env))
48               (specializers-form (make-method-specializers-form
49                                   proto-gf proto-method specializers env)))
50           `(progn
51              ;; Note: We could DECLAIM the ftype of the generic function
52              ;; here, since ANSI specifies that we create it if it does
53              ;; not exist. However, I chose not to, because I think it's
54              ;; more useful to support a style of programming where every
55              ;; generic function has an explicit DEFGENERIC and any typos
56              ;; in DEFMETHODs are warned about. Otherwise
57              ;;
58              ;;   (DEFGENERIC FOO-BAR-BLETCH (X))
59              ;;   (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
60              ;;   (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
61              ;;   (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
62              ;;   (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
63              ;;   (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
64              ;;
65              ;; compiles without raising an error and runs without
66              ;; raising an error (since SIMPLE-VECTOR cases fall through
67              ;; to VECTOR) but still doesn't do what was intended. I hate
68              ;; that kind of bug (code which silently gives the wrong
69              ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
70              ,(make-defmethod-form name qualifiers specializers-form
71                                    (or new-lambda-list unspecialized-lambda-list)
72                                    (if proto-method
73                                        (class-name (class-of proto-method))
74                                        'standard-method)
75                                    initargs-form)))))))
76
77 ;;; `make-specializer-form-using-class'
78 ;;;
79 ;;; To free every new custom generic function class from having to
80 ;;; implement iteration over specializers in
81 ;;; `make-method-specializers-form', we provide a default method
82 ;;;
83 ;;;   make-method-specializers-form standard-g-f standard-method
84 ;;;
85 ;;; which performs this iteration and calls the generic function
86 ;;;
87 ;;;   make-specializer-form-using-class proto-g-f proto-m specializer-names env
88 ;;;
89 ;;; on which custom generic function classes can install methods to
90 ;;; handle their custom specializers. The generic function uses OR
91 ;;; method combination to allow the following idiom:
92 ;;;
93 ;;;   (defmethod make-specializer-form-using-class or
94 ;;;       (proto-generic-function MY-GENERIC-FUNCTION)
95 ;;;       (proto-method standard-method)
96 ;;;       (specializer-name cons)
97 ;;;       (environment t))
98 ;;;     (when (typep specializer-name '(cons (eql MY-SPECIALIZER)))
99 ;;;       MY-SPECIALIZER-FORM))
100 ;;;
101 ;;; The OR method combination lets everything but (my-specializer …)
102 ;;; fall through to the next methods which will, at some point, handle
103 ;;; class and eql specializers and eventually reach an error signaling
104 ;;; method for invalid specializers.
105
106 (defmethod make-method-specializers-form
107     ((proto-generic-function standard-generic-function)
108      (proto-method standard-method)
109      (specializer-names t)
110      (environment t))
111   (flet ((make-parse-form (name)
112            (make-specializer-form-using-class
113             proto-generic-function proto-method name environment)))
114     `(list ,@(mapcar #'make-parse-form specializer-names))))
115
116 ;; TODO same approach for parse-specializer-using-class?
117 (defgeneric make-specializer-form-using-class (proto-generic-function proto-method specializer-name environment)
118   (:method-combination or)
119   #+sb-doc
120   (:documentation
121    "Return a form which, when evaluated in lexical environment
122     ENVIRONMENT, parses the specializer SPECIALIZER-NAME and returns
123     the appropriate specializer object.
124
125     Both PROTO-GENERIC-FUNCTION and PROTO-METHOD may be
126     uninitialized. However their types and prototype can be
127     inspected."))
128
129 ;; Default behavior is signaling an error for not otherwise handled
130 ;; specializers.
131 (defmethod make-specializer-form-using-class or
132     ((proto-generic-function standard-generic-function)
133      (proto-method standard-method)
134      (specializer-name t)
135      (environment t))
136   (error 'simple-reference-error
137          :format-control
138          "~@<~S is not a valid parameter specializer name.~@:>"
139          :format-arguments (list specializer-name)
140          :references (list '(:ansi-cl :macro defmethod)
141                            '(:ansi-cl :glossary "parameter specializer name"))))
142
143 (defmethod make-specializer-form-using-class or
144     ((proto-generic-function standard-generic-function)
145      (proto-method standard-method)
146      (specializer-name symbol)
147      (environment t))
148   `(find-class ',specializer-name))
149
150 (defmethod make-specializer-form-using-class or
151     ((proto-generic-function standard-generic-function)
152      (proto-method standard-method)
153      (specializer-name cons)
154      (environment t))
155   ;; In case of unknown specializer or known specializer with syntax
156   ;; error, TYPECASE may fall through to default method with error
157   ;; signaling.
158   (typecase specializer-name
159     ((cons (eql eql) (cons t null))
160      `(intern-eql-specializer ,(second specializer-name)))
161     ((cons (eql class-eq) (cons t null))
162      `(class-eq-specializer (find-class ',(second specializer-name))))))