| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1992, Giuseppe Attardi. |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
(in-package "CLOS") |
| 10 |
|
| 11 |
;;; ---------------------------------------------------------------------- |
| 12 |
;;; DEFGENERIC |
| 13 |
;;; |
| 14 |
|
| 15 |
(defmacro defgeneric (&whole whole &rest args) |
| 16 |
(multiple-value-bind (function-specifier lambda-list options) |
| 17 |
(parse-defgeneric args) |
| 18 |
(parse-lambda-list lambda-list) |
| 19 |
;; process options |
| 20 |
(multiple-value-bind (option-list method-list) |
| 21 |
(parse-generic-options options lambda-list) |
| 22 |
(let* ((output `(ensure-generic-function ',function-specifier |
| 23 |
:delete-methods t ,@option-list))) |
| 24 |
(ext:register-with-pde |
| 25 |
whole |
| 26 |
(if method-list |
| 27 |
`(progn |
| 28 |
,output |
| 29 |
(associate-methods-to-gfun |
| 30 |
',function-specifier |
| 31 |
,@(loop for m in method-list |
| 32 |
collect `(defmethod ,function-specifier ,@m)))) |
| 33 |
output)))))) |
| 34 |
|
| 35 |
(defun parse-defgeneric (args) |
| 36 |
(declare (si::c-local)) |
| 37 |
;; (values function-specifier lambda-list options) |
| 38 |
(let (function-specifier) |
| 39 |
(unless args |
| 40 |
(simple-program-error "Illegal defgeneric form: missing generic function name")) |
| 41 |
(setq function-specifier (pop args)) |
| 42 |
(unless args |
| 43 |
(simple-program-error "Illegal defgeneric form: missing lambda-list")) |
| 44 |
(values function-specifier (first args) (rest args)))) |
| 45 |
|
| 46 |
(defun parse-generic-options (options lambda-list) |
| 47 |
(declare (si::c-local)) |
| 48 |
(let* ((processed-options '()) |
| 49 |
(method-list '()) |
| 50 |
(declarations '()) |
| 51 |
arg-list) |
| 52 |
(dolist (option options) |
| 53 |
(let ((option-name (first option)) |
| 54 |
option-value) |
| 55 |
(cond ((eq option-name :method) |
| 56 |
;; We do not need to check the validity of this |
| 57 |
;; because DEFMETHOD will do it. |
| 58 |
(push (rest option) method-list)) |
| 59 |
((eq option-name 'declare) |
| 60 |
(setf declarations (append (rest option) declarations))) |
| 61 |
((member option-name processed-options) |
| 62 |
(simple-program-error "Option ~s specified more than once" |
| 63 |
option-name)) |
| 64 |
(t |
| 65 |
(push option-name processed-options) |
| 66 |
;; We leave much of the type checking for SHARED-INITIALIZE |
| 67 |
(setq option-value |
| 68 |
(case option-name |
| 69 |
(:argument-precedence-order |
| 70 |
(rest option)) |
| 71 |
(:method-combination |
| 72 |
(rest option)) |
| 73 |
((:documentation :generic-function-class :method-class) |
| 74 |
(unless (endp (cddr option)) |
| 75 |
(simple-program-error "Too many arguments for option ~A" |
| 76 |
option-name)) |
| 77 |
(second option)) |
| 78 |
(otherwise |
| 79 |
(simple-program-error "~S is not a legal defgeneric option" |
| 80 |
option-name)))) |
| 81 |
(setf arg-list `(',option-name ',option-value ,@arg-list)))))) |
| 82 |
(values `(:lambda-list ',lambda-list ,@arg-list |
| 83 |
,@(when declarations `(:declarations ',declarations))) |
| 84 |
method-list))) |
| 85 |
|
| 86 |
(defun parse-lambda-list (lambda-list &optional post-keyword) |
| 87 |
(declare (si::c-local)) |
| 88 |
(let ((arg (car lambda-list))) |
| 89 |
(cond ((null lambda-list)) |
| 90 |
((eq arg '&AUX) |
| 91 |
(simple-program-error "&aux is not allowed in a generic function lambda-list")) |
| 92 |
((member arg lambda-list-keywords) |
| 93 |
(parse-lambda-list (cdr lambda-list) t)) |
| 94 |
(post-keyword |
| 95 |
;; After a lambda-list-keyword there can be no specializers. |
| 96 |
(parse-lambda-list (cdr lambda-list) t)) |
| 97 |
(t |
| 98 |
(if (listp arg) |
| 99 |
(simple-program-error "the parameters cannot be specialized in generic function lambda-list") |
| 100 |
(parse-lambda-list (cdr lambda-list))))))) |
| 101 |
|
| 102 |
(defun valid-declaration-p (decl) |
| 103 |
;(declare (si::c-local)) |
| 104 |
(and (eq (first decl) 'OPTIMIZE) |
| 105 |
(loop for item in decl |
| 106 |
always (or (atom item) |
| 107 |
(and (consp item) |
| 108 |
(member (first item) |
| 109 |
'(SPEED SPACE COMPILATION-SPEED DEBUG SAFETY))))))) |
| 110 |
|
| 111 |
;;; ---------------------------------------------------------------------- |
| 112 |
;;; GENERIC FUNCTION (RE)INITIALIZATION PROTOCOL |
| 113 |
;; |
| 114 |
|
| 115 |
(defun lambda-list-required-arguments (lambda-list) |
| 116 |
(rest (si::process-lambda-list lambda-list t))) |
| 117 |
|
| 118 |
(defmethod shared-initialize ((gfun generic-function) slot-names &rest initargs |
| 119 |
&key (name nil) |
| 120 |
(lambda-list nil l-l-p) |
| 121 |
(argument-precedence-order nil a-o-p) |
| 122 |
(documentation nil) |
| 123 |
(declarations nil) |
| 124 |
(method-class (find-class 'method)) |
| 125 |
&aux |
| 126 |
(gfun-name (if (slot-boundp gfun 'name) |
| 127 |
(slot-value gfun 'name) |
| 128 |
(or name :anonymous))) |
| 129 |
) |
| 130 |
(declare (ignore initargs slot-names)) |
| 131 |
;; |
| 132 |
;; Check the validity of several fields. |
| 133 |
;; |
| 134 |
(when a-o-p |
| 135 |
(unless l-l-p |
| 136 |
(simple-program-error "When defining generic function ~A~%Supplied :argument-precedence-order, but :lambda-list is missing" |
| 137 |
gfun-name)) |
| 138 |
(dolist (l (lambda-list-required-arguments lambda-list)) |
| 139 |
(unless (= (count l argument-precedence-order) 1) |
| 140 |
(simple-program-error "When defining generic function ~A~%The required argument ~A does not appear exactly once in the ARGUMENT-PRECEDENCE-ORDER list ~A" |
| 141 |
gfun-name l argument-precedence-order)))) |
| 142 |
(unless (every #'valid-declaration-p declarations) |
| 143 |
(simple-program-error "When defining generic function ~A~%Not a valid declaration list: ~A" |
| 144 |
gfun-name declarations)) |
| 145 |
(unless (or (null documentation) (stringp documentation)) |
| 146 |
(error 'simple-type-error |
| 147 |
:format-control "When defining generic function~A~%Not a valid documentation object ~" |
| 148 |
:format-arguments (list gfun-name documentation) |
| 149 |
:datum documentation |
| 150 |
:expected-type '(or null string))) |
| 151 |
(unless (si::subclassp method-class (find-class 'method)) |
| 152 |
(error 'simple-type-error |
| 153 |
:format-control "When defining generic function~A~%Not a valid method class, ~A" |
| 154 |
:format-arguments (list gfun-name method-class) |
| 155 |
:datum method-class |
| 156 |
:expected-type 'method)) |
| 157 |
;; |
| 158 |
;; When supplying a new lambda-list, ensure that it is compatible with |
| 159 |
;; the old list of methods. |
| 160 |
;; |
| 161 |
(when (and l-l-p (slot-boundp gfun 'methods)) |
| 162 |
(unless (every #'(lambda (x) |
| 163 |
(congruent-lambda-p lambda-list x)) |
| 164 |
(mapcar #'method-lambda-list (generic-function-methods gfun))) |
| 165 |
(simple-program-error "Cannot replace the lambda list of ~A with ~A because it is incongruent with some of the methods" |
| 166 |
gfun lambda-list))) |
| 167 |
(call-next-method) |
| 168 |
(let ((combination (generic-function-method-combination gfun))) |
| 169 |
(unless (typep combination 'method-combination) |
| 170 |
(setf (generic-function-method-combination gfun) |
| 171 |
(find-method-combination gfun (first combination) (rest combination))))) |
| 172 |
(when (and l-l-p (not a-o-p)) |
| 173 |
(setf (generic-function-argument-precedence-order gfun) |
| 174 |
(lambda-list-required-arguments lambda-list))) |
| 175 |
(set-generic-function-dispatch gfun) |
| 176 |
gfun) |
| 177 |
|
| 178 |
(defmethod shared-initialize ((gfun standard-generic-function) slot-names |
| 179 |
&rest initargs) |
| 180 |
(declare (ignore slot-names)) |
| 181 |
(call-next-method) |
| 182 |
(when (generic-function-methods gfun) |
| 183 |
(compute-g-f-spec-list gfun)) |
| 184 |
(update-dependents gfun initargs) |
| 185 |
gfun) |
| 186 |
|
| 187 |
(defun associate-methods-to-gfun (name &rest methods) |
| 188 |
(let ((gfun (fdefinition name))) |
| 189 |
(dolist (method methods) |
| 190 |
(setf (getf (method-plist method) :method-from-defgeneric-p) t)) |
| 191 |
gfun)) |
| 192 |
|
| 193 |
(defmethod ensure-generic-function-using-class |
| 194 |
((gfun generic-function) name &rest args &key |
| 195 |
(method-class 'STANDARD-METHOD method-class-p) |
| 196 |
(generic-function-class (class-of gfun)) |
| 197 |
(delete-methods nil)) |
| 198 |
;; modify the existing object |
| 199 |
(setf args (copy-list args)) |
| 200 |
(remf args :generic-function-class) |
| 201 |
(remf args :declare) |
| 202 |
(remf args :environment) |
| 203 |
(remf args :delete-methods) |
| 204 |
;; FIXME! We should check that the class GENERIC-FUNCTION-CLASS is compatible |
| 205 |
;; with the old one. In what sense "compatible" is ment, I do not know! |
| 206 |
;; (See ANSI DEFGENERIC entry) |
| 207 |
(when (symbolp generic-function-class) |
| 208 |
(setf generic-function-class (find-class generic-function-class))) |
| 209 |
(unless (si::subclassp generic-function-class (find-class 'generic-function)) |
| 210 |
(error "~A is not a valid :GENERIC-FUNCTION-CLASS argument for ENSURE-GENERIC-FUNCTION." |
| 211 |
generic-function-class)) |
| 212 |
(when (and method-class-p (symbolp method-class)) |
| 213 |
(setf args (list* :method-class (find-class method-class) args))) |
| 214 |
(when delete-methods |
| 215 |
(dolist (m (copy-list (generic-function-methods gfun))) |
| 216 |
(when (getf (method-plist m) :method-from-defgeneric-p) |
| 217 |
(remove-method gfun m)))) |
| 218 |
(if (eq (class-of gfun) generic-function-class) |
| 219 |
(apply #'reinitialize-instance gfun args) |
| 220 |
(apply #'change-class gfun generic-function-class args))) |
| 221 |
|
| 222 |
(defmethod ensure-generic-function-using-class |
| 223 |
((gfun null) name &rest args &key |
| 224 |
(method-class 'STANDARD-METHOD method-class-p) |
| 225 |
(generic-function-class 'STANDARD-GENERIC-FUNCTION) |
| 226 |
(delete-methods nil)) |
| 227 |
(declare (ignore delete-methods gfun)) |
| 228 |
;; else create a new generic function object |
| 229 |
(setf args (copy-list args)) |
| 230 |
(remf args :generic-function-class) |
| 231 |
(remf args :declare) |
| 232 |
(remf args :environment) |
| 233 |
(remf args :delete-methods) |
| 234 |
(when (and method-class-p (symbolp generic-function-class)) |
| 235 |
(setf args (list* :method-class (find-class method-class) args))) |
| 236 |
(apply #'make-instance generic-function-class :name name args)) |
| 237 |
|
| 238 |
(defun ensure-generic-function (name &rest args &key &allow-other-keys) |
| 239 |
(let ((gfun (si::traced-old-definition name))) |
| 240 |
(cond ((not (legal-generic-function-name-p name)) |
| 241 |
(simple-program-error "~A is not a valid generic function name" name)) |
| 242 |
((not (fboundp name)) |
| 243 |
(setf (fdefinition name) |
| 244 |
(apply #'ensure-generic-function-using-class gfun name args))) |
| 245 |
((si::instancep (or gfun (setf gfun (fdefinition name)))) |
| 246 |
(apply #'ensure-generic-function-using-class gfun name args)) |
| 247 |
((special-operator-p name) |
| 248 |
(simple-program-error "The special operator ~A is not a valid name for a generic function" name)) |
| 249 |
((macro-function name) |
| 250 |
(simple-program-error "The symbol ~A is bound to a macro and is not a valid name for a generic function" name)) |
| 251 |
((not *clos-booted*) |
| 252 |
(setf (fdefinition name) |
| 253 |
(apply #'ensure-generic-function-using-class nil name args)) |
| 254 |
(fdefinition name)) |
| 255 |
(t |
| 256 |
(simple-program-error "The symbol ~A is bound to an ordinary function and is not a valid name for a generic function" name))))) |