Debian Codegraph @

src/clos/kernel.lsp

raw
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 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 (in-package "CLOS")
11
12 (defparameter *clos-booted* nil)
13
14 ;;; ----------------------------------------------------------------------
15 ;;;
16 ;;; FIND-CLASS naming classes.
17 ;;;
18 ;;;
19 ;;; (FIND-CLASS <name>) returns the class named <name>. setf can be used
20 ;;; with find-class to set the class named <name>. These are "extrinsic"
21 ;;; names. Neither find-class nor setf of find-class do anything with the
22 ;;; name slot of the class, they only lookup and change the association from
23 ;;; name to class.
24 ;;;
25 ;;; This is only used during boot. The real one is in built-in.
26 (eval-when (:compile-toplevel)
27 (defun setf-find-class (new-value class &optional errorp env)
28 (warn "Ignoring class definition for ~S" class)))
29
30 (defun setf-find-class (new-value name &optional errorp env)
31 (declare (ignore errorp env))
32 (let ((old-class (find-class name nil)))
33 (cond
34 ((and old-class
35 (or (typep old-class 'built-in-class)
36 (member name '(class built-in-class) :test #'eq)))
37 (unless (eq new-value old-class)
38 (error "The class associated to the CL specifier ~S cannot be changed."
39 name)))
40 ((classp new-value)
41 (setf (gethash name si:*class-name-hash-table*) new-value))
42 ((null new-value) (remhash name si:*class-name-hash-table*))
43 (t (error "~A is not a class." new-value))))
44 new-value)
45
46 (defsetf find-class (&rest x) (v) `(setf-find-class ,v ,@x))
47
48 (defun classp (obj)
49 (and (si:instancep obj)
50 (let ((topmost (find-class 'CLASS nil)))
51 ;; All instances can be classes until the class CLASS has
52 ;; been installed. Otherwise, we check the parents.
53 ;(print (list (class-id (class-of obj))topmost (and topmost (class-precedence-list topmost))))
54 (or (null topmost)
55 (si::subclassp (si::instance-class obj) topmost)))
56 t))
57
58 ;;; ----------------------------------------------------------------------
59 ;;; Methods
60
61 (defun install-method (name qualifiers specializers lambda-list fun &rest options)
62 (declare (notinline ensure-generic-function))
63 ;;(record-definition 'method `(method ,name ,@qualifiers ,specializers))
64 (let* ((gf (ensure-generic-function name))
65 (fun (wrapped-method-function fun))
66 (specializers (mapcar #'(lambda (x)
67 (cond ((consp x) (intern-eql-specializer (second x)))
68 ((typep x 'specializer) x)
69 ((find-class x nil))
70 (t
71 (error "In method definition for ~A, found an invalid specializer ~A" name specializers))))
72 specializers))
73 (method (make-method (generic-function-method-class gf)
74 qualifiers specializers lambda-list
75 fun options)))
76 (add-method gf method)
77 method))
78
79 (defun wrapped-method-function (method-function)
80 #'(lambda (.combined-method-args. *next-methods*)
81 (declare (special .combined-method-args. *next-methods*))
82 (apply method-function .combined-method-args.)))
83
84 ;;; ----------------------------------------------------------------------
85 ;;; early versions
86
87 ;;; early version used during bootstrap
88 (defun ensure-generic-function (name &key (lambda-list (si::unbound) l-l-p))
89 (if (and (fboundp name) (si::instancep (fdefinition name)))
90 (fdefinition name)
91 ;; create a fake standard-generic-function object:
92 (with-early-make-instance +standard-generic-function-slots+
93 (gfun (find-class 'standard-generic-function)
94 :name name
95 :spec-list nil
96 :method-combination (find-method-combination nil 'standard nil)
97 :lambda-list lambda-list
98 :argument-precedence-order
99 (and l-l-p (rest (si::process-lambda-list lambda-list t)))
100 :method-class (find-class 'standard-method)
101 :docstring nil
102 :methods nil
103 :a-p-o-function nil
104 :declarations nil
105 :dependents nil)
106 ;; create a new gfun
107 (set-funcallable-instance-function gfun 'standard-generic-function)
108 (setf (fdefinition name) gfun)
109 gfun)))
110
111 (defun (setf generic-function-name) (new-name gf)
112 (if *clos-booted*
113 (reinitialize-instance gf :name new-name)
114 (setf (slot-value gf 'name) new-name)))
115
116 (defun std-compute-discriminating-function (generic-function)
117 (values (unoptimized-discriminator generic-function) t))
118
119 (defun compute-discriminating-function (gf)
120 (declare (notinline std-compute-discriminating-function))
121 (std-compute-discriminating-function gf))
122
123 (defun set-generic-function-dispatch (gf)
124 ;; We have to decide which discriminating function to install:
125 ;; 1. One supplied by the user (or not possible to optimize in C)
126 ;; 2. One coded in C that follows the MOP
127 ;; 3. One in C specialized for slot accessors
128 ;; 4. One in C specialized for standard-generic-function
129 ;; Respectively:
130 ;; 1. The user supplies the discriminating function[1]
131 ;; 2. The function is not STANDARD-GENERIC-FUNCTION
132 ;; 3. The class of all function methods is STANDARD-OPTIMIZED-*-METHOD
133 ;; 4. The function is a STANDARD-GENERIC-FUNCTION
134 ;;
135 ;; This chain of reasoning uses the fact that the user is not allowed to
136 ;; override standard methods specialized on standard classes.
137 ;;
138 ;; [1] We recognize that the user didn't overwrite our function by examining
139 ;; the second value returned from C-D-F; It is also possible that C-D-F
140 ;; disables the C optimization because it has more efficient strategy.
141 ;;
142 (declare (notinline compute-discriminating-function))
143 (multiple-value-bind (computed-discriminator optimizable)
144 (compute-discriminating-function gf)
145 (unless (not (> (length (slot-value gf 'spec-list)) 63))
146 ;; Code in C caches effective methods; the cache key length is 64 and
147 ;; can't handle functions with more specializable arguments. The first
148 ;; argument is the generic function itself.
149 (setf optimizable nil))
150 (let ((methods (slot-value gf 'methods))
151 (standard-generic-function-p
152 (eq (slot-value (class-of gf) 'name) 'standard-generic-function)))
153 (flet ((only-slot-accessors-p (class-name)
154 (and methods
155 (loop with class = (find-class class-name nil)
156 for m in methods
157 always (eq class (class-of m)))
158 class-name)))
159 (set-funcallable-instance-function
160 gf
161 (cond
162 ((not optimizable) computed-discriminator)
163 ((not standard-generic-function-p) 't)
164 ((only-slot-accessors-p 'standard-optimized-reader-method))
165 ((only-slot-accessors-p 'standard-optimized-writer-method))
166 (t 'standard-generic-function)))))))
167
168 ;;; ----------------------------------------------------------------------
169 ;;; COMPUTE-APPLICABLE-METHODS
170 ;;;
171 ;;; This part is a source of problems because we have to access slots of
172 ;;; various objects, which could potentially lead to infinite recursion as
173 ;;; those accessors require also some dispatch. The solution is to avoid
174 ;;; calling then generic function that implement the accessors.
175 ;;; This is possible because:
176 ;;; 1. The user can only extend compute-applicable-methods if it
177 ;;; defines a method with a subclass of standard-generic-function
178 ;;; 2. The user cannot extend slot-value and friends on standard-classes
179 ;;; due to the restriction "Any method defined by a portable program
180 ;;; on a specified generic function must have at least one specializer
181 ;;; that is neither a specified class nor an eql specializer whose
182 ;;; associated value is an instance of a specified class."
183 ;;; 3. Subclasses of specified classes preserve the slot order in ECL.
184 ;;;
185 (defun std-compute-applicable-methods (gf args)
186 (declare (optimize (speed 3)))
187 (with-early-accessors (+standard-method-slots+
188 +standard-generic-function-slots+
189 +eql-specializer-slots+
190 +standard-class-slots+)
191 (flet ((applicable-method-p (method args)
192 (loop for spec in (method-specializers method)
193 for arg in args
194 always (if (eql-specializer-flag spec)
195 (eql arg (eql-specializer-object spec))
196 (si::of-class-p arg spec)))))
197 (let ((methods (loop for method in (generic-function-methods gf)
198 when (applicable-method-p method args)
199 collect method)))
200 (sort-applicable-methods gf methods (mapcar #'class-of args))))))
201
202 (defun std-compute-applicable-methods-using-classes (gf classes)
203 (declare (optimize (speed 3)))
204 (with-early-accessors (+standard-method-slots+
205 +eql-specializer-slots+
206 +standard-generic-function-slots+)
207 (flet ((applicable-method-p (method classes)
208 ;; EQL specializer invalidates the computation. We still check
209 ;; other classes because maybe later specializer will make this
210 ;; method not applicable. -- jd 2022-01-05
211 (loop with eql-spec-p = nil
212 for spec in (method-specializers method)
213 for class in classes
214 do (if (eql-specializer-flag spec)
215 (let ((object (eql-specializer-object spec)))
216 (if (si::of-class-p object class)
217 (setf eql-spec-p t)
218 (return-from applicable-method-p nil)))
219 (unless (si::subclassp class spec)
220 (return-from applicable-method-p nil)))
221 finally
222 (if eql-spec-p
223 (return-from std-compute-applicable-methods-using-classes
224 (values nil nil))
225 (return t)))))
226 (let* ((methods (loop for method in (generic-function-methods gf)
227 when (applicable-method-p method classes)
228 collect method))
229 (sorted-methods (sort-applicable-methods gf methods classes)))
230 (values sorted-methods t)))))
231
232 (setf (fdefinition 'compute-applicable-methods)
233 #'std-compute-applicable-methods)
234
235 (setf (fdefinition 'compute-applicable-methods-using-classes)
236 #'std-compute-applicable-methods-using-classes)
237
238 (defun sort-applicable-methods (gf applicable-list args-specializers)
239 (declare (optimize (safety 0) (speed 3)))
240 (with-early-accessors (+standard-method-slots+
241 +standard-generic-function-slots+
242 +eql-specializer-slots+
243 +standard-class-slots+)
244 (let ((f (generic-function-a-p-o-function gf))
245 (args-classes
246 (loop for arg-spec in args-specializers
247 for req-args in (generic-function-argument-precedence-order gf)
248 collect (if (eql-specializer-flag arg-spec)
249 (class-of (eql-specializer-object arg-spec))
250 arg-spec))))
251 ;; reorder args to match the precedence order
252 (when f
253 (setf args-classes (funcall f args-classes)))
254 ;; then order the list
255 (do* ((scan applicable-list)
256 (most-specific (first scan) (first scan))
257 (ordered-list '()))
258 ((null (cdr scan))
259 (when most-specific
260 ;; at least one method
261 (nreverse
262 (push most-specific ordered-list))))
263 (dolist (method (cdr scan))
264 (when (eq (compare-methods most-specific method args-classes f)
265 2)
266 (setq most-specific method)))
267 (setq scan (delete most-specific scan))
268 (push most-specific ordered-list)))))
269
270 (defun compare-methods (method-1 method-2 args-classes f)
271 (declare (si::c-local))
272 (with-early-accessors (+standard-method-slots+)
273 (let ((specializers-list-1 (method-specializers method-1))
274 (specializers-list-2 (method-specializers method-2)))
275 (when f
276 (setf specializers-list-1 (funcall f specializers-list-1))
277 (setf specializers-list-2 (funcall f specializers-list-2)))
278 (compare-specializers-lists specializers-list-1
279 specializers-list-2
280 args-classes))))
281
282 (defun compare-specializers-lists (spec-list-1 spec-list-2 args-classes)
283 (declare (si::c-local))
284 (when (or spec-list-1 spec-list-2)
285 (ecase (compare-specializers (first spec-list-1)
286 (first spec-list-2)
287 (first args-classes))
288 (1 '1)
289 (2 '2)
290 (=
291 (compare-specializers-lists (cdr spec-list-1)
292 (cdr spec-list-2)
293 (cdr args-classes)))
294 ((nil)
295 (error "The type specifiers ~S and ~S can not be disambiguated~
296 with respect to the argument specializer: ~S"
297 (or (car spec-list-1) t)
298 (or (car spec-list-2) t)
299 (car args-classes))))))
300
301 (defun fast-subtypep (spec1 spec2)
302 (declare (si::c-local))
303 ;; Specialized version of subtypep which uses the fact that spec1
304 ;; and spec2 are either classes or of the form (EQL x)
305 (with-early-accessors (+eql-specializer-slots+ +standard-class-slots+)
306 (if (eql-specializer-flag spec1)
307 (if (eql-specializer-flag spec2)
308 (eql (eql-specializer-object spec1)
309 (eql-specializer-object spec2))
310 (si::of-class-p (eql-specializer-object spec1) spec2))
311 (if (eql-specializer-flag spec2)
312 ;; There is only one class with a single element, which
313 ;; is NULL = (MEMBER NIL).
314 (and (null (eql-specializer-object spec2))
315 (eq (class-name spec1) 'null))
316 (si::subclassp spec1 spec2)))))
317
318 (defun compare-specializers (spec-1 spec-2 arg-class)
319 (declare (si::c-local))
320 (with-early-accessors (+standard-class-slots+ +standard-class-slots+)
321 (let ((cpl (class-precedence-list arg-class)))
322 (cond ((eq spec-1 spec-2) '=)
323 ((fast-subtypep spec-1 spec-2) '1)
324 ((fast-subtypep spec-2 spec-1) '2)
325 ((eql-specializer-flag spec-1) '1) ; is this enough?
326 ((eql-specializer-flag spec-2) '2) ; Beppe
327 ((member spec-1 (member spec-2 cpl)) '2)
328 ((member spec-2 (member spec-1 cpl)) '1)
329 ;; This will force an error in the caller
330 (t nil)))))
331
332 ;;; The specialization profile is a sequence with as many elements as the
333 ;;; generic function has required arguments. Each element is a cons:
334 ;;;
335 ;;; (CLASS-SPECIALIZED-P . EQL-SPECIALIZERS)
336 ;;;
337 ;;; CLASS-SPECIALIZED-P is used to determine whether the argument has a
338 ;;; specializer for any class other than the builtin-class T.
339 ;;;
340 ;;; EQL-SPECIALIZERS is a list of all EQL-SPECIALIZER-OBJECTs
341 (defun compute-g-f-spec-list (gf)
342 (with-early-accessors (+standard-generic-function-slots+
343 +eql-specializer-slots+
344 +standard-method-slots+)
345 (flet ((nupdate-profile (spec-how-list specializers)
346 (if (null spec-how-list)
347 (loop for spec in specializers
348 collect (if (eql-specializer-flag spec)
349 (list nil (eql-specializer-object spec))
350 (if (eq spec (find-class t))
351 (cons nil nil)
352 (cons t nil))))
353 (loop for spec in specializers
354 for (arg-profile) on spec-how-list
355 do (if (eql-specializer-flag spec)
356 (push (eql-specializer-object spec)
357 (cdr arg-profile))
358 (when (and (not (car arg-profile))
359 (not (eq spec (find-class t))))
360 (setf (car arg-profile) t)))
361 finally (return spec-how-list)))))
362 (setf (generic-function-spec-list gf)
363 (reduce #'nupdate-profile (generic-function-methods gf)
364 :key #'method-specializers :initial-value nil))
365 (let ((function nil)
366 (a-p-o (generic-function-argument-precedence-order gf))
367 (gf-ll (generic-function-lambda-list gf)))
368 (when (consp gf-ll)
369 (let ((required-arguments (rest (si::process-lambda-list gf-ll t))))
370 (unless (equal a-p-o required-arguments)
371 (setf function
372 (coerce `(lambda (%list)
373 (destructuring-bind ,required-arguments %list
374 (list ,@a-p-o)))
375 'function)))))
376 (setf (generic-function-a-p-o-function gf) function))
377 (si:clear-gfun-hash gf))))
378
379 (defun print-object (object stream)
380 (print-unreadable-object (object stream)))