Implement FIND-PACKAGE-FROM-SUBSTRING which works in the same
[sbcl/tcr.git] / src / code / target-package.lisp
blobf78d5bfe686104d012101e1241f1254ba13a1877
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)))
322 ;;; FIXME: should be declared of type PACKAGE, with no NIL init form,
323 ;;; after I get around to cleaning up DOCUMENTATION.
324 (defvar *package* (error "*PACKAGE* should be initialized in cold load!")
325 #!+sb-doc "the current package")
327 ;;; A map from package names to packages.
329 ;; This will be set to a hash-table with a customized test and hashing
330 ;; function. The customized functions allow (CONS STRING LENGTH) as a
331 ;; key to mean (SUBSEQ STRING 0 LENGTH), but without the consing. We
332 ;; need this to implement FIND-PACKAGE-FROM-SUBSTRING which is used by
333 ;; the reader, so we don't have to cons up a new string for each
334 ;; explicitly given package qualifier.
336 (defvar *package-names*)
338 (defun resolve-package-name-designator (designator)
339 ;; FIXME: Perhaps we can declare the returned string to be a
340 ;; SIMPLE-STRING. This would help the %SXHASH-SUBSTRING below.
341 (declare (type (or string cons) designator))
342 (if (consp designator)
343 (destructuring-bind (str . length) designator
344 (declare (string str) (index length))
345 (values str length))
346 designator))
348 (defun package-name-= (designator1 designator2)
349 (multiple-value-bind (str1 length1)
350 (resolve-package-name-designator designator1)
351 (multiple-value-bind (str2 length2)
352 (resolve-package-name-designator designator2)
353 (string= str1 str2 :end1 length1 :end2 length2))))
355 (defun package-name-sxhash (designator)
356 (multiple-value-bind (str length)
357 (resolve-package-name-designator designator)
358 (if length
359 (%sxhash-substring str length)
360 (sxhash str))))
362 (declaim (type hash-table *package-names*))
363 (!cold-init-forms
364 (/show0 "About to define PACKAGE-NAME-= as hash table test")
365 (sb!int:define-hash-table-test 'package-name-=
366 #'package-name-= #'package-name-sxhash)
368 (/show0 "About to set *PACKAGE-NAMES*")
369 (setf *package-names* (make-hash-table :test 'package-name-=)))
372 ;;; This magical variable is T during initialization so that
373 ;;; USE-PACKAGE's of packages that don't yet exist quietly win. Such
374 ;;; packages are thrown onto the list *DEFERRED-USE-PACKAGES* so that
375 ;;; this can be fixed up later.
377 ;;; FIXME: This could be cleaned up the same way I do it in my package
378 ;;; hacking when setting up the cross-compiler. Then we wouldn't have
379 ;;; this extraneous global variable and annoying runtime tests on
380 ;;; package operations. (*DEFERRED-USE-PACKAGES* would also go away.)
381 (defvar *in-package-init*)
383 ;;; pending USE-PACKAGE arguments saved up while *IN-PACKAGE-INIT* is true
384 (defvar *!deferred-use-packages*)
385 (!cold-init-forms
386 (setf *!deferred-use-packages* nil))
388 (define-condition bootstrap-package-not-found (condition)
389 ((name :initarg :name :reader bootstrap-package-name)))
391 (defun bootstrap-package-name-p (package-name)
392 (let ((mismatch (mismatch "SB!" package-name)))
393 (and mismatch (= mismatch 3))))
395 (defun run-debootstrap-hook (package-name)
396 (restart-case
397 (signal 'bootstrap-package-not-found :name package-name)
398 (debootstrap-package ()
399 (if (string= package-name "SB!XC")
400 (find-package "COMMON-LISP")
401 (find-package
402 (substitute #\- #\! package-name :count 1))))))
404 (defun debootstrap-package (&optional condition)
405 (invoke-restart
406 (find-restart-or-control-error 'debootstrap-package condition)))
408 (defun find-package-from-substring (string &optional length)
409 (declare (type string string))
410 (let ((packageoid (gethash (if length
411 (cons string length) ; cf. PACKAGE-NAME-=
412 string)
413 *package-names*)))
414 (if (and (null packageoid)
415 (bootstrap-package-name-p string)
416 ;; KLUDGE: When *IN-PACKAGE-INIT* is T, we're about to
417 ;; create all packages during cold-init, including the
418 ;; SB!FOO packages. We come here because MAKE-PACKAGE
419 ;; invokes FIND-PACKAGE. In this case, PACKAGEOID is NIL
420 ;; (SB!FOO doesn't exist yet), but we do not want to run
421 ;; the debootstrap hook because the condition system
422 ;; isn't fully ready yet.
423 (not *in-package-init*))
424 (run-debootstrap-hook string)
425 packageoid)))
427 (defun find-package (package-designator)
428 (typecase package-designator
429 (package package-designator)
430 (symbol (find-package-from-substring (symbol-name package-designator)))
431 (string (find-package-from-substring package-designator))
432 (character (find-package-from-substring (string package-designator)))
433 (t (error 'type-error
434 :datum package-designator
435 :expected-type '(or character package string symbol)))))
437 ;;; Return a list of packages given a package designator or list of
438 ;;; package designators, or die trying.
439 (defun package-listify (thing)
440 (let ((res ()))
441 (dolist (thing (if (listp thing) thing (list thing)) res)
442 (push (find-undeleted-package-or-lose thing) res))))
444 ;;; Make a package name into a simple-string.
445 (defun package-namify (n)
446 (stringify-package-designator n))
448 ;;; ANSI specifies (in the definition of DELETE-PACKAGE) that PACKAGE-NAME
449 ;;; returns NIL (not an error) for a deleted package, so this is a special
450 ;;; case where we want to use bare %FIND-PACKAGE-OR-LOSE instead of
451 ;;; FIND-UNDELETED-PACKAGE-OR-LOSE.
452 (defun package-name (package-designator)
453 (package-%name (%find-package-or-lose package-designator)))
455 ;;;; operations on package hashtables
457 ;;; Compute a number from the sxhash of the pname and the length which
458 ;;; must be between 2 and 255.
459 (defmacro entry-hash (length sxhash)
460 `(the fixnum
461 (+ (the fixnum
462 (rem (the fixnum
463 (logxor ,length
464 ,sxhash
465 (the fixnum (ash ,sxhash -8))
466 (the fixnum (ash ,sxhash -16))
467 (the fixnum (ash ,sxhash -19))))
468 254))
469 2)))
470 ;;; FIXME: should be wrapped in EVAL-WHEN (COMPILE EXECUTE)
472 ;;; Add a symbol to a package hashtable. The symbol is assumed
473 ;;; not to be present.
474 (defun add-symbol (table symbol)
475 (when (zerop (package-hashtable-free table))
476 ;; The hashtable is full. Resize it to be able to hold twice the
477 ;; amount of symbols than it currently contains. The actual new size
478 ;; can be smaller than twice the current size if the table contained
479 ;; deleted entries.
480 (resize-package-hashtable table
481 (* (- (package-hashtable-size table)
482 (package-hashtable-deleted table))
483 2)))
484 (let* ((vec (package-hashtable-table table))
485 (hash (package-hashtable-hash table))
486 (len (length vec))
487 (sxhash (%sxhash-simple-string (symbol-name symbol)))
488 (h2 (1+ (rem sxhash (- len 2)))))
489 (declare (fixnum sxhash h2))
490 (do ((i (rem sxhash len) (rem (+ i h2) len)))
491 ((< (the fixnum (aref hash i)) 2)
492 (if (zerop (the fixnum (aref hash i)))
493 (decf (package-hashtable-free table))
494 (decf (package-hashtable-deleted table)))
495 (setf (svref vec i) symbol)
496 (setf (aref hash i)
497 (entry-hash (length (symbol-name symbol))
498 sxhash)))
499 (declare (fixnum i)))))
501 ;;; Resize the package hashtables of all packages so that their load
502 ;;; factor is +PACKAGE-HASHTABLE-IMAGE-LOAD-FACTOR+. Called from
503 ;;; SAVE-LISP-AND-DIE to optimize space usage in the image.
504 (defun tune-hashtable-sizes-of-all-packages ()
505 (flet ((tune-table-size (table)
506 (resize-package-hashtable
507 table
508 (round (* (/ +package-rehash-threshold+
509 +package-hashtable-image-load-factor+)
510 (- (package-hashtable-size table)
511 (package-hashtable-free table)
512 (package-hashtable-deleted table)))))))
513 (dolist (package (list-all-packages))
514 (tune-table-size (package-internal-symbols package))
515 (tune-table-size (package-external-symbols package)))))
517 ;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR
518 ;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR
519 ;;; is bound to the symbol. LENGTH and HASH are the length and sxhash
520 ;;; of STRING. ENTRY-HASH is the entry-hash of the string and length.
521 (defmacro with-symbol ((index-var symbol-var table string length sxhash
522 entry-hash)
523 &body forms)
524 (let ((vec (gensym)) (hash (gensym)) (len (gensym)) (h2 (gensym))
525 (name (gensym)) (name-len (gensym)) (ehash (gensym)))
526 `(let* ((,vec (package-hashtable-table ,table))
527 (,hash (package-hashtable-hash ,table))
528 (,len (length ,vec))
529 (,h2 (1+ (the index (rem (the index ,sxhash)
530 (the index (- ,len 2)))))))
531 (declare (type index ,len ,h2))
532 (prog ((,index-var (rem (the index ,sxhash) ,len))
533 ,symbol-var ,ehash)
534 (declare (type (or index null) ,index-var))
535 LOOP
536 (setq ,ehash (aref ,hash ,index-var))
537 (cond ((eql ,ehash ,entry-hash)
538 (setq ,symbol-var (svref ,vec ,index-var))
539 (let* ((,name (symbol-name ,symbol-var))
540 (,name-len (length ,name)))
541 (declare (type index ,name-len))
542 (when (and (= ,name-len ,length)
543 (string= ,string ,name
544 :end1 ,length
545 :end2 ,name-len))
546 (go DOIT))))
547 ((zerop ,ehash)
548 (setq ,index-var nil)
549 (go DOIT)))
550 (setq ,index-var (+ ,index-var ,h2))
551 (when (>= ,index-var ,len)
552 (setq ,index-var (- ,index-var ,len)))
553 (go LOOP)
554 DOIT
555 (return (progn ,@forms))))))
557 ;;; Delete the entry for STRING in TABLE. The entry must exist.
558 (defun nuke-symbol (table string)
559 (declare (simple-string string))
560 (let* ((length (length string))
561 (hash (%sxhash-simple-string string))
562 (ehash (entry-hash length hash)))
563 (declare (type index length hash))
564 (with-symbol (index symbol table string length hash ehash)
565 (setf (aref (package-hashtable-hash table) index) 1)
566 (setf (aref (package-hashtable-table table) index) nil)
567 (incf (package-hashtable-deleted table))))
568 ;; If the table is less than one quarter full, halve its size and
569 ;; rehash the entries.
570 (let* ((size (package-hashtable-size table))
571 (deleted (package-hashtable-deleted table))
572 (used (- size
573 (package-hashtable-free table)
574 deleted)))
575 (declare (type fixnum size deleted used))
576 (when (< used (truncate size 4))
577 (resize-package-hashtable table (* used 2)))))
579 ;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*.
580 ;;; If there is a conflict then give the user a chance to do
581 ;;; something about it.
582 (defun enter-new-nicknames (package nicknames)
583 (declare (type list nicknames))
584 (dolist (n nicknames)
585 (let* ((n (package-namify n))
586 (found (gethash n *package-names*)))
587 (cond ((not found)
588 (setf (gethash n *package-names*) package)
589 (push n (package-%nicknames package)))
590 ((eq found package))
591 ((string= (the string (package-%name found)) n)
592 (cerror "Ignore this nickname."
593 'simple-package-error
594 :package package
595 :format-control "~S is a package name, so it cannot be a nickname for ~S."
596 :format-arguments (list n (package-%name package))))
598 (cerror "Leave this nickname alone."
599 'simple-package-error
600 :package package
601 :format-control "~S is already a nickname for ~S."
602 :format-arguments (list n (package-%name found))))))))
604 (defun make-package (name &key
605 (use '#.*default-package-use-list*)
606 nicknames
607 (internal-symbols 10)
608 (external-symbols 10))
609 #!+sb-doc
610 #.(format nil
611 "Make a new package having the specified NAME, NICKNAMES, and USE
612 list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are estimates for the number of
613 internal and external symbols which will ultimately be present in the package.
614 The default value of USE is implementation-dependent, and in this
615 implementation it is ~S." *default-package-use-list*)
616 (with-packages ()
617 ;; Check for package name conflicts in name and nicknames, then
618 ;; make the package.
619 (when (find-package name)
620 ;; ANSI specifies that this error is correctable.
621 (cerror "Leave existing package alone."
622 "A package named ~S already exists" name))
623 (let* ((name (package-namify name))
624 (package (internal-make-package
625 :%name name
626 :internal-symbols (make-or-remake-package-hashtable
627 internal-symbols)
628 :external-symbols (make-or-remake-package-hashtable
629 external-symbols))))
631 ;; Do a USE-PACKAGE for each thing in the USE list so that checking for
632 ;; conflicting exports among used packages is done.
633 (if *in-package-init*
634 (push (list use package) *!deferred-use-packages*)
635 (use-package use package))
637 ;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
638 ;; which would leave us with possibly-bad side effects from the earlier
639 ;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
640 ;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
641 ;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
642 ;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
643 ;; USE-PACKAGE, too.
644 (enter-new-nicknames package nicknames)
645 (setf (gethash name *package-names*) package))))
647 ;;; Change the name if we can, blast any old nicknames and then
648 ;;; add in any new ones.
650 ;;; FIXME: ANSI claims that NAME is a package designator (not just a
651 ;;; string designator -- weird). Thus, NAME could
652 ;;; be a package instead of a string. Presumably then we should not change
653 ;;; the package name if NAME is the same package that's referred to by PACKAGE.
654 ;;; If it's a *different* package, we should probably signal an error.
655 ;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
656 (defun rename-package (package name &optional (nicknames ()))
657 #!+sb-doc
658 "Changes the name and nicknames for a package."
659 (with-packages ()
660 (let* ((package (find-undeleted-package-or-lose package))
661 (name (package-namify name))
662 (found (find-package name))
663 (nicks (mapcar #'string nicknames)))
664 (unless (or (not found) (eq found package))
665 (error 'simple-package-error
666 :package name
667 :format-control "A package named ~S already exists."
668 :format-arguments (list name)))
669 (with-single-package-locked-error ()
670 (unless (and (string= name (package-name package))
671 (null (set-difference nicks (package-nicknames package)
672 :test #'string=)))
673 (assert-package-unlocked package "rename as ~A~@[ with nickname~P ~
674 ~{~A~^, ~}~]"
675 name (length nicks) nicks))
676 ;; do the renaming
677 (remhash (package-%name package) *package-names*)
678 (dolist (n (package-%nicknames package))
679 (remhash n *package-names*))
680 (setf (package-%name package) name
681 (gethash name *package-names*) package
682 (package-%nicknames package) ())
683 (enter-new-nicknames package nicknames))
684 package)))
686 (defun delete-package (package-designator)
687 #!+sb-doc
688 "Delete the package designated by PACKAGE-DESIGNATOR from the package
689 system data structures."
690 (with-packages ()
691 (let ((package (if (packagep package-designator)
692 package-designator
693 (find-package package-designator))))
694 (cond ((not package)
695 ;; This continuable error is required by ANSI.
696 (cerror
697 "Return ~S."
698 (make-condition
699 'simple-package-error
700 :package package-designator
701 :format-control "There is no package named ~S."
702 :format-arguments (list package-designator))
703 nil))
704 ((not (package-name package)) ; already deleted
705 nil)
707 (with-single-package-locked-error
708 (:package package "deleting package ~A" package)
709 (let ((use-list (package-used-by-list package)))
710 (when use-list
711 ;; This continuable error is specified by ANSI.
712 (cerror
713 "Remove dependency in other packages."
714 (make-condition
715 'simple-package-error
716 :package package
717 :format-control
718 "~@<Package ~S is used by package~P:~2I~_~S~@:>"
719 :format-arguments (list (package-name package)
720 (length use-list)
721 (mapcar #'package-name use-list))))
722 (dolist (p use-list)
723 (unuse-package package p))))
724 (dolist (used (package-use-list package))
725 (unuse-package used package))
726 (do-symbols (sym package)
727 (unintern sym package))
728 (remhash (package-name package) *package-names*)
729 (dolist (nick (package-nicknames package))
730 (remhash nick *package-names*))
731 (setf (package-%name package) nil
732 ;; Setting PACKAGE-%NAME to NIL is required in order to
733 ;; make PACKAGE-NAME return NIL for a deleted package as
734 ;; ANSI requires. Setting the other slots to NIL
735 ;; and blowing away the PACKAGE-HASHTABLES is just done
736 ;; for tidiness and to help the GC.
737 (package-%nicknames package) nil
738 (package-%use-list package) nil
739 (package-tables package) nil
740 (package-%shadowing-symbols package) nil
741 (package-internal-symbols package)
742 (make-or-remake-package-hashtable 0)
743 (package-external-symbols package)
744 (make-or-remake-package-hashtable 0))
745 t))))))
747 (defun list-all-packages ()
748 #!+sb-doc
749 "Return a list of all existing packages."
750 (let ((res ()))
751 (with-packages ()
752 (maphash (lambda (k v)
753 (declare (ignore k))
754 (pushnew v res))
755 *package-names*))
756 res))
758 (defun intern (name &optional (package (sane-package)))
759 #!+sb-doc
760 "Return a symbol in PACKAGE having the specified NAME, creating it
761 if necessary."
762 ;; We just simple-stringify the name and call INTERN*, where the real
763 ;; logic is.
764 (let ((name (if (simple-string-p name)
765 name
766 (coerce name 'simple-string)))
767 (package (find-undeleted-package-or-lose package)))
768 (declare (simple-string name))
769 (intern* name
770 (length name)
771 package)))
773 (defun find-symbol (name &optional (package (sane-package)))
774 #!+sb-doc
775 "Return the symbol named STRING in PACKAGE. If such a symbol is found
776 then the second value is :INTERNAL, :EXTERNAL or :INHERITED to indicate
777 how the symbol is accessible. If no symbol is found then both values
778 are NIL."
779 ;; We just simple-stringify the name and call FIND-SYMBOL*, where the
780 ;; real logic is.
781 (let ((name (if (simple-string-p name) name (coerce name 'simple-string))))
782 (declare (simple-string name))
783 (find-symbol* name
784 (length name)
785 (find-undeleted-package-or-lose package))))
787 ;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
788 ;;; then create it, special-casing the keyword package.
789 (defun intern* (name length package)
790 (declare (simple-string name))
791 (multiple-value-bind (symbol where) (find-symbol* name length package)
792 (cond (where
793 (values symbol where))
795 ;; Let's try again with a lock: the common case has the
796 ;; symbol already interned, handled by the first leg of the
797 ;; COND, but in case another thread is interning in
798 ;; parallel we need to check after grabbing the lock.
799 (with-packages ()
800 (setf (values symbol where) (find-symbol* name length package))
801 (if where
802 (values symbol where)
803 (let ((symbol-name (subseq name 0 length)))
804 (with-single-package-locked-error
805 (:package package "interning ~A" symbol-name)
806 (let ((symbol (make-symbol symbol-name)))
807 (%set-symbol-package symbol package)
808 (cond
809 ((eq package *keyword-package*)
810 (add-symbol (package-external-symbols package) symbol)
811 (%set-symbol-value symbol symbol))
813 (add-symbol (package-internal-symbols package) symbol)))
814 (values symbol nil))))))))))
816 ;;; Check internal and external symbols, then scan down the list
817 ;;; of hashtables for inherited symbols.
818 (defun find-symbol* (string length package)
819 (declare (simple-string string)
820 (type index length))
821 (let* ((hash (%sxhash-simple-substring string length))
822 (ehash (entry-hash length hash)))
823 (declare (type index hash ehash))
824 (with-symbol (found symbol (package-internal-symbols package)
825 string length hash ehash)
826 (when found
827 (return-from find-symbol* (values symbol :internal))))
828 (with-symbol (found symbol (package-external-symbols package)
829 string length hash ehash)
830 (when found
831 (return-from find-symbol* (values symbol :external))))
832 (let ((head (package-tables package)))
833 (do ((prev head table)
834 (table (cdr head) (cdr table)))
835 ((null table) (values nil nil))
836 (with-symbol (found symbol (car table) string length hash ehash)
837 (when found
838 ;; At this point we used to move the table to the
839 ;; beginning of the list, probably on the theory that we'd
840 ;; soon be looking up further items there. Unfortunately
841 ;; that was very much non-thread safe. Since the failure
842 ;; mode was nasty (corruption of the package in a way
843 ;; which would make symbol lookups loop infinitely) and it
844 ;; would be triggered just by doing reads to a resource
845 ;; that users can't do their own locking on, that code has
846 ;; been removed. If we ever add locking to packages,
847 ;; resurrecting that code might make sense, even though it
848 ;; didn't seem to have much of an performance effect in
849 ;; normal use.
851 ;; -- JES, 2006-09-13
852 (return-from find-symbol* (values symbol :inherited))))))))
854 ;;; Similar to FIND-SYMBOL, but only looks for an external symbol.
855 ;;; This is used for fast name-conflict checking in this file and symbol
856 ;;; printing in the printer.
857 (defun find-external-symbol (string package)
858 (declare (simple-string string))
859 (let* ((length (length string))
860 (hash (%sxhash-simple-string string))
861 (ehash (entry-hash length hash)))
862 (declare (type index length hash))
863 (with-symbol (found symbol (package-external-symbols package)
864 string length hash ehash)
865 (values symbol found))))
867 (defun print-symbol-with-prefix (stream symbol colon at)
868 #!+sb-doc
869 "For use with ~/: Write SYMBOL to STREAM as if it is not accessible from
870 the current package."
871 (declare (ignore colon at))
872 ;; Only keywords should be accessible from the keyword package, and
873 ;; keywords are always printed with colons, so this guarantees that the
874 ;; symbol will not be printed without a prefix.
875 (let ((*package* *keyword-package*))
876 (write symbol :stream stream :escape t)))
878 (define-condition name-conflict (reference-condition package-error)
879 ((function :initarg :function :reader name-conflict-function)
880 (datum :initarg :datum :reader name-conflict-datum)
881 (symbols :initarg :symbols :reader name-conflict-symbols))
882 (:default-initargs :references (list '(:ansi-cl :section (11 1 1 2 5))))
883 (:report
884 (lambda (c s)
885 (format s "~@<~S ~S causes name-conflicts in ~S between the ~
886 following symbols:~2I~@:_~
887 ~{~/sb-impl::print-symbol-with-prefix/~^, ~}~:@>"
888 (name-conflict-function c)
889 (name-conflict-datum c)
890 (package-error-package c)
891 (name-conflict-symbols c)))))
893 (defun name-conflict (package function datum &rest symbols)
894 (restart-case
895 (error 'name-conflict :package package :symbols symbols
896 :function function :datum datum)
897 (resolve-conflict (chosen-symbol)
898 :report "Resolve conflict."
899 :interactive
900 (lambda ()
901 (let* ((len (length symbols))
902 (nlen (length (write-to-string len :base 10)))
903 (*print-pretty* t))
904 (format *query-io* "~&~@<Select a symbol to be made accessible in ~
905 package ~A:~2I~@:_~{~{~V,' D. ~
906 ~/sb-impl::print-symbol-with-prefix/~}~@:_~}~
907 ~@:>"
908 (package-name package)
909 (loop for s in symbols
910 for i upfrom 1
911 collect (list nlen i s)))
912 (loop
913 (format *query-io* "~&Enter an integer (between 1 and ~D): " len)
914 (finish-output *query-io*)
915 (let ((i (parse-integer (read-line *query-io*) :junk-allowed t)))
916 (when (and i (<= 1 i len))
917 (return (list (nth (1- i) symbols))))))))
918 (multiple-value-bind (package-symbol status)
919 (find-symbol (symbol-name chosen-symbol) package)
920 (let* ((accessiblep status) ; never NIL here
921 (presentp (and accessiblep
922 (not (eq :inherited status)))))
923 (ecase function
924 ((unintern)
925 (if presentp
926 (if (eq package-symbol chosen-symbol)
927 (shadow (list package-symbol) package)
928 (shadowing-import (list chosen-symbol) package))
929 (shadowing-import (list chosen-symbol) package)))
930 ((use-package export)
931 (if presentp
932 (if (eq package-symbol chosen-symbol)
933 (shadow (list package-symbol) package) ; CLHS 11.1.1.2.5
934 (if (eq (symbol-package package-symbol) package)
935 (unintern package-symbol package) ; CLHS 11.1.1.2.5
936 (shadowing-import (list chosen-symbol) package)))
937 (shadowing-import (list chosen-symbol) package)))
938 ((import)
939 (if presentp
940 (if (eq package-symbol chosen-symbol)
941 nil ; re-importing the same symbol
942 (shadowing-import (list chosen-symbol) package))
943 (shadowing-import (list chosen-symbol) package)))))))))
945 ;;; If we are uninterning a shadowing symbol, then a name conflict can
946 ;;; result, otherwise just nuke the symbol.
947 (defun unintern (symbol &optional (package (sane-package)))
948 #!+sb-doc
949 "Makes SYMBOL no longer present in PACKAGE. If SYMBOL was present then T is
950 returned, otherwise NIL. If PACKAGE is SYMBOL's home package, then it is made
951 uninterned."
952 (with-packages ()
953 (let* ((package (find-undeleted-package-or-lose package))
954 (name (symbol-name symbol))
955 (shadowing-symbols (package-%shadowing-symbols package)))
956 (declare (list shadowing-symbols))
958 (with-single-package-locked-error ()
959 (when (find-symbol name package)
960 (assert-package-unlocked package "uninterning ~A" name))
962 ;; If a name conflict is revealed, give us a chance to
963 ;; shadowing-import one of the accessible symbols.
964 (when (member symbol shadowing-symbols)
965 (let ((cset ()))
966 (dolist (p (package-%use-list package))
967 (multiple-value-bind (s w) (find-external-symbol name p)
968 (when w (pushnew s cset))))
969 (when (cdr cset)
970 (apply #'name-conflict package 'unintern symbol cset)
971 (return-from unintern t)))
972 (setf (package-%shadowing-symbols package)
973 (remove symbol shadowing-symbols)))
975 (multiple-value-bind (s w) (find-symbol name package)
976 (declare (ignore s))
977 (cond ((or (eq w :internal) (eq w :external))
978 (nuke-symbol (if (eq w :internal)
979 (package-internal-symbols package)
980 (package-external-symbols package))
981 name)
982 (if (eq (symbol-package symbol) package)
983 (%set-symbol-package symbol nil))
985 (t nil)))))))
987 ;;; Take a symbol-or-list-of-symbols and return a list, checking types.
988 (defun symbol-listify (thing)
989 (cond ((listp thing)
990 (dolist (s thing)
991 (unless (symbolp s) (error "~S is not a symbol." s)))
992 thing)
993 ((symbolp thing) (list thing))
995 (error "~S is neither a symbol nor a list of symbols." thing))))
997 (defun string-listify (thing)
998 (mapcar #'string (if (listp thing)
999 thing
1000 (list thing))))
1002 ;;; This is like UNINTERN, except if SYMBOL is inherited, it chases
1003 ;;; down the package it is inherited from and uninterns it there. Used
1004 ;;; for name-conflict resolution. Shadowing symbols are not uninterned
1005 ;;; since they do not cause conflicts.
1006 (defun moby-unintern (symbol package)
1007 (unless (member symbol (package-%shadowing-symbols package))
1008 (or (unintern symbol package)
1009 (let ((name (symbol-name symbol)))
1010 (multiple-value-bind (s w) (find-symbol name package)
1011 (declare (ignore s))
1012 (when (eq w :inherited)
1013 (dolist (q (package-%use-list package))
1014 (multiple-value-bind (u x) (find-external-symbol name q)
1015 (declare (ignore u))
1016 (when x
1017 (unintern symbol q)
1018 (return t))))))))))
1020 (defun export (symbols &optional (package (sane-package)))
1021 #!+sb-doc
1022 "Exports SYMBOLS from PACKAGE, checking that no name conflicts result."
1023 (with-packages ()
1024 (let ((package (find-undeleted-package-or-lose package))
1025 (syms ()))
1026 ;; Punt any symbols that are already external.
1027 (dolist (sym (symbol-listify symbols))
1028 (multiple-value-bind (s w)
1029 (find-external-symbol (symbol-name sym) package)
1030 (declare (ignore s))
1031 (unless (or w (member sym syms))
1032 (push sym syms))))
1033 (with-single-package-locked-error ()
1034 (when syms
1035 (assert-package-unlocked package "exporting symbol~P ~{~A~^, ~}"
1036 (length syms) syms))
1037 ;; Find symbols and packages with conflicts.
1038 (let ((used-by (package-%used-by-list package)))
1039 (dolist (sym syms)
1040 (let ((name (symbol-name sym)))
1041 (dolist (p used-by)
1042 (multiple-value-bind (s w) (find-symbol name p)
1043 (when (and w
1044 (not (eq s sym))
1045 (not (member s (package-%shadowing-symbols p))))
1046 ;; Beware: the name conflict is in package P, not in
1047 ;; PACKAGE.
1048 (name-conflict p 'export sym sym s)))))))
1049 ;; Check that all symbols are accessible. If not, ask to import them.
1050 (let ((missing ())
1051 (imports ()))
1052 (dolist (sym syms)
1053 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1054 (cond ((not (and w (eq s sym)))
1055 (push sym missing))
1056 ((eq w :inherited)
1057 (push sym imports)))))
1058 (when missing
1059 (cerror
1060 "~S these symbols into the ~A package."
1061 (make-condition
1062 'simple-package-error
1063 :package package
1064 :format-control
1065 "~@<These symbols are not accessible in the ~A package:~2I~_~S~@:>"
1066 :format-arguments (list (package-%name package) missing))
1067 'import (package-%name package))
1068 (import missing package))
1069 (import imports package))
1071 ;; And now, three pages later, we export the suckers.
1072 (let ((internal (package-internal-symbols package))
1073 (external (package-external-symbols package)))
1074 (dolist (sym syms)
1075 (nuke-symbol internal (symbol-name sym))
1076 (add-symbol external sym))))
1077 t)))
1079 ;;; Check that all symbols are accessible, then move from external to internal.
1080 (defun unexport (symbols &optional (package (sane-package)))
1081 #!+sb-doc
1082 "Makes SYMBOLS no longer exported from PACKAGE."
1083 (with-packages ()
1084 (let ((package (find-undeleted-package-or-lose package))
1085 (syms ()))
1086 (dolist (sym (symbol-listify symbols))
1087 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1088 (cond ((or (not w) (not (eq s sym)))
1089 (error 'simple-package-error
1090 :package package
1091 :format-control "~S is not accessible in the ~A package."
1092 :format-arguments (list sym (package-%name package))))
1093 ((eq w :external) (pushnew sym syms)))))
1094 (with-single-package-locked-error ()
1095 (when syms
1096 (assert-package-unlocked package "unexporting symbol~P ~{~A~^, ~}"
1097 (length syms) syms))
1098 (let ((internal (package-internal-symbols package))
1099 (external (package-external-symbols package)))
1100 (dolist (sym syms)
1101 (add-symbol internal sym)
1102 (nuke-symbol external (symbol-name sym)))))
1103 t)))
1105 ;;; Check for name conflict caused by the import and let the user
1106 ;;; shadowing-import if there is.
1107 (defun import (symbols &optional (package (sane-package)))
1108 #!+sb-doc
1109 "Make SYMBOLS accessible as internal symbols in PACKAGE. If a symbol is
1110 already accessible then it has no effect. If a name conflict would result from
1111 the importation, then a correctable error is signalled."
1112 (with-packages ()
1113 (let* ((package (find-undeleted-package-or-lose package))
1114 (symbols (symbol-listify symbols))
1115 (homeless (remove-if #'symbol-package symbols))
1116 (syms ()))
1117 (with-single-package-locked-error ()
1118 (dolist (sym symbols)
1119 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1120 (cond ((not w)
1121 (let ((found (member sym syms :test #'string=)))
1122 (if found
1123 (when (not (eq (car found) sym))
1124 (name-conflict package 'import sym sym (car found)))
1125 (push sym syms))))
1126 ((not (eq s sym))
1127 (name-conflict package 'import sym sym s))
1128 ((eq w :inherited) (push sym syms)))))
1129 (when (or homeless syms)
1130 (let ((union (delete-duplicates (append homeless syms))))
1131 (assert-package-unlocked package "importing symbol~P ~{~A~^, ~}"
1132 (length union) union)))
1133 ;; Add the new symbols to the internal hashtable.
1134 (let ((internal (package-internal-symbols package)))
1135 (dolist (sym syms)
1136 (add-symbol internal sym)))
1137 ;; If any of the symbols are uninterned, make them be owned by PACKAGE.
1138 (dolist (sym homeless)
1139 (%set-symbol-package sym package))
1140 t))))
1142 ;;; If a conflicting symbol is present, unintern it, otherwise just
1143 ;;; stick the symbol in.
1144 (defun shadowing-import (symbols &optional (package (sane-package)))
1145 #!+sb-doc
1146 "Import SYMBOLS into package, disregarding any name conflict. If
1147 a symbol of the same name is present, then it is uninterned."
1148 (with-packages ()
1149 (let* ((package (find-undeleted-package-or-lose package))
1150 (internal (package-internal-symbols package))
1151 (symbols (symbol-listify symbols))
1152 (lock-asserted-p nil))
1153 (with-single-package-locked-error ()
1154 (dolist (sym symbols)
1155 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
1156 (unless (or lock-asserted-p
1157 (and (eq s sym)
1158 (member s (package-shadowing-symbols package))))
1159 (assert-package-unlocked package "shadowing-importing symbol~P ~
1160 ~{~A~^, ~}" (length symbols) symbols)
1161 (setf lock-asserted-p t))
1162 (unless (and w (not (eq w :inherited)) (eq s sym))
1163 (when (or (eq w :internal) (eq w :external))
1164 ;; If it was shadowed, we don't want UNINTERN to flame out...
1165 (setf (package-%shadowing-symbols package)
1166 (remove s (the list (package-%shadowing-symbols package))))
1167 (unintern s package))
1168 (add-symbol internal sym))
1169 (pushnew sym (package-%shadowing-symbols package)))))))
1172 (defun shadow (symbols &optional (package (sane-package)))
1173 #!+sb-doc
1174 "Make an internal symbol in PACKAGE with the same name as each of the
1175 specified SYMBOLS. If a symbol with the given name is already present in
1176 PACKAGE, then the existing symbol is placed in the shadowing symbols list if
1177 it is not already present."
1178 (with-packages ()
1179 (let* ((package (find-undeleted-package-or-lose package))
1180 (internal (package-internal-symbols package))
1181 (symbols (string-listify symbols))
1182 (lock-asserted-p nil))
1183 (flet ((present-p (w)
1184 (and w (not (eq w :inherited)))))
1185 (with-single-package-locked-error ()
1186 (dolist (name symbols)
1187 (multiple-value-bind (s w) (find-symbol name package)
1188 (unless (or lock-asserted-p
1189 (and (present-p w)
1190 (member s (package-shadowing-symbols package))))
1191 (assert-package-unlocked package "shadowing symbol~P ~{~A~^, ~}"
1192 (length symbols) symbols)
1193 (setf lock-asserted-p t))
1194 (unless (present-p w)
1195 (setq s (make-symbol name))
1196 (%set-symbol-package s package)
1197 (add-symbol internal s))
1198 (pushnew s (package-%shadowing-symbols package))))))))
1201 ;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
1202 (defun use-package (packages-to-use &optional (package (sane-package)))
1203 #!+sb-doc
1204 "Add all the PACKAGES-TO-USE to the use list for PACKAGE so that the
1205 external symbols of the used packages are accessible as internal symbols in
1206 PACKAGE."
1207 (with-packages ()
1208 (let ((packages (package-listify packages-to-use))
1209 (package (find-undeleted-package-or-lose package)))
1211 ;; Loop over each package, USE'ing one at a time...
1212 (with-single-package-locked-error ()
1213 (dolist (pkg packages)
1214 (unless (member pkg (package-%use-list package))
1215 (assert-package-unlocked package "using package~P ~{~A~^, ~}"
1216 (length packages) packages)
1217 (let ((shadowing-symbols (package-%shadowing-symbols package))
1218 (use-list (package-%use-list package)))
1220 ;; If the number of symbols already accessible is less
1221 ;; than the number to be inherited then it is faster to
1222 ;; run the test the other way. This is particularly
1223 ;; valuable in the case of a new package USEing
1224 ;; COMMON-LISP.
1225 (cond
1226 ((< (+ (package-internal-symbol-count package)
1227 (package-external-symbol-count package)
1228 (let ((res 0))
1229 (dolist (p use-list res)
1230 (incf res (package-external-symbol-count p)))))
1231 (package-external-symbol-count pkg))
1232 (do-symbols (sym package)
1233 (multiple-value-bind (s w)
1234 (find-external-symbol (symbol-name sym) pkg)
1235 (when (and w
1236 (not (eq s sym))
1237 (not (member sym shadowing-symbols)))
1238 (name-conflict package 'use-package pkg sym s))))
1239 (dolist (p use-list)
1240 (do-external-symbols (sym p)
1241 (multiple-value-bind (s w)
1242 (find-external-symbol (symbol-name sym) pkg)
1243 (when (and w
1244 (not (eq s sym))
1245 (not (member
1246 (find-symbol (symbol-name sym) package)
1247 shadowing-symbols)))
1248 (name-conflict package 'use-package pkg sym s))))))
1250 (do-external-symbols (sym pkg)
1251 (multiple-value-bind (s w)
1252 (find-symbol (symbol-name sym) package)
1253 (when (and w
1254 (not (eq s sym))
1255 (not (member s shadowing-symbols)))
1256 (name-conflict package 'use-package pkg sym s)))))))
1258 (push pkg (package-%use-list package))
1259 (push (package-external-symbols pkg) (cdr (package-tables package)))
1260 (push package (package-%used-by-list pkg)))))))
1263 (defun unuse-package (packages-to-unuse &optional (package (sane-package)))
1264 #!+sb-doc
1265 "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
1266 (with-packages ()
1267 (let ((package (find-undeleted-package-or-lose package))
1268 (packages (package-listify packages-to-unuse)))
1269 (with-single-package-locked-error ()
1270 (dolist (p packages)
1271 (when (member p (package-use-list package))
1272 (assert-package-unlocked package "unusing package~P ~{~A~^, ~}"
1273 (length packages) packages))
1274 (setf (package-%use-list package)
1275 (remove p (the list (package-%use-list package))))
1276 (setf (package-tables package)
1277 (delete (package-external-symbols p)
1278 (the list (package-tables package))))
1279 (setf (package-%used-by-list p)
1280 (remove package (the list (package-%used-by-list p))))))
1281 t)))
1283 (defun find-all-symbols (string-or-symbol)
1284 #!+sb-doc
1285 "Return a list of all symbols in the system having the specified name."
1286 (let ((string (string string-or-symbol))
1287 (res ()))
1288 (with-packages ()
1289 (maphash (lambda (k v)
1290 (declare (ignore k))
1291 (multiple-value-bind (s w) (find-symbol string v)
1292 (when w (pushnew s res))))
1293 *package-names*))
1294 res))
1296 ;;;; APROPOS and APROPOS-LIST
1298 (defun briefly-describe-symbol (symbol)
1299 (fresh-line)
1300 (prin1 symbol)
1301 (when (boundp symbol)
1302 (write-string " (bound)"))
1303 (when (fboundp symbol)
1304 (write-string " (fbound)")))
1306 (defun apropos-list (string-designator
1307 &optional
1308 package-designator
1309 external-only)
1310 #!+sb-doc
1311 "Like APROPOS, except that it returns a list of the symbols found instead
1312 of describing them."
1313 (if package-designator
1314 (let ((package (find-undeleted-package-or-lose package-designator))
1315 (string (stringify-string-designator string-designator))
1316 (result nil))
1317 (do-symbols (symbol package)
1318 (when (and (eq (symbol-package symbol) package)
1319 (or (not external-only)
1320 (eq (nth-value 1 (find-symbol (symbol-name symbol)
1321 package))
1322 :external))
1323 (search string (symbol-name symbol) :test #'char-equal))
1324 (push symbol result)))
1325 (sort result #'string-lessp))
1326 (mapcan (lambda (package)
1327 (apropos-list string-designator package external-only))
1328 (sort (list-all-packages) #'string-lessp :key #'package-name))))
1330 (defun apropos (string-designator &optional package external-only)
1331 #!+sb-doc
1332 "Briefly describe all symbols which contain the specified STRING.
1333 If PACKAGE is supplied then only describe symbols present in
1334 that package. If EXTERNAL-ONLY then only describe
1335 external symbols in the specified package."
1336 ;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
1337 ;; of some unnecessary consing; and the unnecessary consing shouldn't be an
1338 ;; issue, since this function is is only useful interactively anyway, and
1339 ;; we can cons and GC a lot faster than the typical user can read..
1340 (dolist (symbol (apropos-list string-designator package external-only))
1341 (briefly-describe-symbol symbol))
1342 (values))
1344 ;;;; final initialization
1346 ;;;; The cold loader (GENESIS) makes the data structure in
1347 ;;;; *!INITIAL-SYMBOLS*. We grovel over it, making the specified
1348 ;;;; packages and interning the symbols. For a description of the
1349 ;;;; format of *!INITIAL-SYMBOLS*, see the GENESIS source.
1351 (defvar *!initial-symbols*)
1353 (!cold-init-forms
1355 (setq *in-package-init* t)
1357 (/show0 "about to loop over *!INITIAL-SYMBOLS* to make packages")
1358 (dolist (spec *!initial-symbols*)
1359 (let* ((pkg (apply #'make-package (first spec)))
1360 (internal (package-internal-symbols pkg))
1361 (external (package-external-symbols pkg)))
1362 (/show0 "back from MAKE-PACKAGE, PACKAGE-NAME=..")
1363 (/primitive-print (package-name pkg))
1365 ;; Put internal symbols in the internal hashtable and set package.
1366 (dolist (symbol (second spec))
1367 (add-symbol internal symbol)
1368 (%set-symbol-package symbol pkg))
1370 ;; External symbols same, only go in external table.
1371 (dolist (symbol (third spec))
1372 (add-symbol external symbol)
1373 (%set-symbol-package symbol pkg))
1375 ;; Don't set package for imported symbols.
1376 (dolist (symbol (fourth spec))
1377 (add-symbol internal symbol))
1378 (dolist (symbol (fifth spec))
1379 (add-symbol external symbol))
1381 ;; Put shadowing symbols in the shadowing symbols list.
1382 (setf (package-%shadowing-symbols pkg) (sixth spec))
1384 ;; Set the package documentation
1385 (setf (package-doc-string pkg) (seventh spec))))
1387 ;; FIXME: These assignments are also done at toplevel in
1388 ;; boot-extensions.lisp. They should probably only be done once.
1389 (/show0 "setting up *CL-PACKAGE* and *KEYWORD-PACKAGE*")
1390 (setq *cl-package* (find-package "COMMON-LISP"))
1391 (setq *keyword-package* (find-package "KEYWORD"))
1393 (/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*")
1394 (%makunbound '*!initial-symbols*) ; (so that it gets GCed)
1396 ;; Make some other packages that should be around in the cold load.
1397 ;; The COMMON-LISP-USER package is required by the ANSI standard,
1398 ;; but not completely specified by it, so in the cross-compilation
1399 ;; host Lisp it could contain various symbols, USE-PACKAGEs, or
1400 ;; nicknames that we don't want in our target SBCL. For that reason,
1401 ;; we handle it specially, not dumping the host Lisp version at
1402 ;; genesis time..
1403 (aver (not (find-package "COMMON-LISP-USER")))
1404 ;; ..but instead making our own from scratch here.
1405 (/show0 "about to MAKE-PACKAGE COMMON-LISP-USER")
1406 (make-package "COMMON-LISP-USER"
1407 :nicknames '("CL-USER")
1408 :use '("COMMON-LISP"
1409 ;; ANSI encourages us to put extension packages
1410 ;; in the USE list of COMMON-LISP-USER.
1411 "SB!ALIEN" "SB!ALIEN" "SB!DEBUG"
1412 "SB!EXT" "SB!GRAY" "SB!PROFILE"))
1414 ;; Now do the *!DEFERRED-USE-PACKAGES*.
1415 (/show0 "about to do *!DEFERRED-USE-PACKAGES*")
1416 (dolist (args *!deferred-use-packages*)
1417 (apply #'use-package args))
1419 ;; The Age Of Magic is over, we can behave ANSIly henceforth.
1420 (/show0 "about to SETQ *IN-PACKAGE-INIT*")
1421 (setq *in-package-init* nil)
1423 ;; For the kernel core image wizards, set the package to *CL-PACKAGE*.
1425 ;; FIXME: We should just set this to (FIND-PACKAGE
1426 ;; "COMMON-LISP-USER") once and for all here, instead of setting it
1427 ;; once here and resetting it later.
1428 (setq *package* *cl-package*))
1430 (!cold-init-forms
1431 (/show0 "done with !PACKAGE-COLD-INIT"))
1433 (!defun-from-collected-cold-init-forms !package-cold-init)