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 ;; FIXME: This and the next error needn't have restarts.
481 (with-simple-restart (continue "Ignore this nickname.")
482 (error 'simple-package-error
484 :format-control "~S is a package name, so it cannot be a nickname for ~S."
485 :format-arguments (list n (package-%name package)))))
487 (with-simple-restart (continue "Redefine this nickname.")
488 (error 'simple-package-error
490 :format-control "~S is already a nickname for ~S."
491 :format-arguments (list n (package-%name found))))
492 (setf (gethash n *package-names*) package)
493 (push n (package-%nicknames package)))))))
495 (defun make-package (name &key
496 (use '#.*default-package-use-list*)
498 (internal-symbols 10)
499 (external-symbols 10))
502 "Make a new package having the specified NAME, NICKNAMES, and
503 USE list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are
504 estimates for the number of internal and external symbols which
505 will ultimately be present in the package. The default value of
506 USE is implementation-dependent, and in this implementation
508 *default-package-use-list*)
510 ;; Check for package name conflicts in name and nicknames, then
512 (when (find-package name)
513 ;; ANSI specifies that this error is correctable.
514 (cerror "Leave existing package alone."
515 "A package named ~S already exists" name))
516 (let* ((name (package-namify name))
517 (package (internal-make-package
519 :internal-symbols (make-or-remake-package-hashtable
521 :external-symbols (make-or-remake-package-hashtable
524 ;; Do a USE-PACKAGE for each thing in the USE list so that checking for
525 ;; conflicting exports among used packages is done.
526 (if *in-package-init*
527 (push (list use package) *!deferred-use-packages*)
528 (use-package use package))
530 ;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
531 ;; which would leave us with possibly-bad side effects from the earlier
532 ;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
533 ;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
534 ;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
535 ;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
537 (enter-new-nicknames package nicknames)
538 (setf (gethash name *package-names*) package)))
540 ;;; Change the name if we can, blast any old nicknames and then
541 ;;; add in any new ones.
543 ;;; FIXME: ANSI claims that NAME is a package designator (not just a
544 ;;; string designator -- weird). Thus, NAME could
545 ;;; be a package instead of a string. Presumably then we should not change
546 ;;; the package name if NAME is the same package that's referred to by PACKAGE.
547 ;;; If it's a *different* package, we should probably signal an error.
548 ;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
549 (defun rename-package (package name &optional (nicknames ()))
551 "Changes the name and nicknames for a package."
552 (let* ((package (find-undeleted-package-or-lose package))
554 (found (find-package name))
555 (nicks (mapcar #'string nicknames)))
556 (unless (or (not found) (eq found package))
557 (error 'simple-package-error
559 :format-control "A package named ~S already exists."
560 :format-arguments (list name)))
561 (with-single-package-locked-error ()
562 (unless (and (string= name (package-name package))
563 (null (set-difference nicks (package-nicknames package)
565 (assert-package-unlocked package "rename as ~A~@[ with nickname~P ~
567 name (length nicks) nicks))
569 (remhash (package-%name package) *package-names*)
570 (dolist (n (package-%nicknames package))
571 (remhash n *package-names*))
572 (setf (package-%name package) name
573 (gethash name *package-names*) package
574 (package-%nicknames package) ())
575 (enter-new-nicknames package nicknames))
578 (defun delete-package (package-or-name)
580 "Delete the package-or-name from the package system data structures."
581 (let ((package (if (packagep package-or-name)
583 (find-package package-or-name))))
585 ;; This continuable error is required by ANSI.
586 (with-simple-restart (continue "Return NIL")
587 (error 'simple-package-error
588 :package package-or-name
589 :format-control "There is no package named ~S."
590 :format-arguments (list package-or-name))))
591 ((not (package-name package)) ; already deleted
594 (with-single-package-locked-error
595 (:package package "deleting package ~A" package)
596 (let ((use-list (package-used-by-list package)))
598 ;; This continuable error is specified by ANSI.
600 (continue "Remove dependency in other packages.")
601 (error 'simple-package-error
604 "Package ~S is used by package(s):~% ~S"
606 (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 having the specified name, creating it if necessary."
646 ;; We just simple-stringify the name and call INTERN*, where the real
648 (let ((name (if (simple-string-p name)
650 (coerce name 'simple-string)))
651 (package (find-undeleted-package-or-lose package)))
652 (declare (simple-string name))
657 (defun find-symbol (name &optional (package (sane-package)))
659 "Return the symbol named String in Package. If such a symbol is found
660 then the second value is :internal, :external or :inherited to indicate
661 how the symbol is accessible. If no symbol is found then both values
663 ;; We just simple-stringify the name and call FIND-SYMBOL*, where the
665 (let ((name (if (simple-string-p name) name (coerce name 'simple-string))))
666 (declare (simple-string name))
669 (find-undeleted-package-or-lose package))))
671 ;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
672 ;;; then create it, special-casing the keyword package.
673 (defun intern* (name length package)
674 (declare (simple-string name))
675 (multiple-value-bind (symbol where) (find-symbol* name length package)
677 (values symbol where))
679 (let ((symbol-name (subseq name 0 length)))
680 (with-single-package-locked-error
681 (:package package "interning ~A" symbol-name)
682 (let ((symbol (make-symbol symbol-name)))
683 (%set-symbol-package symbol package)
684 (cond ((eq package *keyword-package*)
685 (add-symbol (package-external-symbols package) symbol)
686 (%set-symbol-value symbol symbol))
688 (add-symbol (package-internal-symbols package) symbol)))
689 (values symbol nil))))))))
691 ;;; Check internal and external symbols, then scan down the list
692 ;;; of hashtables for inherited symbols. When an inherited symbol
693 ;;; is found pull that table to the beginning of the list.
694 (defun find-symbol* (string length package)
695 (declare (simple-string string)
697 (let* ((hash (%sxhash-simple-substring string length))
698 (ehash (entry-hash length hash)))
699 (declare (type index hash ehash))
700 (with-symbol (found symbol (package-internal-symbols package)
701 string length hash ehash)
703 (return-from find-symbol* (values symbol :internal))))
704 (with-symbol (found symbol (package-external-symbols package)
705 string length hash ehash)
707 (return-from find-symbol* (values symbol :external))))
708 (let ((head (package-tables package)))
709 (do ((prev head table)
710 (table (cdr head) (cdr table)))
711 ((null table) (values nil nil))
712 (with-symbol (found symbol (car table) string length hash ehash)
714 (unless (eq prev head)
715 (shiftf (cdr prev) (cdr table) (cdr head) table))
716 (return-from find-symbol* (values symbol :inherited))))))))
718 ;;; Similar to Find-Symbol, but only looks for an external symbol.
719 ;;; This is used for fast name-conflict checking in this file and symbol
720 ;;; printing in the printer.
721 (defun find-external-symbol (string package)
722 (declare (simple-string string))
723 (let* ((length (length string))
724 (hash (%sxhash-simple-string string))
725 (ehash (entry-hash length hash)))
726 (declare (type index length hash))
727 (with-symbol (found symbol (package-external-symbols package)
728 string length hash ehash)
729 (values symbol found))))
731 ;;; If we are uninterning a shadowing symbol, then a name conflict can
732 ;;; result, otherwise just nuke the symbol.
733 (defun unintern (symbol &optional (package (sane-package)))
735 "Makes Symbol no longer present in Package. If Symbol was present
736 then T is returned, otherwise NIL. If Package is Symbol's home
737 package, then it is made uninterned."
738 (let* ((package (find-undeleted-package-or-lose package))
739 (name (symbol-name symbol))
740 (shadowing-symbols (package-%shadowing-symbols package)))
741 (declare (list shadowing-symbols))
743 (with-single-package-locked-error ()
744 (when (find-symbol name package)
745 (assert-package-unlocked package "uninterning ~A" name))
747 ;; If a name conflict is revealed, give use a chance to shadowing-import
748 ;; one of the accessible symbols.
749 (when (member symbol shadowing-symbols)
751 (dolist (p (package-%use-list package))
752 (multiple-value-bind (s w) (find-external-symbol name p)
753 (when w (pushnew s cset))))
757 "Prompt for a symbol to SHADOWING-IMPORT."
758 "Uninterning symbol ~S causes name conflict among these symbols:~%~S"
760 (write-string "Symbol to shadowing-import: " *query-io*)
761 (let ((sym (read *query-io*)))
764 (format *query-io* "~S is not a symbol." sym))
765 ((not (member sym cset))
766 (format *query-io* "~S is not one of the conflicting symbols." sym))
768 (shadowing-import sym package)
769 (return-from unintern t)))))))
770 (setf (package-%shadowing-symbols package)
771 (remove symbol shadowing-symbols)))
773 (multiple-value-bind (s w) (find-symbol name package)
775 (cond ((or (eq w :internal) (eq w :external))
776 (nuke-symbol (if (eq w :internal)
777 (package-internal-symbols package)
778 (package-external-symbols package))
780 (if (eq (symbol-package symbol) package)
781 (%set-symbol-package symbol nil))
785 ;;; Take a symbol-or-list-of-symbols and return a list, checking types.
786 (defun symbol-listify (thing)
789 (unless (symbolp s) (error "~S is not a symbol." s)))
791 ((symbolp thing) (list thing))
793 (error "~S is neither a symbol nor a list of symbols." thing))))
795 (defun string-listify (thing)
796 (mapcar #'string (if (listp thing)
800 ;;; This is like UNINTERN, except if SYMBOL is inherited, it chases
801 ;;; down the package it is inherited from and uninterns it there. Used
802 ;;; for name-conflict resolution. Shadowing symbols are not uninterned
803 ;;; since they do not cause conflicts.
804 (defun moby-unintern (symbol package)
805 (unless (member symbol (package-%shadowing-symbols package))
806 (or (unintern symbol package)
807 (let ((name (symbol-name symbol)))
808 (multiple-value-bind (s w) (find-symbol name package)
810 (when (eq w :inherited)
811 (dolist (q (package-%use-list package))
812 (multiple-value-bind (u x) (find-external-symbol name q)
818 (defun export (symbols &optional (package (sane-package)))
820 "Exports Symbols from Package, checking that no name conflicts result."
821 (let ((package (find-undeleted-package-or-lose package))
823 ;; Punt any symbols that are already external.
824 (dolist (sym (symbol-listify symbols))
825 (multiple-value-bind (s w)
826 (find-external-symbol (symbol-name sym) package)
828 (unless (or w (member sym syms))
830 (with-single-package-locked-error ()
832 (assert-package-unlocked package "exporting symbol~P ~{~A~^, ~}"
834 ;; Find symbols and packages with conflicts.
835 (let ((used-by (package-%used-by-list package))
839 (let ((name (symbol-name sym)))
841 (multiple-value-bind (s w) (find-symbol name p)
842 (when (and w (not (eq s sym))
843 (not (member s (package-%shadowing-symbols p))))
845 (pushnew p cpackages))))))
849 'simple-package-error
852 "Exporting these symbols from the ~A package:~%~S~%~
853 results in name conflicts with these packages:~%~{~A ~}"
855 (list (package-%name package) cset
856 (mapcar #'package-%name cpackages)))
857 (unintern-conflicting-symbols ()
858 :report "Unintern conflicting symbols."
859 (dolist (p cpackages)
861 (moby-unintern sym p))))
862 (skip-exporting-these-symbols ()
863 :report "Skip exporting conflicting symbols."
864 (setq syms (nset-difference syms cset))))))
866 ;; Check that all symbols are accessible. If not, ask to import them.
870 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
871 (cond ((not (and w (eq s sym)))
874 (push sym imports)))))
877 (continue "Import these symbols into the ~A package."
878 (package-%name package))
879 (error 'simple-package-error
882 "These symbols are not accessible in the ~A package:~%~S"
884 (list (package-%name package) missing)))
885 (import missing package))
886 (import imports package))
888 ;; And now, three pages later, we export the suckers.
889 (let ((internal (package-internal-symbols package))
890 (external (package-external-symbols package)))
892 (nuke-symbol internal (symbol-name sym))
893 (add-symbol external sym))))
896 ;;; Check that all symbols are accessible, then move from external to internal.
897 (defun unexport (symbols &optional (package (sane-package)))
899 "Makes Symbols no longer exported from Package."
900 (let ((package (find-undeleted-package-or-lose package))
902 (dolist (sym (symbol-listify symbols))
903 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
904 (cond ((or (not w) (not (eq s sym)))
905 (error 'simple-package-error
907 :format-control "~S is not accessible in the ~A package."
908 :format-arguments (list sym (package-%name package))))
909 ((eq w :external) (pushnew sym syms)))))
910 (with-single-package-locked-error ()
912 (assert-package-unlocked package "unexporting symbol~P ~{~A~^, ~}"
914 (let ((internal (package-internal-symbols package))
915 (external (package-external-symbols package)))
917 (add-symbol internal sym)
918 (nuke-symbol external (symbol-name sym)))))
921 ;;; Check for name conflict caused by the import and let the user
922 ;;; shadowing-import if there is.
923 (defun import (symbols &optional (package (sane-package)))
925 "Make Symbols accessible as internal symbols in Package. If a symbol
926 is already accessible then it has no effect. If a name conflict
927 would result from the importation, then a correctable error is signalled."
928 (let* ((package (find-undeleted-package-or-lose package))
929 (symbols (symbol-listify symbols))
930 (homeless (remove-if #'symbol-package symbols))
933 (dolist (sym symbols)
934 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
936 (let ((found (member sym syms :test #'string=)))
938 (when (not (eq (car found) sym))
941 ((not (eq s sym)) (push sym cset))
942 ((eq w :inherited) (push sym syms)))))
943 (with-single-package-locked-error ()
944 (when (or homeless syms cset)
945 (let ((union (delete-duplicates (append homeless syms cset))))
946 (assert-package-unlocked package "importing symbol~P ~{~A~^, ~}"
947 (length union) union)))
949 ;; ANSI specifies that this error is correctable.
951 (continue "Import these symbols with Shadowing-Import.")
952 (error 'simple-package-error
955 "Importing these symbols into the ~A package ~
956 causes a name conflict:~%~S"
957 :format-arguments (list (package-%name package) cset))))
958 ;; Add the new symbols to the internal hashtable.
959 (let ((internal (package-internal-symbols package)))
961 (add-symbol internal sym)))
962 ;; If any of the symbols are uninterned, make them be owned by Package.
963 (dolist (sym homeless)
964 (%set-symbol-package sym package))
965 (shadowing-import cset package))))
967 ;;; If a conflicting symbol is present, unintern it, otherwise just
968 ;;; stick the symbol in.
969 (defun shadowing-import (symbols &optional (package (sane-package)))
971 "Import Symbols into package, disregarding any name conflict. If
972 a symbol of the same name is present, then it is uninterned.
973 The symbols are added to the Package-Shadowing-Symbols."
974 (let* ((package (find-undeleted-package-or-lose package))
975 (internal (package-internal-symbols package))
976 (symbols (symbol-listify symbols))
977 (lock-asserted-p nil))
978 (with-single-package-locked-error ()
979 (dolist (sym symbols)
980 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
981 (unless (or lock-asserted-p
983 (member s (package-shadowing-symbols package))))
984 (assert-package-unlocked package "shadowing-importing symbol~P ~
985 ~{~A~^, ~}" (length symbols) symbols)
986 (setf lock-asserted-p t))
987 (unless (and w (not (eq w :inherited)) (eq s sym))
988 (when (or (eq w :internal) (eq w :external))
989 ;; If it was shadowed, we don't want UNINTERN to flame out...
990 (setf (package-%shadowing-symbols package)
991 (remove s (the list (package-%shadowing-symbols package))))
992 (unintern s package))
993 (add-symbol internal sym))
994 (pushnew sym (package-%shadowing-symbols package))))))
997 (defun shadow (symbols &optional (package (sane-package)))
999 "Make an internal symbol in Package with the same name as each of the
1000 specified symbols, adding the new symbols to the Package-Shadowing-Symbols.
1001 If a symbol with the given name is already present in Package, then
1002 the existing symbol is placed in the shadowing symbols list if it is
1003 not already present."
1004 (let* ((package (find-undeleted-package-or-lose package))
1005 (internal (package-internal-symbols package))
1006 (symbols (string-listify symbols))
1007 (lock-asserted-p nil))
1008 (flet ((present-p (w)
1009 (and w (not (eq w :inherited)))))
1010 (with-single-package-locked-error ()
1011 (dolist (name symbols)
1012 (multiple-value-bind (s w) (find-symbol name package)
1013 (unless (or lock-asserted-p
1015 (member s (package-shadowing-symbols package))))
1016 (assert-package-unlocked package "shadowing symbol~P ~{~A~^, ~}"
1017 (length symbols) symbols)
1018 (setf lock-asserted-p t))
1019 (unless (present-p w)
1020 (setq s (make-symbol name))
1021 (%set-symbol-package s package)
1022 (add-symbol internal s))
1023 (pushnew s (package-%shadowing-symbols package)))))))
1026 ;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
1027 (defun use-package (packages-to-use &optional (package (sane-package)))
1029 "Add all the Packages-To-Use to the use list for Package so that
1030 the external symbols of the used packages are accessible as internal
1031 symbols in Package."
1032 (let ((packages (package-listify packages-to-use))
1033 (package (find-undeleted-package-or-lose package)))
1035 ;; Loop over each package, USE'ing one at a time...
1036 (with-single-package-locked-error ()
1037 (dolist (pkg packages)
1038 (unless (member pkg (package-%use-list package))
1039 (assert-package-unlocked package "using package~P ~{~A~^, ~}"
1040 (length packages) packages)
1042 (shadowing-symbols (package-%shadowing-symbols package))
1043 (use-list (package-%use-list package)))
1045 ;; If the number of symbols already accessible is less than the
1046 ;; number to be inherited then it is faster to run the test the
1047 ;; other way. This is particularly valuable in the case of
1048 ;; a new package USEing Lisp.
1050 ((< (+ (package-internal-symbol-count package)
1051 (package-external-symbol-count package)
1053 (dolist (p use-list res)
1054 (incf res (package-external-symbol-count p)))))
1055 (package-external-symbol-count pkg))
1056 (do-symbols (sym package)
1057 (multiple-value-bind (s w)
1058 (find-external-symbol (symbol-name sym) pkg)
1059 (when (and w (not (eq s sym))
1060 (not (member sym shadowing-symbols)))
1062 (dolist (p use-list)
1063 (do-external-symbols (sym p)
1064 (multiple-value-bind (s w)
1065 (find-external-symbol (symbol-name sym) pkg)
1066 (when (and w (not (eq s sym))
1067 (not (member (find-symbol (symbol-name sym)
1069 shadowing-symbols)))
1070 (push sym cset))))))
1072 (do-external-symbols (sym pkg)
1073 (multiple-value-bind (s w)
1074 (find-symbol (symbol-name sym) package)
1075 (when (and w (not (eq s sym))
1076 (not (member s shadowing-symbols)))
1081 "Unintern the conflicting symbols in the ~2*~A package."
1082 "Using package ~A results in name conflicts for these symbols:~%~
1084 (package-%name pkg) cset (package-%name package))
1085 (dolist (s cset) (moby-unintern s package))))
1087 (push pkg (package-%use-list package))
1088 (push (package-external-symbols pkg) (cdr (package-tables package)))
1089 (push package (package-%used-by-list pkg))))))
1092 (defun unuse-package (packages-to-unuse &optional (package (sane-package)))
1094 "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
1095 (let ((package (find-undeleted-package-or-lose package))
1096 (packages (package-listify packages-to-unuse)))
1097 (with-single-package-locked-error ()
1098 (dolist (p packages)
1099 (when (member p (package-use-list package))
1100 (assert-package-unlocked package "unusing package~P ~{~A~^, ~}"
1101 (length packages) packages))
1102 (setf (package-%use-list package)
1103 (remove p (the list (package-%use-list package))))
1104 (setf (package-tables package)
1105 (delete (package-external-symbols p)
1106 (the list (package-tables package))))
1107 (setf (package-%used-by-list p)
1108 (remove package (the list (package-%used-by-list p))))))
1111 (defun find-all-symbols (string-or-symbol)
1113 "Return a list of all symbols in the system having the specified name."
1114 (let ((string (string string-or-symbol))
1116 (maphash (lambda (k v)
1117 (declare (ignore k))
1118 (multiple-value-bind (s w) (find-symbol string v)
1119 (when w (pushnew s res))))
1123 ;;;; APROPOS and APROPOS-LIST
1125 (defun briefly-describe-symbol (symbol)
1128 (when (boundp symbol)
1129 (write-string " (bound)"))
1130 (when (fboundp symbol)
1131 (write-string " (fbound)")))
1133 (defun apropos-list (string-designator
1138 "Like APROPOS, except that it returns a list of the symbols found instead
1139 of describing them."
1140 (if package-designator
1141 (let ((package (find-undeleted-package-or-lose package-designator))
1142 (string (stringify-name string-designator "APROPOS search"))
1144 (do-symbols (symbol package)
1145 (when (and (eq (symbol-package symbol) package)
1146 (or (not external-only)
1147 (eq (nth-value 1 (find-symbol (symbol-name symbol)
1150 (search string (symbol-name symbol) :test #'char-equal))
1151 (push symbol result)))
1153 (mapcan (lambda (package)
1154 (apropos-list string-designator package external-only))
1155 (list-all-packages))))
1157 (defun apropos (string-designator &optional package external-only)
1159 "Briefly describe all symbols which contain the specified STRING.
1160 If PACKAGE is supplied then only describe symbols present in
1161 that package. If EXTERNAL-ONLY then only describe
1162 external symbols in the specified package."
1163 ;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
1164 ;; of some unnecessary consing; and the unnecessary consing shouldn't be an
1165 ;; issue, since this function is is only useful interactively anyway, and
1166 ;; we can cons and GC a lot faster than the typical user can read..
1167 (dolist (symbol (apropos-list string-designator package external-only))
1168 (briefly-describe-symbol symbol))
1171 ;;;; final initialization
1173 ;;;; The cold loader (GENESIS) makes the data structure in
1174 ;;;; *!INITIAL-SYMBOLS*. We grovel over it, making the specified
1175 ;;;; packages and interning the symbols. For a description of the
1176 ;;;; format of *!INITIAL-SYMBOLS*, see the GENESIS source.
1178 (defvar *!initial-symbols*)
1182 (setq *in-package-init* t)
1184 (/show0 "about to loop over *!INITIAL-SYMBOLS* to make packages")
1185 (dolist (spec *!initial-symbols*)
1186 (let* ((pkg (apply #'make-package (first spec)))
1187 (internal (package-internal-symbols pkg))
1188 (external (package-external-symbols pkg)))
1189 (/show0 "back from MAKE-PACKAGE, PACKAGE-NAME=..")
1190 (/primitive-print (package-name pkg))
1192 ;; Put internal symbols in the internal hashtable and set package.
1193 (dolist (symbol (second spec))
1194 (add-symbol internal symbol)
1195 (%set-symbol-package symbol pkg))
1197 ;; External symbols same, only go in external table.
1198 (dolist (symbol (third spec))
1199 (add-symbol external symbol)
1200 (%set-symbol-package symbol pkg))
1202 ;; Don't set package for imported symbols.
1203 (dolist (symbol (fourth spec))
1204 (add-symbol internal symbol))
1205 (dolist (symbol (fifth spec))
1206 (add-symbol external symbol))
1208 ;; Put shadowing symbols in the shadowing symbols list.
1209 (setf (package-%shadowing-symbols pkg) (sixth spec))
1210 ;; Set the package documentation
1211 (setf (package-doc-string pkg) (seventh spec))))
1213 ;; FIXME: These assignments are also done at toplevel in
1214 ;; boot-extensions.lisp. They should probably only be done once.
1215 (/show0 "setting up *CL-PACKAGE* and *KEYWORD-PACKAGE*")
1216 (setq *cl-package* (find-package "COMMON-LISP"))
1217 (setq *keyword-package* (find-package "KEYWORD"))
1219 (/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*")
1220 (makunbound '*!initial-symbols*) ; (so that it gets GCed)
1222 ;; Make some other packages that should be around in the cold load.
1223 ;; The COMMON-LISP-USER package is required by the ANSI standard,
1224 ;; but not completely specified by it, so in the cross-compilation
1225 ;; host Lisp it could contain various symbols, USE-PACKAGEs, or
1226 ;; nicknames that we don't want in our target SBCL. For that reason,
1227 ;; we handle it specially, not dumping the host Lisp version at
1229 (aver (not (find-package "COMMON-LISP-USER")))
1230 ;; ..but instead making our own from scratch here.
1231 (/show0 "about to MAKE-PACKAGE COMMON-LISP-USER")
1232 (make-package "COMMON-LISP-USER"
1233 :nicknames '("CL-USER")
1234 :use '("COMMON-LISP"
1235 ;; ANSI encourages us to put extension packages
1236 ;; in the USE list of COMMON-LISP-USER.
1237 "SB!ALIEN" "SB!ALIEN" "SB!DEBUG"
1238 "SB!EXT" "SB!GRAY" "SB!PROFILE"))
1240 ;; Now do the *!DEFERRED-USE-PACKAGES*.
1241 (/show0 "about to do *!DEFERRED-USE-PACKAGES*")
1242 (dolist (args *!deferred-use-packages*)
1243 (apply #'use-package args))
1245 ;; The Age Of Magic is over, we can behave ANSIly henceforth.
1246 (/show0 "about to SETQ *IN-PACKAGE-INIT*")
1247 (setq *in-package-init* nil)
1249 ;; For the kernel core image wizards, set the package to *CL-PACKAGE*.
1251 ;; FIXME: We should just set this to (FIND-PACKAGE
1252 ;; "COMMON-LISP-USER") once and for all here, instead of setting it
1253 ;; once here and resetting it later.
1254 (setq *package* *cl-package*))
1257 (/show0 "done with !PACKAGE-COLD-INIT"))
1259 (!defun-from-collected-cold-init-forms !package-cold-init)