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