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")
26 (!begin-collecting-cold-init-forms)
29 (/show0 "entering !PACKAGE-COLD-INIT"))
31 ;;;; PACKAGE-HASHTABLE stuff
33 (def!method print-object ((table package-hashtable) stream)
34 (declare (type stream stream))
35 (print-unreadable-object (table stream :type t)
37 ":SIZE ~S :FREE ~S :DELETED ~S"
38 (package-hashtable-size table)
39 (package-hashtable-free table)
40 (package-hashtable-deleted table))))
42 ;;; the maximum density we allow in a package hashtable
43 (defconstant package-rehash-threshold 0.75)
45 ;;; Make a package hashtable having a prime number of entries at least
46 ;;; as great as (/ SIZE PACKAGE-REHASH-THRESHOLD). If RES is supplied,
47 ;;; then it is destructively modified to produce the result. This is
48 ;;; useful when changing the size, since there are many pointers to
50 (defun make-or-remake-package-hashtable (size
53 (flet ((actual-package-hashtable-size (size)
54 (loop for n of-type fixnum
55 from (logior (truncate size package-rehash-threshold) 1)
57 when (positive-primep n) return n)))
58 (let* ((n (actual-package-hashtable-size size))
59 (size (truncate (* n package-rehash-threshold)))
60 (table (make-array n))
62 :element-type '(unsigned-byte 8)
65 (setf (package-hashtable-table res) table
66 (package-hashtable-hash res) hash
67 (package-hashtable-size res) size
68 (package-hashtable-free res) size
69 (package-hashtable-deleted res) 0)
70 (setf res (%make-package-hashtable table hash size)))
73 ;;;; package locking operations, built conditionally on :sb-package-locks
77 (defun package-locked-p (package)
79 "Returns T when PACKAGE is locked, NIL otherwise. Signals an error
80 if PACKAGE doesn't designate a valid package."
81 (package-lock (find-undeleted-package-or-lose package)))
83 (defun lock-package (package)
85 "Locks PACKAGE and returns T. Has no effect if PACKAGE was already
86 locked. Signals an error if PACKAGE is not a valid package designator"
87 (setf (package-lock (find-undeleted-package-or-lose package)) t))
89 (defun unlock-package (package)
91 "Unlocks PACKAGE and returns T. Has no effect if PACKAGE was already
92 unlocked. Signals an error if PACKAGE is not a valid package designator."
93 (setf (package-lock (find-undeleted-package-or-lose package)) nil)
96 (defun package-implemented-by-list (package)
98 "Returns a list containing the implementation packages of
99 PACKAGE. Signals an error if PACKAGE is not a valid package designator."
100 (package-%implementation-packages (find-undeleted-package-or-lose package)))
102 (defun package-implements-list (package)
104 "Returns the packages that PACKAGE is an implementation package
105 of. Signals an error if PACKAGE is not a valid package designator."
106 (let ((package (find-undeleted-package-or-lose package)))
107 (loop for x in (list-all-packages)
108 when (member package (package-%implementation-packages x))
111 (defun add-implementation-package (packages-to-add
112 &optional (package *package*))
114 "Adds PACKAGES-TO-ADD as implementation packages of PACKAGE. Signals
115 an error if PACKAGE or any of the PACKAGES-TO-ADD is not a valid
117 (let ((package (find-undeleted-package-or-lose package))
118 (packages-to-add (package-listify packages-to-add)))
119 (setf (package-%implementation-packages package)
120 (union (package-%implementation-packages package)
121 (mapcar #'find-undeleted-package-or-lose packages-to-add)))))
123 (defun remove-implementation-package (packages-to-remove
124 &optional (package *package*))
126 "Removes PACKAGES-TO-REMOVE from the implementation packages of
127 PACKAGE. Signals an error if PACKAGE or any of the PACKAGES-TO-REMOVE
128 is not a valid package designator."
129 (let ((package (find-undeleted-package-or-lose package))
130 (packages-to-remove (package-listify packages-to-remove)))
131 (setf (package-%implementation-packages package)
133 (package-%implementation-packages package)
134 (mapcar #'find-undeleted-package-or-lose packages-to-remove)))))
136 (defmacro with-unlocked-packages ((&rest packages) &body forms)
138 "Unlocks PACKAGES for the dynamic scope of the body. Signals an
139 error if any of PACKAGES is not a valid package designator."
140 (with-unique-names (unlocked-packages)
141 `(let (,unlocked-packages)
144 (dolist (p ',packages)
145 (when (package-locked-p p)
146 (push p ,unlocked-packages)
149 (dolist (p ,unlocked-packages)
150 (when (find-package p)
151 (lock-package p)))))))
153 (defun package-lock-violation (package &key (symbol nil symbol-p)
154 format-control format-arguments)
155 (let* ((restart :continue)
156 (cl-violation-p (eq package *cl-package*))
158 (append (list (if symbol-p
159 'symbol-package-locked-error
160 'package-locked-error)
162 :format-control format-control
163 :format-arguments format-arguments)
164 (when symbol-p (list :symbol symbol))
166 (append '((:sbcl :node "Package Locks"))
168 '((:ansi-cl :section (11 1 2 1 2)))))))))
170 (apply #'cerror "Ignore the package lock." error-arguments)
172 :report "Ignore all package locks in the context of this operation."
173 (setf restart :ignore-all))
175 :report "Unlock the package."
176 (setf restart :unlock-package)))
179 (pushnew package *ignored-package-locks*))
181 (setf *ignored-package-locks* t))
183 (unlock-package package)))))
185 (defun package-lock-violation-p (package &optional (symbol nil symbolp))
186 ;; KLUDGE: (package-lock package) needs to be before
187 ;; comparison to *package*, since during cold init this gets
188 ;; called before *package* is bound -- but no package should
189 ;; be locked at that point.
191 (package-lock package)
192 ;; In package or implementation package
193 (not (or (eq package *package*)
194 (member *package* (package-%implementation-packages package))))
196 (not (eq t *ignored-package-locks*))
197 (or (eq :invalid *ignored-package-locks*)
198 (not (member package *ignored-package-locks*)))
199 ;; declarations for symbols
200 (not (and symbolp (member symbol (disabled-package-locks))))))
202 (defun disabled-package-locks ()
203 (if (boundp 'sb!c::*lexenv*)
204 (sb!c::lexenv-disabled-package-locks sb!c::*lexenv*)
205 sb!c::*disabled-package-locks*))
209 ;;;; more package-locking these are NOPs unless :sb-package-locks is
210 ;;;; in target features. Cross-compiler NOPs for these are in cross-misc.
212 ;;; The right way to establish a package lock context is
213 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR, defined in early-package.lisp
215 ;;; Must be used inside the dynamic contour established by
216 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR
217 (defun assert-package-unlocked (package &optional format-control
218 &rest format-arguments)
220 (declare (ignore format-control format-arguments))
222 (when (package-lock-violation-p package)
223 (package-lock-violation package
224 :format-control format-control
225 :format-arguments format-arguments))
228 ;;; Must be used inside the dynamic contour established by
229 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR.
231 ;;; FIXME: Maybe we should establish such contours for he toplevel
232 ;;; and others, so that %set-fdefinition and others could just use
234 (defun assert-symbol-home-package-unlocked (name format)
236 (declare (ignore format))
238 (let* ((symbol (etypecase name
240 (list (if (eq 'setf (first name))
242 ;; Skip (class-predicate foo), etc.
243 ;; FIXME: MOP and package-lock
244 ;; interaction needs to be thought about.
246 assert-symbol-home-package-unlocked
248 (package (symbol-package symbol)))
249 (when (package-lock-violation-p package symbol)
250 (package-lock-violation package
252 :format-control format
253 :format-arguments (list name))))
257 ;;;; miscellaneous PACKAGE operations
259 (def!method print-object ((package package) stream)
260 (let ((name (package-%name package)))
262 (print-unreadable-object (package stream :type t)
264 (print-unreadable-object (package stream :type t :identity t)
265 (write-string "(deleted)" stream)))))
267 ;;; ANSI says (in the definition of DELETE-PACKAGE) that these, and
268 ;;; most other operations, are unspecified for deleted packages. We
269 ;;; just do the easy thing and signal errors in that case.
270 (macrolet ((def (ext real)
271 `(defun ,ext (x) (,real (find-undeleted-package-or-lose x)))))
272 (def package-nicknames package-%nicknames)
273 (def package-use-list package-%use-list)
274 (def package-used-by-list package-%used-by-list)
275 (def package-shadowing-symbols package-%shadowing-symbols))
277 (defun %package-hashtable-symbol-count (table)
278 (let ((size (the fixnum
279 (- (package-hashtable-size table)
280 (package-hashtable-deleted table)))))
282 (- size (package-hashtable-free table)))))
284 (defun package-internal-symbol-count (package)
285 (%package-hashtable-symbol-count (package-internal-symbols package)))
287 (defun package-external-symbol-count (package)
288 (%package-hashtable-symbol-count (package-external-symbols package)))
290 (defvar *package* (error "*PACKAGE* should be initialized in cold load!")
291 #!+sb-doc "the current package")
292 ;;; FIXME: should be declared of type PACKAGE, with no NIL init form,
293 ;;; after I get around to cleaning up DOCUMENTATION
295 ;;; a map from package names to packages
296 (defvar *package-names*)
297 (declaim (type hash-table *package-names*))
299 (setf *package-names* (make-hash-table :test 'equal)))
301 ;;; This magical variable is T during initialization so that
302 ;;; USE-PACKAGE's of packages that don't yet exist quietly win. Such
303 ;;; packages are thrown onto the list *DEFERRED-USE-PACKAGES* so that
304 ;;; this can be fixed up later.
306 ;;; FIXME: This could be cleaned up the same way I do it in my package
307 ;;; hacking when setting up the cross-compiler. Then we wouldn't have
308 ;;; this extraneous global variable and annoying runtime tests on
309 ;;; package operations. (*DEFERRED-USE-PACKAGES* would also go away.)
310 (defvar *in-package-init*)
312 ;;; pending USE-PACKAGE arguments saved up while *IN-PACKAGE-INIT* is true
313 (defvar *!deferred-use-packages*)
315 (setf *!deferred-use-packages* nil))
317 (define-condition bootstrap-package-not-found (condition)
318 ((name :initarg :name :reader bootstrap-package-name)))
319 (defun debootstrap-package (&optional condition)
321 (find-restart-or-control-error 'debootstrap-package condition)))
323 (defun find-package (package-designator)
324 (flet ((find-package-from-string (string)
325 (declare (type string string))
326 (let ((packageoid (gethash string *package-names*)))
327 (when (and (null packageoid)
328 (not *in-package-init*) ; KLUDGE
329 (let ((mismatch (mismatch "SB!" string)))
330 (and mismatch (= mismatch 3))))
332 (signal 'bootstrap-package-not-found :name string)
333 (debootstrap-package ()
334 (return-from find-package
335 (if (string= string "SB!XC")
336 (find-package "COMMON-LISP")
338 (substitute #\- #\! string :count 1)))))))
340 (typecase package-designator
341 (package package-designator)
342 (symbol (find-package-from-string (symbol-name package-designator)))
343 (string (find-package-from-string package-designator))
344 (character (find-package-from-string (string package-designator)))
345 (t (error 'type-error
346 :datum package-designator
347 :expected-type '(or character package string symbol))))))
349 ;;; Return a list of packages given a package designator or list of
350 ;;; package designators, or die trying.
351 (defun package-listify (thing)
353 (dolist (thing (if (listp thing) thing (list thing)) res)
354 (push (find-undeleted-package-or-lose thing) res))))
356 ;;; Make a package name into a simple-string.
357 (defun package-namify (n)
358 (stringify-name n "package"))
360 ;;; ANSI specifies (in the definition of DELETE-PACKAGE) that PACKAGE-NAME
361 ;;; returns NIL (not an error) for a deleted package, so this is a special
362 ;;; case where we want to use bare %FIND-PACKAGE-OR-LOSE instead of
363 ;;; FIND-UNDELETED-PACKAGE-OR-LOSE.
364 (defun package-name (package-designator)
365 (package-%name (%find-package-or-lose package-designator)))
367 ;;;; operations on package hashtables
369 ;;; Compute a number from the sxhash of the pname and the length which
370 ;;; must be between 2 and 255.
371 (defmacro entry-hash (length sxhash)
377 (the fixnum (ash ,sxhash -8))
378 (the fixnum (ash ,sxhash -16))
379 (the fixnum (ash ,sxhash -19))))
382 ;;; FIXME: should be wrapped in EVAL-WHEN (COMPILE EXECUTE)
384 ;;; Add a symbol to a package hashtable. The symbol is assumed
385 ;;; not to be present.
386 (defun add-symbol (table symbol)
387 (let* ((vec (package-hashtable-table table))
388 (hash (package-hashtable-hash table))
390 (sxhash (%sxhash-simple-string (symbol-name symbol)))
391 (h2 (the fixnum (1+ (the fixnum (rem sxhash
392 (the fixnum (- len 2))))))))
393 (declare (fixnum len sxhash h2))
394 (cond ((zerop (the fixnum (package-hashtable-free table)))
395 (make-or-remake-package-hashtable (* (package-hashtable-size table)
398 (add-symbol table symbol)
401 (when (> (the fixnum (aref hash i)) 1)
402 (add-symbol table (svref vec i)))))
404 (do ((i (rem sxhash len) (rem (+ i h2) len)))
405 ((< (the fixnum (aref hash i)) 2)
406 (if (zerop (the fixnum (aref hash i)))
407 (decf (package-hashtable-free table))
408 (decf (package-hashtable-deleted table)))
409 (setf (svref vec i) symbol)
411 (entry-hash (length (symbol-name symbol))
413 (declare (fixnum i)))))))
415 ;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR
416 ;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR
417 ;;; is bound to the symbol. LENGTH and HASH are the length and sxhash
418 ;;; of STRING. ENTRY-HASH is the entry-hash of the string and length.
419 (defmacro with-symbol ((index-var symbol-var table string length sxhash
422 (let ((vec (gensym)) (hash (gensym)) (len (gensym)) (h2 (gensym))
423 (name (gensym)) (name-len (gensym)) (ehash (gensym)))
424 `(let* ((,vec (package-hashtable-table ,table))
425 (,hash (package-hashtable-hash ,table))
427 (,h2 (1+ (the index (rem (the index ,sxhash)
428 (the index (- ,len 2)))))))
429 (declare (type index ,len ,h2))
430 (prog ((,index-var (rem (the index ,sxhash) ,len))
432 (declare (type (or index null) ,index-var))
434 (setq ,ehash (aref ,hash ,index-var))
435 (cond ((eql ,ehash ,entry-hash)
436 (setq ,symbol-var (svref ,vec ,index-var))
437 (let* ((,name (symbol-name ,symbol-var))
438 (,name-len (length ,name)))
439 (declare (type index ,name-len))
440 (when (and (= ,name-len ,length)
441 (string= ,string ,name
446 (setq ,index-var nil)
448 (setq ,index-var (+ ,index-var ,h2))
449 (when (>= ,index-var ,len)
450 (setq ,index-var (- ,index-var ,len)))
453 (return (progn ,@forms))))))
455 ;;; Delete the entry for STRING in TABLE. The entry must exist.
456 (defun nuke-symbol (table string)
457 (declare (simple-string string))
458 (let* ((length (length string))
459 (hash (%sxhash-simple-string string))
460 (ehash (entry-hash length hash)))
461 (declare (type index length hash))
462 (with-symbol (index symbol table string length hash ehash)
463 (setf (aref (package-hashtable-hash table) index) 1)
464 (setf (aref (package-hashtable-table table) index) nil)
465 (incf (package-hashtable-deleted table)))))
467 ;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*.
468 ;;; If there is a conflict then give the user a chance to do
469 ;;; something about it.
470 (defun enter-new-nicknames (package nicknames)
471 (declare (type list nicknames))
472 (dolist (n nicknames)
473 (let* ((n (package-namify n))
474 (found (gethash n *package-names*)))
476 (setf (gethash n *package-names*) package)
477 (push n (package-%nicknames package)))
479 ((string= (the string (package-%name found)) n)
480 (error 'simple-package-error
482 :format-control "~S is a package name, so it cannot be a nickname for ~S."
483 :format-arguments (list n (package-%name package))))
485 (error 'simple-package-error
487 :format-control "~S is already a nickname for ~S."
488 :format-arguments (list n (package-%name found))))))))
490 (defun make-package (name &key
491 (use '#.*default-package-use-list*)
493 (internal-symbols 10)
494 (external-symbols 10))
497 "Make a new package having the specified NAME, NICKNAMES, and
498 USE list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are
499 estimates for the number of internal and external symbols which
500 will ultimately be present in the package. The default value of
501 USE is implementation-dependent, and in this implementation
503 *default-package-use-list*)
505 ;; Check for package name conflicts in name and nicknames, then
507 (when (find-package name)
508 ;; ANSI specifies that this error is correctable.
509 (cerror "Leave existing package alone."
510 "A package named ~S already exists" name))
511 (let* ((name (package-namify name))
512 (package (internal-make-package
514 :internal-symbols (make-or-remake-package-hashtable
516 :external-symbols (make-or-remake-package-hashtable
519 ;; Do a USE-PACKAGE for each thing in the USE list so that checking for
520 ;; conflicting exports among used packages is done.
521 (if *in-package-init*
522 (push (list use package) *!deferred-use-packages*)
523 (use-package use package))
525 ;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
526 ;; which would leave us with possibly-bad side effects from the earlier
527 ;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
528 ;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
529 ;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
530 ;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
532 (enter-new-nicknames package nicknames)
533 (setf (gethash name *package-names*) package)))
535 ;;; Change the name if we can, blast any old nicknames and then
536 ;;; add in any new ones.
538 ;;; FIXME: ANSI claims that NAME is a package designator (not just a
539 ;;; string designator -- weird). Thus, NAME could
540 ;;; be a package instead of a string. Presumably then we should not change
541 ;;; the package name if NAME is the same package that's referred to by PACKAGE.
542 ;;; If it's a *different* package, we should probably signal an error.
543 ;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
544 (defun rename-package (package name &optional (nicknames ()))
546 "Changes the name and nicknames for a package."
547 (let* ((package (find-undeleted-package-or-lose package))
548 (name (package-namify name))
549 (found (find-package name))
550 (nicks (mapcar #'string nicknames)))
551 (unless (or (not found) (eq found package))
552 (error 'simple-package-error
554 :format-control "A package named ~S already exists."
555 :format-arguments (list name)))
556 (with-single-package-locked-error ()
557 (unless (and (string= name (package-name package))
558 (null (set-difference nicks (package-nicknames package)
560 (assert-package-unlocked package "rename as ~A~@[ with nickname~P ~
562 name (length nicks) nicks))
564 (remhash (package-%name package) *package-names*)
565 (dolist (n (package-%nicknames package))
566 (remhash n *package-names*))
567 (setf (package-%name package) name
568 (gethash name *package-names*) package
569 (package-%nicknames package) ())
570 (enter-new-nicknames package nicknames))
573 (defun delete-package (package-designator)
575 "Delete the package designated by PACKAGE-DESIGNATOR from the package
576 system data structures."
577 (let ((package (if (packagep package-designator)
579 (find-package package-designator))))
581 ;; This continuable error is required by ANSI.
585 'simple-package-error
586 :package package-designator
587 :format-control "There is no package named ~S."
588 :format-arguments (list package-designator))
590 ((not (package-name package)) ; already deleted
593 (with-single-package-locked-error
594 (:package package "deleting package ~A" package)
595 (let ((use-list (package-used-by-list package)))
597 ;; This continuable error is specified by ANSI.
599 "Remove dependency in other packages."
601 'simple-package-error
604 "~@<Package ~S is used by package~P:~2I~_~S~@:>"
605 :format-arguments (list (package-name package)
607 (mapcar #'package-name use-list))))
609 (unuse-package package p))))
610 (dolist (used (package-use-list package))
611 (unuse-package used package))
612 (do-symbols (sym package)
613 (unintern sym package))
614 (remhash (package-name package) *package-names*)
615 (dolist (nick (package-nicknames package))
616 (remhash nick *package-names*))
617 (setf (package-%name package) nil
618 ;; Setting PACKAGE-%NAME to NIL is required in order to
619 ;; make PACKAGE-NAME return NIL for a deleted package as
620 ;; ANSI requires. Setting the other slots to NIL
621 ;; and blowing away the PACKAGE-HASHTABLES is just done
622 ;; for tidiness and to help the GC.
623 (package-%nicknames package) nil
624 (package-%use-list package) nil
625 (package-tables package) nil
626 (package-%shadowing-symbols package) nil
627 (package-internal-symbols package)
628 (make-or-remake-package-hashtable 0)
629 (package-external-symbols package)
630 (make-or-remake-package-hashtable 0))
633 (defun list-all-packages ()
635 "Return a list of all existing packages."
637 (maphash (lambda (k v)
643 (defun intern (name &optional (package (sane-package)))
645 "Return a symbol in PACKAGE having the specified NAME, creating it
647 ;; We just simple-stringify the name and call INTERN*, where the real
649 (let ((name (if (simple-string-p name)
651 (coerce name 'simple-string)))
652 (package (find-undeleted-package-or-lose package)))
653 (declare (simple-string name))
658 (defun find-symbol (name &optional (package (sane-package)))
660 "Return the symbol named STRING in PACKAGE. If such a symbol is found
661 then the second value is :INTERNAL, :EXTERNAL or :INHERITED to indicate
662 how the symbol is accessible. If no symbol is found then both values
664 ;; We just simple-stringify the name and call FIND-SYMBOL*, where the
666 (let ((name (if (simple-string-p name) name (coerce name 'simple-string))))
667 (declare (simple-string name))
670 (find-undeleted-package-or-lose package))))
672 ;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
673 ;;; then create it, special-casing the keyword package.
674 (defun intern* (name length package)
675 (declare (simple-string name))
676 (multiple-value-bind (symbol where) (find-symbol* name length package)
678 (values symbol where))
680 (let ((symbol-name (subseq name 0 length)))
681 (with-single-package-locked-error
682 (:package package "interning ~A" symbol-name)
683 (let ((symbol (make-symbol symbol-name)))
684 (%set-symbol-package symbol package)
685 (cond ((eq package *keyword-package*)
686 (add-symbol (package-external-symbols package) symbol)
687 (%set-symbol-value symbol symbol))
689 (add-symbol (package-internal-symbols package) symbol)))
690 (values symbol nil))))))))
692 ;;; Check internal and external symbols, then scan down the list
693 ;;; of hashtables for inherited symbols. When an inherited symbol
694 ;;; is found pull that table to the beginning of the list.
695 (defun find-symbol* (string length package)
696 (declare (simple-string string)
698 (let* ((hash (%sxhash-simple-substring string length))
699 (ehash (entry-hash length hash)))
700 (declare (type index hash ehash))
701 (with-symbol (found symbol (package-internal-symbols package)
702 string length hash ehash)
704 (return-from find-symbol* (values symbol :internal))))
705 (with-symbol (found symbol (package-external-symbols package)
706 string length hash ehash)
708 (return-from find-symbol* (values symbol :external))))
709 (let ((head (package-tables package)))
710 (do ((prev head table)
711 (table (cdr head) (cdr table)))
712 ((null table) (values nil nil))
713 (with-symbol (found symbol (car table) string length hash ehash)
715 (unless (eq prev head)
716 (shiftf (cdr prev) (cdr table) (cdr head) table))
717 (return-from find-symbol* (values symbol :inherited))))))))
719 ;;; Similar to FIND-SYMBOL, but only looks for an external symbol.
720 ;;; This is used for fast name-conflict checking in this file and symbol
721 ;;; printing in the printer.
722 (defun find-external-symbol (string package)
723 (declare (simple-string string))
724 (let* ((length (length string))
725 (hash (%sxhash-simple-string string))
726 (ehash (entry-hash length hash)))
727 (declare (type index length hash))
728 (with-symbol (found symbol (package-external-symbols package)
729 string length hash ehash)
730 (values symbol found))))
732 (define-condition name-conflict (reference-condition package-error)
733 ((function :initarg :function :reader name-conflict-function)
734 (datum :initarg :datum :reader name-conflict-datum)
735 (symbols :initarg :symbols :reader name-conflict-symbols))
736 (:default-initargs :references (list '(:ansi-cl :section (11 1 1 2 5))))
739 (format s "~@<~S ~S causes name-conflicts in ~S between the ~
740 following symbols:~2I~@:_~{~S~^, ~}~:@>"
741 (name-conflict-function c)
742 (name-conflict-datum c)
743 (package-error-package c)
744 (name-conflict-symbols c)))))
746 (defun name-conflict (package function datum &rest symbols)
748 (error 'name-conflict :package package :symbols symbols
749 :function function :datum datum)
750 (resolve-conflict (s)
751 :report "Resolve conflict."
754 (let* ((len (length symbols))
755 (nlen (length (write-to-string len :base 10))))
756 (format *query-io* "~&~@<Select a symbol to be made accessible in ~
757 package ~A:~2I~@:_~{~{~V,' D. ~S~}~@:_~}~@:>"
758 (package-name package)
759 (loop for s in symbols
761 collect (list nlen i s)))
763 (format *query-io* "~&Enter an integer (between 1 and ~D): " len)
764 (finish-output *query-io*)
765 (let ((i (parse-integer (read-line *query-io*) :junk-allowed t)))
766 (when (and i (<= 1 i len))
767 (return (list (nth (1- i) symbols))))))))
768 (multiple-value-bind (symbol status)
769 (find-symbol (symbol-name s) package)
770 (declare (ignore status)) ; FIXME: is that true?
774 (shadow symbol package)
775 (unintern symbol package)))
777 (shadowing-import s package))
781 (shadowing-import s package)))
785 (shadowing-import s package))))))))
787 #+nil ; this solution gives a variable number of restarts instead, but
788 ; no good way of programmatically choosing between them.
789 (defun name-conflict (package function datum &rest symbols)
790 (let ((condition (make-condition 'name-conflict
791 :package package :symbols symbols
792 :function function :datum datum)))
793 ;; this is a gross violation of modularity, but I can't see any
794 ;; other way to have a variable number of restarts.
795 (let ((*restart-clusters*
799 (multiple-value-bind (accessible-symbol status)
800 (find-symbol (symbol-name s) package)
803 ((eq s accessible-symbol)
807 :name (make-symbol "SHADOWING-IMPORT")
809 (shadowing-import s package)
810 (return-from name-conflict))
813 (format stream "Shadowing-import ~S into ~A."
814 s (package-%name package))))))
815 ((:internal :external)
816 (aver (= (length symbols) 2))
817 ;; ARGH! FIXME: this unintern restart can
818 ;; _still_ leave the system in an
819 ;; unsatisfactory state: if the symbol is a
820 ;; external symbol of a package which is
821 ;; already used by this package, and has also
822 ;; been imported, then uninterning it from this
823 ;; package will still leave it visible!
825 ;; (DEFPACKAGE "FOO" (:EXPORT "SYM"))
826 ;; (DEFPACKAGE "BAR" (:EXPORT "SYM"))
827 ;; (DEFPACKAGE "BAZ" (:USE "FOO"))
828 ;; (IMPORT 'FOO:SYM "BAZ")
829 ;; (USE-PACKAGE "BAR" "BAZ")
831 ;; Now (UNINTERN 'FOO:SYM "BAZ") doesn't
832 ;; resolve the conflict. :-(
834 ;; -- CSR, 2004-10-20
836 :name (make-symbol "UNINTERN")
840 (find s symbols :test-not #'eq)
842 (return-from name-conflict))
846 "Unintern ~S from ~A and import ~S."
848 (package-%name package)
849 (find s symbols :test-not #'eq))))))))
850 (t (list (make-restart
851 :name (make-symbol "SHADOWING-IMPORT")
853 (shadowing-import s package)
854 (return-from name-conflict))
857 (format stream "Shadowing-import ~S into ~A."
858 s (package-%name package)))))))))
860 *restart-clusters*)))
861 (with-condition-restarts condition (car *restart-clusters*)
862 (with-simple-restart (abort "Leave action undone.")
863 (error condition))))))
865 ;;; If we are uninterning a shadowing symbol, then a name conflict can
866 ;;; result, otherwise just nuke the symbol.
867 (defun unintern (symbol &optional (package (sane-package)))
869 "Makes SYMBOL no longer present in PACKAGE. If SYMBOL was present
870 then T is returned, otherwise NIL. If PACKAGE is SYMBOL's home
871 package, then it is made uninterned."
872 (let* ((package (find-undeleted-package-or-lose package))
873 (name (symbol-name symbol))
874 (shadowing-symbols (package-%shadowing-symbols package)))
875 (declare (list shadowing-symbols))
877 (with-single-package-locked-error ()
878 (when (find-symbol name package)
879 (assert-package-unlocked package "uninterning ~A" name))
881 ;; If a name conflict is revealed, give us a chance to
882 ;; shadowing-import one of the accessible symbols.
883 (when (member symbol shadowing-symbols)
885 (dolist (p (package-%use-list package))
886 (multiple-value-bind (s w) (find-external-symbol name p)
887 (when w (pushnew s cset))))
889 (apply #'name-conflict package 'unintern symbol cset)
890 (return-from unintern t)))
891 (setf (package-%shadowing-symbols package)
892 (remove symbol shadowing-symbols)))
894 (multiple-value-bind (s w) (find-symbol name package)
896 (cond ((or (eq w :internal) (eq w :external))
897 (nuke-symbol (if (eq w :internal)
898 (package-internal-symbols package)
899 (package-external-symbols package))
901 (if (eq (symbol-package symbol) package)
902 (%set-symbol-package symbol nil))
906 ;;; Take a symbol-or-list-of-symbols and return a list, checking types.
907 (defun symbol-listify (thing)
910 (unless (symbolp s) (error "~S is not a symbol." s)))
912 ((symbolp thing) (list thing))
914 (error "~S is neither a symbol nor a list of symbols." thing))))
916 (defun string-listify (thing)
917 (mapcar #'string (if (listp thing)
921 ;;; This is like UNINTERN, except if SYMBOL is inherited, it chases
922 ;;; down the package it is inherited from and uninterns it there. Used
923 ;;; for name-conflict resolution. Shadowing symbols are not uninterned
924 ;;; since they do not cause conflicts.
925 (defun moby-unintern (symbol package)
926 (unless (member symbol (package-%shadowing-symbols package))
927 (or (unintern symbol package)
928 (let ((name (symbol-name symbol)))
929 (multiple-value-bind (s w) (find-symbol name package)
931 (when (eq w :inherited)
932 (dolist (q (package-%use-list package))
933 (multiple-value-bind (u x) (find-external-symbol name q)
939 (defun export (symbols &optional (package (sane-package)))
941 "Exports SYMBOLS from PACKAGE, checking that no name conflicts result."
942 (let ((package (find-undeleted-package-or-lose package))
944 ;; Punt any symbols that are already external.
945 (dolist (sym (symbol-listify symbols))
946 (multiple-value-bind (s w)
947 (find-external-symbol (symbol-name sym) package)
949 (unless (or w (member sym syms))
951 (with-single-package-locked-error ()
953 (assert-package-unlocked package "exporting symbol~P ~{~A~^, ~}"
955 ;; Find symbols and packages with conflicts.
956 (let ((used-by (package-%used-by-list package))
959 (let ((name (symbol-name sym)))
961 (multiple-value-bind (s w) (find-symbol name p)
964 (not (member s (package-%shadowing-symbols p))))
965 ;; Beware: the name conflict is in package P, not in
967 (name-conflict p 'export sym sym s)
968 (pushnew sym cset))))))
970 (setq syms (set-difference syms cset))))
971 ;; Check that all symbols are accessible. If not, ask to import them.
975 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
976 (cond ((not (and w (eq s sym)))
979 (push sym imports)))))
982 "~S these symbols into the ~A package."
984 'simple-package-error
987 "~@<These symbols are not accessible in the ~A package:~2I~_~S~@:>"
988 :format-arguments (list (package-%name package) missing))
989 'import (package-%name package))
990 (import missing package))
991 (import imports package))
993 ;; And now, three pages later, we export the suckers.
994 (let ((internal (package-internal-symbols package))
995 (external (package-external-symbols package)))
997 (nuke-symbol internal (symbol-name sym))
998 (add-symbol external sym))))
1001 ;;; Check that all symbols are accessible, then move from external to internal.
1002 (defun unexport (symbols &optional (package (sane-package)))
1004 "Makes SYMBOLS no longer exported from PACKAGE."
1005 (let ((package (find-undeleted-package-or-lose package))
1007 (dolist (sym (symbol-listify symbols))
1008 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1009 (cond ((or (not w) (not (eq s sym)))
1010 (error 'simple-package-error
1012 :format-control "~S is not accessible in the ~A package."
1013 :format-arguments (list sym (package-%name package))))
1014 ((eq w :external) (pushnew sym syms)))))
1015 (with-single-package-locked-error ()
1017 (assert-package-unlocked package "unexporting symbol~P ~{~A~^, ~}"
1018 (length syms) syms))
1019 (let ((internal (package-internal-symbols package))
1020 (external (package-external-symbols package)))
1022 (add-symbol internal sym)
1023 (nuke-symbol external (symbol-name sym)))))
1026 ;;; Check for name conflict caused by the import and let the user
1027 ;;; shadowing-import if there is.
1028 (defun import (symbols &optional (package (sane-package)))
1030 "Make SYMBOLS accessible as internal symbols in PACKAGE. If a symbol
1031 is already accessible then it has no effect. If a name conflict
1032 would result from the importation, then a correctable error is signalled."
1033 (let* ((package (find-undeleted-package-or-lose package))
1034 (symbols (symbol-listify symbols))
1035 (homeless (remove-if #'symbol-package symbols))
1037 (with-single-package-locked-error ()
1038 (dolist (sym symbols)
1039 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1041 (let ((found (member sym syms :test #'string=)))
1043 (when (not (eq (car found) sym))
1044 (name-conflict package 'import sym sym (car found)))
1047 (name-conflict package 'import sym sym s))
1048 ((eq w :inherited) (push sym syms)))))
1049 (when (or homeless syms)
1050 (let ((union (delete-duplicates (append homeless syms))))
1051 (assert-package-unlocked package "importing symbol~P ~{~A~^, ~}"
1052 (length union) union)))
1053 ;; Add the new symbols to the internal hashtable.
1054 (let ((internal (package-internal-symbols package)))
1056 (add-symbol internal sym)))
1057 ;; If any of the symbols are uninterned, make them be owned by PACKAGE.
1058 (dolist (sym homeless)
1059 (%set-symbol-package sym package))
1062 ;;; If a conflicting symbol is present, unintern it, otherwise just
1063 ;;; stick the symbol in.
1064 (defun shadowing-import (symbols &optional (package (sane-package)))
1066 "Import SYMBOLS into package, disregarding any name conflict. If
1067 a symbol of the same name is present, then it is uninterned."
1068 (let* ((package (find-undeleted-package-or-lose package))
1069 (internal (package-internal-symbols package))
1070 (symbols (symbol-listify symbols))
1071 (lock-asserted-p nil))
1072 (with-single-package-locked-error ()
1073 (dolist (sym symbols)
1074 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1075 (unless (or lock-asserted-p
1077 (member s (package-shadowing-symbols package))))
1078 (assert-package-unlocked package "shadowing-importing symbol~P ~
1079 ~{~A~^, ~}" (length symbols) symbols)
1080 (setf lock-asserted-p t))
1081 (unless (and w (not (eq w :inherited)) (eq s sym))
1082 (when (or (eq w :internal) (eq w :external))
1083 ;; If it was shadowed, we don't want UNINTERN to flame out...
1084 (setf (package-%shadowing-symbols package)
1085 (remove s (the list (package-%shadowing-symbols package))))
1086 (unintern s package))
1087 (add-symbol internal sym))
1088 (pushnew sym (package-%shadowing-symbols package))))))
1091 (defun shadow (symbols &optional (package (sane-package)))
1093 "Make an internal symbol in PACKAGE with the same name as each of
1094 the specified SYMBOLS. If a symbol with the given name is already
1095 present in PACKAGE, then the existing symbol is placed in the
1096 shadowing symbols list if it is not already present."
1097 (let* ((package (find-undeleted-package-or-lose package))
1098 (internal (package-internal-symbols package))
1099 (symbols (string-listify symbols))
1100 (lock-asserted-p nil))
1101 (flet ((present-p (w)
1102 (and w (not (eq w :inherited)))))
1103 (with-single-package-locked-error ()
1104 (dolist (name symbols)
1105 (multiple-value-bind (s w) (find-symbol name package)
1106 (unless (or lock-asserted-p
1108 (member s (package-shadowing-symbols package))))
1109 (assert-package-unlocked package "shadowing symbol~P ~{~A~^, ~}"
1110 (length symbols) symbols)
1111 (setf lock-asserted-p t))
1112 (unless (present-p w)
1113 (setq s (make-symbol name))
1114 (%set-symbol-package s package)
1115 (add-symbol internal s))
1116 (pushnew s (package-%shadowing-symbols package)))))))
1119 ;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
1120 (defun use-package (packages-to-use &optional (package (sane-package)))
1122 "Add all the PACKAGES-TO-USE to the use list for PACKAGE so that
1123 the external symbols of the used packages are accessible as internal
1124 symbols in PACKAGE."
1125 (let ((packages (package-listify packages-to-use))
1126 (package (find-undeleted-package-or-lose package)))
1128 ;; Loop over each package, USE'ing one at a time...
1129 (with-single-package-locked-error ()
1130 (dolist (pkg packages)
1131 (unless (member pkg (package-%use-list package))
1132 (assert-package-unlocked package "using package~P ~{~A~^, ~}"
1133 (length packages) packages)
1134 (let ((shadowing-symbols (package-%shadowing-symbols package))
1135 (use-list (package-%use-list package)))
1137 ;; If the number of symbols already accessible is less
1138 ;; than the number to be inherited then it is faster to
1139 ;; run the test the other way. This is particularly
1140 ;; valuable in the case of a new package USEing
1143 ((< (+ (package-internal-symbol-count package)
1144 (package-external-symbol-count package)
1146 (dolist (p use-list res)
1147 (incf res (package-external-symbol-count p)))))
1148 (package-external-symbol-count pkg))
1149 (do-symbols (sym package)
1150 (multiple-value-bind (s w)
1151 (find-external-symbol (symbol-name sym) pkg)
1154 (not (member sym shadowing-symbols)))
1155 (name-conflict package 'use-package pkg sym s))))
1156 (dolist (p use-list)
1157 (do-external-symbols (sym p)
1158 (multiple-value-bind (s w)
1159 (find-external-symbol (symbol-name sym) pkg)
1163 (find-symbol (symbol-name sym) package)
1164 shadowing-symbols)))
1165 (name-conflict package 'use-package pkg sym s))))))
1167 (do-external-symbols (sym pkg)
1168 (multiple-value-bind (s w)
1169 (find-symbol (symbol-name sym) package)
1172 (not (member s shadowing-symbols)))
1173 (name-conflict package 'use-package pkg sym s)))))))
1175 (push pkg (package-%use-list package))
1176 (push (package-external-symbols pkg) (cdr (package-tables package)))
1177 (push package (package-%used-by-list pkg))))))
1180 (defun unuse-package (packages-to-unuse &optional (package (sane-package)))
1182 "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
1183 (let ((package (find-undeleted-package-or-lose package))
1184 (packages (package-listify packages-to-unuse)))
1185 (with-single-package-locked-error ()
1186 (dolist (p packages)
1187 (when (member p (package-use-list package))
1188 (assert-package-unlocked package "unusing package~P ~{~A~^, ~}"
1189 (length packages) packages))
1190 (setf (package-%use-list package)
1191 (remove p (the list (package-%use-list package))))
1192 (setf (package-tables package)
1193 (delete (package-external-symbols p)
1194 (the list (package-tables package))))
1195 (setf (package-%used-by-list p)
1196 (remove package (the list (package-%used-by-list p))))))
1199 (defun find-all-symbols (string-or-symbol)
1201 "Return a list of all symbols in the system having the specified name."
1202 (let ((string (string string-or-symbol))
1204 (maphash (lambda (k v)
1205 (declare (ignore k))
1206 (multiple-value-bind (s w) (find-symbol string v)
1207 (when w (pushnew s res))))
1211 ;;;; APROPOS and APROPOS-LIST
1213 (defun briefly-describe-symbol (symbol)
1216 (when (boundp symbol)
1217 (write-string " (bound)"))
1218 (when (fboundp symbol)
1219 (write-string " (fbound)")))
1221 (defun apropos-list (string-designator
1226 "Like APROPOS, except that it returns a list of the symbols found instead
1227 of describing them."
1228 (if package-designator
1229 (let ((package (find-undeleted-package-or-lose package-designator))
1230 (string (stringify-name string-designator "APROPOS search"))
1232 (do-symbols (symbol package)
1233 (when (and (eq (symbol-package symbol) package)
1234 (or (not external-only)
1235 (eq (nth-value 1 (find-symbol (symbol-name symbol)
1238 (search string (symbol-name symbol) :test #'char-equal))
1239 (push symbol result)))
1241 (mapcan (lambda (package)
1242 (apropos-list string-designator package external-only))
1243 (list-all-packages))))
1245 (defun apropos (string-designator &optional package external-only)
1247 "Briefly describe all symbols which contain the specified STRING.
1248 If PACKAGE is supplied then only describe symbols present in
1249 that package. If EXTERNAL-ONLY then only describe
1250 external symbols in the specified package."
1251 ;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
1252 ;; of some unnecessary consing; and the unnecessary consing shouldn't be an
1253 ;; issue, since this function is is only useful interactively anyway, and
1254 ;; we can cons and GC a lot faster than the typical user can read..
1255 (dolist (symbol (apropos-list string-designator package external-only))
1256 (briefly-describe-symbol symbol))
1259 ;;;; final initialization
1261 ;;;; The cold loader (GENESIS) makes the data structure in
1262 ;;;; *!INITIAL-SYMBOLS*. We grovel over it, making the specified
1263 ;;;; packages and interning the symbols. For a description of the
1264 ;;;; format of *!INITIAL-SYMBOLS*, see the GENESIS source.
1266 (defvar *!initial-symbols*)
1270 (setq *in-package-init* t)
1272 (/show0 "about to loop over *!INITIAL-SYMBOLS* to make packages")
1273 (dolist (spec *!initial-symbols*)
1274 (let* ((pkg (apply #'make-package (first spec)))
1275 (internal (package-internal-symbols pkg))
1276 (external (package-external-symbols pkg)))
1277 (/show0 "back from MAKE-PACKAGE, PACKAGE-NAME=..")
1278 (/primitive-print (package-name pkg))
1280 ;; Put internal symbols in the internal hashtable and set package.
1281 (dolist (symbol (second spec))
1282 (add-symbol internal symbol)
1283 (%set-symbol-package symbol pkg))
1285 ;; External symbols same, only go in external table.
1286 (dolist (symbol (third spec))
1287 (add-symbol external symbol)
1288 (%set-symbol-package symbol pkg))
1290 ;; Don't set package for imported symbols.
1291 (dolist (symbol (fourth spec))
1292 (add-symbol internal symbol))
1293 (dolist (symbol (fifth spec))
1294 (add-symbol external symbol))
1296 ;; Put shadowing symbols in the shadowing symbols list.
1297 (setf (package-%shadowing-symbols pkg) (sixth spec))
1298 ;; Set the package documentation
1299 (setf (package-doc-string pkg) (seventh spec))))
1301 ;; FIXME: These assignments are also done at toplevel in
1302 ;; boot-extensions.lisp. They should probably only be done once.
1303 (/show0 "setting up *CL-PACKAGE* and *KEYWORD-PACKAGE*")
1304 (setq *cl-package* (find-package "COMMON-LISP"))
1305 (setq *keyword-package* (find-package "KEYWORD"))
1307 (/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*")
1308 (makunbound '*!initial-symbols*) ; (so that it gets GCed)
1310 ;; Make some other packages that should be around in the cold load.
1311 ;; The COMMON-LISP-USER package is required by the ANSI standard,
1312 ;; but not completely specified by it, so in the cross-compilation
1313 ;; host Lisp it could contain various symbols, USE-PACKAGEs, or
1314 ;; nicknames that we don't want in our target SBCL. For that reason,
1315 ;; we handle it specially, not dumping the host Lisp version at
1317 (aver (not (find-package "COMMON-LISP-USER")))
1318 ;; ..but instead making our own from scratch here.
1319 (/show0 "about to MAKE-PACKAGE COMMON-LISP-USER")
1320 (make-package "COMMON-LISP-USER"
1321 :nicknames '("CL-USER")
1322 :use '("COMMON-LISP"
1323 ;; ANSI encourages us to put extension packages
1324 ;; in the USE list of COMMON-LISP-USER.
1325 "SB!ALIEN" "SB!ALIEN" "SB!DEBUG"
1326 "SB!EXT" "SB!GRAY" "SB!PROFILE"))
1328 ;; Now do the *!DEFERRED-USE-PACKAGES*.
1329 (/show0 "about to do *!DEFERRED-USE-PACKAGES*")
1330 (dolist (args *!deferred-use-packages*)
1331 (apply #'use-package args))
1333 ;; The Age Of Magic is over, we can behave ANSIly henceforth.
1334 (/show0 "about to SETQ *IN-PACKAGE-INIT*")
1335 (setq *in-package-init* nil)
1337 ;; For the kernel core image wizards, set the package to *CL-PACKAGE*.
1339 ;; FIXME: We should just set this to (FIND-PACKAGE
1340 ;; "COMMON-LISP-USER") once and for all here, instead of setting it
1341 ;; once here and resetting it later.
1342 (setq *package* *cl-package*))
1345 (/show0 "done with !PACKAGE-COLD-INIT"))
1347 (!defun-from-collected-cold-init-forms !package-cold-init)