Declare EXPLICIT-CHECK on CONCATENATE, MAKE-STRING, SET-PPRINT-DISPATCH.
[sbcl.git] / src / code / target-package.lisp
blobabd2145d7f5c084b65f7c6cf1186f2419a4390d9
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 (mapcar #'find-undeleted-package-or-lose (ensure-list thing)))
585 ;;; ANSI specifies (in the definition of DELETE-PACKAGE) that PACKAGE-NAME
586 ;;; returns NIL (not an error) for a deleted package, so this is a special
587 ;;; case where we want to use bare %FIND-PACKAGE-OR-LOSE instead of
588 ;;; FIND-UNDELETED-PACKAGE-OR-LOSE.
589 (defun package-name (package-designator)
590 (package-%name (%find-package-or-lose package-designator)))
592 ;;;; operations on package hashtables
594 ;;; Compute a number between 1 and 255 based on the sxhash of the
595 ;;; pname and the length thereof.
596 (declaim (inline entry-hash))
597 (defun entry-hash (length sxhash)
598 (declare (index length) ((and fixnum unsigned-byte) sxhash))
599 (1+ (rem (logxor length sxhash
600 (ash sxhash -8) (ash sxhash -16) (ash sxhash -19))
601 255)))
603 ;;; Add a symbol to a package hashtable. The symbol is assumed
604 ;;; not to be present.
605 (defun add-symbol (table symbol)
606 (when (zerop (package-hashtable-free table))
607 ;; The hashtable is full. Resize it to be able to hold twice the
608 ;; amount of symbols than it currently contains. The actual new size
609 ;; can be smaller than twice the current size if the table contained
610 ;; deleted entries.
611 (resize-package-hashtable table
612 (* (- (package-hashtable-size table)
613 (package-hashtable-deleted table))
614 2)))
615 (let* ((symvec (package-hashtable-cells table))
616 (len (1- (length symvec)))
617 (hashvec (the hash-vector (aref symvec len)))
618 (sxhash (truly-the fixnum (ensure-symbol-hash symbol)))
619 (h2 (1+ (rem sxhash (- len 2)))))
620 (declare (fixnum sxhash h2))
621 (do ((i (rem sxhash len) (rem (+ i h2) len)))
622 ((eql (aref hashvec i) 0)
623 (if (eql (svref symvec i) 0)
624 (decf (package-hashtable-free table))
625 (decf (package-hashtable-deleted table)))
626 ;; This order of these two SETFs does not matter.
627 ;; An empty symbol cell is skipped on lookup if the hash cell
628 ;; matches something accidentally. "empty" = any fixnum.
629 (setf (svref symvec i) symbol)
630 (setf (aref hashvec i)
631 (entry-hash (length (symbol-name symbol)) sxhash)))
632 (declare (fixnum i)))))
634 ;;; Resize the package hashtables of all packages so that their load
635 ;;; factor is *PACKAGE-HASHTABLE-IMAGE-LOAD-FACTOR*. Called from
636 ;;; SAVE-LISP-AND-DIE to optimize space usage in the image.
637 (defun tune-hashtable-sizes-of-all-packages ()
638 (flet ((tune-table-size (table)
639 (resize-package-hashtable
640 table
641 (round (* (/ *package-rehash-threshold*
642 *package-hashtable-image-load-factor*)
643 (- (package-hashtable-size table)
644 (package-hashtable-free table)
645 (package-hashtable-deleted table)))))))
646 (dolist (package (list-all-packages))
647 (tune-table-size (package-internal-symbols package))
648 (tune-table-size (package-external-symbols package)))))
650 ;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR
651 ;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR
652 ;;; is bound to the symbol. LENGTH and HASH are the length and sxhash
653 ;;; of STRING. ENTRY-HASH is the entry-hash of the string and length.
654 ;;; If the symbol is found, then FORMS are executed; otherwise not.
656 (defmacro with-symbol (((symbol-var &optional (index-var (gensym))) table
657 string length sxhash entry-hash) &body forms)
658 (with-unique-names (vec len hash-vec h2 probed-ehash name)
659 `(let* ((,vec (package-hashtable-cells ,table))
660 (,len (1- (length ,vec)))
661 (,hash-vec (the hash-vector (svref ,vec ,len)))
662 (,index-var (rem (the hash ,sxhash) ,len))
663 (,h2 (1+ (the index (rem (the hash ,sxhash)
664 (the index (- ,len 2)))))))
665 (declare (type index ,len ,h2 ,index-var))
666 (loop
667 (let ((,probed-ehash (aref ,hash-vec ,index-var)))
668 (cond
669 ((eql ,probed-ehash ,entry-hash)
670 (let ((,symbol-var (truly-the symbol (svref ,vec ,index-var))))
671 (when (eq (symbol-hash ,symbol-var) ,sxhash)
672 (let ((,name (symbol-name ,symbol-var)))
673 ;; The pre-test for length is kind of an unimportant
674 ;; optimization, but passing it for both :end arguments
675 ;; requires that it be within bounds for the probed symbol.
676 (when (and (= (length ,name) ,length)
677 (string= ,string ,name
678 :end1 ,length :end2 ,length))
679 (return (progn ,@forms)))))))
680 ((eql ,probed-ehash 0)
681 ;; either a never used cell or a tombstone left by UNINTERN
682 (when (eql (svref ,vec ,index-var) 0) ; really never used
683 (return)))))
684 (when (>= (incf ,index-var ,h2) ,len)
685 (decf ,index-var ,len))))))
687 ;;; Delete the entry for STRING in TABLE. The entry must exist.
688 ;;; Deletion stores -1 for the symbol and 0 for the hash tombstone.
689 ;;; Storing NIL for the symbol, as used to be done, is vulnerable to a rare
690 ;;; concurrency bug because many strings have the same ENTRY-HASH as NIL:
691 ;;; (entry-hash 3 (sxhash "NIL")) => 177 and
692 ;;; (entry-hash 2 (sxhash "3M")) => 177
693 ;;; Suppose, in the former approach, that "3M" is interned,
694 ;;; and then the following sequence of events occur:
695 ;;; - thread 1 performs FIND-SYMBOL on "NIL", hits the hash code at
696 ;;; at index I, and is then context-switched out. When this thread
697 ;; resumes, it assumes a valid symbol in (SVREF SYM-VEC I)
698 ;;; - thread 2 uninterns "3M" and fully completes that, storing NIL
699 ;;; in the slot for the symbol that thread 1 will next read.
700 ;;; - thread 1 continues, and test for STRING= to NIL,
701 ;;; wrongly seeing NIL as a present symbol.
702 ;;; It's possible this is harmless, because NIL is usually inherited from CL,
703 ;;; but if the secondary return value mattered to the application,
704 ;;; it is probably wrong. And of course if NIL was not intended to be found
705 ;;; - as in a package that does not use CL - then finding NIL at all is wrong.
706 ;;; The better approach is to treat 'hash' as purely a heuristic without
707 ;;; ill-effect from false positives. No barrier, nor read-consistency
708 ;;; check is required, since a symbol is both its key and value,
709 ;;; and the "absence of a symbol" marker is never mistaken for a symbol.
711 (defun nuke-symbol (table symbol)
712 (let* ((string (symbol-name symbol))
713 (length (length string))
714 (hash (symbol-hash symbol))
715 (ehash (entry-hash length hash)))
716 (declare (type index length)
717 (type hash hash))
718 (with-symbol ((symbol index) table string length hash ehash)
719 ;; It is suboptimal to grab the vectors again, but not broken,
720 ;; because we have exclusive use of the table for writing.
721 (let* ((symvec (package-hashtable-cells table))
722 (hashvec (the hash-vector (aref symvec (1- (length symvec))))))
723 (setf (aref hashvec index) 0)
724 (setf (aref symvec index) -1)) ; any nonzero fixnum will do
725 (incf (package-hashtable-deleted table))))
726 ;; If the table is less than one quarter full, halve its size and
727 ;; rehash the entries.
728 (let* ((size (package-hashtable-size table))
729 (deleted (package-hashtable-deleted table))
730 (used (- size
731 (package-hashtable-free table)
732 deleted)))
733 (declare (type fixnum size deleted used))
734 (when (< used (truncate size 4))
735 (resize-package-hashtable table (* used 2)))))
737 ;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*. If there is a
738 ;;; conflict then give the user a chance to do something about it. Caller is
739 ;;; responsible for having acquired the mutex via WITH-PACKAGES.
740 (defun %enter-new-nicknames (package nicknames)
741 (declare (type list nicknames))
742 (dolist (n nicknames)
743 (let* ((n (stringify-package-designator n))
744 (found (with-package-names (names)
745 (or (gethash n names)
746 (progn
747 (setf (gethash n names) package)
748 (push n (package-%nicknames package))
749 package)))))
750 (cond ((eq found package))
751 ((string= (the string (package-%name found)) n)
752 (signal-package-cerror
753 package
754 "Ignore this nickname."
755 "~S is a package name, so it cannot be a nickname for ~S."
756 n (package-%name package)))
758 (signal-package-cerror
759 package
760 "Leave this nickname alone."
761 "~S is already a nickname for ~S."
762 n (package-%name found)))))))
764 (defun make-package (name &key
765 (use '#.*default-package-use-list*)
766 nicknames
767 (internal-symbols 10)
768 (external-symbols 10))
769 #!+sb-doc
770 #.(format nil
771 "Make a new package having the specified NAME, NICKNAMES, and USE
772 list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are estimates for the number of
773 internal and external symbols which will ultimately be present in the package.
774 The default value of USE is implementation-dependent, and in this
775 implementation it is ~S." *default-package-use-list*)
776 (prog (clobber)
777 :restart
778 (when (find-package name)
779 ;; ANSI specifies that this error is correctable.
780 (signal-package-cerror
781 name
782 "Clobber existing package."
783 "A package named ~S already exists" name)
784 (setf clobber t))
785 (with-package-graph ()
786 ;; Check for race, signal the error outside the lock.
787 (when (and (not clobber) (find-package name))
788 (go :restart))
789 (let* ((name (stringify-package-designator name))
790 (package
791 (%make-package
792 name
793 (make-package-hashtable internal-symbols)
794 (make-package-hashtable external-symbols))))
796 ;; Do a USE-PACKAGE for each thing in the USE list so that checking for
797 ;; conflicting exports among used packages is done.
798 (use-package use package)
800 ;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
801 ;; which would leave us with possibly-bad side effects from the earlier
802 ;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
803 ;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
804 ;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
805 ;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
806 ;; USE-PACKAGE, too.
807 (%enter-new-nicknames package nicknames)
808 (return (setf (gethash name *package-names*) package))))
809 (bug "never")))
811 ;;; Change the name if we can, blast any old nicknames and then
812 ;;; add in any new ones.
814 ;;; FIXME: ANSI claims that NAME is a package designator (not just a
815 ;;; string designator -- weird). Thus, NAME could
816 ;;; be a package instead of a string. Presumably then we should not change
817 ;;; the package name if NAME is the same package that's referred to by PACKAGE.
818 ;;; If it's a *different* package, we should probably signal an error.
819 ;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
820 (defun rename-package (package-designator name &optional (nicknames ()))
821 #!+sb-doc
822 "Changes the name and nicknames for a package."
823 (prog () :restart
824 (let ((package (find-undeleted-package-or-lose package-designator))
825 (name (stringify-package-designator name))
826 (found (find-package name))
827 (nicks (mapcar #'string nicknames)))
828 (unless (or (not found) (eq found package))
829 (signal-package-error name
830 "A package named ~S already exists." name))
831 (with-single-package-locked-error ()
832 (unless (and (string= name (package-name package))
833 (null (set-difference nicks (package-nicknames package)
834 :test #'string=)))
835 (assert-package-unlocked package "rename as ~A~@[ with nickname~P ~
836 ~{~A~^, ~}~]"
837 name (length nicks) nicks))
838 (with-package-names (names)
839 ;; Check for race conditions now that we have the lock.
840 (unless (eq package (find-package package-designator))
841 (go :restart))
842 ;; Do the renaming.
843 (remhash (package-%name package) names)
844 (dolist (n (package-%nicknames package))
845 (remhash n names))
846 (setf (package-%name package) name
847 (gethash name names) package
848 (package-%nicknames package) ()))
849 (%enter-new-nicknames package nicknames))
850 (return package))))
852 (defun delete-package (package-designator)
853 #!+sb-doc
854 "Delete the package designated by PACKAGE-DESIGNATOR from the package
855 system data structures."
856 (tagbody :restart
857 (let ((package (find-package package-designator)))
858 (cond ((not package)
859 ;; This continuable error is required by ANSI.
860 (signal-package-cerror
861 package-designator
862 "Ignore."
863 "There is no package named ~S." package-designator)
864 (return-from delete-package nil))
865 ((not (package-name package)) ; already deleted
866 (return-from delete-package nil))
868 (with-single-package-locked-error
869 (:package package "deleting package ~A" package)
870 (let ((use-list (package-used-by-list package)))
871 (when use-list
872 ;; This continuable error is specified by ANSI.
873 (signal-package-cerror
874 package
875 "Remove dependency in other packages."
876 "~@<Package ~S is used by package~P:~2I~_~S~@:>"
877 (package-name package)
878 (length use-list)
879 (mapcar #'package-name use-list))
880 (dolist (p use-list)
881 (unuse-package package p))))
882 #!+sb-package-locks
883 (dolist (p (package-implements-list package))
884 (remove-implementation-package package p))
885 (with-package-graph ()
886 ;; Check for races, restart if necessary.
887 (let ((package2 (find-package package-designator)))
888 (when (or (neq package package2) (package-used-by-list package2))
889 (go :restart)))
890 (dolist (used (package-use-list package))
891 (unuse-package used package))
892 (dolist (namer (package-%locally-nicknamed-by package))
893 (setf (package-%local-nicknames namer)
894 (delete package (package-%local-nicknames namer) :key #'cdr)))
895 (setf (package-%locally-nicknamed-by package) nil)
896 (dolist (cell (package-%local-nicknames package))
897 (let ((actual (cdr cell)))
898 (setf (package-%locally-nicknamed-by actual)
899 (delete package (package-%locally-nicknamed-by actual)))))
900 (setf (package-%local-nicknames package) nil)
901 ;; FIXME: lacking a way to advise UNINTERN that this package
902 ;; is pending deletion, a large package conses successively
903 ;; many smaller tables for no good reason.
904 (do-symbols (sym package)
905 (unintern sym package))
906 (with-package-names (names)
907 (remhash (package-name package) names)
908 (dolist (nick (package-nicknames package))
909 (remhash nick names))
910 (setf (package-%name package) nil
911 ;; Setting PACKAGE-%NAME to NIL is required in order to
912 ;; make PACKAGE-NAME return NIL for a deleted package as
913 ;; ANSI requires. Setting the other slots to NIL
914 ;; and blowing away the PACKAGE-HASHTABLES is just done
915 ;; for tidiness and to help the GC.
916 (package-%nicknames package) nil))
917 (setf (package-%use-list package) nil
918 (package-tables package) #()
919 (package-%shadowing-symbols package) nil
920 (package-internal-symbols package)
921 (make-package-hashtable 0)
922 (package-external-symbols package)
923 (make-package-hashtable 0)))
924 (return-from delete-package t)))))))
926 (defun list-all-packages ()
927 #!+sb-doc
928 "Return a list of all existing packages."
929 (let ((res ()))
930 (with-package-names (names)
931 (maphash (lambda (k v)
932 (declare (ignore k))
933 (pushnew v res :test #'eq))
934 names))
935 res))
937 (macrolet ((find/intern (function &rest more-args)
938 ;; Both %FIND-SYMBOL and %INTERN require a SIMPLE-STRING,
939 ;; but accept a LENGTH. Given a non-simple string,
940 ;; we need copy it only if the cumulative displacement
941 ;; into the underlying simple-string is nonzero.
942 ;; There are two things that can be improved
943 ;; about the generated code here:
944 ;; 1. if X is known to satisfy STRINGP (generally any rank-1 array),
945 ;; then testing SIMPLE-<base|character>-STRING-P should not
946 ;; re-test the lowtag. This is constrained by the backends,
947 ;; because there are no type vops that assume a known lowtag.
948 ;; 2. if X is known to satisfy VECTORP, then
949 ;; (NOT (ARRAY-HEADER-P)) implies SIMPLE-P, but the compiler
950 ;; does not actually know that, and generates a check.
951 ;; This is more of a front-end issue.
952 `(multiple-value-bind (name length)
953 (if (simple-string-p name)
954 (values name (length name))
955 (with-array-data ((name name) (start) (end)
956 :check-fill-pointer t)
957 (if (eql start 0)
958 (values name end)
959 (values (subseq name start end)
960 (- end start)))))
961 (truly-the
962 (values symbol (member :internal :external :inherited nil))
963 (,function name length
964 (find-undeleted-package-or-lose package)
965 ,@more-args)))))
967 (defun intern (name &optional (package (sane-package)))
968 #!+sb-doc
969 "Return a symbol in PACKAGE having the specified NAME, creating it
970 if necessary."
971 (find/intern %intern t))
973 (defun find-symbol (name &optional (package (sane-package)))
974 #!+sb-doc
975 "Return the symbol named STRING in PACKAGE. If such a symbol is found
976 then the second value is :INTERNAL, :EXTERNAL or :INHERITED to indicate
977 how the symbol is accessible. If no symbol is found then both values
978 are NIL."
979 (find/intern %find-symbol)))
981 ;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
982 ;;; then create it, special-casing the keyword package.
983 (defun %intern (name length package copy-p)
984 (declare (simple-string name) (index length))
985 (multiple-value-bind (symbol where) (%find-symbol name length package)
986 (cond (where
987 (values symbol where))
989 ;; Let's try again with a lock: the common case has the
990 ;; symbol already interned, handled by the first leg of the
991 ;; COND, but in case another thread is interning in
992 ;; parallel we need to check after grabbing the lock.
993 (with-package-graph ()
994 (setf (values symbol where) (%find-symbol name length package))
995 (if where
996 (values symbol where)
997 (let ((symbol-name (cond ((not copy-p)
998 (aver (= (length name) length))
999 name)
1000 ((typep name '(simple-array nil (*)))
1003 ;; This so that SUBSEQ is inlined,
1004 ;; because we need it fixed for cold init.
1005 (string-dispatch
1006 ((simple-array base-char (*))
1007 (simple-array character (*)))
1008 name
1009 (declare (optimize speed))
1010 (subseq name 0 length))))))
1011 (with-single-package-locked-error
1012 (:package package "interning ~A" symbol-name)
1013 (let ((symbol (make-symbol symbol-name)))
1014 (add-symbol (cond ((eq package *keyword-package*)
1015 (%set-symbol-value symbol symbol)
1016 (package-external-symbols package))
1018 (package-internal-symbols package)))
1019 symbol)
1020 (%set-symbol-package symbol package)
1021 (values symbol nil))))))))))
1023 ;;; Check internal and external symbols, then scan down the list
1024 ;;; of hashtables for inherited symbols.
1025 (defun %find-symbol (string length package)
1026 (declare (simple-string string)
1027 (type index length))
1028 (let* ((hash (compute-symbol-hash string length))
1029 (ehash (entry-hash length hash)))
1030 (declare (type hash hash ehash))
1031 (with-symbol ((symbol) (package-internal-symbols package)
1032 string length hash ehash)
1033 (return-from %find-symbol (values symbol :internal)))
1034 (with-symbol ((symbol) (package-external-symbols package)
1035 string length hash ehash)
1036 (return-from %find-symbol (values symbol :external)))
1037 (let* ((tables (package-tables package))
1038 (n (length tables)))
1039 (unless (eql n 0)
1040 ;; Try the most-recently-used table, then others.
1041 ;; TABLES is treated as circular for this purpose.
1042 (let* ((mru (package-mru-table-index package))
1043 (start (if (< mru n) mru 0))
1044 (i start))
1045 (loop
1046 (with-symbol ((symbol) (locally (declare (optimize (safety 0)))
1047 (svref tables i))
1048 string length hash ehash)
1049 (setf (package-mru-table-index package) i)
1050 (return-from %find-symbol (values symbol :inherited)))
1051 (if (< (decf i) 0) (setq i (1- n)))
1052 (if (= i start) (return)))))))
1053 (values nil nil))
1055 ;;; Similar to FIND-SYMBOL, but only looks for an external symbol.
1056 ;;; Return the symbol and T if found, otherwise two NILs.
1057 ;;; This is used for fast name-conflict checking in this file and symbol
1058 ;;; printing in the printer.
1059 ;;; An optimization is possible here: by accepting either a string or symbol,
1060 ;;; if the symbol's hash slot is nonzero, we can avoid COMPUTE-SYMBOL-HASH.
1061 (defun find-external-symbol (string package)
1062 (declare (simple-string string))
1063 (let* ((length (length string))
1064 (hash (compute-symbol-hash string length))
1065 (ehash (entry-hash length hash)))
1066 (declare (type index length)
1067 (type hash hash))
1068 (with-symbol ((symbol) (package-external-symbols package)
1069 string length hash ehash)
1070 (return-from find-external-symbol (values symbol t))))
1071 (values nil nil))
1073 (define-condition name-conflict (reference-condition package-error)
1074 ((function :initarg :function :reader name-conflict-function)
1075 (datum :initarg :datum :reader name-conflict-datum)
1076 (symbols :initarg :symbols :reader name-conflict-symbols))
1077 (:default-initargs :references (list '(:ansi-cl :section (11 1 1 2 5))))
1078 (:report
1079 (lambda (c s)
1080 (format s "~@<~S ~S causes name-conflicts in ~S between the ~
1081 following symbols:~2I~@:_~
1082 ~{~/sb-impl::print-symbol-with-prefix/~^, ~}~:@>"
1083 (name-conflict-function c)
1084 (name-conflict-datum c)
1085 (package-error-package c)
1086 (name-conflict-symbols c)))))
1088 (defun name-conflict (package function datum &rest symbols)
1089 (flet ((importp (c)
1090 (declare (ignore c))
1091 (eq 'import function))
1092 (use-or-export-p (c)
1093 (declare (ignore c))
1094 (or (eq 'use-package function)
1095 (eq 'export function)))
1096 (old-symbol ()
1097 (car (remove datum symbols))))
1098 (let ((pname (package-name package)))
1099 (restart-case
1100 (error 'name-conflict :package package :symbols symbols
1101 :function function :datum datum)
1102 ;; USE-PACKAGE and EXPORT
1103 (keep-old ()
1104 :report (lambda (s)
1105 (ecase function
1106 (export
1107 (format s "Keep ~S accessible in ~A (shadowing ~S)."
1108 (old-symbol) pname datum))
1109 (use-package
1110 (format s "Keep symbols already accessible ~A (shadowing others)."
1111 pname))))
1112 :test use-or-export-p
1113 (dolist (s (remove-duplicates symbols :test #'string=))
1114 (shadow (symbol-name s) package)))
1115 (take-new ()
1116 :report (lambda (s)
1117 (ecase function
1118 (export
1119 (format s "Make ~S accessible in ~A (uninterning ~S)."
1120 datum pname (old-symbol)))
1121 (use-package
1122 (format s "Make newly exposed symbols accessible in ~A, ~
1123 uninterning old ones."
1124 pname))))
1125 :test use-or-export-p
1126 (dolist (s symbols)
1127 (when (eq s (find-symbol (symbol-name s) package))
1128 (unintern s package))))
1129 ;; IMPORT
1130 (shadowing-import-it ()
1131 :report (lambda (s)
1132 (format s "Shadowing-import ~S, uninterning ~S."
1133 datum (old-symbol)))
1134 :test importp
1135 (shadowing-import datum package))
1136 (dont-import-it ()
1137 :report (lambda (s)
1138 (format s "Don't import ~S, keeping ~S."
1139 datum
1140 (car (remove datum symbols))))
1141 :test importp)
1142 ;; General case. This is exposed via SB-EXT.
1143 (resolve-conflict (chosen-symbol)
1144 :report "Resolve conflict."
1145 :interactive
1146 (lambda ()
1147 (let* ((len (length symbols))
1148 (nlen (length (write-to-string len :base 10)))
1149 (*print-pretty* t))
1150 (format *query-io* "~&~@<Select a symbol to be made accessible in ~
1151 package ~A:~2I~@:_~{~{~V,' D. ~
1152 ~/sb-impl::print-symbol-with-prefix/~}~@:_~}~
1153 ~@:>"
1154 (package-name package)
1155 (loop for s in symbols
1156 for i upfrom 1
1157 collect (list nlen i s)))
1158 (loop
1159 (format *query-io* "~&Enter an integer (between 1 and ~D): " len)
1160 (finish-output *query-io*)
1161 (let ((i (parse-integer (read-line *query-io*) :junk-allowed t)))
1162 (when (and i (<= 1 i len))
1163 (return (list (nth (1- i) symbols))))))))
1164 (multiple-value-bind (package-symbol status)
1165 (find-symbol (symbol-name chosen-symbol) package)
1166 (let* ((accessiblep status) ; never NIL here
1167 (presentp (and accessiblep
1168 (not (eq :inherited status)))))
1169 (ecase function
1170 ((unintern)
1171 (if presentp
1172 (if (eq package-symbol chosen-symbol)
1173 (shadow (list package-symbol) package)
1174 (shadowing-import (list chosen-symbol) package))
1175 (shadowing-import (list chosen-symbol) package)))
1176 ((use-package export)
1177 (if presentp
1178 (if (eq package-symbol chosen-symbol)
1179 (shadow (list package-symbol) package) ; CLHS 11.1.1.2.5
1180 (if (eq (symbol-package package-symbol) package)
1181 (unintern package-symbol package) ; CLHS 11.1.1.2.5
1182 (shadowing-import (list chosen-symbol) package)))
1183 (shadowing-import (list chosen-symbol) package)))
1184 ((import)
1185 (if presentp
1186 (if (eq package-symbol chosen-symbol)
1187 nil ; re-importing the same symbol
1188 (shadowing-import (list chosen-symbol) package))
1189 (shadowing-import (list chosen-symbol) package)))))))))))
1191 ;;; If we are uninterning a shadowing symbol, then a name conflict can
1192 ;;; result, otherwise just nuke the symbol.
1193 (defun unintern (symbol &optional (package (sane-package)))
1194 #!+sb-doc
1195 "Makes SYMBOL no longer present in PACKAGE. If SYMBOL was present then T is
1196 returned, otherwise NIL. If PACKAGE is SYMBOL's home package, then it is made
1197 uninterned."
1198 (with-package-graph ()
1199 (let* ((package (find-undeleted-package-or-lose package))
1200 (name (symbol-name symbol))
1201 (shadowing-symbols (package-%shadowing-symbols package)))
1202 (declare (list shadowing-symbols))
1204 (with-single-package-locked-error ()
1205 (when (find-symbol name package)
1206 (assert-package-unlocked package "uninterning ~A" name))
1208 ;; If a name conflict is revealed, give us a chance to
1209 ;; shadowing-import one of the accessible symbols.
1210 (when (member symbol shadowing-symbols)
1211 (let ((cset ()))
1212 (dolist (p (package-%use-list package))
1213 (multiple-value-bind (s w) (find-external-symbol name p)
1214 ;; S should be derived as SYMBOL so that PUSHNEW can assume #'EQ
1215 ;; as the test, but it's not happening, so restate the obvious.
1216 (when w (pushnew s cset :test #'eq))))
1217 (when (cdr cset)
1218 (apply #'name-conflict package 'unintern symbol cset)
1219 (return-from unintern t)))
1220 (setf (package-%shadowing-symbols package)
1221 (remove symbol shadowing-symbols)))
1223 (multiple-value-bind (s w) (find-symbol name package)
1224 (cond ((not (eq symbol s)) nil)
1225 ((or (eq w :internal) (eq w :external))
1226 (nuke-symbol (if (eq w :internal)
1227 (package-internal-symbols package)
1228 (package-external-symbols package))
1229 symbol)
1230 (if (eq (symbol-package symbol) package)
1231 (%set-symbol-package symbol nil))
1233 (t nil)))))))
1235 ;;; Take a symbol-or-list-of-symbols and return a list, checking types.
1236 (defun symbol-listify (thing)
1237 (cond ((listp thing)
1238 (dolist (s thing)
1239 (unless (symbolp s)
1240 (signal-package-error nil
1241 "~S is not a symbol." s)))
1242 thing)
1243 ((symbolp thing) (list thing))
1245 (signal-package-error nil
1246 "~S is neither a symbol nor a list of symbols."
1247 thing))))
1249 (defun string-listify (thing)
1250 (mapcar #'string (ensure-list thing)))
1252 (defun export (symbols &optional (package (sane-package)))
1253 #!+sb-doc
1254 "Exports SYMBOLS from PACKAGE, checking that no name conflicts result."
1255 (with-package-graph ()
1256 (let ((package (find-undeleted-package-or-lose package))
1257 (symbols (symbol-listify symbols))
1258 (syms ()))
1259 ;; Punt any symbols that are already external.
1260 (dolist (sym symbols)
1261 (multiple-value-bind (s found)
1262 (find-external-symbol (symbol-name sym) package)
1263 (unless (or (and found (eq s sym)) (member sym syms))
1264 (push sym syms))))
1265 (with-single-package-locked-error ()
1266 (when syms
1267 (assert-package-unlocked package "exporting symbol~P ~{~A~^, ~}"
1268 (length syms) syms))
1269 ;; Find symbols and packages with conflicts.
1270 (let ((used-by (package-%used-by-list package)))
1271 (dolist (sym syms)
1272 (let ((name (symbol-name sym)))
1273 (dolist (p used-by)
1274 (multiple-value-bind (s w) (find-symbol name p)
1275 (when (and w
1276 (not (eq s sym))
1277 (not (member s (package-%shadowing-symbols p))))
1278 ;; Beware: the name conflict is in package P, not in
1279 ;; PACKAGE.
1280 (name-conflict p 'export sym sym s)))))))
1281 ;; Check that all symbols are accessible. If not, ask to import them.
1282 (let ((missing ())
1283 (imports ()))
1284 (dolist (sym syms)
1285 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1286 (cond ((not (and w (eq s sym)))
1287 (push sym missing))
1288 ((eq w :inherited)
1289 (push sym imports)))))
1290 (when missing
1291 (signal-package-cerror
1292 package
1293 (format nil "~S these symbols into the ~A package."
1294 'import (package-%name package))
1295 "~@<These symbols are not accessible in the ~A package:~2I~_~S~@:>"
1296 (package-%name package) missing)
1297 (import missing package))
1298 (import imports package))
1300 ;; And now, three pages later, we export the suckers.
1301 (let ((internal (package-internal-symbols package))
1302 (external (package-external-symbols package)))
1303 (dolist (sym syms)
1304 (add-symbol external sym)
1305 (nuke-symbol internal sym))))
1306 t)))
1308 ;;; Check that all symbols are accessible, then move from external to internal.
1309 (defun unexport (symbols &optional (package (sane-package)))
1310 #!+sb-doc
1311 "Makes SYMBOLS no longer exported from PACKAGE."
1312 (with-package-graph ()
1313 (let ((package (find-undeleted-package-or-lose package))
1314 (symbols (symbol-listify symbols))
1315 (syms ()))
1316 (dolist (sym symbols)
1317 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1318 (cond ((or (not w) (not (eq s sym)))
1319 (signal-package-error
1320 package
1321 "~S is not accessible in the ~A package."
1322 sym (package-%name package)))
1323 ((eq w :external) (pushnew sym syms)))))
1324 (with-single-package-locked-error ()
1325 (when syms
1326 (assert-package-unlocked package "unexporting symbol~P ~{~A~^, ~}"
1327 (length syms) syms))
1328 (let ((internal (package-internal-symbols package))
1329 (external (package-external-symbols package)))
1330 (dolist (sym syms)
1331 (add-symbol internal sym)
1332 (nuke-symbol external sym))))
1333 t)))
1335 ;;; Check for name conflict caused by the import and let the user
1336 ;;; shadowing-import if there is.
1337 (defun import (symbols &optional (package (sane-package)))
1338 #!+sb-doc
1339 "Make SYMBOLS accessible as internal symbols in PACKAGE. If a symbol is
1340 already accessible then it has no effect. If a name conflict would result from
1341 the importation, then a correctable error is signalled."
1342 (with-package-graph ()
1343 (let* ((package (find-undeleted-package-or-lose package))
1344 (symbols (symbol-listify symbols))
1345 (homeless (remove-if #'symbol-package symbols))
1346 (syms ()))
1347 (with-single-package-locked-error ()
1348 (dolist (sym symbols)
1349 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1350 (cond ((not w)
1351 (let ((found (member sym syms :test #'string=)))
1352 (if found
1353 (when (not (eq (car found) sym))
1354 (setf syms (remove (car found) syms))
1355 (name-conflict package 'import sym sym (car found)))
1356 (push sym syms))))
1357 ((not (eq s sym))
1358 (name-conflict package 'import sym sym s))
1359 ((eq w :inherited) (push sym syms)))))
1360 (when (or homeless syms)
1361 (let ((union (delete-duplicates (append homeless syms))))
1362 (assert-package-unlocked package "importing symbol~P ~{~A~^, ~}"
1363 (length union) union)))
1364 ;; Add the new symbols to the internal hashtable.
1365 (let ((internal (package-internal-symbols package)))
1366 (dolist (sym syms)
1367 (add-symbol internal sym)))
1368 ;; If any of the symbols are uninterned, make them be owned by PACKAGE.
1369 (dolist (sym homeless)
1370 (%set-symbol-package sym package))
1371 t))))
1373 ;;; If a conflicting symbol is present, unintern it, otherwise just
1374 ;;; stick the symbol in.
1375 (defun shadowing-import (symbols &optional (package (sane-package)))
1376 #!+sb-doc
1377 "Import SYMBOLS into package, disregarding any name conflict. If
1378 a symbol of the same name is present, then it is uninterned."
1379 (with-package-graph ()
1380 (let* ((package (find-undeleted-package-or-lose package))
1381 (internal (package-internal-symbols package))
1382 (symbols (symbol-listify symbols))
1383 (lock-asserted-p nil))
1384 (with-single-package-locked-error ()
1385 (dolist (sym symbols)
1386 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1387 (unless (or lock-asserted-p
1388 (and (eq s sym)
1389 (member s (package-shadowing-symbols package))))
1390 (assert-package-unlocked package "shadowing-importing symbol~P ~
1391 ~{~A~^, ~}" (length symbols) symbols)
1392 (setf lock-asserted-p t))
1393 (unless (and w (not (eq w :inherited)) (eq s sym))
1394 (when (or (eq w :internal) (eq w :external))
1395 ;; If it was shadowed, we don't want UNINTERN to flame out...
1396 (setf (package-%shadowing-symbols package)
1397 (remove s (the list (package-%shadowing-symbols package))))
1398 (unintern s package))
1399 (add-symbol internal sym))
1400 (pushnew sym (package-%shadowing-symbols package)))))))
1403 (defun shadow (symbols &optional (package (sane-package)))
1404 #!+sb-doc
1405 "Make an internal symbol in PACKAGE with the same name as each of the
1406 specified SYMBOLS. If a symbol with the given name is already present in
1407 PACKAGE, then the existing symbol is placed in the shadowing symbols list if
1408 it is not already present."
1409 (with-package-graph ()
1410 (let* ((package (find-undeleted-package-or-lose package))
1411 (internal (package-internal-symbols package))
1412 (symbols (string-listify symbols))
1413 (lock-asserted-p nil))
1414 (flet ((present-p (w)
1415 (and w (not (eq w :inherited)))))
1416 (with-single-package-locked-error ()
1417 (dolist (name symbols)
1418 (multiple-value-bind (s w) (find-symbol name package)
1419 (unless (or lock-asserted-p
1420 (and (present-p w)
1421 (member s (package-shadowing-symbols package))))
1422 (assert-package-unlocked package "shadowing symbol~P ~{~A~^, ~}"
1423 (length symbols) symbols)
1424 (setf lock-asserted-p t))
1425 (unless (present-p w)
1426 (setq s (make-symbol name))
1427 (%set-symbol-package s package)
1428 (add-symbol internal s))
1429 (pushnew s (package-%shadowing-symbols package))))))))
1432 ;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
1433 (defun use-package (packages-to-use &optional (package (sane-package)))
1434 #!+sb-doc
1435 "Add all the PACKAGES-TO-USE to the use list for PACKAGE so that the
1436 external symbols of the used packages are accessible as internal symbols in
1437 PACKAGE."
1438 (with-package-graph ()
1439 (let ((packages (package-listify packages-to-use))
1440 (package (find-undeleted-package-or-lose package)))
1442 ;; Loop over each package, USE'ing one at a time...
1443 (with-single-package-locked-error ()
1444 (dolist (pkg packages)
1445 (unless (member pkg (package-%use-list package))
1446 (assert-package-unlocked package "using package~P ~{~A~^, ~}"
1447 (length packages) packages)
1448 (let ((shadowing-symbols (package-%shadowing-symbols package))
1449 (use-list (package-%use-list package)))
1451 ;; If the number of symbols already accessible is less
1452 ;; than the number to be inherited then it is faster to
1453 ;; run the test the other way. This is particularly
1454 ;; valuable in the case of a new package USEing
1455 ;; COMMON-LISP.
1456 (cond
1457 ((< (+ (package-internal-symbol-count package)
1458 (package-external-symbol-count package)
1459 (let ((res 0))
1460 (dolist (p use-list res)
1461 (incf res (package-external-symbol-count p)))))
1462 (package-external-symbol-count pkg))
1463 (do-symbols (sym package)
1464 (multiple-value-bind (s w)
1465 (find-external-symbol (symbol-name sym) pkg)
1466 (when (and w
1467 (not (eq s sym))
1468 (not (member sym shadowing-symbols)))
1469 (name-conflict package 'use-package pkg sym s))))
1470 (dolist (p use-list)
1471 (do-external-symbols (sym p)
1472 (multiple-value-bind (s w)
1473 (find-external-symbol (symbol-name sym) pkg)
1474 (when (and w
1475 (not (eq s sym))
1476 (not (member
1477 (find-symbol (symbol-name sym) package)
1478 shadowing-symbols)))
1479 (name-conflict package 'use-package pkg sym s))))))
1481 (do-external-symbols (sym pkg)
1482 (multiple-value-bind (s w)
1483 (find-symbol (symbol-name sym) package)
1484 (when (and w
1485 (not (eq s sym))
1486 (not (member s shadowing-symbols)))
1487 (name-conflict package 'use-package pkg sym s)))))))
1489 (push pkg (package-%use-list package))
1490 (setf (package-tables package)
1491 (let ((tbls (package-tables package)))
1492 (replace (make-array (1+ (length tbls))
1493 :initial-element (package-external-symbols pkg))
1494 tbls)))
1495 (push package (package-%used-by-list pkg)))))))
1498 (defun unuse-package (packages-to-unuse &optional (package (sane-package)))
1499 #!+sb-doc
1500 "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
1501 (with-package-graph ()
1502 (let ((package (find-undeleted-package-or-lose package))
1503 (packages (package-listify packages-to-unuse)))
1504 (with-single-package-locked-error ()
1505 (dolist (p packages)
1506 (when (member p (package-use-list package))
1507 (assert-package-unlocked package "unusing package~P ~{~A~^, ~}"
1508 (length packages) packages))
1509 (setf (package-%use-list package)
1510 (remove p (the list (package-%use-list package))))
1511 (setf (package-tables package)
1512 (delete (package-external-symbols p)
1513 (package-tables package)))
1514 (setf (package-%used-by-list p)
1515 (remove package (the list (package-%used-by-list p))))))
1516 t)))
1518 (defun find-all-symbols (string-or-symbol)
1519 #!+sb-doc
1520 "Return a list of all symbols in the system having the specified name."
1521 (let ((string (string string-or-symbol))
1522 (res ()))
1523 (with-package-names (names)
1524 (maphash (lambda (k v)
1525 (declare (ignore k))
1526 (multiple-value-bind (s w) (find-symbol string v)
1527 (when w (pushnew s res))))
1528 names))
1529 res))
1531 ;;;; APROPOS and APROPOS-LIST
1533 (defun briefly-describe-symbol (symbol)
1534 (fresh-line)
1535 (prin1 symbol)
1536 (when (boundp symbol)
1537 (write-string " (bound)"))
1538 (when (fboundp symbol)
1539 (write-string " (fbound)")))
1541 (defun apropos-list (string-designator
1542 &optional
1543 package-designator
1544 external-only)
1545 #!+sb-doc
1546 "Like APROPOS, except that it returns a list of the symbols found instead
1547 of describing them."
1548 (if package-designator
1549 (let ((package (find-undeleted-package-or-lose package-designator))
1550 (string (stringify-string-designator string-designator))
1551 (result nil))
1552 (do-symbols (symbol package)
1553 (when (and (or (not external-only)
1554 (and (eq (symbol-package symbol) package)
1555 (eq (nth-value 1 (find-symbol (symbol-name symbol)
1556 package))
1557 :external)))
1558 (search string (symbol-name symbol) :test #'char-equal))
1559 (pushnew symbol result)))
1560 (sort result #'string-lessp))
1561 (delete-duplicates
1562 (mapcan (lambda (package)
1563 (apropos-list string-designator package external-only))
1564 (sort (list-all-packages) #'string-lessp :key #'package-name)))))
1566 (defun apropos (string-designator &optional package external-only)
1567 #!+sb-doc
1568 "Briefly describe all symbols which contain the specified STRING.
1569 If PACKAGE is supplied then only describe symbols present in
1570 that package. If EXTERNAL-ONLY then only describe
1571 external symbols in the specified package."
1572 ;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
1573 ;; of some unnecessary consing; and the unnecessary consing shouldn't be an
1574 ;; issue, since this function is is only useful interactively anyway, and
1575 ;; we can cons and GC a lot faster than the typical user can read..
1576 (dolist (symbol (apropos-list string-designator package external-only))
1577 (briefly-describe-symbol symbol))
1578 (values))
1580 ;;;; final initialization
1582 ;;;; Due to the relative difficulty - but not impossibility - of manipulating
1583 ;;;; package-hashtables in the cross-compilation host, all interning operations
1584 ;;;; are delayed until cold-init.
1585 ;;;; The cold loader (GENESIS) set *!INITIAL-SYMBOLS* to the target
1586 ;;;; representation of the hosts's *COLD-PACKAGE-SYMBOLS*.
1587 ;;;; The shape of this list is ((package . (externals . internals)) ...)
1588 (defvar *!initial-symbols*)
1590 (defun !package-cold-init ()
1591 (setf *package-graph-lock* (sb!thread:make-mutex :name "Package Graph Lock")
1592 *package-names* (make-hash-table :test 'equal :synchronized t))
1593 (with-package-names (names)
1594 (dolist (spec *!initial-symbols*)
1595 (let ((pkg (car spec)) (symbols (cdr spec)))
1596 ;; the symbol MAKE-TABLE wouldn't magically disappear,
1597 ;; though its only use be to name an FLET in a function
1598 ;; hanging on an otherwise uninternable symbol. strange but true :-(
1599 (flet ((!make-table (input)
1600 (let ((table (make-package-hashtable
1601 (length (the simple-vector input)))))
1602 (dovector (symbol input table)
1603 (add-symbol table symbol)))))
1604 (setf (package-external-symbols pkg) (!make-table (car symbols))
1605 (package-internal-symbols pkg) (!make-table (cdr symbols))))
1606 (setf (package-%shadowing-symbols pkg) nil
1607 (package-%local-nicknames pkg) nil
1608 (package-%locally-nicknamed-by pkg) nil
1609 (package-source-location pkg) nil
1610 (gethash (package-%name pkg) names) pkg)
1611 (let ((nicks (package-%nicknames pkg)))
1612 (setf (package-%nicknames pkg) nil) ; each is pushed in again
1613 (%enter-new-nicknames pkg nicks))
1614 #!+sb-package-locks
1615 (setf (package-lock pkg) nil
1616 (package-%implementation-packages pkg) nil))))
1618 ;; pass 2 - set the 'tables' slots only after all tables have been made
1619 (dolist (spec *!initial-symbols*)
1620 (let ((pkg (car spec)))
1621 (setf (package-tables pkg)
1622 (map 'vector #'package-external-symbols (package-%use-list pkg)))))
1624 (/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*")
1625 (%makunbound '*!initial-symbols*) ; (so that it gets GCed)
1627 ;; For the kernel core image wizards, set the package to *CL-PACKAGE*.
1629 ;; FIXME: We should just set this to (FIND-PACKAGE
1630 ;; "COMMON-LISP-USER") once and for all here, instead of setting it
1631 ;; once here and resetting it later.
1632 (setq *package* *cl-package*))
1634 ;;; support for WITH-PACKAGE-ITERATOR
1636 (defun package-iter-init (access-types pkg-designator-list)
1637 (declare (type (integer 1 7) access-types)) ; a nonzero bitmask over types
1638 (values (logior (ash access-types 3) #b11) 0 #()
1639 (package-listify pkg-designator-list)))
1641 ;; The STATE parameter is comprised of 4 packed fields
1642 ;; [0:1] = substate {0=internal,1=external,2=inherited,3=initial}
1643 ;; [2] = package with inherited symbols has shadowing symbols
1644 ;; [3:5] = enabling bits for {internal,external,inherited}
1645 ;; [6:] = index into 'package-tables'
1647 (defconstant +package-iter-check-shadows+ #b000100)
1649 (defun package-iter-step (start-state index sym-vec pkglist)
1650 ;; the defknown isn't enough
1651 (declare (type fixnum start-state) (type index index)
1652 (type simple-vector sym-vec) (type list pkglist))
1653 (declare (optimize speed))
1654 (labels
1655 ((advance (state) ; STATE is the one just completed
1656 (case (logand state #b11)
1657 ;; Test :INHERITED first because the state repeats for a package
1658 ;; as many times as there are packages it uses. There are enough
1659 ;; bits to count up to 2^23 packages if fixnums are 30 bits.
1661 (when (desired-state-p 2)
1662 (let* ((tables (package-tables (this-package)))
1663 (next-state (the fixnum (+ state (ash 1 6))))
1664 (table-idx (ash next-state -6)))
1665 (when (< table-idx (length tables))
1666 (return-from advance ; remain in state 2
1667 (start next-state (svref tables table-idx))))))
1668 (pop pkglist)
1669 (advance 3)) ; start on next package
1670 (1 ; finished externals, switch to inherited if desired
1671 (when (desired-state-p 2)
1672 (let ((tables (package-tables (this-package))))
1673 (when (plusp (length tables)) ; inherited symbols
1674 (return-from advance ; enter state 2
1675 (start (if (package-%shadowing-symbols (this-package))
1676 (logior 2 +package-iter-check-shadows+) 2)
1677 (svref tables 0))))))
1678 (advance 2)) ; skip state 2
1679 (0 ; finished internals, switch to externals if desired
1680 (if (desired-state-p 1) ; enter state 1
1681 (start 1 (package-external-symbols (this-package)))
1682 (advance 1))) ; skip state 1
1683 (t ; initial state
1684 (cond ((endp pkglist) ; latch into returning NIL forever more
1685 (values 0 0 #() '() nil nil))
1686 ((desired-state-p 0) ; enter state 0
1687 (start 0 (package-internal-symbols (this-package))))
1688 (t (advance 0)))))) ; skip state 0
1689 (desired-state-p (target-state)
1690 (logtest start-state (ash 1 (+ target-state 3))))
1691 (this-package ()
1692 (truly-the package (car pkglist)))
1693 (start (next-state new-table)
1694 (let ((symbols (package-hashtable-cells new-table)))
1695 (package-iter-step (logior (mask-field (byte 3 3) start-state)
1696 next-state)
1697 ;; assert that physical length was nonzero
1698 (the index (1- (length symbols)))
1699 symbols pkglist))))
1700 (declare (inline desired-state-p this-package))
1701 (if (zerop index)
1702 (advance start-state)
1703 (macrolet ((scan (&optional (guard t))
1704 `(loop
1705 (let ((sym (aref sym-vec (decf index))))
1706 (when (and (pkg-symbol-valid-p sym) ,guard)
1707 (return (values start-state index sym-vec pkglist sym
1708 (aref #(:internal :external :inherited)
1709 (logand start-state 3))))))
1710 (when (zerop index)
1711 (return (advance start-state))))))
1712 (declare #-sb-xc-host(optimize (sb!c::insert-array-bounds-checks 0)))
1713 (if (logtest start-state +package-iter-check-shadows+)
1714 (let ((shadows (package-%shadowing-symbols (this-package))))
1715 (scan (not (member sym shadows :test #'string=))))
1716 (scan))))))