Declaim types of %%data-vector-...%%.
[sbcl.git] / src / code / target-package.lisp
blob86893dfeb4b4d796ea014b984c10c3bd749d7db0
1 ;;;; PACKAGEs and stuff like that
2 ;;;;
3 ;;;; Note: The code in this file signals many correctable errors. This
4 ;;;; is not just an arbitrary aesthetic decision on the part of the
5 ;;;; implementor -- many of these are specified by ANSI 11.1.1.2.5,
6 ;;;; "Prevention of Name Conflicts in Packages":
7 ;;;; Within one package, any particular name can refer to at most one
8 ;;;; symbol. A name conflict is said to occur when there would be more
9 ;;;; than one candidate symbol. Any time a name conflict is about to
10 ;;;; occur, a correctable error is signaled.
11 ;;;;
12 ;;;; FIXME: The code contains a lot of type declarations. Are they
13 ;;;; all really necessary?
15 ;;;; This software is part of the SBCL system. See the README file for
16 ;;;; more information.
17 ;;;;
18 ;;;; This software is derived from the CMU CL system, which was
19 ;;;; written at Carnegie Mellon University and released into the
20 ;;;; public domain. The software is in the public domain and is
21 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
22 ;;;; files for more information.
24 (in-package "SB!IMPL")
26 ;;;; Thread safety
27 ;;;;
28 ;;;; ...this could still use work, but the basic idea is:
29 ;;;;
30 ;;;; *PACKAGE-GRAPH-LOCK* is held via WITH-PACKAGE-GRAPH while working on
31 ;;;; package graph, including package -> package links, and interning and
32 ;;;; uninterning symbols.
33 ;;;;
34 ;;;; Hash-table lock on *PACKAGE-NAMES* is held via WITH-PACKAGE-NAMES while
35 ;;;; frobbing name -> package associations.
36 ;;;;
37 ;;;; There should be no deadlocks due to ordering issues between these two, as
38 ;;;; the latter is only held over operations guaranteed to terminate in finite
39 ;;;; time.
40 ;;;;
41 ;;;; Errors may be signalled while holding on to the *PACKAGE-GRAPH-LOCK*,
42 ;;;; which can still lead to pretty damned inconvenient situations -- but
43 ;;;; since FIND-PACKAGE, FIND-SYMBOL from other threads isn't blocked by this,
44 ;;;; the situation isn't *quite* hopeless.
45 ;;;;
46 ;;;; A better long-term solution seems to be in splitting the granularity of
47 ;;;; the *PACKAGE-GRAPH-LOCK* down: for interning a per-package lock should be
48 ;;;; sufficient, though interaction between parallel intern and use-package
49 ;;;; needs to be considered with some care.
51 (defvar *package-graph-lock*)
53 (defun call-with-package-graph (function)
54 (declare (function function))
55 ;; FIXME: Since name conflicts can be signalled while holding the
56 ;; mutex, user code can be run leading to lock ordering problems.
57 (sb!thread:with-recursive-lock (*package-graph-lock*)
58 (funcall function)))
60 ;;; a map from package names to packages
61 (defvar *package-names*)
62 (declaim (type hash-table *package-names*))
64 (defmacro with-package-names ((names &key) &body body)
65 `(let ((,names *package-names*))
66 (with-locked-system-table (,names)
67 ,@body)))
69 ;;;; PACKAGE-HASHTABLE stuff
71 (def!method print-object ((table package-hashtable) stream)
72 (declare (type stream stream))
73 (print-unreadable-object (table stream :type t :identity t)
74 (let* ((n-live (%package-hashtable-symbol-count table))
75 (n-deleted (package-hashtable-deleted table))
76 (n-filled (+ n-live n-deleted))
77 (n-cells (1- (length (package-hashtable-cells table)))))
78 (format stream
79 "(~D+~D)/~D [~@[~,3f words/sym,~]load=~,1f%]"
80 n-live n-deleted n-cells
81 (unless (zerop n-live)
82 (/ (* (1+ (/ sb!vm:n-word-bytes)) n-cells) n-live))
83 (* 100 (/ n-filled n-cells))))))
85 ;;; the maximum load factor we allow in a package hashtable
86 (!defparameter *package-rehash-threshold* 3/4)
88 ;;; the load factor desired for a package hashtable when writing a
89 ;;; core image
90 (!defparameter *package-hashtable-image-load-factor* 1/2)
92 ;;; Make a package hashtable having a prime number of entries at least
93 ;;; as great as (/ SIZE *PACKAGE-REHASH-THRESHOLD*). If RES is supplied,
94 ;;; then it is destructively modified to produce the result. This is
95 ;;; useful when changing the size, since there are many pointers to
96 ;;; the hashtable.
97 ;;; Actually, the smallest table built here has three entries. This
98 ;;; is necessary because the double hashing step size is calculated
99 ;;; using a division by the table size minus two.
100 (defun make-package-hashtable (size)
101 (flet ((actual-package-hashtable-size (size)
102 (loop for n of-type fixnum
103 from (logior (ceiling size *package-rehash-threshold*) 1)
104 by 2
105 when (positive-primep n) return n)))
106 (let* ((n (actual-package-hashtable-size size))
107 (size (truncate (* n *package-rehash-threshold*)))
108 (table (make-array (1+ n) :initial-element 0)))
109 (setf (aref table n)
110 (make-array n :element-type '(unsigned-byte 8)
111 :initial-element 0))
112 (%make-package-hashtable table size))))
114 (declaim (inline pkg-symbol-valid-p))
115 (defun pkg-symbol-valid-p (x) (not (fixnump x)))
117 ;;; Destructively resize TABLE to have room for at least SIZE entries
118 ;;; and rehash its existing entries.
119 (defun resize-package-hashtable (table size)
120 (let* ((symvec (package-hashtable-cells table))
121 (len (1- (length symvec)))
122 (temp-table (make-package-hashtable size)))
123 (dotimes (i len)
124 (let ((sym (svref symvec i)))
125 (when (pkg-symbol-valid-p sym)
126 (add-symbol temp-table sym))))
127 (setf (package-hashtable-cells table) (package-hashtable-cells temp-table)
128 (package-hashtable-size table) (package-hashtable-size temp-table)
129 (package-hashtable-free table) (package-hashtable-free temp-table)
130 (package-hashtable-deleted table) 0)))
132 ;;;; package locking operations, built conditionally on :sb-package-locks
134 #!+sb-package-locks
135 (progn
136 (defun package-locked-p (package)
137 #!+sb-doc
138 "Returns T when PACKAGE is locked, NIL otherwise. Signals an error
139 if PACKAGE doesn't designate a valid package."
140 (package-lock (find-undeleted-package-or-lose package)))
142 (defun lock-package (package)
143 #!+sb-doc
144 "Locks PACKAGE and returns T. Has no effect if PACKAGE was already
145 locked. Signals an error if PACKAGE is not a valid package designator"
146 (setf (package-lock (find-undeleted-package-or-lose package)) t))
148 (defun unlock-package (package)
149 #!+sb-doc
150 "Unlocks PACKAGE and returns T. Has no effect if PACKAGE was already
151 unlocked. Signals an error if PACKAGE is not a valid package designator."
152 (setf (package-lock (find-undeleted-package-or-lose package)) nil)
155 (defun package-implemented-by-list (package)
156 #!+sb-doc
157 "Returns a list containing the implementation packages of
158 PACKAGE. Signals an error if PACKAGE is not a valid package designator."
159 (package-%implementation-packages (find-undeleted-package-or-lose package)))
161 (defun package-implements-list (package)
162 #!+sb-doc
163 "Returns the packages that PACKAGE is an implementation package
164 of. Signals an error if PACKAGE is not a valid package designator."
165 (let ((package (find-undeleted-package-or-lose package)))
166 (loop for x in (list-all-packages)
167 when (member package (package-%implementation-packages x))
168 collect x)))
170 (defun add-implementation-package (packages-to-add
171 &optional (package *package*))
172 #!+sb-doc
173 "Adds PACKAGES-TO-ADD as implementation packages of PACKAGE. Signals
174 an error if PACKAGE or any of the PACKAGES-TO-ADD is not a valid
175 package designator."
176 (let ((package (find-undeleted-package-or-lose package))
177 (packages-to-add (package-listify packages-to-add)))
178 (setf (package-%implementation-packages package)
179 (union (package-%implementation-packages package)
180 (mapcar #'find-undeleted-package-or-lose packages-to-add)))))
182 (defun remove-implementation-package (packages-to-remove
183 &optional (package *package*))
184 #!+sb-doc
185 "Removes PACKAGES-TO-REMOVE from the implementation packages of
186 PACKAGE. Signals an error if PACKAGE or any of the PACKAGES-TO-REMOVE
187 is not a valid package designator."
188 (let ((package (find-undeleted-package-or-lose package))
189 (packages-to-remove (package-listify packages-to-remove)))
190 (setf (package-%implementation-packages package)
191 (nset-difference
192 (package-%implementation-packages package)
193 (mapcar #'find-undeleted-package-or-lose packages-to-remove)))))
195 (defmacro with-unlocked-packages ((&rest packages) &body forms)
196 #!+sb-doc
197 "Unlocks PACKAGES for the dynamic scope of the body. Signals an
198 error if any of PACKAGES is not a valid package designator."
199 (with-unique-names (unlocked-packages)
200 `(let (,unlocked-packages)
201 (unwind-protect
202 (progn
203 (dolist (p ',packages)
204 (when (package-locked-p p)
205 (push p ,unlocked-packages)
206 (unlock-package p)))
207 ,@forms)
208 (dolist (p ,unlocked-packages)
209 (when (find-package p)
210 (lock-package p)))))))
212 (defun package-lock-violation (package &key (symbol nil symbol-p)
213 format-control format-arguments)
214 (let* ((restart :continue)
215 (cl-violation-p (eq package *cl-package*))
216 (error-arguments
217 (append (list (if symbol-p
218 'symbol-package-locked-error
219 'package-locked-error)
220 :package package
221 :format-control format-control
222 :format-arguments format-arguments)
223 (when symbol-p (list :symbol symbol))
224 (list :references
225 (append '((:sbcl :node "Package Locks"))
226 (when cl-violation-p
227 '((:ansi-cl :section (11 1 2 1 2)))))))))
228 (restart-case
229 (apply #'cerror "Ignore the package lock." error-arguments)
230 (:ignore-all ()
231 :report "Ignore all package locks in the context of this operation."
232 (setf restart :ignore-all))
233 (:unlock-package ()
234 :report "Unlock the package."
235 (setf restart :unlock-package)))
236 (ecase restart
237 (:continue
238 (pushnew package *ignored-package-locks*))
239 (:ignore-all
240 (setf *ignored-package-locks* t))
241 (:unlock-package
242 (unlock-package package)))))
244 (defun package-lock-violation-p (package &optional (symbol nil symbolp))
245 ;; KLUDGE: (package-lock package) needs to be before
246 ;; comparison to *package*, since during cold init this gets
247 ;; called before *package* is bound -- but no package should
248 ;; be locked at that point.
249 (and package
250 (package-lock package)
251 ;; In package or implementation package
252 (not (or (eq package *package*)
253 (member *package* (package-%implementation-packages package))))
254 ;; Runtime disabling
255 (not (eq t *ignored-package-locks*))
256 (or (eq :invalid *ignored-package-locks*)
257 (not (member package *ignored-package-locks*)))
258 ;; declarations for symbols
259 (not (and symbolp (member symbol (disabled-package-locks))))))
261 (defun disabled-package-locks ()
262 (if (boundp 'sb!c::*lexenv*)
263 (sb!c::lexenv-disabled-package-locks sb!c::*lexenv*)
264 sb!c::*disabled-package-locks*))
266 ) ; progn
268 ;;;; more package-locking these are NOPs unless :sb-package-locks is
269 ;;;; in target features. Cross-compiler NOPs for these are in cross-misc.
271 ;;; The right way to establish a package lock context is
272 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR, defined in early-package.lisp
274 ;;; Must be used inside the dynamic contour established by
275 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR
276 (defun assert-package-unlocked (package &optional format-control
277 &rest format-arguments)
278 #!-sb-package-locks
279 (declare (ignore format-control format-arguments))
280 #!+sb-package-locks
281 (when (package-lock-violation-p package)
282 (package-lock-violation package
283 :format-control format-control
284 :format-arguments format-arguments))
285 package)
287 ;;; Must be used inside the dynamic contour established by
288 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR.
290 ;;; FIXME: Maybe we should establish such contours for he toplevel
291 ;;; and others, so that %set-fdefinition and others could just use
292 ;;; this.
293 (defun assert-symbol-home-package-unlocked (name &optional format-control
294 &rest format-arguments)
295 #!-sb-package-locks
296 (declare (ignore format-control format-arguments))
297 #!+sb-package-locks
298 (let* ((symbol (etypecase name
299 (symbol name)
300 ((cons (eql setf) cons) (second name))
301 ;; Skip lists of length 1, single conses and
302 ;; (class-predicate foo), etc. FIXME: MOP and
303 ;; package-lock interaction needs to be thought
304 ;; about.
305 (list
306 (return-from assert-symbol-home-package-unlocked
307 name))))
308 (package (symbol-package symbol)))
309 (when (package-lock-violation-p package symbol)
310 (package-lock-violation package
311 :symbol symbol
312 :format-control format-control
313 :format-arguments (cons name format-arguments))))
314 name)
317 ;;;; miscellaneous PACKAGE operations
319 (def!method print-object ((package package) stream)
320 (let ((name (package-%name package)))
321 (print-unreadable-object (package stream :type t :identity (not name))
322 (if name (prin1 name stream) (write-string "(deleted)" stream)))))
324 ;;; ANSI says (in the definition of DELETE-PACKAGE) that these, and
325 ;;; most other operations, are unspecified for deleted packages. We
326 ;;; just do the easy thing and signal errors in that case.
327 (macrolet ((def (ext real)
328 `(defun ,ext (package-designator)
329 (,real (find-undeleted-package-or-lose package-designator)))))
330 (def package-nicknames package-%nicknames)
331 (def package-use-list package-%use-list)
332 (def package-used-by-list package-%used-by-list)
333 (def package-shadowing-symbols package-%shadowing-symbols))
335 (defun package-local-nicknames (package-designator)
336 #!+sb-doc
337 "Returns an alist of \(local-nickname . actual-package) describing the
338 nicknames local to the designated package.
340 When in the designated package, calls to FIND-PACKAGE with the any of the
341 local-nicknames will return the corresponding actual-package instead. This
342 also affects all implied calls to FIND-PACKAGE, including those performed by
343 the reader.
345 When printing a package prefix for a symbol with a package local nickname, the
346 local nickname is used instead of the real name in order to preserve
347 print-read consistency.
349 See also: ADD-PACKAGE-LOCAL-NICKNAME, PACKAGE-LOCALLY-NICKNAMED-BY-LIST,
350 REMOVE-PACKAGE-LOCAL-NICKNAME, and the DEFPACKAGE option :LOCAL-NICKNAMES.
352 Experimental: interface subject to change."
353 (copy-tree
354 (package-%local-nicknames
355 (find-undeleted-package-or-lose package-designator))))
357 (defun signal-package-error (package format-control &rest format-args)
358 (error 'simple-package-error
359 :package package
360 :format-control format-control
361 :format-arguments format-args))
363 (defun signal-package-cerror (package continue-string
364 format-control &rest format-args)
365 (cerror continue-string
366 'simple-package-error
367 :package package
368 :format-control format-control
369 :format-arguments format-args))
371 (defun package-locally-nicknamed-by-list (package-designator)
372 #!+sb-doc
373 "Returns a list of packages which have a local nickname for the designated
374 package.
376 See also: ADD-PACKAGE-LOCAL-NICKNAME, PACKAGE-LOCAL-NICKNAMES,
377 REMOVE-PACKAGE-LOCAL-NICKNAME, and the DEFPACKAGE option :LOCAL-NICKNAMES.
379 Experimental: interface subject to change."
380 (copy-list
381 (package-%locally-nicknamed-by
382 (find-undeleted-package-or-lose package-designator))))
384 (defun add-package-local-nickname (local-nickname actual-package
385 &optional (package-designator (sane-package)))
386 #!+sb-doc
387 "Adds LOCAL-NICKNAME for ACTUAL-PACKAGE in the designated package, defaulting
388 to current package. LOCAL-NICKNAME must be a string designator, and
389 ACTUAL-PACKAGE must be a package designator.
391 Returns the designated package.
393 Signals a continuable error if LOCAL-NICKNAME is already a package local
394 nickname for a different package, or if LOCAL-NICKNAME is one of \"CL\",
395 \"COMMON-LISP\", or, \"KEYWORD\", or if LOCAL-NICKNAME is a global name or
396 nickname for the package to which the nickname would be added.
398 When in the designated package, calls to FIND-PACKAGE with the LOCAL-NICKNAME
399 will return the package the designated ACTUAL-PACKAGE instead. This also
400 affects all implied calls to FIND-PACKAGE, including those performed by the
401 reader.
403 When printing a package prefix for a symbol with a package local nickname,
404 local nickname is used instead of the real name in order to preserve
405 print-read consistency.
407 See also: PACKAGE-LOCAL-NICKNAMES, PACKAGE-LOCALLY-NICKNAMED-BY-LIST,
408 REMOVE-PACKAGE-LOCAL-NICKNAME, and the DEFPACKAGE option :LOCAL-NICKNAMES.
410 Experimental: interface subject to change."
411 (let* ((nick (string local-nickname))
412 (actual (find-package-using-package actual-package nil))
413 (package (find-undeleted-package-or-lose package-designator))
414 (existing (package-%local-nicknames package))
415 (cell (assoc nick existing :test #'string=)))
416 (unless actual
417 (signal-package-error
418 package-designator
419 "The name ~S does not designate any package."
420 actual-package))
421 (unless (package-name actual)
422 (signal-package-error
423 actual
424 "Cannot add ~A as local nickname for a deleted package: ~S"
425 nick actual))
426 (with-single-package-locked-error
427 (:package package "adding ~A as a local nickname for ~A"
428 nick actual))
429 (when (member nick '("CL" "COMMON-LISP" "KEYWORD") :test #'string=)
430 (signal-package-cerror
431 actual
432 "Continue, use it as local nickname anyways."
433 "Attempt to use ~A as a package local nickname (for ~A)."
434 nick (package-name actual)))
435 (when (string= nick (package-name package))
436 (signal-package-cerror
437 package
438 "Continue, use it as a local nickname anyways."
439 "Attempt to use ~A as a package local nickname (for ~A) in ~
440 package named globally ~A."
441 nick (package-name actual) nick))
442 (when (member nick (package-nicknames package) :test #'string=)
443 (signal-package-cerror
444 package
445 "Continue, use it as a local nickname anyways."
446 "Attempt to use ~A as a package local nickname (for ~A) in ~
447 package nicknamed globally ~A."
448 nick (package-name actual) nick))
449 (when (and cell (neq actual (cdr cell)))
450 (restart-case
451 (signal-package-error
452 actual
453 "~@<Cannot add ~A as local nickname for ~A in ~A: ~
454 already nickname for ~A.~:@>"
455 nick (package-name actual)
456 (package-name package) (package-name (cdr cell)))
457 (keep-old ()
458 :report (lambda (s)
459 (format s "Keep ~A as local nicname for ~A."
460 nick (package-name (cdr cell)))))
461 (change-nick ()
462 :report (lambda (s)
463 (format s "Use ~A as local nickname for ~A instead."
464 nick (package-name actual)))
465 (let ((old (cdr cell)))
466 (with-package-graph ()
467 (setf (package-%locally-nicknamed-by old)
468 (delete package (package-%locally-nicknamed-by old)))
469 (push package (package-%locally-nicknamed-by actual))
470 (setf (cdr cell) actual)))))
471 (return-from add-package-local-nickname package))
472 (unless cell
473 (with-package-graph ()
474 (push (cons nick actual) (package-%local-nicknames package))
475 (push package (package-%locally-nicknamed-by actual))))
476 package))
478 (defun remove-package-local-nickname (old-nickname
479 &optional (package-designator (sane-package)))
480 #!+sb-doc
481 "If the designated package had OLD-NICKNAME as a local nickname for
482 another package, it is removed. Returns true if the nickname existed and was
483 removed, and NIL otherwise.
485 See also: ADD-PACKAGE-LOCAL-NICKNAME, PACKAGE-LOCAL-NICKNAMES,
486 PACKAGE-LOCALLY-NICKNAMED-BY-LIST, and the DEFPACKAGE option :LOCAL-NICKNAMES.
488 Experimental: interface subject to change."
489 (let* ((nick (string old-nickname))
490 (package (find-undeleted-package-or-lose package-designator))
491 (existing (package-%local-nicknames package))
492 (cell (assoc nick existing :test #'string=)))
493 (when cell
494 (with-single-package-locked-error
495 (:package package "removing local nickname ~A for ~A"
496 nick (cdr cell)))
497 (with-package-graph ()
498 (let ((old (cdr cell)))
499 (setf (package-%local-nicknames package) (delete cell existing))
500 (setf (package-%locally-nicknamed-by old)
501 (delete package (package-%locally-nicknamed-by old)))))
502 t)))
504 (defun %package-hashtable-symbol-count (table)
505 (let ((size (the fixnum
506 (- (package-hashtable-size table)
507 (package-hashtable-deleted table)))))
508 (the fixnum
509 (- size (package-hashtable-free table)))))
511 (defun package-internal-symbol-count (package)
512 (%package-hashtable-symbol-count (package-internal-symbols package)))
514 (defun package-external-symbol-count (package)
515 (%package-hashtable-symbol-count (package-external-symbols package)))
517 (defvar *package* (error "*PACKAGE* should be initialized in cold load!")
518 #!+sb-doc "the current package")
520 (define-condition bootstrap-package-not-found (condition)
521 ((name :initarg :name :reader bootstrap-package-name)))
522 (defun debootstrap-package (&optional condition)
523 (invoke-restart
524 (find-restart-or-control-error 'debootstrap-package condition)))
526 (defun find-package (package-designator)
527 #!+sb-doc
528 "If PACKAGE-DESIGNATOR is a package, it is returned. Otherwise PACKAGE-DESIGNATOR
529 must be a string designator, in which case the package it names is located and returned.
531 As an SBCL extension, the current package may affect the way a package name is
532 resolved: if the current package has local nicknames specified, package names
533 matching those are resolved to the packages associated with them instead.
535 Example:
537 (defpackage :a)
538 (defpackage :example (:use :cl) (:local-nicknames (:x :a)))
539 (let ((*package* (find-package :example)))
540 (find-package :x)) => #<PACKAGE A>
542 See also: ADD-PACKAGE-LOCAL-NICKNAME, PACKAGE-LOCAL-NICKNAMES,
543 REMOVE-PACKAGE-LOCAL-NICKNAME, and the DEFPACKAGE option :LOCAL-NICKNAMES."
544 (find-package-using-package package-designator
545 (when (boundp '*package*)
546 *package*)))
548 ;;; This is undocumented and unexported for now, but the idea is that by
549 ;;; making this a generic function then packages with custom package classes
550 ;;; could hook into this to provide their own resolution.
551 (defun find-package-using-package (package-designator base)
552 (flet ((find-package-from-string (string)
553 (declare (type string string))
554 (let* ((nicknames (when base
555 (package-%local-nicknames base)))
556 (nicknamed (when nicknames
557 (cdr (assoc string nicknames :test #'string=))))
558 (packageoid (or nicknamed (gethash string *package-names*))))
559 (if (and (null packageoid)
560 ;; FIXME: should never need 'debootstrap' hack
561 (let ((mismatch (mismatch "SB!" string)))
562 (and mismatch (= mismatch 3))))
563 (restart-case
564 (signal 'bootstrap-package-not-found :name string)
565 (debootstrap-package ()
566 (if (string= string "SB!XC")
567 (find-package "COMMON-LISP")
568 (find-package
569 (substitute #\- #\! string :count 1)))))
570 packageoid))))
571 (typecase package-designator
572 (package package-designator)
573 (symbol (find-package-from-string (symbol-name package-designator)))
574 (string (find-package-from-string package-designator))
575 (character (find-package-from-string (string package-designator)))
576 (t (error 'type-error
577 :datum package-designator
578 :expected-type '(or character package string symbol))))))
580 ;;; Return a list of packages given a package designator or list of
581 ;;; package designators, or die trying.
582 (defun package-listify (thing)
583 (if (listp thing)
584 (mapcar #'find-undeleted-package-or-lose thing)
585 (list (find-undeleted-package-or-lose thing))))
587 ;;; ANSI specifies (in the definition of DELETE-PACKAGE) that PACKAGE-NAME
588 ;;; returns NIL (not an error) for a deleted package, so this is a special
589 ;;; case where we want to use bare %FIND-PACKAGE-OR-LOSE instead of
590 ;;; FIND-UNDELETED-PACKAGE-OR-LOSE.
591 (defun package-name (package-designator)
592 (package-%name (%find-package-or-lose package-designator)))
594 ;;;; operations on package hashtables
596 ;;; Compute a number between 1 and 255 based on the sxhash of the
597 ;;; pname and the length thereof.
598 (declaim (inline entry-hash))
599 (defun entry-hash (length sxhash)
600 (declare (index length) ((and fixnum unsigned-byte) sxhash))
601 (1+ (rem (logxor length sxhash
602 (ash sxhash -8) (ash sxhash -16) (ash sxhash -19))
603 255)))
605 ;;; Add a symbol to a package hashtable. The symbol is assumed
606 ;;; not to be present.
607 (defun add-symbol (table symbol)
608 (when (zerop (package-hashtable-free table))
609 ;; The hashtable is full. Resize it to be able to hold twice the
610 ;; amount of symbols than it currently contains. The actual new size
611 ;; can be smaller than twice the current size if the table contained
612 ;; deleted entries.
613 (resize-package-hashtable table
614 (* (- (package-hashtable-size table)
615 (package-hashtable-deleted table))
616 2)))
617 (let* ((symvec (package-hashtable-cells table))
618 (len (1- (length symvec)))
619 (hashvec (the hash-vector (aref symvec len)))
620 (sxhash (truly-the fixnum (ensure-symbol-hash symbol)))
621 (h2 (1+ (rem sxhash (- len 2)))))
622 (declare (fixnum sxhash h2))
623 (do ((i (rem sxhash len) (rem (+ i h2) len)))
624 ((eql (aref hashvec i) 0)
625 (if (eql (svref symvec i) 0)
626 (decf (package-hashtable-free table))
627 (decf (package-hashtable-deleted table)))
628 ;; This order of these two SETFs does not matter.
629 ;; An empty symbol cell is skipped on lookup if the hash cell
630 ;; matches something accidentally. "empty" = any fixnum.
631 (setf (svref symvec i) symbol)
632 (setf (aref hashvec i)
633 (entry-hash (length (symbol-name symbol)) sxhash)))
634 (declare (fixnum i)))))
636 ;;; Resize the package hashtables of all packages so that their load
637 ;;; factor is *PACKAGE-HASHTABLE-IMAGE-LOAD-FACTOR*. Called from
638 ;;; SAVE-LISP-AND-DIE to optimize space usage in the image.
639 (defun tune-hashtable-sizes-of-all-packages ()
640 (flet ((tune-table-size (table)
641 (resize-package-hashtable
642 table
643 (round (* (/ *package-rehash-threshold*
644 *package-hashtable-image-load-factor*)
645 (- (package-hashtable-size table)
646 (package-hashtable-free table)
647 (package-hashtable-deleted table)))))))
648 (dolist (package (list-all-packages))
649 (tune-table-size (package-internal-symbols package))
650 (tune-table-size (package-external-symbols package)))))
652 ;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR
653 ;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR
654 ;;; is bound to the symbol. LENGTH and HASH are the length and sxhash
655 ;;; of STRING. ENTRY-HASH is the entry-hash of the string and length.
656 ;;; If the symbol is found, then FORMS are executed; otherwise not.
658 (defmacro with-symbol (((symbol-var &optional (index-var (gensym))) table
659 string length sxhash entry-hash) &body forms)
660 (with-unique-names (vec len hash-vec h2 probed-ehash name)
661 `(let* ((,vec (package-hashtable-cells ,table))
662 (,len (1- (length ,vec)))
663 (,hash-vec (the hash-vector (svref ,vec ,len)))
664 (,index-var (rem (the hash ,sxhash) ,len))
665 (,h2 (1+ (the index (rem (the hash ,sxhash)
666 (the index (- ,len 2)))))))
667 (declare (type index ,len ,h2 ,index-var))
668 (loop
669 (let ((,probed-ehash (aref ,hash-vec ,index-var)))
670 (cond
671 ((eql ,probed-ehash ,entry-hash)
672 (let ((,symbol-var (truly-the symbol (svref ,vec ,index-var))))
673 (when (eq (symbol-hash ,symbol-var) ,sxhash)
674 (let ((,name (symbol-name ,symbol-var)))
675 ;; The pre-test for length is kind of an unimportant
676 ;; optimization, but passing it for both :end arguments
677 ;; requires that it be within bounds for the probed symbol.
678 (when (and (= (length ,name) ,length)
679 (string= ,string ,name
680 :end1 ,length :end2 ,length))
681 (return (progn ,@forms)))))))
682 ((eql ,probed-ehash 0)
683 ;; either a never used cell or a tombstone left by UNINTERN
684 (when (eql (svref ,vec ,index-var) 0) ; really never used
685 (return)))))
686 (when (>= (incf ,index-var ,h2) ,len)
687 (decf ,index-var ,len))))))
689 ;;; Delete the entry for STRING in TABLE. The entry must exist.
690 ;;; Deletion stores -1 for the symbol and 0 for the hash tombstone.
691 ;;; Storing NIL for the symbol, as used to be done, is vulnerable to a rare
692 ;;; concurrency bug because many strings have the same ENTRY-HASH as NIL:
693 ;;; (entry-hash 3 (sxhash "NIL")) => 177 and
694 ;;; (entry-hash 2 (sxhash "3M")) => 177
695 ;;; Suppose, in the former approach, that "3M" is interned,
696 ;;; and then the following sequence of events occur:
697 ;;; - thread 1 performs FIND-SYMBOL on "NIL", hits the hash code at
698 ;;; at index I, and is then context-switched out. When this thread
699 ;; resumes, it assumes a valid symbol in (SVREF SYM-VEC I)
700 ;;; - thread 2 uninterns "3M" and fully completes that, storing NIL
701 ;;; in the slot for the symbol that thread 1 will next read.
702 ;;; - thread 1 continues, and test for STRING= to NIL,
703 ;;; wrongly seeing NIL as a present symbol.
704 ;;; It's possible this is harmless, because NIL is usually inherited from CL,
705 ;;; but if the secondary return value mattered to the application,
706 ;;; it is probably wrong. And of course if NIL was not intended to be found
707 ;;; - as in a package that does not use CL - then finding NIL at all is wrong.
708 ;;; The better approach is to treat 'hash' as purely a heuristic without
709 ;;; ill-effect from false positives. No barrier, nor read-consistency
710 ;;; check is required, since a symbol is both its key and value,
711 ;;; and the "absence of a symbol" marker is never mistaken for a symbol.
713 (defun nuke-symbol (table symbol)
714 (let* ((string (symbol-name symbol))
715 (length (length string))
716 (hash (symbol-hash symbol))
717 (ehash (entry-hash length hash)))
718 (declare (type index length)
719 (type hash hash))
720 (with-symbol ((symbol index) table string length hash ehash)
721 ;; It is suboptimal to grab the vectors again, but not broken,
722 ;; because we have exclusive use of the table for writing.
723 (let* ((symvec (package-hashtable-cells table))
724 (hashvec (the hash-vector (aref symvec (1- (length symvec))))))
725 (setf (aref hashvec index) 0)
726 (setf (aref symvec index) -1)) ; any nonzero fixnum will do
727 (incf (package-hashtable-deleted table))))
728 ;; If the table is less than one quarter full, halve its size and
729 ;; rehash the entries.
730 (let* ((size (package-hashtable-size table))
731 (deleted (package-hashtable-deleted table))
732 (used (- size
733 (package-hashtable-free table)
734 deleted)))
735 (declare (type fixnum size deleted used))
736 (when (< used (truncate size 4))
737 (resize-package-hashtable table (* used 2)))))
739 ;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*. If there is a
740 ;;; conflict then give the user a chance to do something about it. Caller is
741 ;;; responsible for having acquired the mutex via WITH-PACKAGES.
742 (defun %enter-new-nicknames (package nicknames)
743 (declare (type list nicknames))
744 (dolist (n nicknames)
745 (let* ((n (stringify-package-designator n))
746 (found (with-package-names (names)
747 (or (gethash n names)
748 (progn
749 (setf (gethash n names) package)
750 (push n (package-%nicknames package))
751 package)))))
752 (cond ((eq found package))
753 ((string= (the string (package-%name found)) n)
754 (signal-package-cerror
755 package
756 "Ignore this nickname."
757 "~S is a package name, so it cannot be a nickname for ~S."
758 n (package-%name package)))
760 (signal-package-cerror
761 package
762 "Leave this nickname alone."
763 "~S is already a nickname for ~S."
764 n (package-%name found)))))))
766 (defun make-package (name &key
767 (use '#.*default-package-use-list*)
768 nicknames
769 (internal-symbols 10)
770 (external-symbols 10))
771 #!+sb-doc
772 #.(format nil
773 "Make a new package having the specified NAME, NICKNAMES, and USE
774 list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are estimates for the number of
775 internal and external symbols which will ultimately be present in the package.
776 The default value of USE is implementation-dependent, and in this
777 implementation it is ~S." *default-package-use-list*)
778 (prog (clobber)
779 :restart
780 (when (find-package name)
781 ;; ANSI specifies that this error is correctable.
782 (signal-package-cerror
783 name
784 "Clobber existing package."
785 "A package named ~S already exists" name)
786 (setf clobber t))
787 (with-package-graph ()
788 ;; Check for race, signal the error outside the lock.
789 (when (and (not clobber) (find-package name))
790 (go :restart))
791 (let* ((name (stringify-package-designator name))
792 (package
793 (%make-package
794 name
795 (make-package-hashtable internal-symbols)
796 (make-package-hashtable external-symbols))))
798 ;; Do a USE-PACKAGE for each thing in the USE list so that checking for
799 ;; conflicting exports among used packages is done.
800 (use-package use package)
802 ;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
803 ;; which would leave us with possibly-bad side effects from the earlier
804 ;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
805 ;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
806 ;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
807 ;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
808 ;; USE-PACKAGE, too.
809 (%enter-new-nicknames package nicknames)
810 (return (setf (gethash name *package-names*) package))))
811 (bug "never")))
813 ;;; Change the name if we can, blast any old nicknames and then
814 ;;; add in any new ones.
816 ;;; FIXME: ANSI claims that NAME is a package designator (not just a
817 ;;; string designator -- weird). Thus, NAME could
818 ;;; be a package instead of a string. Presumably then we should not change
819 ;;; the package name if NAME is the same package that's referred to by PACKAGE.
820 ;;; If it's a *different* package, we should probably signal an error.
821 ;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
822 (defun rename-package (package-designator name &optional (nicknames ()))
823 #!+sb-doc
824 "Changes the name and nicknames for a package."
825 (prog () :restart
826 (let ((package (find-undeleted-package-or-lose package-designator))
827 (name (stringify-package-designator name))
828 (found (find-package name))
829 (nicks (mapcar #'string nicknames)))
830 (unless (or (not found) (eq found package))
831 (signal-package-error name
832 "A package named ~S already exists." name))
833 (with-single-package-locked-error ()
834 (unless (and (string= name (package-name package))
835 (null (set-difference nicks (package-nicknames package)
836 :test #'string=)))
837 (assert-package-unlocked
838 package "renaming as ~A~@[ with nickname~*~P ~1@*~{~A~^, ~}~]"
839 name nicks (length nicks)))
840 (with-package-names (names)
841 ;; Check for race conditions now that we have the lock.
842 (unless (eq package (find-package package-designator))
843 (go :restart))
844 ;; Do the renaming.
845 (remhash (package-%name package) names)
846 (dolist (n (package-%nicknames package))
847 (remhash n names))
848 (setf (package-%name package) name
849 (gethash name names) package
850 (package-%nicknames package) ()))
851 (%enter-new-nicknames package nicknames))
852 (return package))))
854 (defun delete-package (package-designator)
855 #!+sb-doc
856 "Delete the package designated by PACKAGE-DESIGNATOR from the package
857 system data structures."
858 (tagbody :restart
859 (let ((package (find-package package-designator)))
860 (cond ((not package)
861 ;; This continuable error is required by ANSI.
862 (signal-package-cerror
863 package-designator
864 "Ignore."
865 "There is no package named ~S." package-designator)
866 (return-from delete-package nil))
867 ((not (package-name package)) ; already deleted
868 (return-from delete-package nil))
870 (with-single-package-locked-error
871 (:package package "deleting package ~A" package)
872 (let ((use-list (package-used-by-list package)))
873 (when use-list
874 ;; This continuable error is specified by ANSI.
875 (signal-package-cerror
876 package
877 "Remove dependency in other packages."
878 "~@<Package ~S is used by package~P:~2I~_~S~@:>"
879 (package-name package)
880 (length use-list)
881 (mapcar #'package-name use-list))
882 (dolist (p use-list)
883 (unuse-package package p))))
884 #!+sb-package-locks
885 (dolist (p (package-implements-list package))
886 (remove-implementation-package package p))
887 (with-package-graph ()
888 ;; Check for races, restart if necessary.
889 (let ((package2 (find-package package-designator)))
890 (when (or (neq package package2) (package-used-by-list package2))
891 (go :restart)))
892 (dolist (used (package-use-list package))
893 (unuse-package used package))
894 (dolist (namer (package-%locally-nicknamed-by package))
895 (setf (package-%local-nicknames namer)
896 (delete package (package-%local-nicknames namer) :key #'cdr)))
897 (setf (package-%locally-nicknamed-by package) nil)
898 (dolist (cell (package-%local-nicknames package))
899 (let ((actual (cdr cell)))
900 (setf (package-%locally-nicknamed-by actual)
901 (delete package (package-%locally-nicknamed-by actual)))))
902 (setf (package-%local-nicknames package) nil)
903 ;; FIXME: lacking a way to advise UNINTERN that this package
904 ;; is pending deletion, a large package conses successively
905 ;; many smaller tables for no good reason.
906 (do-symbols (sym package)
907 (unintern sym package))
908 (with-package-names (names)
909 (remhash (package-name package) names)
910 (dolist (nick (package-nicknames package))
911 (remhash nick names))
912 (setf (package-%name package) nil
913 ;; Setting PACKAGE-%NAME to NIL is required in order to
914 ;; make PACKAGE-NAME return NIL for a deleted package as
915 ;; ANSI requires. Setting the other slots to NIL
916 ;; and blowing away the PACKAGE-HASHTABLES is just done
917 ;; for tidiness and to help the GC.
918 (package-%nicknames package) nil))
919 (setf (package-%use-list package) nil
920 (package-tables package) #()
921 (package-%shadowing-symbols package) nil
922 (package-internal-symbols package)
923 (make-package-hashtable 0)
924 (package-external-symbols package)
925 (make-package-hashtable 0)))
926 (return-from delete-package t)))))))
928 (defun list-all-packages ()
929 #!+sb-doc
930 "Return a list of all existing packages."
931 (let ((res ()))
932 (with-package-names (names)
933 (maphash (lambda (k v)
934 (declare (ignore k))
935 (pushnew v res :test #'eq))
936 names))
937 res))
939 (macrolet ((find/intern (function &rest more-args)
940 ;; Both %FIND-SYMBOL and %INTERN require a SIMPLE-STRING,
941 ;; but accept a LENGTH. Given a non-simple string,
942 ;; we need copy it only if the cumulative displacement
943 ;; into the underlying simple-string is nonzero.
944 ;; There are two things that can be improved
945 ;; about the generated code here:
946 ;; 1. if X is known to satisfy STRINGP (generally any rank-1 array),
947 ;; then testing SIMPLE-<base|character>-STRING-P should not
948 ;; re-test the lowtag. This is constrained by the backends,
949 ;; because there are no type vops that assume a known lowtag.
950 ;; 2. if X is known to satisfy VECTORP, then
951 ;; (NOT (ARRAY-HEADER-P)) implies SIMPLE-P, but the compiler
952 ;; does not actually know that, and generates a check.
953 ;; This is more of a front-end issue.
954 `(multiple-value-bind (name length)
955 (if (simple-string-p name)
956 (values name (length name))
957 (with-array-data ((name name) (start) (end)
958 :check-fill-pointer t)
959 (if (eql start 0)
960 (values name end)
961 (values (subseq name start end)
962 (- end start)))))
963 (truly-the
964 (values symbol (member :internal :external :inherited nil))
965 (,function name length
966 (find-undeleted-package-or-lose package)
967 ,@more-args)))))
969 (defun intern (name &optional (package (sane-package)))
970 #!+sb-doc
971 "Return a symbol in PACKAGE having the specified NAME, creating it
972 if necessary."
973 (find/intern %intern t))
975 (defun find-symbol (name &optional (package (sane-package)))
976 #!+sb-doc
977 "Return the symbol named STRING in PACKAGE. If such a symbol is found
978 then the second value is :INTERNAL, :EXTERNAL or :INHERITED to indicate
979 how the symbol is accessible. If no symbol is found then both values
980 are NIL."
981 (find/intern %find-symbol)))
983 ;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
984 ;;; then create it, special-casing the keyword package.
985 (defun %intern (name length package copy-p)
986 (declare (simple-string name) (index length))
987 (multiple-value-bind (symbol where) (%find-symbol name length package)
988 (cond (where
989 (values symbol where))
991 ;; Let's try again with a lock: the common case has the
992 ;; symbol already interned, handled by the first leg of the
993 ;; COND, but in case another thread is interning in
994 ;; parallel we need to check after grabbing the lock.
995 (with-package-graph ()
996 (setf (values symbol where) (%find-symbol name length package))
997 (if where
998 (values symbol where)
999 (let ((symbol-name (cond ((not copy-p)
1000 (aver (= (length name) length))
1001 name)
1002 ((typep name '(simple-array nil (*)))
1005 ;; This so that SUBSEQ is inlined,
1006 ;; because we need it fixed for cold init.
1007 (string-dispatch
1008 ((simple-array base-char (*))
1009 (simple-array character (*)))
1010 name
1011 (declare (optimize speed))
1012 (subseq name 0 length))))))
1013 (with-single-package-locked-error
1014 (:package package "interning ~A" symbol-name)
1015 (let ((symbol (make-symbol symbol-name)))
1016 (add-symbol (cond ((eq package *keyword-package*)
1017 (%set-symbol-value symbol symbol)
1018 (package-external-symbols package))
1020 (package-internal-symbols package)))
1021 symbol)
1022 (%set-symbol-package symbol package)
1023 (values symbol nil))))))))))
1025 ;;; Check internal and external symbols, then scan down the list
1026 ;;; of hashtables for inherited symbols.
1027 (defun %find-symbol (string length package)
1028 (declare (simple-string string)
1029 (type index length))
1030 (let* ((hash (compute-symbol-hash string length))
1031 (ehash (entry-hash length hash)))
1032 (declare (type hash hash ehash))
1033 (with-symbol ((symbol) (package-internal-symbols package)
1034 string length hash ehash)
1035 (return-from %find-symbol (values symbol :internal)))
1036 (with-symbol ((symbol) (package-external-symbols package)
1037 string length hash ehash)
1038 (return-from %find-symbol (values symbol :external)))
1039 (let* ((tables (package-tables package))
1040 (n (length tables)))
1041 (unless (eql n 0)
1042 ;; Try the most-recently-used table, then others.
1043 ;; TABLES is treated as circular for this purpose.
1044 (let* ((mru (package-mru-table-index package))
1045 (start (if (< mru n) mru 0))
1046 (i start))
1047 (loop
1048 (with-symbol ((symbol) (locally (declare (optimize (safety 0)))
1049 (svref tables i))
1050 string length hash ehash)
1051 (setf (package-mru-table-index package) i)
1052 (return-from %find-symbol (values symbol :inherited)))
1053 (if (< (decf i) 0) (setq i (1- n)))
1054 (if (= i start) (return)))))))
1055 (values nil nil))
1057 ;;; Similar to FIND-SYMBOL, but only looks for an external symbol.
1058 ;;; Return the symbol and T if found, otherwise two NILs.
1059 ;;; This is used for fast name-conflict checking in this file and symbol
1060 ;;; printing in the printer.
1061 ;;; An optimization is possible here: by accepting either a string or symbol,
1062 ;;; if the symbol's hash slot is nonzero, we can avoid COMPUTE-SYMBOL-HASH.
1063 (defun find-external-symbol (string package)
1064 (declare (simple-string string))
1065 (let* ((length (length string))
1066 (hash (compute-symbol-hash string length))
1067 (ehash (entry-hash length hash)))
1068 (declare (type index length)
1069 (type hash hash))
1070 (with-symbol ((symbol) (package-external-symbols package)
1071 string length hash ehash)
1072 (return-from find-external-symbol (values symbol t))))
1073 (values nil nil))
1075 (define-condition name-conflict (reference-condition package-error)
1076 ((function :initarg :function :reader name-conflict-function)
1077 (datum :initarg :datum :reader name-conflict-datum)
1078 (symbols :initarg :symbols :reader name-conflict-symbols))
1079 (:default-initargs :references (list '(:ansi-cl :section (11 1 1 2 5))))
1080 (:report
1081 (lambda (c s)
1082 (format s "~@<~S ~S causes name-conflicts in ~S between the ~
1083 following symbols:~2I~@:_~
1084 ~{~/sb-impl::print-symbol-with-prefix/~^, ~}~:@>"
1085 (name-conflict-function c)
1086 (name-conflict-datum c)
1087 (package-error-package c)
1088 (name-conflict-symbols c)))))
1090 (defun name-conflict (package function datum &rest symbols)
1091 (flet ((importp (c)
1092 (declare (ignore c))
1093 (eq 'import function))
1094 (use-or-export-p (c)
1095 (declare (ignore c))
1096 (or (eq 'use-package function)
1097 (eq 'export function)))
1098 (old-symbol ()
1099 (car (remove datum symbols))))
1100 (let ((pname (package-name package)))
1101 (restart-case
1102 (error 'name-conflict :package package :symbols symbols
1103 :function function :datum datum)
1104 ;; USE-PACKAGE and EXPORT
1105 (keep-old ()
1106 :report (lambda (s)
1107 (ecase function
1108 (export
1109 (format s "Keep ~S accessible in ~A (shadowing ~S)."
1110 (old-symbol) pname datum))
1111 (use-package
1112 (format s "Keep symbols already accessible ~A (shadowing others)."
1113 pname))))
1114 :test use-or-export-p
1115 (dolist (s (remove-duplicates symbols :test #'string=))
1116 (shadow (symbol-name s) package)))
1117 (take-new ()
1118 :report (lambda (s)
1119 (ecase function
1120 (export
1121 (format s "Make ~S accessible in ~A (uninterning ~S)."
1122 datum pname (old-symbol)))
1123 (use-package
1124 (format s "Make newly exposed symbols accessible in ~A, ~
1125 uninterning old ones."
1126 pname))))
1127 :test use-or-export-p
1128 (dolist (s symbols)
1129 (when (eq s (find-symbol (symbol-name s) package))
1130 (unintern s package))))
1131 ;; IMPORT
1132 (shadowing-import-it ()
1133 :report (lambda (s)
1134 (format s "Shadowing-import ~S, uninterning ~S."
1135 datum (old-symbol)))
1136 :test importp
1137 (shadowing-import datum package))
1138 (dont-import-it ()
1139 :report (lambda (s)
1140 (format s "Don't import ~S, keeping ~S."
1141 datum
1142 (car (remove datum symbols))))
1143 :test importp)
1144 ;; General case. This is exposed via SB-EXT.
1145 (resolve-conflict (chosen-symbol)
1146 :report "Resolve conflict."
1147 :interactive
1148 (lambda ()
1149 (let* ((len (length symbols))
1150 (nlen (length (write-to-string len :base 10)))
1151 (*print-pretty* t))
1152 (format *query-io* "~&~@<Select a symbol to be made accessible in ~
1153 package ~A:~2I~@:_~{~{~V,' D. ~
1154 ~/sb-impl::print-symbol-with-prefix/~}~@:_~}~
1155 ~@:>"
1156 (package-name package)
1157 (loop for s in symbols
1158 for i upfrom 1
1159 collect (list nlen i s)))
1160 (loop
1161 (format *query-io* "~&Enter an integer (between 1 and ~D): " len)
1162 (finish-output *query-io*)
1163 (let ((i (parse-integer (read-line *query-io*) :junk-allowed t)))
1164 (when (and i (<= 1 i len))
1165 (return (list (nth (1- i) symbols))))))))
1166 (multiple-value-bind (package-symbol status)
1167 (find-symbol (symbol-name chosen-symbol) package)
1168 (let* ((accessiblep status) ; never NIL here
1169 (presentp (and accessiblep
1170 (not (eq :inherited status)))))
1171 (ecase function
1172 ((unintern)
1173 (if presentp
1174 (if (eq package-symbol chosen-symbol)
1175 (shadow (list package-symbol) package)
1176 (shadowing-import (list chosen-symbol) package))
1177 (shadowing-import (list chosen-symbol) package)))
1178 ((use-package export)
1179 (if presentp
1180 (if (eq package-symbol chosen-symbol)
1181 (shadow (list package-symbol) package) ; CLHS 11.1.1.2.5
1182 (if (eq (symbol-package package-symbol) package)
1183 (unintern package-symbol package) ; CLHS 11.1.1.2.5
1184 (shadowing-import (list chosen-symbol) package)))
1185 (shadowing-import (list chosen-symbol) package)))
1186 ((import)
1187 (if presentp
1188 (if (eq package-symbol chosen-symbol)
1189 nil ; re-importing the same symbol
1190 (shadowing-import (list chosen-symbol) package))
1191 (shadowing-import (list chosen-symbol) package)))))))))))
1193 ;;; If we are uninterning a shadowing symbol, then a name conflict can
1194 ;;; result, otherwise just nuke the symbol.
1195 (defun unintern (symbol &optional (package (sane-package)))
1196 #!+sb-doc
1197 "Makes SYMBOL no longer present in PACKAGE. If SYMBOL was present then T is
1198 returned, otherwise NIL. If PACKAGE is SYMBOL's home package, then it is made
1199 uninterned."
1200 (with-package-graph ()
1201 (let* ((package (find-undeleted-package-or-lose package))
1202 (name (symbol-name symbol))
1203 (shadowing-symbols (package-%shadowing-symbols package)))
1204 (declare (list shadowing-symbols))
1206 (with-single-package-locked-error ()
1207 (when (find-symbol name package)
1208 (assert-package-unlocked package "uninterning ~A" name))
1210 ;; If a name conflict is revealed, give us a chance to
1211 ;; shadowing-import one of the accessible symbols.
1212 (when (member symbol shadowing-symbols)
1213 (let ((cset ()))
1214 (dolist (p (package-%use-list package))
1215 (multiple-value-bind (s w) (find-external-symbol name p)
1216 ;; S should be derived as SYMBOL so that PUSHNEW can assume #'EQ
1217 ;; as the test, but it's not happening, so restate the obvious.
1218 (when w (pushnew s cset :test #'eq))))
1219 (when (cdr cset)
1220 (apply #'name-conflict package 'unintern symbol cset)
1221 (return-from unintern t)))
1222 (setf (package-%shadowing-symbols package)
1223 (remove symbol shadowing-symbols)))
1225 (multiple-value-bind (s w) (find-symbol name package)
1226 (cond ((not (eq symbol s)) nil)
1227 ((or (eq w :internal) (eq w :external))
1228 (nuke-symbol (if (eq w :internal)
1229 (package-internal-symbols package)
1230 (package-external-symbols package))
1231 symbol)
1232 (if (eq (symbol-package symbol) package)
1233 (%set-symbol-package symbol nil))
1235 (t nil)))))))
1237 ;;; Take a symbol-or-list-of-symbols and return a list, checking types.
1238 (defun symbol-listify (thing)
1239 (cond ((listp thing)
1240 (dolist (s thing)
1241 (unless (symbolp s)
1242 (signal-package-error nil
1243 "~S is not a symbol." s)))
1244 thing)
1245 ((symbolp thing) (list thing))
1247 (signal-package-error nil
1248 "~S is neither a symbol nor a list of symbols."
1249 thing))))
1251 (defun string-listify (thing)
1252 (mapcar #'string (ensure-list thing)))
1254 (defun export (symbols &optional (package (sane-package)))
1255 #!+sb-doc
1256 "Exports SYMBOLS from PACKAGE, checking that no name conflicts result."
1257 (with-package-graph ()
1258 (let ((package (find-undeleted-package-or-lose package))
1259 (symbols (symbol-listify symbols))
1260 (syms ()))
1261 ;; Punt any symbols that are already external.
1262 (dolist (sym symbols)
1263 (multiple-value-bind (s found)
1264 (find-external-symbol (symbol-name sym) package)
1265 (unless (or (and found (eq s sym)) (member sym syms))
1266 (push sym syms))))
1267 (with-single-package-locked-error ()
1268 (when syms
1269 (assert-package-unlocked package "exporting symbol~P ~{~A~^, ~}"
1270 (length syms) syms))
1271 ;; Find symbols and packages with conflicts.
1272 (let ((used-by (package-%used-by-list package)))
1273 (dolist (sym syms)
1274 (let ((name (symbol-name sym)))
1275 (dolist (p used-by)
1276 (multiple-value-bind (s w) (find-symbol name p)
1277 (when (and w
1278 (not (eq s sym))
1279 (not (member s (package-%shadowing-symbols p))))
1280 ;; Beware: the name conflict is in package P, not in
1281 ;; PACKAGE.
1282 (name-conflict p 'export sym sym s)))))))
1283 ;; Check that all symbols are accessible. If not, ask to import them.
1284 (let ((missing ())
1285 (imports ()))
1286 (dolist (sym syms)
1287 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1288 (cond ((not (and w (eq s sym)))
1289 (push sym missing))
1290 ((eq w :inherited)
1291 (push sym imports)))))
1292 (when missing
1293 (signal-package-cerror
1294 package
1295 (format nil "~S these symbols into the ~A package."
1296 'import (package-%name package))
1297 "~@<These symbols are not accessible in the ~A package:~2I~_~S~@:>"
1298 (package-%name package) missing)
1299 (import missing package))
1300 (import imports package))
1302 ;; And now, three pages later, we export the suckers.
1303 (let ((internal (package-internal-symbols package))
1304 (external (package-external-symbols package)))
1305 (dolist (sym syms)
1306 (add-symbol external sym)
1307 (nuke-symbol internal sym))))
1308 t)))
1310 ;;; Check that all symbols are accessible, then move from external to internal.
1311 (defun unexport (symbols &optional (package (sane-package)))
1312 #!+sb-doc
1313 "Makes SYMBOLS no longer exported from PACKAGE."
1314 (with-package-graph ()
1315 (let ((package (find-undeleted-package-or-lose package))
1316 (symbols (symbol-listify symbols))
1317 (syms ()))
1318 (dolist (sym symbols)
1319 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1320 (cond ((or (not w) (not (eq s sym)))
1321 (signal-package-error
1322 package
1323 "~S is not accessible in the ~A package."
1324 sym (package-%name package)))
1325 ((eq w :external) (pushnew sym syms)))))
1326 (with-single-package-locked-error ()
1327 (when syms
1328 (assert-package-unlocked package "unexporting symbol~P ~{~A~^, ~}"
1329 (length syms) syms))
1330 (let ((internal (package-internal-symbols package))
1331 (external (package-external-symbols package)))
1332 (dolist (sym syms)
1333 (add-symbol internal sym)
1334 (nuke-symbol external sym))))
1335 t)))
1337 ;;; Check for name conflict caused by the import and let the user
1338 ;;; shadowing-import if there is.
1339 (defun import (symbols &optional (package (sane-package)))
1340 #!+sb-doc
1341 "Make SYMBOLS accessible as internal symbols in PACKAGE. If a symbol is
1342 already accessible then it has no effect. If a name conflict would result from
1343 the importation, then a correctable error is signalled."
1344 (with-package-graph ()
1345 (let* ((package (find-undeleted-package-or-lose package))
1346 (symbols (symbol-listify symbols))
1347 (homeless (remove-if #'symbol-package symbols))
1348 (syms ()))
1349 (with-single-package-locked-error ()
1350 (dolist (sym symbols)
1351 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1352 (cond ((not w)
1353 (let ((found (member sym syms :test #'string=)))
1354 (if found
1355 (when (not (eq (car found) sym))
1356 (setf syms (remove (car found) syms))
1357 (name-conflict package 'import sym sym (car found)))
1358 (push sym syms))))
1359 ((not (eq s sym))
1360 (name-conflict package 'import sym sym s))
1361 ((eq w :inherited) (push sym syms)))))
1362 (when (or homeless syms)
1363 (let ((union (delete-duplicates (append homeless syms))))
1364 (assert-package-unlocked package "importing symbol~P ~{~A~^, ~}"
1365 (length union) union)))
1366 ;; Add the new symbols to the internal hashtable.
1367 (let ((internal (package-internal-symbols package)))
1368 (dolist (sym syms)
1369 (add-symbol internal sym)))
1370 ;; If any of the symbols are uninterned, make them be owned by PACKAGE.
1371 (dolist (sym homeless)
1372 (%set-symbol-package sym package))
1373 t))))
1375 ;;; If a conflicting symbol is present, unintern it, otherwise just
1376 ;;; stick the symbol in.
1377 (defun shadowing-import (symbols &optional (package (sane-package)))
1378 #!+sb-doc
1379 "Import SYMBOLS into package, disregarding any name conflict. If
1380 a symbol of the same name is present, then it is uninterned."
1381 (with-package-graph ()
1382 (let* ((package (find-undeleted-package-or-lose package))
1383 (internal (package-internal-symbols package))
1384 (symbols (symbol-listify symbols))
1385 (lock-asserted-p nil))
1386 (with-single-package-locked-error ()
1387 (dolist (sym symbols)
1388 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1389 (unless (or lock-asserted-p
1390 (and (eq s sym)
1391 (member s (package-shadowing-symbols package))))
1392 (assert-package-unlocked package "shadowing-importing symbol~P ~
1393 ~{~A~^, ~}" (length symbols) symbols)
1394 (setf lock-asserted-p t))
1395 (unless (and w (not (eq w :inherited)) (eq s sym))
1396 (when (or (eq w :internal) (eq w :external))
1397 ;; If it was shadowed, we don't want UNINTERN to flame out...
1398 (setf (package-%shadowing-symbols package)
1399 (remove s (the list (package-%shadowing-symbols package))))
1400 (unintern s package))
1401 (add-symbol internal sym))
1402 (pushnew sym (package-%shadowing-symbols package)))))))
1405 (defun shadow (symbols &optional (package (sane-package)))
1406 #!+sb-doc
1407 "Make an internal symbol in PACKAGE with the same name as each of the
1408 specified SYMBOLS. If a symbol with the given name is already present in
1409 PACKAGE, then the existing symbol is placed in the shadowing symbols list if
1410 it is not already present."
1411 (with-package-graph ()
1412 (let* ((package (find-undeleted-package-or-lose package))
1413 (internal (package-internal-symbols package))
1414 (symbols (string-listify symbols))
1415 (lock-asserted-p nil))
1416 (flet ((present-p (w)
1417 (and w (not (eq w :inherited)))))
1418 (with-single-package-locked-error ()
1419 (dolist (name symbols)
1420 (multiple-value-bind (s w) (find-symbol name package)
1421 (unless (or lock-asserted-p
1422 (and (present-p w)
1423 (member s (package-shadowing-symbols package))))
1424 (assert-package-unlocked package "shadowing symbol~P ~{~A~^, ~}"
1425 (length symbols) symbols)
1426 (setf lock-asserted-p t))
1427 (unless (present-p w)
1428 (setq s (make-symbol name))
1429 (%set-symbol-package s package)
1430 (add-symbol internal s))
1431 (pushnew s (package-%shadowing-symbols package))))))))
1434 ;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
1435 (defun use-package (packages-to-use &optional (package (sane-package)))
1436 #!+sb-doc
1437 "Add all the PACKAGES-TO-USE to the use list for PACKAGE so that the
1438 external symbols of the used packages are accessible as internal symbols in
1439 PACKAGE."
1440 (with-package-graph ()
1441 (let ((packages (package-listify packages-to-use))
1442 (package (find-undeleted-package-or-lose package)))
1444 ;; Loop over each package, USE'ing one at a time...
1445 (with-single-package-locked-error ()
1446 (dolist (pkg packages)
1447 (unless (member pkg (package-%use-list package))
1448 (assert-package-unlocked package "using package~P ~{~A~^, ~}"
1449 (length packages) packages)
1450 (let ((shadowing-symbols (package-%shadowing-symbols package))
1451 (use-list (package-%use-list package)))
1453 ;; If the number of symbols already accessible is less
1454 ;; than the number to be inherited then it is faster to
1455 ;; run the test the other way. This is particularly
1456 ;; valuable in the case of a new package USEing
1457 ;; COMMON-LISP.
1458 (cond
1459 ((< (+ (package-internal-symbol-count package)
1460 (package-external-symbol-count package)
1461 (let ((res 0))
1462 (dolist (p use-list res)
1463 (incf res (package-external-symbol-count p)))))
1464 (package-external-symbol-count pkg))
1465 (do-symbols (sym package)
1466 (multiple-value-bind (s w)
1467 (find-external-symbol (symbol-name sym) pkg)
1468 (when (and w
1469 (not (eq s sym))
1470 (not (member sym shadowing-symbols)))
1471 (name-conflict package 'use-package pkg sym s))))
1472 (dolist (p use-list)
1473 (do-external-symbols (sym p)
1474 (multiple-value-bind (s w)
1475 (find-external-symbol (symbol-name sym) pkg)
1476 (when (and w
1477 (not (eq s sym))
1478 (not (member
1479 (find-symbol (symbol-name sym) package)
1480 shadowing-symbols)))
1481 (name-conflict package 'use-package pkg sym s))))))
1483 (do-external-symbols (sym pkg)
1484 (multiple-value-bind (s w)
1485 (find-symbol (symbol-name sym) package)
1486 (when (and w
1487 (not (eq s sym))
1488 (not (member s shadowing-symbols)))
1489 (name-conflict package 'use-package pkg sym s)))))))
1491 (push pkg (package-%use-list package))
1492 (setf (package-tables package)
1493 (let ((tbls (package-tables package)))
1494 (replace (make-array (1+ (length tbls))
1495 :initial-element (package-external-symbols pkg))
1496 tbls)))
1497 (push package (package-%used-by-list pkg)))))))
1500 (defun unuse-package (packages-to-unuse &optional (package (sane-package)))
1501 #!+sb-doc
1502 "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
1503 (with-package-graph ()
1504 (let ((package (find-undeleted-package-or-lose package))
1505 (packages (package-listify packages-to-unuse)))
1506 (with-single-package-locked-error ()
1507 (dolist (p packages)
1508 (when (member p (package-use-list package))
1509 (assert-package-unlocked package "unusing package~P ~{~A~^, ~}"
1510 (length packages) packages))
1511 (setf (package-%use-list package)
1512 (remove p (the list (package-%use-list package))))
1513 (setf (package-tables package)
1514 (delete (package-external-symbols p)
1515 (package-tables package)))
1516 (setf (package-%used-by-list p)
1517 (remove package (the list (package-%used-by-list p))))))
1518 t)))
1520 (defun find-all-symbols (string-or-symbol)
1521 #!+sb-doc
1522 "Return a list of all symbols in the system having the specified name."
1523 (let ((string (string string-or-symbol))
1524 (res ()))
1525 (with-package-names (names)
1526 (maphash (lambda (k v)
1527 (declare (ignore k))
1528 (multiple-value-bind (s w) (find-symbol string v)
1529 (when w (pushnew s res))))
1530 names))
1531 res))
1533 ;;;; APROPOS and APROPOS-LIST
1535 (defun briefly-describe-symbol (symbol)
1536 (fresh-line)
1537 (prin1 symbol)
1538 (when (boundp symbol)
1539 (write-string " (bound)"))
1540 (when (fboundp symbol)
1541 (write-string " (fbound)")))
1543 (defun apropos-list (string-designator
1544 &optional
1545 package-designator
1546 external-only)
1547 #!+sb-doc
1548 "Like APROPOS, except that it returns a list of the symbols found instead
1549 of describing them."
1550 (if package-designator
1551 (let ((package (find-undeleted-package-or-lose package-designator))
1552 (string (stringify-string-designator string-designator))
1553 (result nil))
1554 (do-symbols (symbol package)
1555 (when (and (or (not external-only)
1556 (and (eq (symbol-package symbol) package)
1557 (eq (nth-value 1 (find-symbol (symbol-name symbol)
1558 package))
1559 :external)))
1560 (search string (symbol-name symbol) :test #'char-equal))
1561 (pushnew symbol result)))
1562 (sort result #'string-lessp))
1563 (delete-duplicates
1564 (mapcan (lambda (package)
1565 (apropos-list string-designator package external-only))
1566 (sort (list-all-packages) #'string-lessp :key #'package-name)))))
1568 (defun apropos (string-designator &optional package external-only)
1569 #!+sb-doc
1570 "Briefly describe all symbols which contain the specified STRING.
1571 If PACKAGE is supplied then only describe symbols present in
1572 that package. If EXTERNAL-ONLY then only describe
1573 external symbols in the specified package."
1574 ;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
1575 ;; of some unnecessary consing; and the unnecessary consing shouldn't be an
1576 ;; issue, since this function is is only useful interactively anyway, and
1577 ;; we can cons and GC a lot faster than the typical user can read..
1578 (dolist (symbol (apropos-list string-designator package external-only))
1579 (briefly-describe-symbol symbol))
1580 (values))
1582 ;;;; final initialization
1584 ;;;; Due to the relative difficulty - but not impossibility - of manipulating
1585 ;;;; package-hashtables in the cross-compilation host, all interning operations
1586 ;;;; are delayed until cold-init.
1587 ;;;; The cold loader (GENESIS) set *!INITIAL-SYMBOLS* to the target
1588 ;;;; representation of the hosts's *COLD-PACKAGE-SYMBOLS*.
1589 ;;;; The shape of this list is ((package . (externals . internals)) ...)
1590 (defvar *!initial-symbols*)
1592 (defun !package-cold-init ()
1593 (setf *package-graph-lock* (sb!thread:make-mutex :name "Package Graph Lock")
1594 *package-names* (make-hash-table :test 'equal :synchronized t))
1595 (with-package-names (names)
1596 (dolist (spec *!initial-symbols*)
1597 (let ((pkg (car spec)) (symbols (cdr spec)))
1598 ;; the symbol MAKE-TABLE wouldn't magically disappear,
1599 ;; though its only use be to name an FLET in a function
1600 ;; hanging on an otherwise uninternable symbol. strange but true :-(
1601 (flet ((!make-table (input)
1602 (let ((table (make-package-hashtable
1603 (length (the simple-vector input)))))
1604 (dovector (symbol input table)
1605 (add-symbol table symbol)))))
1606 (setf (package-external-symbols pkg) (!make-table (car symbols))
1607 (package-internal-symbols pkg) (!make-table (cdr symbols))))
1608 (setf (package-%local-nicknames pkg) nil
1609 (package-%locally-nicknamed-by pkg) nil
1610 (package-source-location pkg) nil
1611 (gethash (package-%name pkg) names) pkg)
1612 (let ((nicks (package-%nicknames pkg)))
1613 (setf (package-%nicknames pkg) nil) ; each is pushed in again
1614 (%enter-new-nicknames pkg nicks))
1615 #!+sb-package-locks
1616 (setf (package-lock pkg) nil
1617 (package-%implementation-packages pkg) nil))))
1619 ;; pass 2 - set the 'tables' slots only after all tables have been made
1620 (dolist (spec *!initial-symbols*)
1621 (let ((pkg (car spec)))
1622 (setf (package-tables pkg)
1623 (map 'vector #'package-external-symbols (package-%use-list pkg)))))
1625 (/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*")
1626 (%makunbound '*!initial-symbols*) ; (so that it gets GCed)
1628 ;; For the kernel core image wizards, set the package to *CL-PACKAGE*.
1630 ;; FIXME: We should just set this to (FIND-PACKAGE
1631 ;; "COMMON-LISP-USER") once and for all here, instead of setting it
1632 ;; once here and resetting it later.
1633 (setq *package* *cl-package*))
1635 ;;; support for WITH-PACKAGE-ITERATOR
1637 (defun package-iter-init (access-types pkg-designator-list)
1638 (declare (type (integer 1 7) access-types)) ; a nonzero bitmask over types
1639 (values (logior (ash access-types 3) #b11) 0 #()
1640 (package-listify pkg-designator-list)))
1642 ;; The STATE parameter is comprised of 4 packed fields
1643 ;; [0:1] = substate {0=internal,1=external,2=inherited,3=initial}
1644 ;; [2] = package with inherited symbols has shadowing symbols
1645 ;; [3:5] = enabling bits for {internal,external,inherited}
1646 ;; [6:] = index into 'package-tables'
1648 (defconstant +package-iter-check-shadows+ #b000100)
1650 (defun package-iter-step (start-state index sym-vec pkglist)
1651 ;; the defknown isn't enough
1652 (declare (type fixnum start-state) (type index index)
1653 (type simple-vector sym-vec) (type list pkglist))
1654 (declare (optimize speed))
1655 (labels
1656 ((advance (state) ; STATE is the one just completed
1657 (case (logand state #b11)
1658 ;; Test :INHERITED first because the state repeats for a package
1659 ;; as many times as there are packages it uses. There are enough
1660 ;; bits to count up to 2^23 packages if fixnums are 30 bits.
1662 (when (desired-state-p 2)
1663 (let* ((tables (package-tables (this-package)))
1664 (next-state (the fixnum (+ state (ash 1 6))))
1665 (table-idx (ash next-state -6)))
1666 (when (< table-idx (length tables))
1667 (return-from advance ; remain in state 2
1668 (start next-state (svref tables table-idx))))))
1669 (pop pkglist)
1670 (advance 3)) ; start on next package
1671 (1 ; finished externals, switch to inherited if desired
1672 (when (desired-state-p 2)
1673 (let ((tables (package-tables (this-package))))
1674 (when (plusp (length tables)) ; inherited symbols
1675 (return-from advance ; enter state 2
1676 (start (if (package-%shadowing-symbols (this-package))
1677 (logior 2 +package-iter-check-shadows+) 2)
1678 (svref tables 0))))))
1679 (advance 2)) ; skip state 2
1680 (0 ; finished internals, switch to externals if desired
1681 (if (desired-state-p 1) ; enter state 1
1682 (start 1 (package-external-symbols (this-package)))
1683 (advance 1))) ; skip state 1
1684 (t ; initial state
1685 (cond ((endp pkglist) ; latch into returning NIL forever more
1686 (values 0 0 #() '() nil nil))
1687 ((desired-state-p 0) ; enter state 0
1688 (start 0 (package-internal-symbols (this-package))))
1689 (t (advance 0)))))) ; skip state 0
1690 (desired-state-p (target-state)
1691 (logtest start-state (ash 1 (+ target-state 3))))
1692 (this-package ()
1693 (truly-the package (car pkglist)))
1694 (start (next-state new-table)
1695 (let ((symbols (package-hashtable-cells new-table)))
1696 (package-iter-step (logior (mask-field (byte 3 3) start-state)
1697 next-state)
1698 ;; assert that physical length was nonzero
1699 (the index (1- (length symbols)))
1700 symbols pkglist))))
1701 (declare (inline desired-state-p this-package))
1702 (if (zerop index)
1703 (advance start-state)
1704 (macrolet ((scan (&optional (guard t))
1705 `(loop
1706 (let ((sym (aref sym-vec (decf index))))
1707 (when (and (pkg-symbol-valid-p sym) ,guard)
1708 (return (values start-state index sym-vec pkglist sym
1709 (aref #(:internal :external :inherited)
1710 (logand start-state 3))))))
1711 (when (zerop index)
1712 (return (advance start-state))))))
1713 (declare #-sb-xc-host(optimize (sb!c::insert-array-bounds-checks 0)))
1714 (if (logtest start-state +package-iter-check-shadows+)
1715 (let ((shadows (package-%shadowing-symbols (this-package))))
1716 (scan (not (member sym shadows :test #'string=))))
1717 (scan))))))
1719 (defun program-assert-symbol-home-package-unlocked (context symbol control)
1720 #!-sb-package-locks
1721 (declare (ignore context symbol control))
1722 #!+sb-package-locks
1723 (handler-bind ((package-lock-violation
1724 (lambda (condition)
1725 (ecase context
1726 (:compile
1727 (warn "Compile-time package lock violation:~% ~A"
1728 condition)
1729 (sb!c:compiler-error condition))
1730 (:eval
1731 (eval-error condition))))))
1732 (with-single-package-locked-error (:symbol symbol control))))