1.0.9.48: texi2pdf rework (Aymeric Vincent sbcl-devel 2007-09-05)
[sbcl/lichteblau.git] / src / code / target-package.lisp
blob2e5bef5dbad7e24e3cacb93d5f85b5520420321a
1 ;;;; PACKAGEs and stuff like that
2 ;;;;
3 ;;;; Note: The code in this file signals many correctable errors. This
4 ;;;; is not just an arbitrary aesthetic decision on the part of the
5 ;;;; implementor -- many of these are specified by ANSI 11.1.1.2.5,
6 ;;;; "Prevention of Name Conflicts in Packages":
7 ;;;; Within one package, any particular name can refer to at most one
8 ;;;; symbol. A name conflict is said to occur when there would be more
9 ;;;; than one candidate symbol. Any time a name conflict is about to
10 ;;;; occur, a correctable error is signaled.
11 ;;;;
12 ;;;; FIXME: The code contains a lot of type declarations. Are they
13 ;;;; all really necessary?
15 ;;;; This software is part of the SBCL system. See the README file for
16 ;;;; more information.
17 ;;;;
18 ;;;; This software is derived from the CMU CL system, which was
19 ;;;; written at Carnegie Mellon University and released into the
20 ;;;; public domain. The software is in the public domain and is
21 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
22 ;;;; files for more information.
24 (in-package "SB!IMPL")
26 (!begin-collecting-cold-init-forms)
28 (!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)
36 (format stream
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 load factor we allow in a package hashtable
43 (defconstant +package-rehash-threshold+ 0.75)
45 ;;; the load factor desired for a package hashtable when writing a
46 ;;; core image
47 (defconstant +package-hashtable-image-load-factor+ 0.5)
49 ;;; All destructive package modifications are serialized on this lock.
50 (defvar *package-lock*)
52 (!cold-init-forms
53 (setf *package-lock* (sb!thread::make-spinlock :name "Package Lock")))
55 (defmacro with-packages ((&key) &body forms)
56 `(sb!thread::with-recursive-spinlock (*package-lock*)
57 ,@forms))
59 ;;; Make a package hashtable having a prime number of entries at least
60 ;;; as great as (/ SIZE +PACKAGE-REHASH-THRESHOLD+). If RES is supplied,
61 ;;; then it is destructively modified to produce the result. This is
62 ;;; useful when changing the size, since there are many pointers to
63 ;;; the hashtable.
64 ;;; Actually, the smallest table built here has three entries. This
65 ;;; is necessary because the double hashing step size is calculated
66 ;;; using a division by the table size minus two.
67 (defun make-or-remake-package-hashtable (size
68 &optional
69 res)
70 (flet ((actual-package-hashtable-size (size)
71 (loop for n of-type fixnum
72 from (logior (ceiling size +package-rehash-threshold+) 1)
73 by 2
74 when (positive-primep n) return n)))
75 (let* ((n (actual-package-hashtable-size size))
76 (size (truncate (* n +package-rehash-threshold+)))
77 (table (make-array n))
78 (hash (make-array n
79 :element-type '(unsigned-byte 8)
80 :initial-element 0)))
81 (if res
82 (setf (package-hashtable-table res) table
83 (package-hashtable-hash res) hash
84 (package-hashtable-size res) size
85 (package-hashtable-free res) size
86 (package-hashtable-deleted res) 0)
87 (setf res (%make-package-hashtable table hash size)))
88 res)))
90 ;;; Destructively resize TABLE to have room for at least SIZE entries
91 ;;; and rehash its existing entries.
92 (defun resize-package-hashtable (table size)
93 (let* ((vec (package-hashtable-table table))
94 (hash (package-hashtable-hash table))
95 (len (length vec)))
96 (make-or-remake-package-hashtable size table)
97 (dotimes (i len)
98 (when (> (aref hash i) 1)
99 (add-symbol table (svref vec i))))))
101 ;;;; package locking operations, built conditionally on :sb-package-locks
103 #!+sb-package-locks
104 (progn
105 (defun package-locked-p (package)
106 #!+sb-doc
107 "Returns T when PACKAGE is locked, NIL otherwise. Signals an error
108 if PACKAGE doesn't designate a valid package."
109 (package-lock (find-undeleted-package-or-lose package)))
111 (defun lock-package (package)
112 #!+sb-doc
113 "Locks PACKAGE and returns T. Has no effect if PACKAGE was already
114 locked. Signals an error if PACKAGE is not a valid package designator"
115 (setf (package-lock (find-undeleted-package-or-lose package)) t))
117 (defun unlock-package (package)
118 #!+sb-doc
119 "Unlocks PACKAGE and returns T. Has no effect if PACKAGE was already
120 unlocked. Signals an error if PACKAGE is not a valid package designator."
121 (setf (package-lock (find-undeleted-package-or-lose package)) nil)
124 (defun package-implemented-by-list (package)
125 #!+sb-doc
126 "Returns a list containing the implementation packages of
127 PACKAGE. Signals an error if PACKAGE is not a valid package designator."
128 (package-%implementation-packages (find-undeleted-package-or-lose package)))
130 (defun package-implements-list (package)
131 #!+sb-doc
132 "Returns the packages that PACKAGE is an implementation package
133 of. Signals an error if PACKAGE is not a valid package designator."
134 (let ((package (find-undeleted-package-or-lose package)))
135 (loop for x in (list-all-packages)
136 when (member package (package-%implementation-packages x))
137 collect x)))
139 (defun add-implementation-package (packages-to-add
140 &optional (package *package*))
141 #!+sb-doc
142 "Adds PACKAGES-TO-ADD as implementation packages of PACKAGE. Signals
143 an error if PACKAGE or any of the PACKAGES-TO-ADD is not a valid
144 package designator."
145 (let ((package (find-undeleted-package-or-lose package))
146 (packages-to-add (package-listify packages-to-add)))
147 (setf (package-%implementation-packages package)
148 (union (package-%implementation-packages package)
149 (mapcar #'find-undeleted-package-or-lose packages-to-add)))))
151 (defun remove-implementation-package (packages-to-remove
152 &optional (package *package*))
153 #!+sb-doc
154 "Removes PACKAGES-TO-REMOVE from the implementation packages of
155 PACKAGE. Signals an error if PACKAGE or any of the PACKAGES-TO-REMOVE
156 is not a valid package designator."
157 (let ((package (find-undeleted-package-or-lose package))
158 (packages-to-remove (package-listify packages-to-remove)))
159 (setf (package-%implementation-packages package)
160 (nset-difference
161 (package-%implementation-packages package)
162 (mapcar #'find-undeleted-package-or-lose packages-to-remove)))))
164 (defmacro with-unlocked-packages ((&rest packages) &body forms)
165 #!+sb-doc
166 "Unlocks PACKAGES for the dynamic scope of the body. Signals an
167 error if any of PACKAGES is not a valid package designator."
168 (with-unique-names (unlocked-packages)
169 `(let (,unlocked-packages)
170 (unwind-protect
171 (progn
172 (dolist (p ',packages)
173 (when (package-locked-p p)
174 (push p ,unlocked-packages)
175 (unlock-package p)))
176 ,@forms)
177 (dolist (p ,unlocked-packages)
178 (when (find-package p)
179 (lock-package p)))))))
181 (defun package-lock-violation (package &key (symbol nil symbol-p)
182 format-control format-arguments)
183 (let* ((restart :continue)
184 (cl-violation-p (eq package *cl-package*))
185 (error-arguments
186 (append (list (if symbol-p
187 'symbol-package-locked-error
188 'package-locked-error)
189 :package package
190 :format-control format-control
191 :format-arguments format-arguments)
192 (when symbol-p (list :symbol symbol))
193 (list :references
194 (append '((:sbcl :node "Package Locks"))
195 (when cl-violation-p
196 '((:ansi-cl :section (11 1 2 1 2)))))))))
197 (restart-case
198 (apply #'cerror "Ignore the package lock." error-arguments)
199 (:ignore-all ()
200 :report "Ignore all package locks in the context of this operation."
201 (setf restart :ignore-all))
202 (:unlock-package ()
203 :report "Unlock the package."
204 (setf restart :unlock-package)))
205 (ecase restart
206 (:continue
207 (pushnew package *ignored-package-locks*))
208 (:ignore-all
209 (setf *ignored-package-locks* t))
210 (:unlock-package
211 (unlock-package package)))))
213 (defun package-lock-violation-p (package &optional (symbol nil symbolp))
214 ;; KLUDGE: (package-lock package) needs to be before
215 ;; comparison to *package*, since during cold init this gets
216 ;; called before *package* is bound -- but no package should
217 ;; be locked at that point.
218 (and package
219 (package-lock package)
220 ;; In package or implementation package
221 (not (or (eq package *package*)
222 (member *package* (package-%implementation-packages package))))
223 ;; Runtime disabling
224 (not (eq t *ignored-package-locks*))
225 (or (eq :invalid *ignored-package-locks*)
226 (not (member package *ignored-package-locks*)))
227 ;; declarations for symbols
228 (not (and symbolp (member symbol (disabled-package-locks))))))
230 (defun disabled-package-locks ()
231 (if (boundp 'sb!c::*lexenv*)
232 (sb!c::lexenv-disabled-package-locks sb!c::*lexenv*)
233 sb!c::*disabled-package-locks*))
235 ) ; progn
237 ;;;; more package-locking these are NOPs unless :sb-package-locks is
238 ;;;; in target features. Cross-compiler NOPs for these are in cross-misc.
240 ;;; The right way to establish a package lock context is
241 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR, defined in early-package.lisp
243 ;;; Must be used inside the dynamic contour established by
244 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR
245 (defun assert-package-unlocked (package &optional format-control
246 &rest format-arguments)
247 #!-sb-package-locks
248 (declare (ignore format-control format-arguments))
249 #!+sb-package-locks
250 (when (package-lock-violation-p package)
251 (package-lock-violation package
252 :format-control format-control
253 :format-arguments format-arguments))
254 package)
256 ;;; Must be used inside the dynamic contour established by
257 ;;; WITH-SINGLE-PACKAGE-LOCKED-ERROR.
259 ;;; FIXME: Maybe we should establish such contours for he toplevel
260 ;;; and others, so that %set-fdefinition and others could just use
261 ;;; this.
262 (defun assert-symbol-home-package-unlocked (name format)
263 #!-sb-package-locks
264 (declare (ignore format))
265 #!+sb-package-locks
266 (let* ((symbol (etypecase name
267 (symbol name)
268 (list (if (and (consp (cdr name))
269 (eq 'setf (first name)))
270 (second name)
271 ;; Skip lists of length 1, single conses and
272 ;; (class-predicate foo), etc.
273 ;; FIXME: MOP and package-lock
274 ;; interaction needs to be thought about.
275 (return-from
276 assert-symbol-home-package-unlocked
277 name)))))
278 (package (symbol-package symbol)))
279 (when (package-lock-violation-p package symbol)
280 (package-lock-violation package
281 :symbol symbol
282 :format-control format
283 :format-arguments (list name))))
284 name)
287 ;;;; miscellaneous PACKAGE operations
289 (def!method print-object ((package package) stream)
290 (let ((name (package-%name package)))
291 (if name
292 (print-unreadable-object (package stream :type t)
293 (prin1 name stream))
294 (print-unreadable-object (package stream :type t :identity t)
295 (write-string "(deleted)" stream)))))
297 ;;; ANSI says (in the definition of DELETE-PACKAGE) that these, and
298 ;;; most other operations, are unspecified for deleted packages. We
299 ;;; just do the easy thing and signal errors in that case.
300 (macrolet ((def (ext real)
301 `(defun ,ext (x) (,real (find-undeleted-package-or-lose x)))))
302 (def package-nicknames package-%nicknames)
303 (def package-use-list package-%use-list)
304 (def package-used-by-list package-%used-by-list)
305 (def package-shadowing-symbols package-%shadowing-symbols))
307 (defun %package-hashtable-symbol-count (table)
308 (let ((size (the fixnum
309 (- (package-hashtable-size table)
310 (package-hashtable-deleted table)))))
311 (the fixnum
312 (- size (package-hashtable-free table)))))
314 (defun package-internal-symbol-count (package)
315 (%package-hashtable-symbol-count (package-internal-symbols package)))
317 (defun package-external-symbol-count (package)
318 (%package-hashtable-symbol-count (package-external-symbols package)))
320 (defvar *package* (error "*PACKAGE* should be initialized in cold load!")
321 #!+sb-doc "the current package")
322 ;;; FIXME: should be declared of type PACKAGE, with no NIL init form,
323 ;;; after I get around to cleaning up DOCUMENTATION
325 ;;; a map from package names to packages
326 (defvar *package-names*)
327 (declaim (type hash-table *package-names*))
328 (!cold-init-forms
329 (setf *package-names* (make-hash-table :test 'equal)))
331 ;;; This magical variable is T during initialization so that
332 ;;; USE-PACKAGE's of packages that don't yet exist quietly win. Such
333 ;;; packages are thrown onto the list *DEFERRED-USE-PACKAGES* so that
334 ;;; this can be fixed up later.
336 ;;; FIXME: This could be cleaned up the same way I do it in my package
337 ;;; hacking when setting up the cross-compiler. Then we wouldn't have
338 ;;; this extraneous global variable and annoying runtime tests on
339 ;;; package operations. (*DEFERRED-USE-PACKAGES* would also go away.)
340 (defvar *in-package-init*)
342 ;;; pending USE-PACKAGE arguments saved up while *IN-PACKAGE-INIT* is true
343 (defvar *!deferred-use-packages*)
344 (!cold-init-forms
345 (setf *!deferred-use-packages* nil))
347 (define-condition bootstrap-package-not-found (condition)
348 ((name :initarg :name :reader bootstrap-package-name)))
349 (defun debootstrap-package (&optional condition)
350 (invoke-restart
351 (find-restart-or-control-error 'debootstrap-package condition)))
353 (defun find-package (package-designator)
354 (flet ((find-package-from-string (string)
355 (declare (type string string))
356 (let ((packageoid (gethash string *package-names*)))
357 (when (and (null packageoid)
358 (not *in-package-init*) ; KLUDGE
359 (let ((mismatch (mismatch "SB!" string)))
360 (and mismatch (= mismatch 3))))
361 (restart-case
362 (signal 'bootstrap-package-not-found :name string)
363 (debootstrap-package ()
364 (return-from find-package
365 (if (string= string "SB!XC")
366 (find-package "COMMON-LISP")
367 (find-package
368 (substitute #\- #\! string :count 1)))))))
369 packageoid)))
370 (typecase package-designator
371 (package package-designator)
372 (symbol (find-package-from-string (symbol-name package-designator)))
373 (string (find-package-from-string package-designator))
374 (character (find-package-from-string (string package-designator)))
375 (t (error 'type-error
376 :datum package-designator
377 :expected-type '(or character package string symbol))))))
379 ;;; Return a list of packages given a package designator or list of
380 ;;; package designators, or die trying.
381 (defun package-listify (thing)
382 (let ((res ()))
383 (dolist (thing (if (listp thing) thing (list thing)) res)
384 (push (find-undeleted-package-or-lose thing) res))))
386 ;;; Make a package name into a simple-string.
387 (defun package-namify (n)
388 (stringify-package-designator n))
390 ;;; ANSI specifies (in the definition of DELETE-PACKAGE) that PACKAGE-NAME
391 ;;; returns NIL (not an error) for a deleted package, so this is a special
392 ;;; case where we want to use bare %FIND-PACKAGE-OR-LOSE instead of
393 ;;; FIND-UNDELETED-PACKAGE-OR-LOSE.
394 (defun package-name (package-designator)
395 (package-%name (%find-package-or-lose package-designator)))
397 ;;;; operations on package hashtables
399 ;;; Compute a number from the sxhash of the pname and the length which
400 ;;; must be between 2 and 255.
401 (defmacro entry-hash (length sxhash)
402 `(the fixnum
403 (+ (the fixnum
404 (rem (the fixnum
405 (logxor ,length
406 ,sxhash
407 (the fixnum (ash ,sxhash -8))
408 (the fixnum (ash ,sxhash -16))
409 (the fixnum (ash ,sxhash -19))))
410 254))
411 2)))
412 ;;; FIXME: should be wrapped in EVAL-WHEN (COMPILE EXECUTE)
414 ;;; Add a symbol to a package hashtable. The symbol is assumed
415 ;;; not to be present.
416 (defun add-symbol (table symbol)
417 (when (zerop (package-hashtable-free table))
418 ;; The hashtable is full. Resize it to be able to hold twice the
419 ;; amount of symbols than it currently contains. The actual new size
420 ;; can be smaller than twice the current size if the table contained
421 ;; deleted entries.
422 (resize-package-hashtable table
423 (* (- (package-hashtable-size table)
424 (package-hashtable-deleted table))
425 2)))
426 (let* ((vec (package-hashtable-table table))
427 (hash (package-hashtable-hash table))
428 (len (length vec))
429 (sxhash (%sxhash-simple-string (symbol-name symbol)))
430 (h2 (1+ (rem sxhash (- len 2)))))
431 (declare (fixnum sxhash h2))
432 (do ((i (rem sxhash len) (rem (+ i h2) len)))
433 ((< (the fixnum (aref hash i)) 2)
434 (if (zerop (the fixnum (aref hash i)))
435 (decf (package-hashtable-free table))
436 (decf (package-hashtable-deleted table)))
437 (setf (svref vec i) symbol)
438 (setf (aref hash i)
439 (entry-hash (length (symbol-name symbol))
440 sxhash)))
441 (declare (fixnum i)))))
443 ;;; Resize the package hashtables of all packages so that their load
444 ;;; factor is +PACKAGE-HASHTABLE-IMAGE-LOAD-FACTOR+. Called from
445 ;;; SAVE-LISP-AND-DIE to optimize space usage in the image.
446 (defun tune-hashtable-sizes-of-all-packages ()
447 (flet ((tune-table-size (table)
448 (resize-package-hashtable
449 table
450 (round (* (/ +package-rehash-threshold+
451 +package-hashtable-image-load-factor+)
452 (- (package-hashtable-size table)
453 (package-hashtable-free table)
454 (package-hashtable-deleted table)))))))
455 (dolist (package (list-all-packages))
456 (tune-table-size (package-internal-symbols package))
457 (tune-table-size (package-external-symbols package)))))
459 ;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR
460 ;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR
461 ;;; is bound to the symbol. LENGTH and HASH are the length and sxhash
462 ;;; of STRING. ENTRY-HASH is the entry-hash of the string and length.
463 (defmacro with-symbol ((index-var symbol-var table string length sxhash
464 entry-hash)
465 &body forms)
466 (let ((vec (gensym)) (hash (gensym)) (len (gensym)) (h2 (gensym))
467 (name (gensym)) (name-len (gensym)) (ehash (gensym)))
468 `(let* ((,vec (package-hashtable-table ,table))
469 (,hash (package-hashtable-hash ,table))
470 (,len (length ,vec))
471 (,h2 (1+ (the index (rem (the index ,sxhash)
472 (the index (- ,len 2)))))))
473 (declare (type index ,len ,h2))
474 (prog ((,index-var (rem (the index ,sxhash) ,len))
475 ,symbol-var ,ehash)
476 (declare (type (or index null) ,index-var))
477 LOOP
478 (setq ,ehash (aref ,hash ,index-var))
479 (cond ((eql ,ehash ,entry-hash)
480 (setq ,symbol-var (svref ,vec ,index-var))
481 (let* ((,name (symbol-name ,symbol-var))
482 (,name-len (length ,name)))
483 (declare (type index ,name-len))
484 (when (and (= ,name-len ,length)
485 (string= ,string ,name
486 :end1 ,length
487 :end2 ,name-len))
488 (go DOIT))))
489 ((zerop ,ehash)
490 (setq ,index-var nil)
491 (go DOIT)))
492 (setq ,index-var (+ ,index-var ,h2))
493 (when (>= ,index-var ,len)
494 (setq ,index-var (- ,index-var ,len)))
495 (go LOOP)
496 DOIT
497 (return (progn ,@forms))))))
499 ;;; Delete the entry for STRING in TABLE. The entry must exist.
500 (defun nuke-symbol (table string)
501 (declare (simple-string string))
502 (let* ((length (length string))
503 (hash (%sxhash-simple-string string))
504 (ehash (entry-hash length hash)))
505 (declare (type index length hash))
506 (with-symbol (index symbol table string length hash ehash)
507 (setf (aref (package-hashtable-hash table) index) 1)
508 (setf (aref (package-hashtable-table table) index) nil)
509 (incf (package-hashtable-deleted table))))
510 ;; If the table is less than one quarter full, halve its size and
511 ;; rehash the entries.
512 (let* ((size (package-hashtable-size table))
513 (deleted (package-hashtable-deleted table))
514 (used (- size
515 (package-hashtable-free table)
516 deleted)))
517 (declare (type fixnum size deleted used))
518 (when (< used (truncate size 4))
519 (resize-package-hashtable table (* used 2)))))
521 ;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*.
522 ;;; If there is a conflict then give the user a chance to do
523 ;;; something about it.
524 (defun enter-new-nicknames (package nicknames)
525 (declare (type list nicknames))
526 (dolist (n nicknames)
527 (let* ((n (package-namify n))
528 (found (gethash n *package-names*)))
529 (cond ((not found)
530 (setf (gethash n *package-names*) package)
531 (push n (package-%nicknames package)))
532 ((eq found package))
533 ((string= (the string (package-%name found)) n)
534 (cerror "Ignore this nickname."
535 'simple-package-error
536 :package package
537 :format-control "~S is a package name, so it cannot be a nickname for ~S."
538 :format-arguments (list n (package-%name package))))
540 (cerror "Leave this nickname alone."
541 'simple-package-error
542 :package package
543 :format-control "~S is already a nickname for ~S."
544 :format-arguments (list n (package-%name found))))))))
546 (defun make-package (name &key
547 (use '#.*default-package-use-list*)
548 nicknames
549 (internal-symbols 10)
550 (external-symbols 10))
551 #!+sb-doc
552 #.(format nil
553 "Make a new package having the specified NAME, NICKNAMES, and USE
554 list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are estimates for the number of
555 internal and external symbols which will ultimately be present in the package.
556 The default value of USE is implementation-dependent, and in this
557 implementation it is ~S." *default-package-use-list*)
558 (with-packages ()
559 ;; Check for package name conflicts in name and nicknames, then
560 ;; make the package.
561 (when (find-package name)
562 ;; ANSI specifies that this error is correctable.
563 (cerror "Leave existing package alone."
564 "A package named ~S already exists" name))
565 (let* ((name (package-namify name))
566 (package (internal-make-package
567 :%name name
568 :internal-symbols (make-or-remake-package-hashtable
569 internal-symbols)
570 :external-symbols (make-or-remake-package-hashtable
571 external-symbols))))
573 ;; Do a USE-PACKAGE for each thing in the USE list so that checking for
574 ;; conflicting exports among used packages is done.
575 (if *in-package-init*
576 (push (list use package) *!deferred-use-packages*)
577 (use-package use package))
579 ;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
580 ;; which would leave us with possibly-bad side effects from the earlier
581 ;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
582 ;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
583 ;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
584 ;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
585 ;; USE-PACKAGE, too.
586 (enter-new-nicknames package nicknames)
587 (setf (gethash name *package-names*) package))))
589 ;;; Change the name if we can, blast any old nicknames and then
590 ;;; add in any new ones.
592 ;;; FIXME: ANSI claims that NAME is a package designator (not just a
593 ;;; string designator -- weird). Thus, NAME could
594 ;;; be a package instead of a string. Presumably then we should not change
595 ;;; the package name if NAME is the same package that's referred to by PACKAGE.
596 ;;; If it's a *different* package, we should probably signal an error.
597 ;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
598 (defun rename-package (package name &optional (nicknames ()))
599 #!+sb-doc
600 "Changes the name and nicknames for a package."
601 (with-packages ()
602 (let* ((package (find-undeleted-package-or-lose package))
603 (name (package-namify name))
604 (found (find-package name))
605 (nicks (mapcar #'string nicknames)))
606 (unless (or (not found) (eq found package))
607 (error 'simple-package-error
608 :package name
609 :format-control "A package named ~S already exists."
610 :format-arguments (list name)))
611 (with-single-package-locked-error ()
612 (unless (and (string= name (package-name package))
613 (null (set-difference nicks (package-nicknames package)
614 :test #'string=)))
615 (assert-package-unlocked package "rename as ~A~@[ with nickname~P ~
616 ~{~A~^, ~}~]"
617 name (length nicks) nicks))
618 ;; do the renaming
619 (remhash (package-%name package) *package-names*)
620 (dolist (n (package-%nicknames package))
621 (remhash n *package-names*))
622 (setf (package-%name package) name
623 (gethash name *package-names*) package
624 (package-%nicknames package) ())
625 (enter-new-nicknames package nicknames))
626 package)))
628 (defun delete-package (package-designator)
629 #!+sb-doc
630 "Delete the package designated by PACKAGE-DESIGNATOR from the package
631 system data structures."
632 (with-packages ()
633 (let ((package (if (packagep package-designator)
634 package-designator
635 (find-package package-designator))))
636 (cond ((not package)
637 ;; This continuable error is required by ANSI.
638 (cerror
639 "Return ~S."
640 (make-condition
641 'simple-package-error
642 :package package-designator
643 :format-control "There is no package named ~S."
644 :format-arguments (list package-designator))
645 nil))
646 ((not (package-name package)) ; already deleted
647 nil)
649 (with-single-package-locked-error
650 (:package package "deleting package ~A" package)
651 (let ((use-list (package-used-by-list package)))
652 (when use-list
653 ;; This continuable error is specified by ANSI.
654 (cerror
655 "Remove dependency in other packages."
656 (make-condition
657 'simple-package-error
658 :package package
659 :format-control
660 "~@<Package ~S is used by package~P:~2I~_~S~@:>"
661 :format-arguments (list (package-name package)
662 (length use-list)
663 (mapcar #'package-name use-list))))
664 (dolist (p use-list)
665 (unuse-package package p))))
666 (dolist (used (package-use-list package))
667 (unuse-package used package))
668 (do-symbols (sym package)
669 (unintern sym package))
670 (remhash (package-name package) *package-names*)
671 (dolist (nick (package-nicknames package))
672 (remhash nick *package-names*))
673 (setf (package-%name package) nil
674 ;; Setting PACKAGE-%NAME to NIL is required in order to
675 ;; make PACKAGE-NAME return NIL for a deleted package as
676 ;; ANSI requires. Setting the other slots to NIL
677 ;; and blowing away the PACKAGE-HASHTABLES is just done
678 ;; for tidiness and to help the GC.
679 (package-%nicknames package) nil
680 (package-%use-list package) nil
681 (package-tables package) nil
682 (package-%shadowing-symbols package) nil
683 (package-internal-symbols package)
684 (make-or-remake-package-hashtable 0)
685 (package-external-symbols package)
686 (make-or-remake-package-hashtable 0))
687 t))))))
689 (defun list-all-packages ()
690 #!+sb-doc
691 "Return a list of all existing packages."
692 (let ((res ()))
693 (maphash (lambda (k v)
694 (declare (ignore k))
695 (pushnew v res))
696 *package-names*)
697 res))
699 (defun intern (name &optional (package (sane-package)))
700 #!+sb-doc
701 "Return a symbol in PACKAGE having the specified NAME, creating it
702 if necessary."
703 ;; We just simple-stringify the name and call INTERN*, where the real
704 ;; logic is.
705 (let ((name (if (simple-string-p name)
706 name
707 (coerce name 'simple-string)))
708 (package (find-undeleted-package-or-lose package)))
709 (declare (simple-string name))
710 (intern* name
711 (length name)
712 package)))
714 (defun find-symbol (name &optional (package (sane-package)))
715 #!+sb-doc
716 "Return the symbol named STRING in PACKAGE. If such a symbol is found
717 then the second value is :INTERNAL, :EXTERNAL or :INHERITED to indicate
718 how the symbol is accessible. If no symbol is found then both values
719 are NIL."
720 ;; We just simple-stringify the name and call FIND-SYMBOL*, where the
721 ;; real logic is.
722 (let ((name (if (simple-string-p name) name (coerce name 'simple-string))))
723 (declare (simple-string name))
724 (find-symbol* name
725 (length name)
726 (find-undeleted-package-or-lose package))))
728 ;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
729 ;;; then create it, special-casing the keyword package.
730 (defun intern* (name length package)
731 (declare (simple-string name))
732 (multiple-value-bind (symbol where) (find-symbol* name length package)
733 (cond (where
734 (values symbol where))
736 ;; Let's try again with a lock: the common case has the
737 ;; symbol already interned, handled by the first leg of the
738 ;; COND, but in case another thread is interning in
739 ;; parallel we need to check after grabbing the lock.
740 (with-packages ()
741 (setf (values symbol where) (find-symbol* name length package))
742 (if where
743 (values symbol where)
744 (let ((symbol-name (subseq name 0 length)))
745 (with-single-package-locked-error
746 (:package package "interning ~A" symbol-name)
747 (let ((symbol (make-symbol symbol-name)))
748 (%set-symbol-package symbol package)
749 (cond
750 ((eq package *keyword-package*)
751 (add-symbol (package-external-symbols package) symbol)
752 (%set-symbol-value symbol symbol))
754 (add-symbol (package-internal-symbols package) symbol)))
755 (values symbol nil))))))))))
757 ;;; Check internal and external symbols, then scan down the list
758 ;;; of hashtables for inherited symbols.
759 (defun find-symbol* (string length package)
760 (declare (simple-string string)
761 (type index length))
762 (let* ((hash (%sxhash-simple-substring string length))
763 (ehash (entry-hash length hash)))
764 (declare (type index hash ehash))
765 (with-symbol (found symbol (package-internal-symbols package)
766 string length hash ehash)
767 (when found
768 (return-from find-symbol* (values symbol :internal))))
769 (with-symbol (found symbol (package-external-symbols package)
770 string length hash ehash)
771 (when found
772 (return-from find-symbol* (values symbol :external))))
773 (let ((head (package-tables package)))
774 (do ((prev head table)
775 (table (cdr head) (cdr table)))
776 ((null table) (values nil nil))
777 (with-symbol (found symbol (car table) string length hash ehash)
778 (when found
779 ;; At this point we used to move the table to the
780 ;; beginning of the list, probably on the theory that we'd
781 ;; soon be looking up further items there. Unfortunately
782 ;; that was very much non-thread safe. Since the failure
783 ;; mode was nasty (corruption of the package in a way
784 ;; which would make symbol lookups loop infinitely) and it
785 ;; would be triggered just by doing reads to a resource
786 ;; that users can't do their own locking on, that code has
787 ;; been removed. If we ever add locking to packages,
788 ;; resurrecting that code might make sense, even though it
789 ;; didn't seem to have much of an performance effect in
790 ;; normal use.
792 ;; -- JES, 2006-09-13
793 (return-from find-symbol* (values symbol :inherited))))))))
795 ;;; Similar to FIND-SYMBOL, but only looks for an external symbol.
796 ;;; This is used for fast name-conflict checking in this file and symbol
797 ;;; printing in the printer.
798 (defun find-external-symbol (string package)
799 (declare (simple-string string))
800 (let* ((length (length string))
801 (hash (%sxhash-simple-string string))
802 (ehash (entry-hash length hash)))
803 (declare (type index length hash))
804 (with-symbol (found symbol (package-external-symbols package)
805 string length hash ehash)
806 (values symbol found))))
808 (defun print-symbol-with-prefix (stream symbol colon at)
809 #!+sb-doc
810 "For use with ~/: Write SYMBOL to STREAM as if it is not accessible from
811 the current package."
812 (declare (ignore colon at))
813 ;; Only keywords should be accessible from the keyword package, and
814 ;; keywords are always printed with colons, so this guarantees that the
815 ;; symbol will not be printed without a prefix.
816 (let ((*package* *keyword-package*))
817 (write symbol :stream stream :escape t)))
819 (define-condition name-conflict (reference-condition package-error)
820 ((function :initarg :function :reader name-conflict-function)
821 (datum :initarg :datum :reader name-conflict-datum)
822 (symbols :initarg :symbols :reader name-conflict-symbols))
823 (:default-initargs :references (list '(:ansi-cl :section (11 1 1 2 5))))
824 (:report
825 (lambda (c s)
826 (format s "~@<~S ~S causes name-conflicts in ~S between the ~
827 following symbols:~2I~@:_~
828 ~{~/sb-impl::print-symbol-with-prefix/~^, ~}~:@>"
829 (name-conflict-function c)
830 (name-conflict-datum c)
831 (package-error-package c)
832 (name-conflict-symbols c)))))
834 (defun name-conflict (package function datum &rest symbols)
835 (restart-case
836 (error 'name-conflict :package package :symbols symbols
837 :function function :datum datum)
838 (resolve-conflict (s)
839 :report "Resolve conflict."
840 :interactive
841 (lambda ()
842 (let* ((len (length symbols))
843 (nlen (length (write-to-string len :base 10)))
844 (*print-pretty* t))
845 (format *query-io* "~&~@<Select a symbol to be made accessible in ~
846 package ~A:~2I~@:_~{~{~V,' D. ~
847 ~/sb-impl::print-symbol-with-prefix/~}~@:_~}~
848 ~@:>"
849 (package-name package)
850 (loop for s in symbols
851 for i upfrom 1
852 collect (list nlen i s)))
853 (loop
854 (format *query-io* "~&Enter an integer (between 1 and ~D): " len)
855 (finish-output *query-io*)
856 (let ((i (parse-integer (read-line *query-io*) :junk-allowed t)))
857 (when (and i (<= 1 i len))
858 (return (list (nth (1- i) symbols))))))))
859 (multiple-value-bind (symbol status)
860 (find-symbol (symbol-name s) package)
861 (declare (ignore status)) ; FIXME: is that true?
862 (case function
863 ((export)
864 (if (eq symbol s)
865 (shadow symbol package)
866 (unintern symbol package)))
867 ((unintern)
868 (shadowing-import s package))
869 ((import)
870 (if (eq symbol s)
871 nil ; do nothing
872 (shadowing-import s package)))
873 ((use-package)
874 (if (eq symbol s)
875 (shadow s package)
876 (shadowing-import s package))))))))
878 #+nil ; this solution gives a variable number of restarts instead, but
879 ; no good way of programmatically choosing between them.
880 (defun name-conflict (package function datum &rest symbols)
881 (let ((condition (make-condition 'name-conflict
882 :package package :symbols symbols
883 :function function :datum datum)))
884 ;; this is a gross violation of modularity, but I can't see any
885 ;; other way to have a variable number of restarts.
886 (let ((*restart-clusters*
887 (cons
888 (mapcan
889 (lambda (s)
890 (multiple-value-bind (accessible-symbol status)
891 (find-symbol (symbol-name s) package)
892 (cond
893 ;; difficult case
894 ((eq s accessible-symbol)
895 (ecase status
896 ((:inherited)
897 (list (make-restart
898 :name (make-symbol "SHADOWING-IMPORT")
899 :function (lambda ()
900 (shadowing-import s package)
901 (return-from name-conflict))
902 :report-function
903 (lambda (stream)
904 (format stream "Shadowing-import ~S into ~A."
905 s (package-%name package))))))
906 ((:internal :external)
907 (aver (= (length symbols) 2))
908 ;; ARGH! FIXME: this unintern restart can
909 ;; _still_ leave the system in an
910 ;; unsatisfactory state: if the symbol is a
911 ;; external symbol of a package which is
912 ;; already used by this package, and has also
913 ;; been imported, then uninterning it from this
914 ;; package will still leave it visible!
916 ;; (DEFPACKAGE "FOO" (:EXPORT "SYM"))
917 ;; (DEFPACKAGE "BAR" (:EXPORT "SYM"))
918 ;; (DEFPACKAGE "BAZ" (:USE "FOO"))
919 ;; (IMPORT 'FOO:SYM "BAZ")
920 ;; (USE-PACKAGE "BAR" "BAZ")
922 ;; Now (UNINTERN 'FOO:SYM "BAZ") doesn't
923 ;; resolve the conflict. :-(
925 ;; -- CSR, 2004-10-20
926 (list (make-restart
927 :name (make-symbol "UNINTERN")
928 :function (lambda ()
929 (unintern s package)
930 (import
931 (find s symbols :test-not #'eq)
932 package)
933 (return-from name-conflict))
934 :report-function
935 (lambda (stream)
936 (format stream
937 "Unintern ~S from ~A and import ~S."
939 (package-%name package)
940 (find s symbols :test-not #'eq))))))))
941 (t (list (make-restart
942 :name (make-symbol "SHADOWING-IMPORT")
943 :function (lambda ()
944 (shadowing-import s package)
945 (return-from name-conflict))
946 :report-function
947 (lambda (stream)
948 (format stream "Shadowing-import ~S into ~A."
949 s (package-%name package)))))))))
950 symbols)
951 *restart-clusters*)))
952 (with-condition-restarts condition (car *restart-clusters*)
953 (with-simple-restart (abort "Leave action undone.")
954 (error condition))))))
956 ;;; If we are uninterning a shadowing symbol, then a name conflict can
957 ;;; result, otherwise just nuke the symbol.
958 (defun unintern (symbol &optional (package (sane-package)))
959 #!+sb-doc
960 "Makes SYMBOL no longer present in PACKAGE. If SYMBOL was present then T is
961 returned, otherwise NIL. If PACKAGE is SYMBOL's home package, then it is made
962 uninterned."
963 (with-packages ()
964 (let* ((package (find-undeleted-package-or-lose package))
965 (name (symbol-name symbol))
966 (shadowing-symbols (package-%shadowing-symbols package)))
967 (declare (list shadowing-symbols))
969 (with-single-package-locked-error ()
970 (when (find-symbol name package)
971 (assert-package-unlocked package "uninterning ~A" name))
973 ;; If a name conflict is revealed, give us a chance to
974 ;; shadowing-import one of the accessible symbols.
975 (when (member symbol shadowing-symbols)
976 (let ((cset ()))
977 (dolist (p (package-%use-list package))
978 (multiple-value-bind (s w) (find-external-symbol name p)
979 (when w (pushnew s cset))))
980 (when (cdr cset)
981 (apply #'name-conflict package 'unintern symbol cset)
982 (return-from unintern t)))
983 (setf (package-%shadowing-symbols package)
984 (remove symbol shadowing-symbols)))
986 (multiple-value-bind (s w) (find-symbol name package)
987 (declare (ignore s))
988 (cond ((or (eq w :internal) (eq w :external))
989 (nuke-symbol (if (eq w :internal)
990 (package-internal-symbols package)
991 (package-external-symbols package))
992 name)
993 (if (eq (symbol-package symbol) package)
994 (%set-symbol-package symbol nil))
996 (t nil)))))))
998 ;;; Take a symbol-or-list-of-symbols and return a list, checking types.
999 (defun symbol-listify (thing)
1000 (cond ((listp thing)
1001 (dolist (s thing)
1002 (unless (symbolp s) (error "~S is not a symbol." s)))
1003 thing)
1004 ((symbolp thing) (list thing))
1006 (error "~S is neither a symbol nor a list of symbols." thing))))
1008 (defun string-listify (thing)
1009 (mapcar #'string (if (listp thing)
1010 thing
1011 (list thing))))
1013 ;;; This is like UNINTERN, except if SYMBOL is inherited, it chases
1014 ;;; down the package it is inherited from and uninterns it there. Used
1015 ;;; for name-conflict resolution. Shadowing symbols are not uninterned
1016 ;;; since they do not cause conflicts.
1017 (defun moby-unintern (symbol package)
1018 (unless (member symbol (package-%shadowing-symbols package))
1019 (or (unintern symbol package)
1020 (let ((name (symbol-name symbol)))
1021 (multiple-value-bind (s w) (find-symbol name package)
1022 (declare (ignore s))
1023 (when (eq w :inherited)
1024 (dolist (q (package-%use-list package))
1025 (multiple-value-bind (u x) (find-external-symbol name q)
1026 (declare (ignore u))
1027 (when x
1028 (unintern symbol q)
1029 (return t))))))))))
1031 (defun export (symbols &optional (package (sane-package)))
1032 #!+sb-doc
1033 "Exports SYMBOLS from PACKAGE, checking that no name conflicts result."
1034 (with-packages ()
1035 (let ((package (find-undeleted-package-or-lose package))
1036 (syms ()))
1037 ;; Punt any symbols that are already external.
1038 (dolist (sym (symbol-listify symbols))
1039 (multiple-value-bind (s w)
1040 (find-external-symbol (symbol-name sym) package)
1041 (declare (ignore s))
1042 (unless (or w (member sym syms))
1043 (push sym syms))))
1044 (with-single-package-locked-error ()
1045 (when syms
1046 (assert-package-unlocked package "exporting symbol~P ~{~A~^, ~}"
1047 (length syms) syms))
1048 ;; Find symbols and packages with conflicts.
1049 (let ((used-by (package-%used-by-list package))
1050 (cset ()))
1051 (dolist (sym syms)
1052 (let ((name (symbol-name sym)))
1053 (dolist (p used-by)
1054 (multiple-value-bind (s w) (find-symbol name p)
1055 (when (and w
1056 (not (eq s sym))
1057 (not (member s (package-%shadowing-symbols p))))
1058 ;; Beware: the name conflict is in package P, not in
1059 ;; PACKAGE.
1060 (name-conflict p 'export sym sym s)
1061 (pushnew sym cset))))))
1062 (when cset
1063 (setq syms (set-difference syms cset))))
1064 ;; Check that all symbols are accessible. If not, ask to import them.
1065 (let ((missing ())
1066 (imports ()))
1067 (dolist (sym syms)
1068 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1069 (cond ((not (and w (eq s sym)))
1070 (push sym missing))
1071 ((eq w :inherited)
1072 (push sym imports)))))
1073 (when missing
1074 (cerror
1075 "~S these symbols into the ~A package."
1076 (make-condition
1077 'simple-package-error
1078 :package package
1079 :format-control
1080 "~@<These symbols are not accessible in the ~A package:~2I~_~S~@:>"
1081 :format-arguments (list (package-%name package) missing))
1082 'import (package-%name package))
1083 (import missing package))
1084 (import imports package))
1086 ;; And now, three pages later, we export the suckers.
1087 (let ((internal (package-internal-symbols package))
1088 (external (package-external-symbols package)))
1089 (dolist (sym syms)
1090 (nuke-symbol internal (symbol-name sym))
1091 (add-symbol external sym))))
1092 t)))
1094 ;;; Check that all symbols are accessible, then move from external to internal.
1095 (defun unexport (symbols &optional (package (sane-package)))
1096 #!+sb-doc
1097 "Makes SYMBOLS no longer exported from PACKAGE."
1098 (with-packages ()
1099 (let ((package (find-undeleted-package-or-lose package))
1100 (syms ()))
1101 (dolist (sym (symbol-listify symbols))
1102 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1103 (cond ((or (not w) (not (eq s sym)))
1104 (error 'simple-package-error
1105 :package package
1106 :format-control "~S is not accessible in the ~A package."
1107 :format-arguments (list sym (package-%name package))))
1108 ((eq w :external) (pushnew sym syms)))))
1109 (with-single-package-locked-error ()
1110 (when syms
1111 (assert-package-unlocked package "unexporting symbol~P ~{~A~^, ~}"
1112 (length syms) syms))
1113 (let ((internal (package-internal-symbols package))
1114 (external (package-external-symbols package)))
1115 (dolist (sym syms)
1116 (add-symbol internal sym)
1117 (nuke-symbol external (symbol-name sym)))))
1118 t)))
1120 ;;; Check for name conflict caused by the import and let the user
1121 ;;; shadowing-import if there is.
1122 (defun import (symbols &optional (package (sane-package)))
1123 #!+sb-doc
1124 "Make SYMBOLS accessible as internal symbols in PACKAGE. If a symbol is
1125 already accessible then it has no effect. If a name conflict would result from
1126 the importation, then a correctable error is signalled."
1127 (with-packages ()
1128 (let* ((package (find-undeleted-package-or-lose package))
1129 (symbols (symbol-listify symbols))
1130 (homeless (remove-if #'symbol-package symbols))
1131 (syms ()))
1132 (with-single-package-locked-error ()
1133 (dolist (sym symbols)
1134 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1135 (cond ((not w)
1136 (let ((found (member sym syms :test #'string=)))
1137 (if found
1138 (when (not (eq (car found) sym))
1139 (name-conflict package 'import sym sym (car found)))
1140 (push sym syms))))
1141 ((not (eq s sym))
1142 (name-conflict package 'import sym sym s))
1143 ((eq w :inherited) (push sym syms)))))
1144 (when (or homeless syms)
1145 (let ((union (delete-duplicates (append homeless syms))))
1146 (assert-package-unlocked package "importing symbol~P ~{~A~^, ~}"
1147 (length union) union)))
1148 ;; Add the new symbols to the internal hashtable.
1149 (let ((internal (package-internal-symbols package)))
1150 (dolist (sym syms)
1151 (add-symbol internal sym)))
1152 ;; If any of the symbols are uninterned, make them be owned by PACKAGE.
1153 (dolist (sym homeless)
1154 (%set-symbol-package sym package))
1155 t))))
1157 ;;; If a conflicting symbol is present, unintern it, otherwise just
1158 ;;; stick the symbol in.
1159 (defun shadowing-import (symbols &optional (package (sane-package)))
1160 #!+sb-doc
1161 "Import SYMBOLS into package, disregarding any name conflict. If
1162 a symbol of the same name is present, then it is uninterned."
1163 (with-packages ()
1164 (let* ((package (find-undeleted-package-or-lose package))
1165 (internal (package-internal-symbols package))
1166 (symbols (symbol-listify symbols))
1167 (lock-asserted-p nil))
1168 (with-single-package-locked-error ()
1169 (dolist (sym symbols)
1170 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1171 (unless (or lock-asserted-p
1172 (and (eq s sym)
1173 (member s (package-shadowing-symbols package))))
1174 (assert-package-unlocked package "shadowing-importing symbol~P ~
1175 ~{~A~^, ~}" (length symbols) symbols)
1176 (setf lock-asserted-p t))
1177 (unless (and w (not (eq w :inherited)) (eq s sym))
1178 (when (or (eq w :internal) (eq w :external))
1179 ;; If it was shadowed, we don't want UNINTERN to flame out...
1180 (setf (package-%shadowing-symbols package)
1181 (remove s (the list (package-%shadowing-symbols package))))
1182 (unintern s package))
1183 (add-symbol internal sym))
1184 (pushnew sym (package-%shadowing-symbols package)))))))
1187 (defun shadow (symbols &optional (package (sane-package)))
1188 #!+sb-doc
1189 "Make an internal symbol in PACKAGE with the same name as each of the
1190 specified SYMBOLS. If a symbol with the given name is already present in
1191 PACKAGE, then the existing symbol is placed in the shadowing symbols list if
1192 it is not already present."
1193 (with-packages ()
1194 (let* ((package (find-undeleted-package-or-lose package))
1195 (internal (package-internal-symbols package))
1196 (symbols (string-listify symbols))
1197 (lock-asserted-p nil))
1198 (flet ((present-p (w)
1199 (and w (not (eq w :inherited)))))
1200 (with-single-package-locked-error ()
1201 (dolist (name symbols)
1202 (multiple-value-bind (s w) (find-symbol name package)
1203 (unless (or lock-asserted-p
1204 (and (present-p w)
1205 (member s (package-shadowing-symbols package))))
1206 (assert-package-unlocked package "shadowing symbol~P ~{~A~^, ~}"
1207 (length symbols) symbols)
1208 (setf lock-asserted-p t))
1209 (unless (present-p w)
1210 (setq s (make-symbol name))
1211 (%set-symbol-package s package)
1212 (add-symbol internal s))
1213 (pushnew s (package-%shadowing-symbols package))))))))
1216 ;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
1217 (defun use-package (packages-to-use &optional (package (sane-package)))
1218 #!+sb-doc
1219 "Add all the PACKAGES-TO-USE to the use list for PACKAGE so that the
1220 external symbols of the used packages are accessible as internal symbols in
1221 PACKAGE."
1222 (with-packages ()
1223 (let ((packages (package-listify packages-to-use))
1224 (package (find-undeleted-package-or-lose package)))
1226 ;; Loop over each package, USE'ing one at a time...
1227 (with-single-package-locked-error ()
1228 (dolist (pkg packages)
1229 (unless (member pkg (package-%use-list package))
1230 (assert-package-unlocked package "using package~P ~{~A~^, ~}"
1231 (length packages) packages)
1232 (let ((shadowing-symbols (package-%shadowing-symbols package))
1233 (use-list (package-%use-list package)))
1235 ;; If the number of symbols already accessible is less
1236 ;; than the number to be inherited then it is faster to
1237 ;; run the test the other way. This is particularly
1238 ;; valuable in the case of a new package USEing
1239 ;; COMMON-LISP.
1240 (cond
1241 ((< (+ (package-internal-symbol-count package)
1242 (package-external-symbol-count package)
1243 (let ((res 0))
1244 (dolist (p use-list res)
1245 (incf res (package-external-symbol-count p)))))
1246 (package-external-symbol-count pkg))
1247 (do-symbols (sym package)
1248 (multiple-value-bind (s w)
1249 (find-external-symbol (symbol-name sym) pkg)
1250 (when (and w
1251 (not (eq s sym))
1252 (not (member sym shadowing-symbols)))
1253 (name-conflict package 'use-package pkg sym s))))
1254 (dolist (p use-list)
1255 (do-external-symbols (sym p)
1256 (multiple-value-bind (s w)
1257 (find-external-symbol (symbol-name sym) pkg)
1258 (when (and w
1259 (not (eq s sym))
1260 (not (member
1261 (find-symbol (symbol-name sym) package)
1262 shadowing-symbols)))
1263 (name-conflict package 'use-package pkg sym s))))))
1265 (do-external-symbols (sym pkg)
1266 (multiple-value-bind (s w)
1267 (find-symbol (symbol-name sym) package)
1268 (when (and w
1269 (not (eq s sym))
1270 (not (member s shadowing-symbols)))
1271 (name-conflict package 'use-package pkg sym s)))))))
1273 (push pkg (package-%use-list package))
1274 (push (package-external-symbols pkg) (cdr (package-tables package)))
1275 (push package (package-%used-by-list pkg)))))))
1278 (defun unuse-package (packages-to-unuse &optional (package (sane-package)))
1279 #!+sb-doc
1280 "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
1281 (with-packages ()
1282 (let ((package (find-undeleted-package-or-lose package))
1283 (packages (package-listify packages-to-unuse)))
1284 (with-single-package-locked-error ()
1285 (dolist (p packages)
1286 (when (member p (package-use-list package))
1287 (assert-package-unlocked package "unusing package~P ~{~A~^, ~}"
1288 (length packages) packages))
1289 (setf (package-%use-list package)
1290 (remove p (the list (package-%use-list package))))
1291 (setf (package-tables package)
1292 (delete (package-external-symbols p)
1293 (the list (package-tables package))))
1294 (setf (package-%used-by-list p)
1295 (remove package (the list (package-%used-by-list p))))))
1296 t)))
1298 (defun find-all-symbols (string-or-symbol)
1299 #!+sb-doc
1300 "Return a list of all symbols in the system having the specified name."
1301 (let ((string (string string-or-symbol))
1302 (res ()))
1303 (maphash (lambda (k v)
1304 (declare (ignore k))
1305 (multiple-value-bind (s w) (find-symbol string v)
1306 (when w (pushnew s res))))
1307 *package-names*)
1308 res))
1310 ;;;; APROPOS and APROPOS-LIST
1312 (defun briefly-describe-symbol (symbol)
1313 (fresh-line)
1314 (prin1 symbol)
1315 (when (boundp symbol)
1316 (write-string " (bound)"))
1317 (when (fboundp symbol)
1318 (write-string " (fbound)")))
1320 (defun apropos-list (string-designator
1321 &optional
1322 package-designator
1323 external-only)
1324 #!+sb-doc
1325 "Like APROPOS, except that it returns a list of the symbols found instead
1326 of describing them."
1327 (if package-designator
1328 (let ((package (find-undeleted-package-or-lose package-designator))
1329 (string (stringify-string-designator string-designator))
1330 (result nil))
1331 (do-symbols (symbol package)
1332 (when (and (eq (symbol-package symbol) package)
1333 (or (not external-only)
1334 (eq (nth-value 1 (find-symbol (symbol-name symbol)
1335 package))
1336 :external))
1337 (search string (symbol-name symbol) :test #'char-equal))
1338 (push symbol result)))
1339 result)
1340 (mapcan (lambda (package)
1341 (apropos-list string-designator package external-only))
1342 (list-all-packages))))
1344 (defun apropos (string-designator &optional package external-only)
1345 #!+sb-doc
1346 "Briefly describe all symbols which contain the specified STRING.
1347 If PACKAGE is supplied then only describe symbols present in
1348 that package. If EXTERNAL-ONLY then only describe
1349 external symbols in the specified package."
1350 ;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
1351 ;; of some unnecessary consing; and the unnecessary consing shouldn't be an
1352 ;; issue, since this function is is only useful interactively anyway, and
1353 ;; we can cons and GC a lot faster than the typical user can read..
1354 (dolist (symbol (apropos-list string-designator package external-only))
1355 (briefly-describe-symbol symbol))
1356 (values))
1358 ;;;; final initialization
1360 ;;;; The cold loader (GENESIS) makes the data structure in
1361 ;;;; *!INITIAL-SYMBOLS*. We grovel over it, making the specified
1362 ;;;; packages and interning the symbols. For a description of the
1363 ;;;; format of *!INITIAL-SYMBOLS*, see the GENESIS source.
1365 (defvar *!initial-symbols*)
1367 (!cold-init-forms
1369 (setq *in-package-init* t)
1371 (/show0 "about to loop over *!INITIAL-SYMBOLS* to make packages")
1372 (dolist (spec *!initial-symbols*)
1373 (let* ((pkg (apply #'make-package (first spec)))
1374 (internal (package-internal-symbols pkg))
1375 (external (package-external-symbols pkg)))
1376 (/show0 "back from MAKE-PACKAGE, PACKAGE-NAME=..")
1377 (/primitive-print (package-name pkg))
1379 ;; Put internal symbols in the internal hashtable and set package.
1380 (dolist (symbol (second spec))
1381 (add-symbol internal symbol)
1382 (%set-symbol-package symbol pkg))
1384 ;; External symbols same, only go in external table.
1385 (dolist (symbol (third spec))
1386 (add-symbol external symbol)
1387 (%set-symbol-package symbol pkg))
1389 ;; Don't set package for imported symbols.
1390 (dolist (symbol (fourth spec))
1391 (add-symbol internal symbol))
1392 (dolist (symbol (fifth spec))
1393 (add-symbol external symbol))
1395 ;; Put shadowing symbols in the shadowing symbols list.
1396 (setf (package-%shadowing-symbols pkg) (sixth spec))
1397 ;; Set the package documentation
1398 (setf (package-doc-string pkg) (seventh spec))))
1400 ;; FIXME: These assignments are also done at toplevel in
1401 ;; boot-extensions.lisp. They should probably only be done once.
1402 (/show0 "setting up *CL-PACKAGE* and *KEYWORD-PACKAGE*")
1403 (setq *cl-package* (find-package "COMMON-LISP"))
1404 (setq *keyword-package* (find-package "KEYWORD"))
1406 (/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*")
1407 (makunbound '*!initial-symbols*) ; (so that it gets GCed)
1409 ;; Make some other packages that should be around in the cold load.
1410 ;; The COMMON-LISP-USER package is required by the ANSI standard,
1411 ;; but not completely specified by it, so in the cross-compilation
1412 ;; host Lisp it could contain various symbols, USE-PACKAGEs, or
1413 ;; nicknames that we don't want in our target SBCL. For that reason,
1414 ;; we handle it specially, not dumping the host Lisp version at
1415 ;; genesis time..
1416 (aver (not (find-package "COMMON-LISP-USER")))
1417 ;; ..but instead making our own from scratch here.
1418 (/show0 "about to MAKE-PACKAGE COMMON-LISP-USER")
1419 (make-package "COMMON-LISP-USER"
1420 :nicknames '("CL-USER")
1421 :use '("COMMON-LISP"
1422 ;; ANSI encourages us to put extension packages
1423 ;; in the USE list of COMMON-LISP-USER.
1424 "SB!ALIEN" "SB!ALIEN" "SB!DEBUG"
1425 "SB!EXT" "SB!GRAY" "SB!PROFILE"))
1427 ;; Now do the *!DEFERRED-USE-PACKAGES*.
1428 (/show0 "about to do *!DEFERRED-USE-PACKAGES*")
1429 (dolist (args *!deferred-use-packages*)
1430 (apply #'use-package args))
1432 ;; The Age Of Magic is over, we can behave ANSIly henceforth.
1433 (/show0 "about to SETQ *IN-PACKAGE-INIT*")
1434 (setq *in-package-init* nil)
1436 ;; For the kernel core image wizards, set the package to *CL-PACKAGE*.
1438 ;; FIXME: We should just set this to (FIND-PACKAGE
1439 ;; "COMMON-LISP-USER") once and for all here, instead of setting it
1440 ;; once here and resetting it later.
1441 (setq *package* *cl-package*))
1443 (!cold-init-forms
1444 (/show0 "done with !PACKAGE-COLD-INIT"))
1446 (!defun-from-collected-cold-init-forms !package-cold-init)