1 ;;;; PACKAGEs and stuff like that
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.
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.
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")
28 ;;;; ...this could still use work, but the basic idea is:
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.
34 ;;;; Hash-table lock on *PACKAGE-NAMES* is held via WITH-PACKAGE-NAMES while
35 ;;;; frobbing name -> package associations.
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
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.
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
*)
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
)
69 ;;;; PACKAGE-HASHTABLE stuff
71 (defmethod 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 (length (package-hashtable-cells table
))))
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
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
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)
105 when
(positive-primep n
) return n
)))
106 (let* ((n (actual-package-hashtable-size size
))
107 ;; SIZE is how many symbols we'd like to be able to store,
108 ;; but the number of physical cells is N, chosen for its primality.
109 (size (truncate (* n
*package-rehash-threshold
*)))
110 (table (make-array n
:initial-element
0)))
111 (%make-package-hashtable table size
))))
113 (declaim (inline pkg-symbol-valid-p
))
114 (defun pkg-symbol-valid-p (x) (not (fixnump x
)))
116 ;;; Destructively resize TABLE to have room for at least SIZE entries
117 ;;; and rehash its existing entries.
118 (defun resize-package-hashtable (table size
)
119 (let ((temp-table (make-package-hashtable size
)))
120 (dovector (sym (package-hashtable-cells table
))
121 (when (pkg-symbol-valid-p sym
)
122 (add-symbol temp-table sym
)))
123 (setf (package-hashtable-cells table
) (package-hashtable-cells temp-table
)
124 (package-hashtable-size table
) (package-hashtable-size temp-table
)
125 (package-hashtable-free table
) (package-hashtable-free temp-table
)
126 (package-hashtable-deleted table
) 0)))
128 ;;;; package locking operations, built conditionally on :sb-package-locks
132 (defun package-locked-p (package)
133 "Returns T when PACKAGE is locked, NIL otherwise. Signals an error
134 if PACKAGE doesn't designate a valid package."
135 (package-lock (find-undeleted-package-or-lose package
)))
137 (defun lock-package (package)
138 "Locks PACKAGE and returns T. Has no effect if PACKAGE was already
139 locked. Signals an error if PACKAGE is not a valid package designator"
140 (setf (package-lock (find-undeleted-package-or-lose package
)) t
))
142 (defun unlock-package (package)
143 "Unlocks PACKAGE and returns T. Has no effect if PACKAGE was already
144 unlocked. Signals an error if PACKAGE is not a valid package designator."
145 (setf (package-lock (find-undeleted-package-or-lose package
)) nil
)
148 (defun package-implemented-by-list (package)
149 "Returns a list containing the implementation packages of
150 PACKAGE. Signals an error if PACKAGE is not a valid package designator."
151 (package-%implementation-packages
(find-undeleted-package-or-lose package
)))
153 (defun package-implements-list (package)
154 "Returns the packages that PACKAGE is an implementation package
155 of. Signals an error if PACKAGE is not a valid package designator."
156 (let ((package (find-undeleted-package-or-lose package
)))
157 (loop for x in
(list-all-packages)
158 when
(member package
(package-%implementation-packages x
))
161 (defun add-implementation-package (packages-to-add
162 &optional
(package *package
*))
163 "Adds PACKAGES-TO-ADD as implementation packages of PACKAGE. Signals
164 an error if PACKAGE or any of the PACKAGES-TO-ADD is not a valid
166 (let ((package (find-undeleted-package-or-lose package
))
167 (packages-to-add (package-listify packages-to-add
)))
168 (setf (package-%implementation-packages package
)
169 (union (package-%implementation-packages package
)
170 (mapcar #'find-undeleted-package-or-lose packages-to-add
)))))
172 (defun remove-implementation-package (packages-to-remove
173 &optional
(package *package
*))
174 "Removes PACKAGES-TO-REMOVE from the implementation packages of
175 PACKAGE. Signals an error if PACKAGE or any of the PACKAGES-TO-REMOVE
176 is not a valid package designator."
177 (let ((package (find-undeleted-package-or-lose package
))
178 (packages-to-remove (package-listify packages-to-remove
)))
179 (setf (package-%implementation-packages package
)
181 (package-%implementation-packages package
)
182 (mapcar #'find-undeleted-package-or-lose packages-to-remove
)))))
184 (defmacro with-unlocked-packages
((&rest packages
) &body forms
)
185 "Unlocks PACKAGES for the dynamic scope of the body. Signals an
186 error if any of PACKAGES is not a valid package designator."
187 (with-unique-names (unlocked-packages)
188 `(let (,unlocked-packages
)
191 (dolist (p ',packages
)
192 (when (package-locked-p p
)
193 (push p
,unlocked-packages
)
196 (dolist (p ,unlocked-packages
)
197 (when (find-package p
)
198 (lock-package p
)))))))
200 (defun package-lock-violation (package &key
(symbol nil symbol-p
)
201 format-control format-arguments
)
202 (let* ((restart :continue
)
203 (cl-violation-p (eq package
*cl-package
*))
205 (append (list (if symbol-p
206 'symbol-package-locked-error
207 'package-locked-error
)
209 :format-control format-control
210 :format-arguments format-arguments
)
211 (when symbol-p
(list :symbol symbol
))
213 (append '((:sbcl
:node
"Package Locks"))
215 '((:ansi-cl
:section
(11 1 2 1 2)))))))))
217 (apply #'cerror
"Ignore the package lock." error-arguments
)
219 :report
"Ignore all package locks in the context of this operation."
220 (setf restart
:ignore-all
))
222 :report
"Unlock the package."
223 (setf restart
:unlock-package
)))
226 (pushnew package
*ignored-package-locks
*))
228 (setf *ignored-package-locks
* t
))
230 (unlock-package package
)))))
232 (defun package-lock-violation-p (package &optional
(symbol nil symbolp
))
233 ;; KLUDGE: (package-lock package) needs to be before
234 ;; comparison to *package*, since during cold init this gets
235 ;; called before *package* is bound -- but no package should
236 ;; be locked at that point.
238 (package-lock package
)
239 ;; In package or implementation package
240 (not (or (eq package
*package
*)
241 (member *package
* (package-%implementation-packages package
))))
243 (not (eq t
*ignored-package-locks
*))
244 (or (eq :invalid
*ignored-package-locks
*)
245 (not (member package
*ignored-package-locks
*)))
246 ;; declarations for symbols
247 (not (and symbolp
(lexically-unlocked-symbol-p symbol
)))))
249 (defun lexically-unlocked-symbol-p (symbol)
251 (if (boundp 'sb
!c
::*lexenv
*)
252 (let ((list (sb!c
::lexenv-disabled-package-locks sb
!c
::*lexenv
*)))
253 ;; The so-called LIST might be an interpreter env.
256 (return-from lexically-unlocked-symbol-p
257 (sb!interpreter
::lexically-unlocked-symbol-p
260 sb
!c
::*disabled-package-locks
*)))
264 ;;;; more package-locking these are NOPs unless :sb-package-locks is
265 ;;;; in target features. Cross-compiler NOPs for these are in cross-misc.
267 ;;; The right way to establish a package lock context is
268 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR, defined in early-package.lisp
270 ;;; Must be used inside the dynamic contour established by
271 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR
272 (defun assert-package-unlocked (package &optional format-control
273 &rest format-arguments
)
275 (declare (ignore format-control format-arguments
))
277 (when (package-lock-violation-p package
)
278 (package-lock-violation package
279 :format-control format-control
280 :format-arguments format-arguments
))
283 ;;; Must be used inside the dynamic contour established by
284 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR.
286 ;;; FIXME: Maybe we should establish such contours for he toplevel
287 ;;; and others, so that %set-fdefinition and others could just use
289 (defun assert-symbol-home-package-unlocked (name &optional format-control
290 &rest format-arguments
)
292 (declare (ignore format-control format-arguments
))
294 (let* ((symbol (etypecase name
296 ;; Istm that the right way to declare that you want to allow
297 ;; overriding the lock on (SETF X) is to list (SETF X) in
298 ;; the declaration, not expect that X means itself and SETF.
299 ;; Worse still, the syntax ({ENABLE|DISABLE}-..-locks (SETF X))
300 ;; is broken, and yet we make no indication of it.
301 ((cons (eql setf
) cons
) (second name
))
302 ;; Skip lists of length 1, single conses and
303 ;; (class-predicate foo), etc. FIXME: MOP and
304 ;; package-lock interaction needs to be thought
307 (return-from assert-symbol-home-package-unlocked
309 (package (symbol-package symbol
)))
310 (when (package-lock-violation-p package symbol
)
311 (package-lock-violation package
313 :format-control format-control
314 :format-arguments
(cons name format-arguments
))))
318 ;;;; miscellaneous PACKAGE operations
320 (defmethod print-object ((package package
) stream
)
321 (let ((name (package-%name package
)))
322 (print-unreadable-object (package stream
:type t
:identity
(not name
))
323 (if name
(prin1 name stream
) (write-string "(deleted)" stream
)))))
325 ;;; ANSI says (in the definition of DELETE-PACKAGE) that these, and
326 ;;; most other operations, are unspecified for deleted packages. We
327 ;;; just do the easy thing and signal errors in that case.
328 (macrolet ((def (ext real
)
329 `(defun ,ext
(package-designator)
330 (,real
(find-undeleted-package-or-lose package-designator
)))))
331 (def package-nicknames package-%nicknames
)
332 (def package-use-list package-%use-list
)
333 (def package-used-by-list package-%used-by-list
)
334 (def package-shadowing-symbols package-%shadowing-symbols
))
336 (defun package-local-nicknames (package-designator)
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
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."
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
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
368 :format-control format-control
369 :format-arguments format-args
))
371 (defun package-locally-nicknamed-by-list (package-designator)
372 "Returns a list of packages which have a local nickname for the designated
375 See also: ADD-PACKAGE-LOCAL-NICKNAME, PACKAGE-LOCAL-NICKNAMES,
376 REMOVE-PACKAGE-LOCAL-NICKNAME, and the DEFPACKAGE option :LOCAL-NICKNAMES.
378 Experimental: interface subject to change."
380 (package-%locally-nicknamed-by
381 (find-undeleted-package-or-lose package-designator
))))
383 (defun add-package-local-nickname (local-nickname actual-package
384 &optional
(package-designator (sane-package)))
385 "Adds LOCAL-NICKNAME for ACTUAL-PACKAGE in the designated package, defaulting
386 to current package. LOCAL-NICKNAME must be a string designator, and
387 ACTUAL-PACKAGE must be a package designator.
389 Returns the designated package.
391 Signals a continuable error if LOCAL-NICKNAME is already a package local
392 nickname for a different package, or if LOCAL-NICKNAME is one of \"CL\",
393 \"COMMON-LISP\", or, \"KEYWORD\", or if LOCAL-NICKNAME is a global name or
394 nickname for the package to which the nickname would be added.
396 When in the designated package, calls to FIND-PACKAGE with the LOCAL-NICKNAME
397 will return the package the designated ACTUAL-PACKAGE instead. This also
398 affects all implied calls to FIND-PACKAGE, including those performed by the
401 When printing a package prefix for a symbol with a package local nickname,
402 local nickname is used instead of the real name in order to preserve
403 print-read consistency.
405 See also: PACKAGE-LOCAL-NICKNAMES, PACKAGE-LOCALLY-NICKNAMED-BY-LIST,
406 REMOVE-PACKAGE-LOCAL-NICKNAME, and the DEFPACKAGE option :LOCAL-NICKNAMES.
408 Experimental: interface subject to change."
409 (let* ((nick (string local-nickname
))
410 (actual (find-package-using-package actual-package nil
))
411 (package (find-undeleted-package-or-lose package-designator
))
412 (existing (package-%local-nicknames package
))
413 (cell (assoc nick existing
:test
#'string
=)))
415 (signal-package-error
417 "The name ~S does not designate any package."
419 (unless (package-name actual
)
420 (signal-package-error
422 "Cannot add ~A as local nickname for a deleted package: ~S"
424 (with-single-package-locked-error
425 (:package package
"adding ~A as a local nickname for ~A"
427 (when (member nick
'("CL" "COMMON-LISP" "KEYWORD") :test
#'string
=)
428 (signal-package-cerror
430 "Continue, use it as local nickname anyways."
431 "Attempt to use ~A as a package local nickname (for ~A)."
432 nick
(package-name actual
)))
433 (when (string= nick
(package-name package
))
434 (signal-package-cerror
436 "Continue, use it as a local nickname anyways."
437 "Attempt to use ~A as a package local nickname (for ~A) in ~
438 package named globally ~A."
439 nick
(package-name actual
) nick
))
440 (when (member nick
(package-nicknames package
) :test
#'string
=)
441 (signal-package-cerror
443 "Continue, use it as a local nickname anyways."
444 "Attempt to use ~A as a package local nickname (for ~A) in ~
445 package nicknamed globally ~A."
446 nick
(package-name actual
) nick
))
447 (when (and cell
(neq actual
(cdr cell
)))
449 (signal-package-error
451 "~@<Cannot add ~A as local nickname for ~A in ~A: ~
452 already nickname for ~A.~:@>"
453 nick
(package-name actual
)
454 (package-name package
) (package-name (cdr cell
)))
457 (format s
"Keep ~A as local nicname for ~A."
458 nick
(package-name (cdr cell
)))))
461 (format s
"Use ~A as local nickname for ~A instead."
462 nick
(package-name actual
)))
463 (let ((old (cdr cell
)))
464 (with-package-graph ()
465 (setf (package-%locally-nicknamed-by old
)
466 (delete package
(package-%locally-nicknamed-by old
)))
467 (push package
(package-%locally-nicknamed-by actual
))
468 (setf (cdr cell
) actual
)))))
469 (return-from add-package-local-nickname package
))
471 (with-package-graph ()
472 (push (cons nick actual
) (package-%local-nicknames package
))
473 (push package
(package-%locally-nicknamed-by actual
))))
476 (defun remove-package-local-nickname (old-nickname
477 &optional
(package-designator (sane-package)))
478 "If the designated package had OLD-NICKNAME as a local nickname for
479 another package, it is removed. Returns true if the nickname existed and was
480 removed, and NIL otherwise.
482 See also: ADD-PACKAGE-LOCAL-NICKNAME, PACKAGE-LOCAL-NICKNAMES,
483 PACKAGE-LOCALLY-NICKNAMED-BY-LIST, and the DEFPACKAGE option :LOCAL-NICKNAMES.
485 Experimental: interface subject to change."
486 (let* ((nick (string old-nickname
))
487 (package (find-undeleted-package-or-lose package-designator
))
488 (existing (package-%local-nicknames package
))
489 (cell (assoc nick existing
:test
#'string
=)))
491 (with-single-package-locked-error
492 (:package package
"removing local nickname ~A for ~A"
494 (with-package-graph ()
495 (let ((old (cdr cell
)))
496 (setf (package-%local-nicknames package
) (delete cell existing
))
497 (setf (package-%locally-nicknamed-by old
)
498 (delete package
(package-%locally-nicknamed-by old
)))))
501 (defun %package-hashtable-symbol-count
(table)
502 (let ((size (the fixnum
503 (- (package-hashtable-size table
)
504 (package-hashtable-deleted table
)))))
506 (- size
(package-hashtable-free table
)))))
508 (defun package-internal-symbol-count (package)
509 (%package-hashtable-symbol-count
(package-internal-symbols package
)))
511 (defun package-external-symbol-count (package)
512 (%package-hashtable-symbol-count
(package-external-symbols package
)))
514 (defvar *package
* (error "*PACKAGE* should be initialized in cold load!")
515 "the current package")
517 (define-condition bootstrap-package-not-found
(condition)
518 ((name :initarg
:name
:reader bootstrap-package-name
)))
519 (defun debootstrap-package (&optional condition
)
521 (find-restart-or-control-error 'debootstrap-package condition
)))
523 (defun find-package (package-designator)
524 "If PACKAGE-DESIGNATOR is a package, it is returned. Otherwise PACKAGE-DESIGNATOR
525 must be a string designator, in which case the package it names is located and returned.
527 As an SBCL extension, the current package may affect the way a package name is
528 resolved: if the current package has local nicknames specified, package names
529 matching those are resolved to the packages associated with them instead.
534 (defpackage :example (:use :cl) (:local-nicknames (:x :a)))
535 (let ((*package* (find-package :example)))
536 (find-package :x)) => #<PACKAGE A>
538 See also: ADD-PACKAGE-LOCAL-NICKNAME, PACKAGE-LOCAL-NICKNAMES,
539 REMOVE-PACKAGE-LOCAL-NICKNAME, and the DEFPACKAGE option :LOCAL-NICKNAMES."
540 (find-package-using-package package-designator
541 (when (boundp '*package
*)
544 ;;; This is undocumented and unexported for now, but the idea is that by
545 ;;; making this a generic function then packages with custom package classes
546 ;;; could hook into this to provide their own resolution.
547 (defun find-package-using-package (package-designator base
)
548 (typecase package-designator
549 (package package-designator
)
550 ;; Rather than use STRINGIFY-STRING-DESIGNATOR, we check type by hand
551 ;; to avoid consing a new simple-base-string if the designator is one
552 ;; that would undergo coercion entailing allocation.
553 ((or symbol string character
)
554 (let ((string (string package-designator
)))
555 (let* ((nicknames (when base
556 (package-%local-nicknames base
)))
557 (nicknamed (when nicknames
558 (cdr (assoc string nicknames
:test
#'string
=))))
559 (packageoid (or nicknamed
(gethash string
*package-names
*))))
560 (if (and (null packageoid
)
561 ;; FIXME: should never need 'debootstrap' hack
562 (let ((mismatch (mismatch "SB!" string
)))
563 (and mismatch
(= mismatch
3))))
565 (signal 'bootstrap-package-not-found
:name string
)
566 (debootstrap-package ()
567 (if (string= string
"SB!XC")
568 (find-package "COMMON-LISP")
570 (substitute #\-
#\
! string
:count
1)))))
572 ;; Is there a fundamental reason we don't declare the FTYPE
573 ;; of FIND-PACKAGE-USING-PACKAGE letting the compiler do the checking?
574 (t (error 'type-error
575 :datum package-designator
576 :expected-type
'(or character package string symbol
)))))
578 ;;; Return a list of packages given a package designator or list of
579 ;;; package designators, or die trying.
580 (defun package-listify (thing)
582 (mapcar #'find-undeleted-package-or-lose thing
)
583 (list (find-undeleted-package-or-lose 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 ;;; Add a symbol to a package hashtable. The symbol MUST NOT be present.
595 (defun add-symbol (table symbol
)
596 (when (zerop (package-hashtable-free table
))
597 ;; The hashtable is full. Resize it to be able to hold twice the
598 ;; amount of symbols than it currently contains. The actual new size
599 ;; can be smaller than twice the current size if the table contained
601 (resize-package-hashtable table
602 (* (- (package-hashtable-size table
)
603 (package-hashtable-deleted table
))
605 (let* ((symvec (package-hashtable-cells table
))
606 (len (length symvec
))
607 (sxhash (truly-the fixnum
(ensure-symbol-hash symbol
)))
608 (h2 (1+ (rem sxhash
(- len
2)))))
609 (declare (fixnum sxhash h2
))
610 (do ((i (rem sxhash len
) (rem (+ i h2
) len
)))
611 ((fixnump (svref symvec i
))
612 ;; Interning has to pre-check whether the symbol existed in any
613 ;; package used by the package in which intern is happening,
614 ;; so it's not really useful to be lock-free here,
615 ;; even though we could be. (We're already inside a mutex)
616 (let ((old (svref symvec i
)))
617 (setf (svref symvec i
) symbol
)
619 (decf (package-hashtable-free table
)) ; unused
620 (decf (package-hashtable-deleted table
))))) ; tombstone
621 (declare (fixnum i
)))))
623 ;;; Resize the package hashtables of all packages so that their load
624 ;;; factor is *PACKAGE-HASHTABLE-IMAGE-LOAD-FACTOR*. Called from
625 ;;; SAVE-LISP-AND-DIE to optimize space usage in the image.
626 (defun tune-hashtable-sizes-of-all-packages ()
627 (flet ((tune-table-size (table)
628 (resize-package-hashtable
630 (round (* (/ *package-rehash-threshold
*
631 *package-hashtable-image-load-factor
*)
632 (- (package-hashtable-size table
)
633 (package-hashtable-free table
)
634 (package-hashtable-deleted table
)))))))
635 (dolist (package (list-all-packages))
636 (tune-table-size (package-internal-symbols package
))
637 (tune-table-size (package-external-symbols package
)))))
639 ;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR
640 ;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR
641 ;;; is bound to the symbol. LENGTH and HASH are the length and sxhash
642 ;;; of STRING. ENTRY-HASH is the entry-hash of the string and length.
643 ;;; If the symbol is found, then FORMS are executed; otherwise not.
645 (defmacro with-symbol
(((symbol-var &optional
(index-var (gensym))) table
646 string length sxhash
) &body forms
)
647 (with-unique-names (vec len h2 probed-thing name
)
648 `(let* ((,vec
(package-hashtable-cells ,table
))
650 (,index-var
(rem (the hash
,sxhash
) ,len
))
651 (,h2
(1+ (the index
(rem ,sxhash
(the index
(- ,len
2)))))))
652 (declare (type index
,len
,h2
,index-var
))
654 (let ((,probed-thing
(svref ,vec
,index-var
)))
655 (cond ((not (fixnump ,probed-thing
))
656 (let ((,symbol-var
(truly-the symbol
,probed-thing
)))
657 (when (eq (symbol-hash ,symbol-var
) ,sxhash
)
658 (let ((,name
(symbol-name ,symbol-var
)))
659 ;; The pre-test for length is kind of an unimportant
660 ;; optimization, but passing it for both :end arguments
661 ;; requires that it be within bounds for the probed symbol.
662 (when (and (= (length ,name
) ,length
)
663 (string= ,string
,name
664 :end1
,length
:end2
,length
))
665 (return (progn ,@forms
)))))))
666 ;; either a never used cell or a tombstone left by UNINTERN
667 ((eql ,probed-thing
0) ; really never used
669 (when (>= (incf ,index-var
,h2
) ,len
)
670 (decf ,index-var
,len
))))))
672 ;;; Delete SYMBOL from TABLE, storing -1 in its place. SYMBOL must exist.
674 (defun nuke-symbol (table symbol
)
675 (let* ((string (symbol-name symbol
))
676 (length (length string
))
677 (hash (symbol-hash symbol
)))
678 (declare (type index length
)
680 (with-symbol ((symbol index
) table string length hash
)
681 ;; It is suboptimal to grab the vectors again, but not broken,
682 ;; because we have exclusive use of the table for writing.
683 (let ((symvec (package-hashtable-cells table
)))
684 (setf (aref symvec index
) -
1))
685 (incf (package-hashtable-deleted table
))))
686 ;; If the table is less than one quarter full, halve its size and
687 ;; rehash the entries.
688 (let* ((size (package-hashtable-size table
))
689 (deleted (package-hashtable-deleted table
))
691 (package-hashtable-free table
)
693 (declare (type fixnum size deleted used
))
694 (when (< used
(truncate size
4))
695 (resize-package-hashtable table
(* used
2)))))
697 ;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*. If there is a
698 ;;; conflict then give the user a chance to do something about it. Caller is
699 ;;; responsible for having acquired the mutex via WITH-PACKAGES.
700 (defun %enter-new-nicknames
(package nicknames
)
701 (declare (type list nicknames
))
702 (dolist (n nicknames
)
703 (let ((found (with-package-names (names)
704 (or (gethash (the simple-string n
) names
)
706 (setf (gethash n names
) package
)
707 (push n
(package-%nicknames package
))
709 (cond ((eq found package
))
710 ((string= (the string
(package-%name found
)) n
)
711 (signal-package-cerror
713 "Ignore this nickname."
714 "~S is a package name, so it cannot be a nickname for ~S."
715 n
(package-%name package
)))
717 (signal-package-cerror
719 "Leave this nickname alone."
720 "~S is already a nickname for ~S."
721 n
(package-%name found
)))))))
723 ;;; ANSI specifies that:
724 ;;; (1) MAKE-PACKAGE and DEFPACKAGE use the same default package-use-list
725 ;;; (2) that it (as an implementation-defined value) should be documented,
726 ;;; which we do in the doc string.
727 ;;; For OAOO reasons we give a name to this value and then use #. readmacro
728 ;;; to splice it in as a constant. Anyone who actually wants a random value
729 ;;; is free to :USE (PACKAGE-USE-LIST :CL-USER) or whatever.
730 (defglobal *!default-package-use-list
* nil
)
732 (defun make-package (name &key
733 (use '#.
*!default-package-use-list
*)
735 (internal-symbols 10)
736 (external-symbols 10))
738 "Make a new package having the specified NAME, NICKNAMES, and USE
739 list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are estimates for the number of
740 internal and external symbols which will ultimately be present in the package.
741 The default value of USE is implementation-dependent, and in this
742 implementation it is ~S." *!default-package-use-list
*)
743 (prog ((name (stringify-string-designator name
))
744 (nicks (stringify-string-designators nicknames
))
747 (when (find-package name
)
748 ;; ANSI specifies that this error is correctable.
749 (signal-package-cerror
751 "Clobber existing package."
752 "A package named ~S already exists" name
)
754 (with-package-graph ()
755 ;; Check for race, signal the error outside the lock.
756 (when (and (not clobber
) (find-package name
))
761 (make-package-hashtable internal-symbols
)
762 (make-package-hashtable external-symbols
))))
764 ;; Do a USE-PACKAGE for each thing in the USE list so that checking for
765 ;; conflicting exports among used packages is done.
766 (use-package use package
)
768 ;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
769 ;; which would leave us with possibly-bad side effects from the earlier
770 ;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
771 ;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
772 ;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
773 ;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
775 (%enter-new-nicknames package nicks
)
776 (return (setf (gethash name
*package-names
*) package
))))
779 ;;; Change the name if we can, blast any old nicknames and then
780 ;;; add in any new ones.
782 ;;; FIXME: ANSI claims that NAME is a package designator (not just a
783 ;;; string designator -- weird). Thus, NAME could
784 ;;; be a package instead of a string. Presumably then we should not change
785 ;;; the package name if NAME is the same package that's referred to by PACKAGE.
786 ;;; If it's a *different* package, we should probably signal an error.
787 ;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
788 (defun rename-package (package-designator name
&optional
(nicknames ()))
789 "Changes the name and nicknames for a package."
790 (prog ((nicks (stringify-string-designators nicknames
)))
792 (let ((package (find-undeleted-package-or-lose package-designator
))
793 ;; This is the "weirdness" alluded to. Do it in the loop in case
794 ;; the stringified value changes on restart when NAME is a package.
795 (name (stringify-package-designator name
))
796 (found (find-package name
)))
797 (unless (or (not found
) (eq found package
))
798 (signal-package-error name
799 "A package named ~S already exists." name
))
800 (with-single-package-locked-error ()
801 (unless (and (string= name
(package-name package
))
802 (null (set-difference nicks
(package-nicknames package
)
804 (assert-package-unlocked
805 package
"renaming as ~A~@[ with nickname~*~P ~1@*~{~A~^, ~}~]"
806 name nicks
(length nicks
)))
807 (with-package-names (names)
808 ;; Check for race conditions now that we have the lock.
809 (unless (eq package
(find-package package-designator
))
812 (remhash (package-%name package
) names
)
813 (dolist (n (package-%nicknames package
))
815 (setf (package-%name package
) name
816 (gethash name names
) package
817 (package-%nicknames package
) ()))
818 (%enter-new-nicknames package nicks
))
821 (defun delete-package (package-designator)
822 "Delete the package designated by PACKAGE-DESIGNATOR from the package
823 system data structures."
825 (let ((package (find-package package-designator
)))
827 ;; This continuable error is required by ANSI.
828 (signal-package-cerror
831 "There is no package named ~S." package-designator
)
832 (return-from delete-package nil
))
833 ((not (package-name package
)) ; already deleted
834 (return-from delete-package nil
))
836 (with-single-package-locked-error
837 (:package package
"deleting package ~A" package
)
838 (let ((use-list (package-used-by-list package
)))
840 ;; This continuable error is specified by ANSI.
841 (signal-package-cerror
843 "Remove dependency in other packages."
844 "~@<Package ~S is used by package~P:~2I~_~S~@:>"
845 (package-name package
)
847 (mapcar #'package-name use-list
))
849 (unuse-package package p
))))
851 (dolist (p (package-implements-list package
))
852 (remove-implementation-package package p
))
853 (with-package-graph ()
854 ;; Check for races, restart if necessary.
855 (let ((package2 (find-package package-designator
)))
856 (when (or (neq package package2
) (package-used-by-list package2
))
858 (dolist (used (package-use-list package
))
859 (unuse-package used package
))
860 (dolist (namer (package-%locally-nicknamed-by package
))
861 (setf (package-%local-nicknames namer
)
862 (delete package
(package-%local-nicknames namer
) :key
#'cdr
)))
863 (setf (package-%locally-nicknamed-by package
) nil
)
864 (dolist (cell (package-%local-nicknames package
))
865 (let ((actual (cdr cell
)))
866 (setf (package-%locally-nicknamed-by actual
)
867 (delete package
(package-%locally-nicknamed-by actual
)))))
868 (setf (package-%local-nicknames package
) nil
)
869 ;; FIXME: lacking a way to advise UNINTERN that this package
870 ;; is pending deletion, a large package conses successively
871 ;; many smaller tables for no good reason.
872 (do-symbols (sym package
)
873 (unintern sym package
))
874 (with-package-names (names)
875 (remhash (package-name package
) names
)
876 (dolist (nick (package-nicknames package
))
877 (remhash nick names
))
878 (setf (package-%name package
) nil
879 ;; Setting PACKAGE-%NAME to NIL is required in order to
880 ;; make PACKAGE-NAME return NIL for a deleted package as
881 ;; ANSI requires. Setting the other slots to NIL
882 ;; and blowing away the PACKAGE-HASHTABLES is just done
883 ;; for tidiness and to help the GC.
884 (package-%nicknames package
) nil
))
885 (setf (package-%use-list package
) nil
886 (package-tables package
) #()
887 (package-%shadowing-symbols package
) nil
888 (package-internal-symbols package
)
889 (make-package-hashtable 0)
890 (package-external-symbols package
)
891 (make-package-hashtable 0)))
892 (return-from delete-package t
)))))))
894 (defun list-all-packages ()
895 "Return a list of all existing packages."
897 (with-package-names (names)
898 (maphash (lambda (k v
)
900 (pushnew v res
:test
#'eq
))
904 (macrolet ((find/intern
(function &rest more-args
)
905 ;; Both %FIND-SYMBOL and %INTERN require a SIMPLE-STRING,
906 ;; but accept a LENGTH. Given a non-simple string,
907 ;; we need copy it only if the cumulative displacement
908 ;; into the underlying simple-string is nonzero.
909 ;; There are two things that can be improved
910 ;; about the generated code here:
911 ;; 1. if X is known to satisfy STRINGP (generally any rank-1 array),
912 ;; then testing SIMPLE-<base|character>-STRING-P should not
913 ;; re-test the lowtag. This is constrained by the backends,
914 ;; because there are no type vops that assume a known lowtag.
915 ;; 2. if X is known to satisfy VECTORP, then
916 ;; (NOT (ARRAY-HEADER-P)) implies SIMPLE-P, but the compiler
917 ;; does not actually know that, and generates a check.
918 ;; This is more of a front-end issue.
919 `(multiple-value-bind (name length
)
920 (if (simple-string-p name
)
921 (values name
(length name
))
922 (with-array-data ((name name
) (start) (end)
923 :check-fill-pointer t
)
926 (values (subseq name start end
)
929 (values symbol
(member :internal
:external
:inherited nil
))
930 (,function name length
931 (find-undeleted-package-or-lose package
)
934 (defun intern (name &optional
(package (sane-package)))
935 "Return a symbol in PACKAGE having the specified NAME, creating it
937 (find/intern %intern
(if (base-string-p name
) 'base-char
'character
)))
939 (defun find-symbol (name &optional
(package (sane-package)))
940 "Return the symbol named STRING in PACKAGE. If such a symbol is found
941 then the second value is :INTERNAL, :EXTERNAL or :INHERITED to indicate
942 how the symbol is accessible. If no symbol is found then both values
944 (find/intern %find-symbol
)))
946 ;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
947 ;;; then create it, special-casing the keyword package.
948 ;;; If a new symbol is created, its print name will be an array of ELT-TYPE.
949 ;;; The fasloader always supplies NAME as a (SIMPLE-ARRAY <ELT-TYPE> 1),
950 ;;; but the reader uses a buffer of CHARACTER, which, based on a flag,
951 ;;; can be demoted to an array of BASE-CHAR.
952 (defun %intern
(name length package elt-type
)
953 (declare (simple-string name
) (index length
))
954 (multiple-value-bind (symbol where
) (%find-symbol name length package
)
956 (values symbol where
))
958 ;; Let's try again with a lock: the common case has the
959 ;; symbol already interned, handled by the first leg of the
960 ;; COND, but in case another thread is interning in
961 ;; parallel we need to check after grabbing the lock.
962 (with-package-graph ()
963 (setf (values symbol where
) (%find-symbol name length package
))
965 (values symbol where
)
967 (logically-readonlyize
968 (replace (make-string length
:element-type elt-type
)
970 (with-single-package-locked-error
971 (:package package
"interning ~A" symbol-name
)
972 (let ((symbol ; Symbol kind: 1=keyword, 2=other interned
973 (%make-symbol
(if (eq package
*keyword-package
*) 1 2)
975 ;; Simultaneous INTERN calls must not return an incompletely
976 ;; initialized object, so that
977 ;; (symbol-package (intern x #<pkg>)) = #<pkg>
978 (%set-symbol-package symbol package
)
979 (add-symbol (cond ((eq package
*keyword-package
*)
980 (%set-symbol-value symbol symbol
)
981 (package-external-symbols package
))
983 (package-internal-symbols package
)))
985 (values symbol nil
))))))))))
987 ;;; Check internal and external symbols, then scan down the list
988 ;;; of hashtables for inherited symbols.
989 (defun %find-symbol
(string length package
)
990 (declare (simple-string string
)
992 (let ((hash (compute-symbol-hash string length
)))
993 (declare (type hash hash
))
994 (with-symbol ((symbol) (package-internal-symbols package
) string length hash
)
995 (return-from %find-symbol
(values symbol
:internal
)))
996 (with-symbol ((symbol) (package-external-symbols package
) string length hash
)
997 (return-from %find-symbol
(values symbol
:external
)))
998 (let* ((tables (package-tables package
))
1001 ;; Try the most-recently-used table, then others.
1002 ;; TABLES is treated as circular for this purpose.
1003 (let* ((mru (package-mru-table-index package
))
1004 (start (if (< mru n
) mru
0))
1007 (with-symbol ((symbol) (locally (declare (optimize (safety 0)))
1010 (setf (package-mru-table-index package
) i
)
1011 (return-from %find-symbol
(values symbol
:inherited
)))
1012 (if (< (decf i
) 0) (setq i
(1- n
)))
1013 (if (= i start
) (return)))))))
1016 ;;; Similar to FIND-SYMBOL, but only looks for an external symbol.
1017 ;;; Return the symbol and T if found, otherwise two NILs.
1018 ;;; This is used for fast name-conflict checking in this file and symbol
1019 ;;; printing in the printer.
1020 ;;; An optimization is possible here: by accepting either a string or symbol,
1021 ;;; if the symbol's hash slot is nonzero, we can avoid COMPUTE-SYMBOL-HASH.
1022 (defun find-external-symbol (string package
)
1023 (declare (simple-string string
))
1024 (let* ((length (length string
))
1025 (hash (compute-symbol-hash string length
)))
1026 (declare (type index length
) (type hash hash
))
1027 (with-symbol ((symbol) (package-external-symbols package
) string length hash
)
1028 (return-from find-external-symbol
(values symbol t
))))
1031 (define-condition name-conflict
(reference-condition package-error
)
1032 ((function :initarg
:function
:reader name-conflict-function
)
1033 (datum :initarg
:datum
:reader name-conflict-datum
)
1034 (symbols :initarg
:symbols
:reader name-conflict-symbols
))
1035 (:default-initargs
:references
(list '(:ansi-cl
:section
(11 1 1 2 5))))
1038 (format s
"~@<~S ~S causes name-conflicts in ~S between the ~
1039 following symbols: ~2I~@:_~
1040 ~{~/sb-impl::print-symbol-with-prefix/~^, ~}~:@>"
1041 (name-conflict-function c
)
1042 (name-conflict-datum c
)
1043 (package-error-package c
)
1044 (name-conflict-symbols c
)))))
1046 (defun name-conflict (package function datum
&rest symbols
)
1048 (declare (ignore c
))
1049 (eq 'import function
))
1050 (use-or-export-p (c)
1051 (declare (ignore c
))
1052 (or (eq 'use-package function
)
1053 (eq 'export function
)))
1055 (car (remove datum symbols
))))
1056 (let ((pname (package-name package
)))
1058 (error 'name-conflict
:package package
:symbols symbols
1059 :function function
:datum datum
)
1060 ;; USE-PACKAGE and EXPORT
1065 (format s
"Keep ~S accessible in ~A (shadowing ~S)."
1066 (old-symbol) pname datum
))
1068 (format s
"Keep symbols already accessible in ~A (shadowing others)."
1070 :test use-or-export-p
1071 (dolist (s (remove-duplicates symbols
:test
#'string
=))
1072 (shadow (symbol-name s
) package
)))
1077 (format s
"Make ~S accessible in ~A (uninterning ~S)."
1078 datum pname
(old-symbol)))
1080 (format s
"Make newly exposed symbols accessible in ~A, ~
1081 uninterning old ones."
1083 :test use-or-export-p
1085 (when (eq s
(find-symbol (symbol-name s
) package
))
1086 (unintern s package
))))
1088 (shadowing-import-it ()
1090 (format s
"Shadowing-import ~S, uninterning ~S."
1091 datum
(old-symbol)))
1093 (shadowing-import datum package
))
1096 (format s
"Don't import ~S, keeping ~S."
1098 (car (remove datum symbols
))))
1100 ;; General case. This is exposed via SB-EXT.
1101 (resolve-conflict (chosen-symbol)
1102 :report
"Resolve conflict."
1105 (let* ((len (length symbols
))
1106 (nlen (length (write-to-string len
:base
10)))
1108 (format *query-io
* "~&~@<Select a symbol to be made accessible in ~
1109 package ~A:~2I~@:_~{~{~V,' D. ~
1110 ~/sb-impl::print-symbol-with-prefix/~}~@:_~}~
1112 (package-name package
)
1113 (loop for s in symbols
1115 collect
(list nlen i s
)))
1117 (format *query-io
* "~&Enter an integer (between 1 and ~D): " len
)
1118 (finish-output *query-io
*)
1119 (let ((i (parse-integer (read-line *query-io
*) :junk-allowed t
)))
1120 (when (and i
(<= 1 i len
))
1121 (return (list (nth (1- i
) symbols
))))))))
1122 (multiple-value-bind (package-symbol status
)
1123 (find-symbol (symbol-name chosen-symbol
) package
)
1124 (let* ((accessiblep status
) ; never NIL here
1125 (presentp (and accessiblep
1126 (not (eq :inherited status
)))))
1130 (if (eq package-symbol chosen-symbol
)
1131 (shadow (list package-symbol
) package
)
1132 (shadowing-import (list chosen-symbol
) package
))
1133 (shadowing-import (list chosen-symbol
) package
)))
1134 ((use-package export
)
1136 (if (eq package-symbol chosen-symbol
)
1137 (shadow (list package-symbol
) package
) ; CLHS 11.1.1.2.5
1138 (if (eq (symbol-package package-symbol
) package
)
1139 (unintern package-symbol package
) ; CLHS 11.1.1.2.5
1140 (shadowing-import (list chosen-symbol
) package
)))
1141 (shadowing-import (list chosen-symbol
) package
)))
1144 (if (eq package-symbol chosen-symbol
)
1145 nil
; re-importing the same symbol
1146 (shadowing-import (list chosen-symbol
) package
))
1147 (shadowing-import (list chosen-symbol
) package
)))))))))))
1149 ;;; If we are uninterning a shadowing symbol, then a name conflict can
1150 ;;; result, otherwise just nuke the symbol.
1151 (defun unintern (symbol &optional
(package (sane-package)))
1152 "Makes SYMBOL no longer present in PACKAGE. If SYMBOL was present then T is
1153 returned, otherwise NIL. If PACKAGE is SYMBOL's home package, then it is made
1155 (with-package-graph ()
1156 (let* ((package (find-undeleted-package-or-lose package
))
1157 (name (symbol-name symbol
))
1158 (shadowing-symbols (package-%shadowing-symbols package
)))
1159 (declare (list shadowing-symbols
))
1161 (with-single-package-locked-error ()
1162 (when (find-symbol name package
)
1163 (assert-package-unlocked package
"uninterning ~A" name
))
1165 ;; If a name conflict is revealed, give us a chance to
1166 ;; shadowing-import one of the accessible symbols.
1167 (when (member symbol shadowing-symbols
)
1169 (dolist (p (package-%use-list package
))
1170 (multiple-value-bind (s w
) (find-external-symbol name p
)
1171 ;; S should be derived as SYMBOL so that PUSHNEW can assume #'EQ
1172 ;; as the test, but it's not happening, so restate the obvious.
1173 (when w
(pushnew s cset
:test
#'eq
))))
1175 (apply #'name-conflict package
'unintern symbol cset
)
1176 (return-from unintern t
)))
1177 (setf (package-%shadowing-symbols package
)
1178 (remove symbol shadowing-symbols
)))
1180 (multiple-value-bind (s w
) (find-symbol name package
)
1181 (cond ((not (eq symbol s
)) nil
)
1182 ((or (eq w
:internal
) (eq w
:external
))
1183 (nuke-symbol (if (eq w
:internal
)
1184 (package-internal-symbols package
)
1185 (package-external-symbols package
))
1187 (if (eq (symbol-package symbol
) package
)
1188 (%set-symbol-package symbol nil
))
1192 ;;; Take a symbol-or-list-of-symbols and return a list, checking types.
1193 (defun symbol-listify (thing)
1194 (cond ((listp thing
)
1197 (signal-package-error nil
1198 "~S is not a symbol." s
)))
1200 ((symbolp thing
) (list thing
))
1202 (signal-package-error nil
1203 "~S is neither a symbol nor a list of symbols."
1206 (defun string-listify (thing)
1207 (mapcar #'string
(ensure-list thing
)))
1209 (defun export (symbols &optional
(package (sane-package)))
1210 "Exports SYMBOLS from PACKAGE, checking that no name conflicts result."
1211 (with-package-graph ()
1212 (let ((package (find-undeleted-package-or-lose package
))
1213 (symbols (symbol-listify symbols
))
1215 ;; Punt any symbols that are already external.
1216 (dolist (sym symbols
)
1217 (multiple-value-bind (s found
)
1218 (find-external-symbol (symbol-name sym
) package
)
1219 (unless (or (and found
(eq s sym
)) (member sym syms
))
1221 (with-single-package-locked-error ()
1223 (assert-package-unlocked package
"exporting symbol~P ~{~A~^, ~}"
1224 (length syms
) syms
))
1225 ;; Find symbols and packages with conflicts.
1226 (let ((used-by (package-%used-by-list package
)))
1228 (let ((name (symbol-name sym
)))
1230 (multiple-value-bind (s w
) (find-symbol name p
)
1233 (not (member s
(package-%shadowing-symbols p
))))
1234 ;; Beware: the name conflict is in package P, not in
1236 (name-conflict p
'export sym sym s
)))))))
1237 ;; Check that all symbols are accessible. If not, ask to import them.
1241 (multiple-value-bind (s w
) (find-symbol (symbol-name sym
) package
)
1242 (cond ((not (and w
(eq s sym
)))
1245 (push sym imports
)))))
1247 (signal-package-cerror
1249 (format nil
"~S these symbols into the ~A package."
1250 'import
(package-%name package
))
1251 "~@<These symbols are not accessible in the ~A package:~2I~_~S~@:>"
1252 (package-%name package
) missing
)
1253 (import missing package
))
1254 (import imports package
))
1256 ;; And now, three pages later, we export the suckers.
1257 (let ((internal (package-internal-symbols package
))
1258 (external (package-external-symbols package
)))
1260 (add-symbol external sym
)
1261 (nuke-symbol internal sym
))))
1264 ;;; Check that all symbols are accessible, then move from external to internal.
1265 (defun unexport (symbols &optional
(package (sane-package)))
1266 "Makes SYMBOLS no longer exported from PACKAGE."
1267 (with-package-graph ()
1268 (let ((package (find-undeleted-package-or-lose package
))
1269 (symbols (symbol-listify symbols
))
1271 (dolist (sym symbols
)
1272 (multiple-value-bind (s w
) (find-symbol (symbol-name sym
) package
)
1273 (cond ((or (not w
) (not (eq s sym
)))
1274 (signal-package-error
1276 "~S is not accessible in the ~A package."
1277 sym
(package-%name package
)))
1278 ((eq w
:external
) (pushnew sym syms
)))))
1279 (with-single-package-locked-error ()
1281 (assert-package-unlocked package
"unexporting symbol~P ~{~A~^, ~}"
1282 (length syms
) syms
))
1283 (let ((internal (package-internal-symbols package
))
1284 (external (package-external-symbols package
)))
1286 (add-symbol internal sym
)
1287 (nuke-symbol external sym
))))
1290 ;;; Check for name conflict caused by the import and let the user
1291 ;;; shadowing-import if there is.
1292 (defun import (symbols &optional
(package (sane-package)))
1293 "Make SYMBOLS accessible as internal symbols in PACKAGE. If a symbol is
1294 already accessible then it has no effect. If a name conflict would result from
1295 the importation, then a correctable error is signalled."
1296 (with-package-graph ()
1297 (let* ((package (find-undeleted-package-or-lose package
))
1298 (symbols (symbol-listify symbols
))
1299 (homeless (remove-if #'symbol-package symbols
))
1301 (with-single-package-locked-error ()
1302 (dolist (sym symbols
)
1303 (multiple-value-bind (s w
) (find-symbol (symbol-name sym
) package
)
1305 (let ((found (member sym syms
:test
#'string
=)))
1307 (when (not (eq (car found
) sym
))
1308 (setf syms
(remove (car found
) syms
))
1309 (name-conflict package
'import sym sym
(car found
)))
1312 (name-conflict package
'import sym sym s
))
1313 ((eq w
:inherited
) (push sym syms
)))))
1314 (when (or homeless syms
)
1315 (let ((union (delete-duplicates (append homeless syms
))))
1316 (assert-package-unlocked package
"importing symbol~P ~{~A~^, ~}"
1317 (length union
) union
)))
1318 ;; Add the new symbols to the internal hashtable.
1319 (let ((internal (package-internal-symbols package
)))
1321 (add-symbol internal sym
)))
1322 ;; If any of the symbols are uninterned, make them be owned by PACKAGE.
1323 (dolist (sym homeless
)
1324 (%set-symbol-package sym package
))
1327 ;;; If a conflicting symbol is present, unintern it, otherwise just
1328 ;;; stick the symbol in.
1329 (defun shadowing-import (symbols &optional
(package (sane-package)))
1330 "Import SYMBOLS into package, disregarding any name conflict. If
1331 a symbol of the same name is present, then it is uninterned."
1332 (with-package-graph ()
1333 (let* ((package (find-undeleted-package-or-lose package
))
1334 (internal (package-internal-symbols package
))
1335 (symbols (symbol-listify symbols
))
1336 (lock-asserted-p nil
))
1337 (with-single-package-locked-error ()
1338 (dolist (sym symbols
)
1339 (multiple-value-bind (s w
) (find-symbol (symbol-name sym
) package
)
1340 (unless (or lock-asserted-p
1342 (member s
(package-shadowing-symbols package
))))
1343 (assert-package-unlocked package
"shadowing-importing symbol~P ~
1344 ~{~A~^, ~}" (length symbols
) symbols
)
1345 (setf lock-asserted-p t
))
1346 (unless (and w
(not (eq w
:inherited
)) (eq s sym
))
1347 (when (or (eq w
:internal
) (eq w
:external
))
1348 ;; If it was shadowed, we don't want UNINTERN to flame out...
1349 (setf (package-%shadowing-symbols package
)
1350 (remove s
(the list
(package-%shadowing-symbols package
))))
1351 (unintern s package
))
1352 (add-symbol internal sym
))
1353 (pushnew sym
(package-%shadowing-symbols package
)))))))
1356 (defun shadow (symbols &optional
(package (sane-package)))
1357 "Make an internal symbol in PACKAGE with the same name as each of the
1358 specified SYMBOLS. If a symbol with the given name is already present in
1359 PACKAGE, then the existing symbol is placed in the shadowing symbols list if
1360 it is not already present."
1361 (with-package-graph ()
1362 (let* ((package (find-undeleted-package-or-lose package
))
1363 (internal (package-internal-symbols package
))
1364 (symbols (string-listify symbols
))
1365 (lock-asserted-p nil
))
1366 (flet ((present-p (w)
1367 (and w
(not (eq w
:inherited
)))))
1368 (with-single-package-locked-error ()
1369 (dolist (name symbols
)
1370 (multiple-value-bind (s w
) (find-symbol name package
)
1371 (unless (or lock-asserted-p
1373 (member s
(package-shadowing-symbols package
))))
1374 (assert-package-unlocked package
"shadowing symbol~P ~{~A~^, ~}"
1375 (length symbols
) symbols
)
1376 (setf lock-asserted-p t
))
1377 (unless (present-p w
)
1378 (setq s
(%make-symbol
2 name
)) ; 2 = random interned symbol
1379 (%set-symbol-package s package
)
1380 (add-symbol internal s
))
1381 (pushnew s
(package-%shadowing-symbols package
))))))))
1384 ;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
1385 (defun use-package (packages-to-use &optional
(package (sane-package)))
1386 "Add all the PACKAGES-TO-USE to the use list for PACKAGE so that the
1387 external symbols of the used packages are accessible as internal symbols in
1389 (with-package-graph ()
1390 (let ((packages (package-listify packages-to-use
))
1391 (package (find-undeleted-package-or-lose package
)))
1393 ;; Loop over each package, USE'ing one at a time...
1394 (with-single-package-locked-error ()
1395 (dolist (pkg packages
)
1396 (unless (member pkg
(package-%use-list package
))
1397 (assert-package-unlocked package
"using package~P ~{~A~^, ~}"
1398 (length packages
) packages
)
1399 (let ((shadowing-symbols (package-%shadowing-symbols package
))
1400 (use-list (package-%use-list package
)))
1402 ;; If the number of symbols already accessible is less
1403 ;; than the number to be inherited then it is faster to
1404 ;; run the test the other way. This is particularly
1405 ;; valuable in the case of a new package USEing
1408 ((< (+ (package-internal-symbol-count package
)
1409 (package-external-symbol-count package
)
1411 (dolist (p use-list res
)
1412 (incf res
(package-external-symbol-count p
)))))
1413 (package-external-symbol-count pkg
))
1414 (do-symbols (sym package
)
1415 (multiple-value-bind (s w
)
1416 (find-external-symbol (symbol-name sym
) pkg
)
1419 (not (member sym shadowing-symbols
)))
1420 (name-conflict package
'use-package pkg sym s
))))
1421 (dolist (p use-list
)
1422 (do-external-symbols (sym p
)
1423 (multiple-value-bind (s w
)
1424 (find-external-symbol (symbol-name sym
) pkg
)
1428 (find-symbol (symbol-name sym
) package
)
1429 shadowing-symbols
)))
1430 (name-conflict package
'use-package pkg sym s
))))))
1432 (do-external-symbols (sym pkg
)
1433 (multiple-value-bind (s w
)
1434 (find-symbol (symbol-name sym
) package
)
1437 (not (member s shadowing-symbols
)))
1438 (name-conflict package
'use-package pkg sym s
)))))))
1440 (push pkg
(package-%use-list package
))
1441 (setf (package-tables package
)
1442 (let ((tbls (package-tables package
)))
1443 (replace (make-array (1+ (length tbls
))
1444 :initial-element
(package-external-symbols pkg
))
1446 (push package
(package-%used-by-list pkg
)))))))
1449 (defun unuse-package (packages-to-unuse &optional
(package (sane-package)))
1450 "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
1451 (with-package-graph ()
1452 (let ((package (find-undeleted-package-or-lose package
))
1453 (packages (package-listify packages-to-unuse
)))
1454 (with-single-package-locked-error ()
1455 (dolist (p packages
)
1456 (when (member p
(package-use-list package
))
1457 (assert-package-unlocked package
"unusing package~P ~{~A~^, ~}"
1458 (length packages
) packages
))
1459 (setf (package-%use-list package
)
1460 (remove p
(the list
(package-%use-list package
))))
1461 (setf (package-tables package
)
1462 (delete (package-external-symbols p
)
1463 (package-tables package
)))
1464 (setf (package-%used-by-list p
)
1465 (remove package
(the list
(package-%used-by-list p
))))))
1468 (defun find-all-symbols (string-or-symbol)
1469 "Return a list of all symbols in the system having the specified name."
1470 (let ((string (string string-or-symbol
))
1472 (with-package-names (names)
1473 (maphash (lambda (k v
)
1474 (declare (ignore k
))
1475 (multiple-value-bind (s w
) (find-symbol string v
)
1476 (when w
(pushnew s res
))))
1480 ;;;; APROPOS and APROPOS-LIST
1482 (defun briefly-describe-symbol (symbol)
1485 (when (boundp symbol
)
1486 (write-string " (bound)"))
1487 (when (fboundp symbol
)
1488 (write-string " (fbound)")))
1490 (defun apropos-list (string-designator
1494 "Like APROPOS, except that it returns a list of the symbols found instead
1495 of describing them."
1496 (if package-designator
1497 (let ((package (find-undeleted-package-or-lose package-designator
))
1498 (string (stringify-string-designator string-designator
))
1500 (do-symbols (symbol package
)
1501 (when (and (or (not external-only
)
1502 (and (eq (symbol-package symbol
) package
)
1503 (eq (nth-value 1 (find-symbol (symbol-name symbol
)
1506 (search string
(symbol-name symbol
) :test
#'char-equal
))
1507 (pushnew symbol result
)))
1508 (sort result
#'string-lessp
))
1510 (mapcan (lambda (package)
1511 (apropos-list string-designator package external-only
))
1512 (sort (list-all-packages) #'string-lessp
:key
#'package-name
)))))
1514 (defun apropos (string-designator &optional package external-only
)
1515 "Briefly describe all symbols which contain the specified STRING.
1516 If PACKAGE is supplied then only describe symbols present in
1517 that package. If EXTERNAL-ONLY then only describe
1518 external symbols in the specified package."
1519 ;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
1520 ;; of some unnecessary consing; and the unnecessary consing shouldn't be an
1521 ;; issue, since this function is is only useful interactively anyway, and
1522 ;; we can cons and GC a lot faster than the typical user can read..
1523 (dolist (symbol (apropos-list string-designator package external-only
))
1524 (briefly-describe-symbol symbol
))
1527 ;;;; final initialization
1529 ;;;; Due to the relative difficulty - but not impossibility - of manipulating
1530 ;;;; package-hashtables in the cross-compilation host, all interning operations
1531 ;;;; are delayed until cold-init.
1532 ;;;; The cold loader (GENESIS) set *!INITIAL-SYMBOLS* to the target
1533 ;;;; representation of the hosts's *COLD-PACKAGE-SYMBOLS*.
1534 ;;;; The shape of this list is ((package . (externals . internals)) ...)
1535 (defvar *!initial-symbols
*)
1537 (defun !package-cold-init
()
1538 (setf *package-graph-lock
* (sb!thread
:make-mutex
:name
"Package Graph Lock")
1539 *package-names
* (make-hash-table :test
'equal
:synchronized t
))
1540 (with-package-names (names)
1541 (dolist (spec *!initial-symbols
*)
1542 (let ((pkg (car spec
)) (symbols (cdr spec
)))
1543 ;; the symbol MAKE-TABLE wouldn't magically disappear,
1544 ;; though its only use be to name an FLET in a function
1545 ;; hanging on an otherwise uninternable symbol. strange but true :-(
1546 (flet ((!make-table
(input)
1547 (let ((table (make-package-hashtable
1548 (length (the simple-vector input
)))))
1549 (dovector (symbol input table
)
1550 (add-symbol table symbol
)))))
1551 (setf (package-external-symbols pkg
) (!make-table
(car symbols
))
1552 (package-internal-symbols pkg
) (!make-table
(cdr symbols
))))
1553 (setf (package-%local-nicknames pkg
) nil
1554 (package-%locally-nicknamed-by pkg
) nil
1555 (package-source-location pkg
) nil
1556 (gethash (package-%name pkg
) names
) pkg
)
1557 (dolist (nick (package-%nicknames pkg
))
1558 (setf (gethash nick names
) pkg
))
1560 (setf (package-lock pkg
) nil
1561 (package-%implementation-packages pkg
) nil
))))
1563 ;; pass 2 - set the 'tables' slots only after all tables have been made
1564 (dolist (spec *!initial-symbols
*)
1565 (let ((pkg (car spec
)))
1566 (setf (package-tables pkg
)
1567 (map 'vector
#'package-external-symbols
(package-%use-list pkg
)))))
1569 (/show0
"about to MAKUNBOUND *!INITIAL-SYMBOLS*")
1570 (%makunbound
'*!initial-symbols
*)) ; (so that it gets GCed)
1572 ;;; support for WITH-PACKAGE-ITERATOR
1574 (defun package-iter-init (access-types pkg-designator-list
)
1575 (declare (type (integer 1 7) access-types
)) ; a nonzero bitmask over types
1576 (values (logior (ash access-types
3) #b11
) 0 #()
1577 (package-listify pkg-designator-list
)))
1579 ;; The STATE parameter is comprised of 4 packed fields
1580 ;; [0:1] = substate {0=internal,1=external,2=inherited,3=initial}
1581 ;; [2] = package with inherited symbols has shadowing symbols
1582 ;; [3:5] = enabling bits for {internal,external,inherited}
1583 ;; [6:] = index into 'package-tables'
1585 (defconstant +package-iter-check-shadows
+ #b000100
)
1587 (defun package-iter-step (start-state index sym-vec pkglist
)
1588 ;; the defknown isn't enough
1589 (declare (type fixnum start-state
) (type index index
)
1590 (type simple-vector sym-vec
) (type list pkglist
))
1591 (declare (optimize speed
))
1593 ((advance (state) ; STATE is the one just completed
1594 (case (logand state
#b11
)
1595 ;; Test :INHERITED first because the state repeats for a package
1596 ;; as many times as there are packages it uses. There are enough
1597 ;; bits to count up to 2^23 packages if fixnums are 30 bits.
1599 (when (desired-state-p 2)
1600 (let* ((tables (package-tables (this-package)))
1601 (next-state (the fixnum
(+ state
(ash 1 6))))
1602 (table-idx (ash next-state -
6)))
1603 (when (< table-idx
(length tables
))
1604 (return-from advance
; remain in state 2
1605 (start next-state
(svref tables table-idx
))))))
1607 (advance 3)) ; start on next package
1608 (1 ; finished externals, switch to inherited if desired
1609 (when (desired-state-p 2)
1610 (let ((tables (package-tables (this-package))))
1611 (when (plusp (length tables
)) ; inherited symbols
1612 (return-from advance
; enter state 2
1613 (start (if (package-%shadowing-symbols
(this-package))
1614 (logior 2 +package-iter-check-shadows
+) 2)
1615 (svref tables
0))))))
1616 (advance 2)) ; skip state 2
1617 (0 ; finished internals, switch to externals if desired
1618 (if (desired-state-p 1) ; enter state 1
1619 (start 1 (package-external-symbols (this-package)))
1620 (advance 1))) ; skip state 1
1622 (cond ((endp pkglist
) ; latch into returning NIL forever more
1623 (values 0 0 #() '() nil nil
))
1624 ((desired-state-p 0) ; enter state 0
1625 (start 0 (package-internal-symbols (this-package))))
1626 (t (advance 0)))))) ; skip state 0
1627 (desired-state-p (target-state)
1628 (logtest start-state
(ash 1 (+ target-state
3))))
1630 (truly-the package
(car pkglist
)))
1631 (start (next-state new-table
)
1632 (let ((symbols (package-hashtable-cells new-table
)))
1633 (package-iter-step (logior (mask-field (byte 3 3) start-state
)
1635 ;; assert that physical length was nonzero
1636 (the index
(length symbols
))
1638 (declare (inline desired-state-p this-package
))
1640 (advance start-state
)
1641 (macrolet ((scan (&optional
(guard t
))
1643 (let ((sym (aref sym-vec
(decf index
))))
1644 (when (and (pkg-symbol-valid-p sym
) ,guard
)
1645 (return (values start-state index sym-vec pkglist sym
1646 (aref #(:internal
:external
:inherited
)
1647 (logand start-state
3))))))
1649 (return (advance start-state
))))))
1650 (declare (optimize (sb!c
::insert-array-bounds-checks
0)))
1651 (if (logtest start-state
+package-iter-check-shadows
+)
1652 (let ((shadows (package-%shadowing-symbols
(this-package))))
1653 (scan (not (member sym shadows
:test
#'string
=))))
1656 (defun program-assert-symbol-home-package-unlocked (context symbol control
)
1658 (declare (ignore context symbol control
))
1660 (handler-bind ((package-lock-violation
1664 ;; FIXME: Code containing a lexically impermissible
1665 ;; violation causes both a warning AND an error.
1666 ;; The warning is enough. It's ugly that both happen.
1667 (warn "Compile-time package lock violation:~% ~A"
1669 (sb!c
:compiler-error condition
))
1671 (eval-error condition
))))))
1672 (with-single-package-locked-error (:symbol symbol control
))))