Declare COERCE and two helpers as EXPLICIT-CHECK.
[sbcl.git] / src / pcl / vector.lisp
blobbb6a857273dffe4047ecf6c751032a3de4f5f52e
1 ;;;; permutation vectors
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
6 ;;;; This software is derived from software originally released by Xerox
7 ;;;; Corporation. Copyright and release statements follow. Later modifications
8 ;;;; to the software are in the public domain and are provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; information.
12 ;;;; copyright information from original PCL sources:
13 ;;;;
14 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
15 ;;;; All rights reserved.
16 ;;;;
17 ;;;; Use and copying of this software and preparation of derivative works based
18 ;;;; upon this software are permitted. Any distribution of this software or
19 ;;;; derivative works must comply with all applicable United States export
20 ;;;; control laws.
21 ;;;;
22 ;;;; This software is made available AS IS, and Xerox Corporation makes no
23 ;;;; warranty about the software, its performance or its conformity to any
24 ;;;; specification.
26 (in-package "SB-PCL")
28 ;;;; Up to 1.0.9.24 SBCL used to have a sketched out implementation
29 ;;;; for optimizing GF calls inside method bodies using a PV approach,
30 ;;;; inherited from the original PCL. This was never completed, and
31 ;;;; was removed at that point to make the code easier to understand
32 ;;;; -- but:
33 ;;;;
34 ;;;; FIXME: It would be possible to optimize GF calls inside method
35 ;;;; bodies using permutation vectors: if all the arguments to the
36 ;;;; GF are specializers parameters, we can assign a permutation index
37 ;;;; to each such (GF . ARGS) tuple inside a method body, and use this
38 ;;;; to cache effective method functions.
40 (declaim (inline make-pv-table))
41 (defstruct (pv-table (:predicate pv-tablep)
42 (:copier nil))
43 (cache nil :type (or cache null))
44 (pv-size 0 :type fixnum)
45 (slot-name-lists nil :type list))
47 (defun make-pv-table-type-declaration (var)
48 `(type pv-table ,var))
50 ;;; Used for interning parts of SLOT-NAME-LISTS, as part of
51 ;;; PV-TABLE interning -- just to save space.
52 (defvar *slot-name-lists* (make-hash-table :test 'equal))
54 ;;; Used for interning PV-TABLES, keyed by the SLOT-NAME-LISTS
55 ;;; used.
56 (defvar *pv-tables* (make-hash-table :test 'equal))
58 ;;; ...and one lock to rule them. Lock because for certain (rare)
59 ;;; cases this lock might be grabbed in the course of method dispatch
60 ;;; -- and mostly this is already under the *world-lock*
61 (defvar *pv-lock*
62 (sb-thread:make-mutex :name "pv table index lock"))
64 (defun intern-pv-table (&key slot-name-lists)
65 (flet ((intern-slot-names (slot-names)
66 ;; FIXME: NIL at the head of the list is a remnant from
67 ;; old purged code, that hasn't been quite cleaned up yet.
68 ;; ...but as long as we assume it is there, we may as well
69 ;; assert it.
70 (aver (not (car slot-names)))
71 (or (gethash slot-names *slot-name-lists*)
72 (setf (gethash slot-names *slot-name-lists*) slot-names)))
73 (%intern-pv-table (snl)
74 (or (gethash snl *pv-tables*)
75 (setf (gethash snl *pv-tables*)
76 (make-pv-table :slot-name-lists snl
77 :pv-size (* 2 (reduce #'+ snl
78 :key (lambda (slots)
79 (length (cdr slots))))))))))
80 (sb-thread:with-mutex (*pv-lock*)
81 (%intern-pv-table (mapcar #'intern-slot-names slot-name-lists)))))
83 (defun use-standard-slot-access-p (class slot-name type)
84 (or (not (eq **boot-state** 'complete))
85 (and (standard-class-p class)
86 (let ((slotd (find-slot-definition class slot-name)))
87 (and slotd
88 (slot-accessor-std-p slotd type))))))
90 (defun slot-missing-info (class slot-name)
91 (make-slot-info
92 :reader (lambda (object)
93 (values (slot-missing class object slot-name 'slot-value)))
94 :boundp (lambda (object)
95 (and (slot-missing class object slot-name 'slot-boundp) t))
96 :writer (lambda (new-value object)
97 (slot-missing class object slot-name 'setf new-value)
98 new-value)))
100 (defun compute-pv (slot-name-lists wrappers)
101 (let ((wrappers (ensure-list wrappers))
102 elements)
103 (dolist (slot-names slot-name-lists)
104 (when slot-names
105 (let* ((wrapper (pop wrappers))
106 (std-p (layout-for-std-class-p wrapper))
107 (class (wrapper-class* wrapper)))
108 (dolist (slot-name (cdr slot-names))
109 (let ((cell
110 (or (find-slot-cell wrapper slot-name)
111 (cons nil (slot-missing-info class slot-name)))))
112 (push (when (and std-p (use-standard-slot-access-p class slot-name 'all))
113 (car cell))
114 elements)
115 (push (or (cdr cell)
116 (bug "No SLOT-INFO for ~S in ~S" slot-name class))
117 elements))))))
118 (let* ((n (length elements))
119 (pv (make-array n)))
120 (loop for i from (1- n) downto 0
121 do (setf (svref pv i) (pop elements)))
122 pv)))
124 (defun pv-table-lookup (pv-table pv-wrappers)
125 (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
126 (cache (or (pv-table-cache pv-table)
127 (setf (pv-table-cache pv-table)
128 (make-cache :key-count (- (length slot-name-lists)
129 (count nil slot-name-lists))
130 :value t
131 :size 2)))))
132 (multiple-value-bind (hitp value) (probe-cache cache pv-wrappers)
133 (if hitp
134 value
135 (let* ((pv (compute-pv slot-name-lists pv-wrappers))
136 (new-cache (fill-cache cache pv-wrappers pv)))
137 ;; This is safe: if another thread races us here the loser just
138 ;; misses the next time as well.
139 (unless (eq new-cache cache)
140 (setf (pv-table-cache pv-table) new-cache))
141 pv)))))
143 (defun make-pv-type-declaration (var)
144 `(type simple-vector ,var))
146 ;;; Sometimes we want to finalize if we can, but it's OK if
147 ;;; we can't.
148 (defun try-finalize-inheritance (class)
149 (unless (typep class 'forward-referenced-class)
150 (when (every (lambda (super)
151 (or (eq super class)
152 (class-finalized-p super)
153 (try-finalize-inheritance super)))
154 (class-direct-superclasses class))
155 (finalize-inheritance class)
156 t)))
158 (declaim (ftype (sfunction (class) class) ensure-class-finalized))
159 (defun ensure-class-finalized (class)
160 (unless (class-finalized-p class)
161 (finalize-inheritance class))
162 class)
164 (defun can-optimize-access (form required-parameters env)
165 (destructuring-bind (op var-form slot-name-form &optional new-value) form
166 (let ((type (ecase op
167 (slot-value 'reader)
168 (set-slot-value 'writer)
169 (slot-boundp 'boundp)))
170 (var (extract-the var-form))
171 (slot-name (constant-form-value slot-name-form env)))
172 (when (and (symbolp var) (not (var-special-p var env)))
173 (let* ((rebound? (caddr (var-declaration '%variable-rebinding var env)))
174 (parameter-or-nil (car (memq (or rebound? var)
175 required-parameters))))
176 (when parameter-or-nil
177 (let* ((class-name (caddr (var-declaration '%class
178 parameter-or-nil
179 env)))
180 (class (find-class class-name nil)))
181 (cond ((not (eq **boot-state** 'complete))
182 (setq class nil))
183 ((and class (not (class-finalized-p class)))
184 ;; The class itself is never forward-referenced
185 ;; here, but its superclasses may be.
186 (unless (try-finalize-inheritance class)
187 (when (boundp 'sb-c:*lexenv*)
188 (sb-c:compiler-notify
189 "~@<Cannot optimize slot access, inheritance of ~S is not ~
190 yet finalizable due to forward-referenced superclasses:~
191 ~% ~S~:@>"
192 class form))
193 (setf class nil))))
194 (when (and class-name (not (eq class-name t)))
195 (when (not (and class
196 (memq *the-class-structure-object*
197 (class-precedence-list class))))
198 (aver type)
199 (values (cons parameter-or-nil (or class class-name))
200 slot-name
201 new-value))))))))))
203 ;;; Check whether the binding of the named variable is modified in the
204 ;;; method body.
205 (defun parameter-modified-p (parameter-name env)
206 (let ((modified-variables (%macroexpand '%parameter-binding-modified env)))
207 (memq parameter-name modified-variables)))
209 (defun optimize-slot-value (form slots required-parameters env)
210 (multiple-value-bind (sparameter slot-name)
211 (can-optimize-access form required-parameters env)
212 (if sparameter
213 (let ((optimized-form
214 (optimize-instance-access slots :read sparameter
215 slot-name nil)))
216 ;; We don't return the optimized form directly, since there's
217 ;; still a chance that we'll find out later on that the
218 ;; optimization should not have been done, for example due to
219 ;; the walker encountering a SETQ on SPARAMETER later on in
220 ;; the body [ see for example clos.impure.lisp test with :name
221 ;; ((:setq :method-parameter) slot-value)) ]. Instead we defer
222 ;; the decision until the compiler macroexpands
223 ;; OPTIMIZED-SLOT-VALUE.
225 ;; Note that we must still call OPTIMIZE-INSTANCE-ACCESS at
226 ;; this point (instead of when expanding
227 ;; OPTIMIZED-SLOT-VALUE), since it mutates the structure of
228 ;; SLOTS. If that mutation isn't done during the walking,
229 ;; MAKE-METHOD-LAMBDA-INTERNAL won't wrap a correct PV-BINDING
230 ;; form around the body, and compilation will fail. -- JES,
231 ;; 2006-09-18
232 `(optimized-slot-value ,form ,(car sparameter) ,optimized-form))
233 `(accessor-slot-value ,@(cdr form)))))
235 (defmacro optimized-slot-value (form parameter-name optimized-form
236 &environment env)
237 ;; Either use OPTIMIZED-FORM or fall back to the safe
238 ;; ACCESSOR-SLOT-VALUE.
239 (if (parameter-modified-p parameter-name env)
240 `(accessor-slot-value ,@(cdr form))
241 optimized-form))
243 (defun optimize-set-slot-value (form slots required-parameters env)
244 (multiple-value-bind (sparameter slot-name new-value)
245 (can-optimize-access form required-parameters env)
246 (if sparameter
247 (let ((optimized-form
248 (optimize-instance-access slots :write sparameter
249 slot-name new-value (safe-code-p env))))
250 ;; See OPTIMIZE-SLOT-VALUE
251 `(optimized-set-slot-value ,form ,(car sparameter) ,optimized-form))
252 `(accessor-set-slot-value ,@(cdr form)))))
254 (defmacro optimized-set-slot-value (form parameter-name optimized-form
255 &environment env)
256 (cond ((parameter-modified-p parameter-name env)
257 ;; ACCESSOR-SET-SLOT-VALUE doesn't do type-checking,
258 ;; so we need to use SAFE-SET-SLOT-VALUE.
259 (if (safe-code-p env)
260 `(safe-set-slot-value ,@(cdr form)))
261 `(accessor-set-slot-value ,@(cdr form)))
263 optimized-form)))
265 (defun optimize-slot-boundp (form slots required-parameters env)
266 (multiple-value-bind (sparameter slot-name)
267 (can-optimize-access form required-parameters env)
268 (if sparameter
269 (let ((optimized-form
270 (optimize-instance-access slots :boundp sparameter
271 slot-name nil)))
272 ;; See OPTIMIZE-SLOT-VALUE
273 `(optimized-slot-boundp ,form ,(car sparameter) ,optimized-form))
274 `(accessor-slot-boundp ,@(cdr form)))))
276 (defmacro optimized-slot-boundp (form parameter-name optimized-form
277 &environment env)
278 (if (parameter-modified-p parameter-name env)
279 `(accessor-slot-boundp ,@(cdr form))
280 optimized-form))
282 ;;; The SLOTS argument is an alist, the CAR of each entry is the name
283 ;;; of a required parameter to the function. The alist is in order, so
284 ;;; the position of an entry in the alist corresponds to the
285 ;;; argument's position in the lambda list.
286 (defun optimize-instance-access (slots read/write sparameter slot-name
287 new-value &optional safep)
288 (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
289 (parameter (if (consp sparameter) (car sparameter) sparameter)))
290 (if (and (eq **boot-state** 'complete)
291 (classp class)
292 (memq *the-class-structure-object* (class-precedence-list class)))
293 (let ((slotd (find-slot-definition class slot-name)))
294 (ecase read/write
295 (:read
296 `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
297 (:write
298 `(setf (,(slot-definition-defstruct-accessor-symbol slotd)
299 ,parameter)
300 ,new-value))
301 (:boundp
302 t)))
303 (let* ((parameter-entry (assq parameter slots))
304 (slot-entry (assq slot-name (cdr parameter-entry)))
305 (position (posq parameter-entry slots))
306 (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
307 (unless parameter-entry
308 (bug "slot optimization bewilderment: O-I-A"))
309 (unless slot-entry
310 (setq slot-entry (list slot-name))
311 (push slot-entry (cdr parameter-entry)))
312 (push pv-offset-form (cdr slot-entry))
313 (ecase read/write
314 (:read
315 `(instance-read ,pv-offset-form ,parameter ,position
316 ',slot-name ',class))
317 (:write
318 `(let ((.new-value. ,new-value))
319 (instance-write ,pv-offset-form ,parameter ,position
320 ',slot-name ',class .new-value. ,safep)))
321 (:boundp
322 `(instance-boundp ,pv-offset-form ,parameter ,position
323 ',slot-name ',class)))))))
325 (define-walker-template pv-offset) ; These forms get munged by mutate slots.
326 (defmacro pv-offset (arg) arg)
327 (define-walker-template instance-accessor-parameter)
328 (defmacro instance-accessor-parameter (x) x)
330 ;;; It is safe for these two functions to be wrong. They just try to
331 ;;; guess what the most likely case will be.
332 (defun generate-fast-class-slot-access-p (class-form slot-name-form)
333 (let ((class (and (constantp class-form) (constant-form-value class-form)))
334 (slot-name (and (constantp slot-name-form)
335 (constant-form-value slot-name-form))))
336 (and (eq **boot-state** 'complete)
337 (standard-class-p class)
338 (not (eq class *the-class-t*)) ; shouldn't happen, though.
339 (let ((slotd (find-slot-definition class slot-name)))
340 (and slotd (eq :class (slot-definition-allocation slotd)))))))
342 (defun constant-value-or-nil (form)
343 (and (constantp form) (constant-form-value form)))
345 (defun slot-access-strategy (class slot-name type &optional conservative)
346 ;; CONSERVATIVE means we should assume custom access pattern even if
347 ;; there are no custom accessors defined if the metaclass is non-standard.
349 ;; This is needed because DEFCLASS generates accessor methods before possible
350 ;; SLOT-VALUE-USING-CLASS methods are defined, which causes them to take
351 ;; the slow path unless we make the conservative assumption here.
352 (if (eq **boot-state** 'complete)
353 (let (slotd)
354 (cond ((or
355 ;; Conditions, structures, and classes for which FIND-CLASS
356 ;; doesn't return them yet.
357 ;; FIXME: surely we can get faster accesses for structures?
358 (not (standard-class-p class))
359 ;; Should not happen... (FIXME: assert instead?)
360 (eq class *the-class-t*)
361 (not (class-finalized-p class))
362 ;; Strangeness...
363 (not (setf slotd (find-slot-definition class slot-name))))
364 :accessor)
365 ((and (slot-accessor-std-p slotd type)
366 (or (not conservative) (eq *the-class-standard-class* (class-of class))))
367 ;; The best case.
368 :standard)
370 :custom)))
371 :standard))
373 ;;;; SLOT-VALUE
375 (defmacro instance-read (pv-offset parameter position slot-name class)
376 (ecase (slot-access-strategy (constant-value-or-nil class)
377 (constant-value-or-nil slot-name)
378 'reader)
379 (:standard
380 `(instance-read-standard
381 .pv. ,(slot-vector-symbol position)
382 ,pv-offset (accessor-slot-value ,parameter ,slot-name)
383 ,(if (generate-fast-class-slot-access-p class slot-name)
384 :class :instance)))
385 (:custom
386 `(instance-read-custom .pv. ,pv-offset ,parameter))
387 (:accessor
388 `(accessor-slot-value ,parameter ,slot-name))))
390 (defmacro instance-read-standard (pv slots pv-offset default &optional kind)
391 (unless (member kind '(nil :instance :class))
392 (error "illegal kind argument to ~S: ~S" 'instance-read-standard kind))
393 (let* ((index (gensym))
394 (value index))
395 `(locally (declare #.*optimize-speed*)
396 (let ((,index (svref ,pv ,pv-offset))
397 (,slots (truly-the simple-vector ,slots)))
398 (setq ,value (typecase ,index
399 ;; FIXME: the line marked by KLUDGE below (and
400 ;; the analogous spot in
401 ;; INSTANCE-WRITE-STANDARD) is there purely to
402 ;; suppress a type mismatch warning that
403 ;; propagates through to user code.
404 ;; Presumably SLOTS at this point can never
405 ;; actually be NIL, but the compiler seems to
406 ;; think it could, so we put this here to shut
407 ;; it up. (see also mail Rudi Schlatte
408 ;; sbcl-devel 2003-09-21) -- CSR, 2003-11-30
409 ,@(when (or (null kind) (eq kind :instance))
410 `((fixnum
411 (clos-slots-ref ,slots ,index))))
412 ,@(when (or (null kind) (eq kind :class))
413 `((cons (cdr ,index))))
415 +slot-unbound+)))
416 (if (eq ,value +slot-unbound+)
417 ,default
418 ,value)))))
420 (defmacro instance-read-custom (pv pv-offset parameter)
421 `(locally (declare #.*optimize-speed*)
422 (funcall (slot-info-reader (svref ,pv (1+ ,pv-offset))) ,parameter)))
424 ;;;; (SETF SLOT-VALUE)
426 (defmacro instance-write (pv-offset parameter position slot-name class new-value
427 &optional check-type-p)
428 (ecase (slot-access-strategy (constant-value-or-nil class)
429 (constant-value-or-nil slot-name)
430 'writer)
431 (:standard
432 `(instance-write-standard
433 .pv. ,(slot-vector-symbol position)
434 ,pv-offset ,new-value
435 ;; KLUDGE: .GOOD-NEW-VALUE. is type-checked by the time this form
436 ;; is executed (if it is executed).
437 (accessor-set-slot-value ,parameter ,slot-name .good-new-value.)
438 ,(if (generate-fast-class-slot-access-p class slot-name)
439 :class :instance)
440 ,check-type-p))
441 (:custom
442 `(instance-write-custom .pv. ,pv-offset ,parameter ,new-value))
443 (:accessor
444 (if check-type-p
445 ;; FIXME: We don't want this here. If it's _possible_ the fast path
446 ;; is applicable, we want to use it as well.
447 `(safe-set-slot-value ,parameter ,slot-name ,new-value)
448 `(accessor-set-slot-value ,parameter ,slot-name ,new-value)))))
450 (defmacro instance-write-standard (pv slots pv-offset new-value default
451 &optional kind safep)
452 (unless (member kind '(nil :instance :class))
453 (error "illegal kind argument to ~S: ~S" 'instance-write-standard kind))
454 (let* ((index (gensym))
455 (new-value-form
456 (if safep
457 `(let ((.typecheckfun. (slot-info-typecheck (svref ,pv (1+ ,pv-offset)))))
458 (declare (type (or function null) .typecheckfun.))
459 (if .typecheckfun.
460 (funcall .typecheckfun. ,new-value)
461 ,new-value))
462 new-value)))
463 `(locally (declare #.*optimize-speed*)
464 (let ((.good-new-value. ,new-value-form)
465 (,index (svref ,pv ,pv-offset)))
466 (typecase ,index
467 ,@(when (or (null kind) (eq kind :instance))
468 `((fixnum (and ,slots
469 (setf (clos-slots-ref ,slots ,index)
470 .good-new-value.)))))
471 ,@(when (or (null kind) (eq kind :class))
472 `((cons (setf (cdr ,index) .good-new-value.))))
473 (t ,default))))))
475 (defmacro instance-write-custom (pv pv-offset parameter new-value)
476 `(locally (declare #.*optimize-speed*)
477 (funcall (slot-info-writer (svref ,pv (1+ ,pv-offset))) ,new-value ,parameter)))
479 ;;;; SLOT-BOUNDP
481 (defmacro instance-boundp (pv-offset parameter position slot-name class)
482 (ecase (slot-access-strategy (constant-value-or-nil class)
483 (constant-value-or-nil slot-name)
484 'boundp)
485 (:standard
486 `(instance-boundp-standard
487 .pv. ,(slot-vector-symbol position)
488 ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
489 ,(if (generate-fast-class-slot-access-p class slot-name)
490 :class :instance)))
491 (:custom
492 `(instance-boundp-custom .pv. ,pv-offset ,parameter))
493 (:accessor
494 `(accessor-slot-boundp ,parameter ,slot-name))))
496 (defmacro instance-boundp-standard (pv slots pv-offset default
497 &optional kind)
498 (unless (member kind '(nil :instance :class))
499 (error "illegal kind argument to ~S: ~S" 'instance-boundp-standard kind))
500 (let* ((index (gensym)))
501 `(locally (declare #.*optimize-speed*)
502 (let ((,index (svref ,pv ,pv-offset)))
503 (typecase ,index
504 ,@(when (or (null kind) (eq kind :instance))
505 `((fixnum (not (and ,slots
506 (eq (clos-slots-ref ,slots ,index)
507 +slot-unbound+))))))
508 ,@(when (or (null kind) (eq kind :class))
509 `((cons (not (eq (cdr ,index) +slot-unbound+)))))
510 (t ,default))))))
512 (defmacro instance-boundp-custom (pv pv-offset parameter)
513 `(locally (declare #.*optimize-speed*)
514 (funcall (slot-info-boundp (svref ,pv (1+ ,pv-offset))) ,parameter)))
516 ;;; This magic function has quite a job to do indeed.
518 ;;; The careful reader will recall that <slots> contains all of the
519 ;;; optimized slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.
520 ;;; Each of these is a call to either INSTANCE-READ or INSTANCE-WRITE.
522 ;;; At the time these calls were produced, the first argument was
523 ;;; specified as the symbol .PV-OFFSET.; what we have to do now is
524 ;;; convert those pv-offset arguments into the actual number that is
525 ;;; the correct offset into the pv.
527 ;;; But first, oh but first, we sort <slots> a bit so that for each
528 ;;; argument we have the slots in alphabetical order. This
529 ;;; canonicalizes the PV-TABLE's a bit and will hopefully lead to
530 ;;; having fewer PV's floating around. Even if the gain is only
531 ;;; modest, it costs nothing.
532 (defun slot-name-lists-from-slots (slots)
533 (let ((slots (mutate-slots slots)))
534 (let* ((slot-name-lists
535 (mapcar (lambda (parameter-entry)
536 (cons nil (mapcar #'car (cdr parameter-entry))))
537 slots)))
538 (mapcar (lambda (r+snl)
539 (when (or (car r+snl) (cdr r+snl))
540 r+snl))
541 slot-name-lists))))
543 (defun mutate-slots (slots)
544 (let ((sorted-slots (sort-slots slots))
545 (pv-offset -1))
546 (dolist (parameter-entry sorted-slots)
547 (dolist (slot-entry (cdr parameter-entry))
548 (incf pv-offset)
549 (dolist (form (cdr slot-entry))
550 (setf (cadr form) pv-offset))
551 ;; Count one more for the slot we use for SLOT-INFO.
552 (incf pv-offset)))
553 sorted-slots))
555 (defun symbol-pkg-name (sym)
556 (let ((pkg (symbol-package sym)))
557 (if pkg (package-name pkg) "")))
559 ;;; FIXME: Because of the existence of UNINTERN and RENAME-PACKAGE,
560 ;;; the part of this ordering which is based on SYMBOL-PKG-NAME is not
561 ;;; stable. This ordering is only used in to
562 ;;; SLOT-NAME-LISTS-FROM-SLOTS, where it serves to "canonicalize the
563 ;;; PV-TABLE's a bit and will hopefully lead to having fewer PV's
564 ;;; floating around", so it sounds as though the instability won't
565 ;;; actually lead to bugs, just small inefficiency. But still, it
566 ;;; would be better to reimplement this function as a comparison based
567 ;;; on SYMBOL-HASH:
568 ;;; * stable comparison
569 ;;; * smaller code (here, and in being able to discard SYMBOL-PKG-NAME)
570 ;;; * faster code.
571 (defun symbol-lessp (a b)
572 (if (eq (symbol-package a)
573 (symbol-package b))
574 (string-lessp (symbol-name a)
575 (symbol-name b))
576 (string-lessp (symbol-pkg-name a)
577 (symbol-pkg-name b))))
579 (defun symbol-or-cons-lessp (a b)
580 (etypecase a
581 (symbol (etypecase b
582 (symbol (symbol-lessp a b))
583 (cons t)))
584 (cons (etypecase b
585 (symbol nil)
586 (cons (if (eq (car a) (car b))
587 (symbol-or-cons-lessp (cdr a) (cdr b))
588 (symbol-or-cons-lessp (car a) (car b))))))))
590 (defun sort-slots (slots)
591 (mapcar (lambda (parameter-entry)
592 (cons (car parameter-entry)
593 (sort (cdr parameter-entry) ;slot entries
594 #'symbol-or-cons-lessp
595 :key #'car)))
596 slots))
599 ;;;; This needs to work in terms of metatypes and also needs to work
600 ;;;; for automatically generated reader and writer functions.
601 ;;;; Automatically generated reader and writer functions use this
602 ;;;; stuff too.
604 (defmacro pv-binding ((required-parameters slot-name-lists pv-table-form)
605 &body body)
606 (let (slot-vars pv-parameters)
607 (loop for slots in slot-name-lists
608 for required-parameter in required-parameters
609 for i from 0
610 do (when slots
611 (push required-parameter pv-parameters)
612 (push (slot-vector-symbol i) slot-vars)))
613 `(pv-binding1 (,pv-table-form
614 ,(nreverse pv-parameters) ,(nreverse slot-vars))
615 ,@body)))
617 (defmacro pv-binding1 ((pv-table-form pv-parameters slot-vars)
618 &body body)
619 `(pv-env (,pv-table-form ,pv-parameters)
620 (let (,@(mapcar (lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
621 slot-vars pv-parameters))
622 (declare (ignorable ,@(mapcar #'identity slot-vars)))
623 ,@body)))
625 ;;; This will only be visible in PV-ENV when the default MAKE-METHOD-LAMBDA is
626 ;;; overridden.
627 (define-symbol-macro pv-env-environment overridden)
629 (defmacro pv-env (&environment env
630 (pv-table-form pv-parameters)
631 &rest forms)
632 ;; Decide which expansion to use based on the state of the PV-ENV-ENVIRONMENT
633 ;; symbol-macrolet.
634 (if (eq (macroexpand 'pv-env-environment env) 'default)
635 `(locally (declare (simple-vector .pv.))
636 ,@forms)
637 `(let* ((.pv-table. ,pv-table-form)
638 (.pv. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters)))
639 (declare ,(make-pv-type-declaration '.pv.))
640 ,@forms)))
642 (defun split-declarations (body args req-args cnm-p parameters-setqd)
643 (let ((inner-decls nil)
644 (outer-decls nil)
645 decl)
646 (loop
647 (when (null body)
648 (return nil))
649 (setq decl (car body))
650 (unless (and (consp decl) (eq (car decl) 'declare))
651 (return nil))
652 (dolist (form (cdr decl))
653 (when (consp form)
654 (let* ((name (car form)))
655 (cond ((eq '%class name)
656 (push `(declare ,form) inner-decls))
657 ((or (member name '(ignore ignorable special dynamic-extent type))
658 (info :type :kind name))
659 (let* ((inners nil)
660 (outers nil)
661 (tail (cdr form))
662 (head (if (eq 'type name)
663 (list name (pop tail))
664 (list name))))
665 (dolist (var tail)
666 (if (member var args :test #'eq)
667 ;; Quietly remove IGNORE declarations on
668 ;; args when a next-method is involved, to
669 ;; prevent compiler warnings about ignored
670 ;; args being read.
671 (unless (and (eq 'ignore name)
672 (member var req-args :test #'eq)
673 (or cnm-p (member var parameters-setqd)))
674 (push var outers))
675 (push var inners)))
676 (when outers
677 (push `(declare (,@head ,@outers)) outer-decls))
678 (when inners
679 (push `(declare (,@head ,@inners)) inner-decls))))
681 ;; All other declarations are not variable declarations,
682 ;; so they become outer declarations.
683 (push `(declare ,form) outer-decls))))))
684 (setq body (cdr body)))
685 (values outer-decls inner-decls body)))
687 ;;; Convert a lambda expression containing a SB-PCL::%METHOD-NAME
688 ;;; declaration (which is a naming style internal to PCL) into an
689 ;;; SB-INT:NAMED-LAMBDA expression (which is a naming style used
690 ;;; throughout SBCL, understood by the main compiler); or if there's
691 ;;; no SB-PCL::%METHOD-NAME declaration, then just return the original
692 ;;; lambda expression.
693 (defun name-method-lambda (method-lambda)
694 (let ((method-name *method-name*))
695 (if method-name
696 `(named-lambda (slow-method ,@method-name) ,@(rest method-lambda))
697 method-lambda)))
699 (defun make-method-initargs-form-internal (method-lambda initargs env)
700 (declare (ignore env))
701 (let (method-lambda-args
702 lmf ; becomes body of function
703 lmf-params)
704 (if (not (and (= 3 (length method-lambda))
705 (= 2 (length (setq method-lambda-args (cadr method-lambda))))
706 (consp (setq lmf (third method-lambda)))
707 (eq 'simple-lexical-method-functions (car lmf))
708 (eq (car method-lambda-args)
709 (cadr (setq lmf-params (cadr lmf))))
710 (eq (cadr method-lambda-args)
711 (caddr lmf-params))))
712 `(list* :function ,(name-method-lambda method-lambda)
713 ',initargs)
714 (let* ((lambda-list (car lmf-params))
715 (nreq 0)
716 (restp nil)
717 (args nil))
718 (dolist (arg lambda-list)
719 (when (member arg '(&optional &rest &key))
720 (setq restp t)
721 (return nil))
722 (when (eq arg '&aux)
723 (return nil))
724 (incf nreq)
725 (push arg args))
726 (setq args (nreverse args))
727 (setf (getf (getf initargs 'plist) :arg-info) (cons nreq restp))
728 (make-method-initargs-form-internal1
729 initargs (cddr lmf) args lmf-params restp)))))
731 (defun lambda-list-parameter-names (lambda-list)
732 ;; Given a valid lambda list, extract the parameter names.
733 (loop for x in lambda-list
734 with res = nil
735 do (unless (member x lambda-list-keywords :test #'eq)
736 (if (consp x)
737 (let ((name (car x)))
738 (if (consp name)
739 ;; ... ((:BAR FOO) 1)
740 (push (second name) res)
741 ;; ... (FOO 1)
742 (push name res))
743 ;; ... (... 1 FOO-P)
744 (let ((name-p (cddr x)))
745 (when name-p
746 (push (car name-p) res))))
747 ;; ... FOO
748 (push x res)))
749 finally (return res)))
751 (defun make-method-initargs-form-internal1
752 (initargs body req-args lmf-params restp)
753 (let* (;; The lambda-list of the method, minus specifiers
754 (lambda-list (car lmf-params))
755 ;; Names of the parameters that will be in the outermost lambda-list
756 ;; (and whose bound declarations thus need to be in OUTER-DECLS).
757 (outer-parameters req-args)
758 ;; The lambda-list used by BIND-ARGS
759 (bind-list lambda-list)
760 (parameters-setqd (getf (cdr lmf-params) :parameters-setqd))
761 (auxp (member '&aux bind-list))
762 (call-next-method-p (getf (cdr lmf-params) :call-next-method-p)))
763 ;; Try to use the normal function call machinery instead of BIND-ARGS
764 ;; binding the arguments, unless:
765 (unless (or ;; If all arguments are required, BIND-ARGS will be a no-op
766 ;; in any case.
767 (and (not restp) (not auxp))
768 ;; CALL-NEXT-METHOD wants to use BIND-ARGS, and needs a
769 ;; list of all non-required arguments.
770 call-next-method-p)
771 (setf ;; We don't want a binding for .REST-ARG.
772 restp nil
773 ;; Get all the parameters for declaration parsing
774 outer-parameters (lambda-list-parameter-names lambda-list)
775 ;; Ensure that BIND-ARGS won't do anything (since
776 ;; BIND-LIST won't contain any non-required parameters,
777 ;; and REQ-ARGS will be of an equal length). We still want
778 ;; to pass BIND-LIST to FAST-LEXICAL-METHOD-FUNCTIONS so
779 ;; that BIND-FAST-LEXICAL-METHOD-FUNCTIONS can take care
780 ;; of rebinding SETQd required arguments around the method
781 ;; body.
782 bind-list req-args))
783 (multiple-value-bind (outer-decls inner-decls body-sans-decls)
784 (split-declarations
785 body outer-parameters req-args call-next-method-p parameters-setqd)
786 (let* ((rest-arg (when restp
787 '.rest-arg.))
788 (fmf-lambda-list (if rest-arg
789 (append req-args (list '&rest rest-arg))
790 (if call-next-method-p
791 req-args
792 lambda-list))))
793 `(list*
794 :function
795 (let* ((fmf (,(if *method-name* 'named-lambda 'lambda)
796 ,@(when *method-name*
797 ;; function name
798 (list `(fast-method ,@*method-name*)))
799 ;; The lambda-list of the FMF
800 (.pv. .next-method-call. ,@fmf-lambda-list)
801 ;; body of the function
802 (declare (ignorable .pv. .next-method-call.)
803 (disable-package-locks pv-env-environment))
804 ,@outer-decls
805 (symbol-macrolet ((pv-env-environment default))
806 (fast-lexical-method-functions
807 (,bind-list .next-method-call. ,req-args ,rest-arg
808 ,@(cdddr lmf-params))
809 ,@inner-decls
810 ,@body-sans-decls))))
811 (mf (%make-method-function fmf nil)))
812 (set-funcallable-instance-function
813 mf (method-function-from-fast-function fmf ',(getf initargs 'plist)))
815 ',initargs)))))
817 ;;; Use arrays and hash tables and the fngen stuff to make this much
818 ;;; better. It doesn't really matter, though, because a function
819 ;;; returned by this will get called only when the user explicitly
820 ;;; funcalls a result of method-function. BUT, this is needed to make
821 ;;; early methods work.
822 (defun method-function-from-fast-function (fmf plist)
823 (declare (type function fmf))
824 (let* ((method-function nil)
825 (snl (getf plist :slot-name-lists))
826 (pv-table (when snl
827 (intern-pv-table :slot-name-lists snl))))
828 (setq method-function
829 (lambda (method-args next-methods)
830 (let* ((pv (when pv-table
831 (get-pv method-args pv-table)))
832 (nm (car next-methods))
833 (nms (cdr next-methods))
834 (nmc (when nm
835 (make-method-call
836 :function (if (std-instance-p nm)
837 (method-function nm)
839 :call-method-args (list nms)))))
840 (apply fmf pv nmc method-args))))
841 ;; FIXME: this looks dangerous.
842 (let* ((fname (%fun-name fmf)))
843 (when (and fname (eq (car fname) 'fast-method))
844 (set-fun-name method-function (cons 'slow-method (cdr fname)))))
845 method-function))
847 ;;; this is similar to the above, only not quite. Only called when
848 ;;; the MOP is heavily involved. Not quite parallel to
849 ;;; METHOD-FUNCTION-FROM-FAST-METHOD-FUNCTION, because we can close
850 ;;; over the actual PV-CELL in this case.
851 (defun method-function-from-fast-method-call (fmc)
852 (let* ((fmf (fast-method-call-function fmc))
853 (pv (fast-method-call-pv fmc)))
854 (lambda (method-args next-methods)
855 (let* ((nm (car next-methods))
856 (nms (cdr next-methods))
857 (nmc (when nm
858 (make-method-call
859 :function (if (std-instance-p nm)
860 (method-function nm)
862 :call-method-args (list nms)))))
863 (apply fmf pv nmc method-args)))))
865 (defun get-pv (method-args pv-table)
866 (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
867 (when pv-wrappers
868 (pv-table-lookup pv-table pv-wrappers))))
870 (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
871 (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
873 (defun pv-wrappers-from-pv-args (&rest args)
874 (loop for arg in args
875 collect (valid-wrapper-of arg)))
877 (defun pv-wrappers-from-all-args (pv-table args)
878 (loop for snl in (pv-table-slot-name-lists pv-table)
879 and arg in args
880 when snl
881 collect (valid-wrapper-of arg)))
883 ;;; Return the subset of WRAPPERS which is used in the cache
884 ;;; of PV-TABLE.
885 (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
886 (loop for snl in (pv-table-slot-name-lists pv-table) and w in wrappers
887 when snl
888 collect w))