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