1 ;;;; PACKAGEs and stuff like that
3 ;;;; Note: The code in this file signals many correctable errors. This
4 ;;;; is not just an arbitrary aesthetic decision on the part of the
5 ;;;; implementor -- many of these are specified by ANSI 11.1.1.2.5,
6 ;;;; "Prevention of Name Conflicts in Packages":
7 ;;;; Within one package, any particular name can refer to at most one
8 ;;;; symbol. A name conflict is said to occur when there would be more
9 ;;;; than one candidate symbol. Any time a name conflict is about to
10 ;;;; occur, a correctable error is signaled.
12 ;;;; FIXME: The code contains a lot of type declarations. Are they
13 ;;;; all really necessary?
15 ;;;; This software is part of the SBCL system. See the README file for
16 ;;;; more information.
18 ;;;; This software is derived from the CMU CL system, which was
19 ;;;; written at Carnegie Mellon University and released into the
20 ;;;; public domain. The software is in the public domain and is
21 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
22 ;;;; files for more information.
24 (in-package "SB!IMPL")
26 (!begin-collecting-cold-init-forms)
29 (/show0 "entering !PACKAGE-COLD-INIT"))
31 ;;;; PACKAGE-HASHTABLE stuff
33 (def!method print-object ((table package-hashtable) stream)
34 (declare (type stream stream))
35 (print-unreadable-object (table stream :type t)
37 ":SIZE ~S :FREE ~S :DELETED ~S"
38 (package-hashtable-size table)
39 (package-hashtable-free table)
40 (package-hashtable-deleted table))))
42 ;;; the maximum density we allow in a package hashtable
43 (defconstant package-rehash-threshold 0.75)
45 ;;; Make a package hashtable having a prime number of entries at least
46 ;;; as great as (/ SIZE PACKAGE-REHASH-THRESHOLD). If RES is supplied,
47 ;;; then it is destructively modified to produce the result. This is
48 ;;; useful when changing the size, since there are many pointers to
50 (defun make-or-remake-package-hashtable (size
53 (flet ((actual-package-hashtable-size (size)
54 (loop for n of-type fixnum
55 from (logior (truncate size package-rehash-threshold) 1)
57 when (positive-primep n) return n)))
58 (let* ((n (actual-package-hashtable-size size))
59 (size (truncate (* n package-rehash-threshold)))
60 (table (make-array n))
62 :element-type '(unsigned-byte 8)
65 (setf (package-hashtable-table res) table
66 (package-hashtable-hash res) hash
67 (package-hashtable-size res) size
68 (package-hashtable-free res) size
69 (package-hashtable-deleted res) 0)
70 (setf res (%make-package-hashtable table hash size)))
73 ;;;; miscellaneous PACKAGE operations
75 (def!method print-object ((package package) stream)
76 (let ((name (package-%name package)))
78 (print-unreadable-object (package stream :type t)
80 (print-unreadable-object (package stream :type t :identity t)
81 (write-string "(deleted)" stream)))))
83 ;;; ANSI says (in the definition of DELETE-PACKAGE) that these, and
84 ;;; most other operations, are unspecified for deleted packages. We
85 ;;; just do the easy thing and signal errors in that case.
86 (macrolet ((def (ext real)
87 `(defun ,ext (x) (,real (find-undeleted-package-or-lose x)))))
88 (def package-nicknames package-%nicknames)
89 (def package-use-list package-%use-list)
90 (def package-used-by-list package-%used-by-list)
91 (def package-shadowing-symbols package-%shadowing-symbols))
93 (defun %package-hashtable-symbol-count (table)
94 (let ((size (the fixnum
95 (- (package-hashtable-size table)
96 (package-hashtable-deleted table)))))
98 (- size (package-hashtable-free table)))))
100 (defun package-internal-symbol-count (package)
101 (%package-hashtable-symbol-count (package-internal-symbols package)))
103 (defun package-external-symbol-count (package)
104 (%package-hashtable-symbol-count (package-external-symbols package)))
106 (defvar *package* (error "*PACKAGE* should be initialized in cold load!")
107 #!+sb-doc "the current package")
108 ;;; FIXME: should be declared of type PACKAGE, with no NIL init form,
109 ;;; after I get around to cleaning up DOCUMENTATION
111 ;;; a map from package names to packages
112 (defvar *package-names*)
113 (declaim (type hash-table *package-names*))
115 (setf *package-names* (make-hash-table :test 'equal)))
117 ;;; This magical variable is T during initialization so that
118 ;;; USE-PACKAGE's of packages that don't yet exist quietly win. Such
119 ;;; packages are thrown onto the list *DEFERRED-USE-PACKAGES* so that
120 ;;; this can be fixed up later.
122 ;;; FIXME: This could be cleaned up the same way I do it in my package
123 ;;; hacking when setting up the cross-compiler. Then we wouldn't have
124 ;;; this extraneous global variable and annoying runtime tests on
125 ;;; package operations. (*DEFERRED-USE-PACKAGES* would also go away.)
126 (defvar *in-package-init*)
128 ;;; pending USE-PACKAGE arguments saved up while *IN-PACKAGE-INIT* is true
129 (defvar *!deferred-use-packages*)
131 (setf *!deferred-use-packages* nil))
133 ;;; FIXME: I rewrote this. Test it and the stuff that calls it.
134 (defun find-package (package-designator)
135 (flet ((find-package-from-string (string)
136 (declare (type string string))
137 (values (gethash string *package-names*))))
138 (declare (inline find-package-from-string))
139 (typecase package-designator
140 (package package-designator)
141 (symbol (find-package-from-string (symbol-name package-designator)))
142 (string (find-package-from-string package-designator))
143 (character (find-package-from-string (string package-designator)))
144 (t (error 'type-error
145 :datum package-designator
146 :expected-type '(or character package string symbol))))))
148 ;;; Return a list of packages given a package designator or list of
149 ;;; package designators, or die trying.
150 (defun package-listify (thing)
152 (dolist (thing (if (listp thing) thing (list thing)) res)
153 (push (find-undeleted-package-or-lose thing) res))))
155 ;;; Make a package name into a simple-string.
156 (defun package-namify (n)
157 (stringify-name n "package"))
159 ;;; ANSI specifies (in the definition of DELETE-PACKAGE) that PACKAGE-NAME
160 ;;; returns NIL (not an error) for a deleted package, so this is a special
161 ;;; case where we want to use bare %FIND-PACKAGE-OR-LOSE instead of
162 ;;; FIND-UNDELETED-PACKAGE-OR-LOSE.
163 (defun package-name (package-designator)
164 (package-%name (%find-package-or-lose package-designator)))
166 ;;;; operations on package hashtables
168 ;;; Compute a number from the sxhash of the pname and the length which
169 ;;; must be between 2 and 255.
170 (defmacro entry-hash (length sxhash)
176 (the fixnum (ash ,sxhash -8))
177 (the fixnum (ash ,sxhash -16))
178 (the fixnum (ash ,sxhash -19))))
181 ;;; FIXME: should be wrapped in EVAL-WHEN (COMPILE EXECUTE)
183 ;;; Add a symbol to a package hashtable. The symbol is assumed
184 ;;; not to be present.
185 (defun add-symbol (table symbol)
186 (let* ((vec (package-hashtable-table table))
187 (hash (package-hashtable-hash table))
189 (sxhash (%sxhash-simple-string (symbol-name symbol)))
190 (h2 (the fixnum (1+ (the fixnum (rem sxhash
191 (the fixnum (- len 2))))))))
192 (declare (fixnum len sxhash h2))
193 (cond ((zerop (the fixnum (package-hashtable-free table)))
194 (make-or-remake-package-hashtable (* (package-hashtable-size table)
197 (add-symbol table symbol)
200 (when (> (the fixnum (aref hash i)) 1)
201 (add-symbol table (svref vec i)))))
203 (do ((i (rem sxhash len) (rem (+ i h2) len)))
204 ((< (the fixnum (aref hash i)) 2)
205 (if (zerop (the fixnum (aref hash i)))
206 (decf (package-hashtable-free table))
207 (decf (package-hashtable-deleted table)))
208 (setf (svref vec i) symbol)
210 (entry-hash (length (symbol-name symbol))
212 (declare (fixnum i)))))))
214 ;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR
215 ;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR
216 ;;; is bound to the symbol. LENGTH and HASH are the length and sxhash
217 ;;; of STRING. ENTRY-HASH is the entry-hash of the string and length.
218 (defmacro with-symbol ((index-var symbol-var table string length sxhash
221 (let ((vec (gensym)) (hash (gensym)) (len (gensym)) (h2 (gensym))
222 (name (gensym)) (name-len (gensym)) (ehash (gensym)))
223 `(let* ((,vec (package-hashtable-table ,table))
224 (,hash (package-hashtable-hash ,table))
226 (,h2 (1+ (the index (rem (the index ,sxhash)
227 (the index (- ,len 2)))))))
228 (declare (type index ,len ,h2))
229 (prog ((,index-var (rem (the index ,sxhash) ,len))
231 (declare (type (or index null) ,index-var))
233 (setq ,ehash (aref ,hash ,index-var))
234 (cond ((eql ,ehash ,entry-hash)
235 (setq ,symbol-var (svref ,vec ,index-var))
236 (let* ((,name (symbol-name ,symbol-var))
237 (,name-len (length ,name)))
238 (declare (type index ,name-len))
239 (when (and (= ,name-len ,length)
240 (string= ,string ,name
245 (setq ,index-var nil)
247 (setq ,index-var (+ ,index-var ,h2))
248 (when (>= ,index-var ,len)
249 (setq ,index-var (- ,index-var ,len)))
252 (return (progn ,@forms))))))
254 ;;; Delete the entry for STRING in TABLE. The entry must exist.
255 (defun nuke-symbol (table string)
256 (declare (simple-string string))
257 (let* ((length (length string))
258 (hash (%sxhash-simple-string string))
259 (ehash (entry-hash length hash)))
260 (declare (type index length hash))
261 (with-symbol (index symbol table string length hash ehash)
262 (setf (aref (package-hashtable-hash table) index) 1)
263 (setf (aref (package-hashtable-table table) index) nil)
264 (incf (package-hashtable-deleted table)))))
266 ;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*.
267 ;;; If there is a conflict then give the user a chance to do
268 ;;; something about it.
269 (defun enter-new-nicknames (package nicknames)
270 (declare (type list nicknames))
271 (dolist (n nicknames)
272 (let* ((n (package-namify n))
273 (found (gethash n *package-names*)))
275 (setf (gethash n *package-names*) package)
276 (push n (package-%nicknames package)))
278 ((string= (the string (package-%name found)) n)
279 ;; FIXME: This and the next error needn't have restarts.
280 (with-simple-restart (continue "Ignore this nickname.")
281 (error 'simple-package-error
283 :format-control "~S is a package name, so it cannot be a nickname for ~S."
284 :format-arguments (list n (package-%name package)))))
286 (with-simple-restart (continue "Redefine this nickname.")
287 (error 'simple-package-error
289 :format-control "~S is already a nickname for ~S."
290 :format-arguments (list n (package-%name found))))
291 (setf (gethash n *package-names*) package)
292 (push n (package-%nicknames package)))))))
294 (defun make-package (name &key
295 (use '#.*default-package-use-list*)
297 (internal-symbols 10)
298 (external-symbols 10))
301 "Make a new package having the specified NAME, NICKNAMES, and
302 USE list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are
303 estimates for the number of internal and external symbols which
304 will ultimately be present in the package. The default value of
305 USE is implementation-dependent, and in this implementation
307 *default-package-use-list*)
309 ;; Check for package name conflicts in name and nicknames, then
311 (when (find-package name)
312 ;; ANSI specifies that this error is correctable.
313 (cerror "Leave existing package alone."
314 "A package named ~S already exists" name))
315 (let* ((name (package-namify name))
316 (package (internal-make-package
318 :internal-symbols (make-or-remake-package-hashtable
320 :external-symbols (make-or-remake-package-hashtable
323 ;; Do a USE-PACKAGE for each thing in the USE list so that checking for
324 ;; conflicting exports among used packages is done.
325 (if *in-package-init*
326 (push (list use package) *!deferred-use-packages*)
327 (use-package use package))
329 ;; FIXME: ENTER-NEW-NICKNAMES can fail (ERROR) if nicknames are illegal,
330 ;; which would leave us with possibly-bad side effects from the earlier
331 ;; USE-PACKAGE (e.g. this package on the used-by lists of other packages,
332 ;; but not in *PACKAGE-NAMES*, and possibly import side effects too?).
333 ;; Perhaps this can be solved by just moving ENTER-NEW-NICKNAMES before
334 ;; USE-PACKAGE, but I need to check what kinds of errors can be caused by
336 (enter-new-nicknames package nicknames)
337 (setf (gethash name *package-names*) package)))
339 ;;; Change the name if we can, blast any old nicknames and then
340 ;;; add in any new ones.
342 ;;; FIXME: ANSI claims that NAME is a package designator (not just a
343 ;;; string designator -- weird). Thus, NAME could
344 ;;; be a package instead of a string. Presumably then we should not change
345 ;;; the package name if NAME is the same package that's referred to by PACKAGE.
346 ;;; If it's a *different* package, we should probably signal an error.
347 ;;; (perhaps (ERROR 'ANSI-WEIRDNESS ..):-)
348 (defun rename-package (package name &optional (nicknames ()))
350 "Changes the name and nicknames for a package."
351 (let* ((package (find-undeleted-package-or-lose package))
353 (found (find-package name)))
354 (unless (or (not found) (eq found package))
355 (error 'simple-package-error
357 :format-control "A package named ~S already exists."
358 :format-arguments (list name)))
359 (remhash (package-%name package) *package-names*)
360 (dolist (n (package-%nicknames package))
361 (remhash n *package-names*))
362 (setf (package-%name package) name)
363 (setf (gethash name *package-names*) package)
364 (setf (package-%nicknames package) ())
365 (enter-new-nicknames package nicknames)
368 (defun delete-package (package-or-name)
370 "Delete the package-or-name from the package system data structures."
371 (let ((package (if (packagep package-or-name)
373 (find-package package-or-name))))
375 ;; This continuable error is required by ANSI.
376 (with-simple-restart (continue "Return NIL")
377 (error 'simple-package-error
378 :package package-or-name
379 :format-control "There is no package named ~S."
380 :format-arguments (list package-or-name))))
381 ((not (package-name package)) ; already deleted
384 (let ((use-list (package-used-by-list package)))
386 ;; This continuable error is specified by ANSI.
388 (continue "Remove dependency in other packages.")
389 (error 'simple-package-error
392 "Package ~S is used by package(s):~% ~S"
394 (list (package-name package)
395 (mapcar #'package-name use-list))))
397 (unuse-package package p))))
398 (dolist (used (package-use-list package))
399 (unuse-package used package))
400 (do-symbols (sym package)
401 (unintern sym package))
402 (remhash (package-name package) *package-names*)
403 (dolist (nick (package-nicknames package))
404 (remhash nick *package-names*))
405 (setf (package-%name package) nil
406 ;; Setting PACKAGE-%NAME to NIL is required in order to
407 ;; make PACKAGE-NAME return NIL for a deleted package as
408 ;; ANSI requires. Setting the other slots to NIL
409 ;; and blowing away the PACKAGE-HASHTABLES is just done
410 ;; for tidiness and to help the GC.
411 (package-%nicknames package) nil
412 (package-%use-list package) nil
413 (package-tables package) nil
414 (package-%shadowing-symbols package) nil
415 (package-internal-symbols package)
416 (make-or-remake-package-hashtable 0)
417 (package-external-symbols package)
418 (make-or-remake-package-hashtable 0))
421 (defun list-all-packages ()
423 "Return a list of all existing packages."
425 (maphash (lambda (k v)
431 (defun intern (name &optional (package (sane-package)))
433 "Return a symbol having the specified name, creating it if necessary."
434 ;; We just simple-stringify the name and call INTERN*, where the real
436 (let ((name (if (simple-string-p name)
438 (coerce name 'simple-string))))
439 (declare (simple-string name))
442 (find-undeleted-package-or-lose package))))
444 (defun find-symbol (name &optional (package (sane-package)))
446 "Return the symbol named String in Package. If such a symbol is found
447 then the second value is :internal, :external or :inherited to indicate
448 how the symbol is accessible. If no symbol is found then both values
450 ;; We just simple-stringify the name and call FIND-SYMBOL*, where the
452 (let ((name (if (simple-string-p name) name (coerce name 'simple-string))))
453 (declare (simple-string name))
456 (find-undeleted-package-or-lose package))))
458 ;;; If the symbol named by the first LENGTH characters of NAME doesn't exist,
459 ;;; then create it, special-casing the keyword package.
460 (defun intern* (name length package)
461 (declare (simple-string name))
462 (multiple-value-bind (symbol where) (find-symbol* name length package)
464 (values symbol where)
465 (let ((symbol (make-symbol (subseq name 0 length))))
466 (%set-symbol-package symbol package)
467 (cond ((eq package *keyword-package*)
468 (add-symbol (package-external-symbols package) symbol)
469 (%set-symbol-value symbol symbol))
471 (add-symbol (package-internal-symbols package) symbol)))
472 (values symbol nil)))))
474 ;;; Check internal and external symbols, then scan down the list
475 ;;; of hashtables for inherited symbols. When an inherited symbol
476 ;;; is found pull that table to the beginning of the list.
477 (defun find-symbol* (string length package)
478 (declare (simple-string string)
480 (let* ((hash (%sxhash-simple-substring string length))
481 (ehash (entry-hash length hash)))
482 (declare (type index hash ehash))
483 (with-symbol (found symbol (package-internal-symbols package)
484 string length hash ehash)
486 (return-from find-symbol* (values symbol :internal))))
487 (with-symbol (found symbol (package-external-symbols package)
488 string length hash ehash)
490 (return-from find-symbol* (values symbol :external))))
491 (let ((head (package-tables package)))
492 (do ((prev head table)
493 (table (cdr head) (cdr table)))
494 ((null table) (values nil nil))
495 (with-symbol (found symbol (car table) string length hash ehash)
497 (unless (eq prev head)
498 (shiftf (cdr prev) (cdr table) (cdr head) table))
499 (return-from find-symbol* (values symbol :inherited))))))))
501 ;;; Similar to Find-Symbol, but only looks for an external symbol.
502 ;;; This is used for fast name-conflict checking in this file and symbol
503 ;;; printing in the printer.
504 (defun find-external-symbol (string package)
505 (declare (simple-string string))
506 (let* ((length (length string))
507 (hash (%sxhash-simple-string string))
508 (ehash (entry-hash length hash)))
509 (declare (type index length hash))
510 (with-symbol (found symbol (package-external-symbols package)
511 string length hash ehash)
512 (values symbol found))))
514 ;;; If we are uninterning a shadowing symbol, then a name conflict can
515 ;;; result, otherwise just nuke the symbol.
516 (defun unintern (symbol &optional (package (sane-package)))
518 "Makes Symbol no longer present in Package. If Symbol was present
519 then T is returned, otherwise NIL. If Package is Symbol's home
520 package, then it is made uninterned."
521 (let* ((package (find-undeleted-package-or-lose package))
522 (name (symbol-name symbol))
523 (shadowing-symbols (package-%shadowing-symbols package)))
524 (declare (list shadowing-symbols))
526 ;; If a name conflict is revealed, give use a chance to shadowing-import
527 ;; one of the accessible symbols.
528 (when (member symbol shadowing-symbols)
530 (dolist (p (package-%use-list package))
531 (multiple-value-bind (s w) (find-external-symbol name p)
532 (when w (pushnew s cset))))
536 "Prompt for a symbol to SHADOWING-IMPORT."
537 "Uninterning symbol ~S causes name conflict among these symbols:~%~S"
539 (write-string "Symbol to shadowing-import: " *query-io*)
540 (let ((sym (read *query-io*)))
543 (format *query-io* "~S is not a symbol."))
544 ((not (member sym cset))
545 (format *query-io* "~S is not one of the conflicting symbols."))
547 (shadowing-import sym package)
548 (return-from unintern t)))))))
549 (setf (package-%shadowing-symbols package)
550 (remove symbol shadowing-symbols)))
552 (multiple-value-bind (s w) (find-symbol name package)
554 (cond ((or (eq w :internal) (eq w :external))
555 (nuke-symbol (if (eq w :internal)
556 (package-internal-symbols package)
557 (package-external-symbols package))
559 (if (eq (symbol-package symbol) package)
560 (%set-symbol-package symbol nil))
564 ;;; Take a symbol-or-list-of-symbols and return a list, checking types.
565 (defun symbol-listify (thing)
568 (unless (symbolp s) (error "~S is not a symbol." s)))
570 ((symbolp thing) (list thing))
572 (error "~S is neither a symbol nor a list of symbols." thing))))
574 ;;; This is like UNINTERN, except if SYMBOL is inherited, it chases
575 ;;; down the package it is inherited from and uninterns it there. Used
576 ;;; for name-conflict resolution. Shadowing symbols are not uninterned
577 ;;; since they do not cause conflicts.
578 (defun moby-unintern (symbol package)
579 (unless (member symbol (package-%shadowing-symbols package))
580 (or (unintern symbol package)
581 (let ((name (symbol-name symbol)))
582 (multiple-value-bind (s w) (find-symbol name package)
584 (when (eq w :inherited)
585 (dolist (q (package-%use-list package))
586 (multiple-value-bind (u x) (find-external-symbol name q)
592 (defun export (symbols &optional (package (sane-package)))
594 "Exports Symbols from Package, checking that no name conflicts result."
595 (let ((package (find-undeleted-package-or-lose package))
597 ;; Punt any symbols that are already external.
598 (dolist (sym (symbol-listify symbols))
599 (multiple-value-bind (s w)
600 (find-external-symbol (symbol-name sym) package)
602 (unless (or w (member sym syms))
604 ;; Find symbols and packages with conflicts.
605 (let ((used-by (package-%used-by-list package))
609 (let ((name (symbol-name sym)))
611 (multiple-value-bind (s w) (find-symbol name p)
612 (when (and w (not (eq s sym))
613 (not (member s (package-%shadowing-symbols p))))
615 (pushnew p cpackages))))))
619 'simple-package-error
622 "Exporting these symbols from the ~A package:~%~S~%~
623 results in name conflicts with these packages:~%~{~A ~}"
625 (list (package-%name package) cset
626 (mapcar #'package-%name cpackages)))
627 (unintern-conflicting-symbols ()
628 :report "Unintern conflicting symbols."
629 (dolist (p cpackages)
631 (moby-unintern sym p))))
632 (skip-exporting-these-symbols ()
633 :report "Skip exporting conflicting symbols."
634 (setq syms (nset-difference syms cset))))))
636 ;; Check that all symbols are accessible. If not, ask to import them.
640 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
641 (cond ((not (and w (eq s sym)))
644 (push sym imports)))))
647 (continue "Import these symbols into the ~A package."
648 (package-%name package))
649 (error 'simple-package-error
652 "These symbols are not accessible in the ~A package:~%~S"
654 (list (package-%name package) missing)))
655 (import missing package))
656 (import imports package))
658 ;; And now, three pages later, we export the suckers.
659 (let ((internal (package-internal-symbols package))
660 (external (package-external-symbols package)))
662 (nuke-symbol internal (symbol-name sym))
663 (add-symbol external sym)))
666 ;;; Check that all symbols are accessible, then move from external to internal.
667 (defun unexport (symbols &optional (package (sane-package)))
669 "Makes Symbols no longer exported from Package."
670 (let ((package (find-undeleted-package-or-lose package))
672 (dolist (sym (symbol-listify symbols))
673 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
674 (cond ((or (not w) (not (eq s sym)))
675 (error 'simple-package-error
677 :format-control "~S is not accessible in the ~A package."
678 :format-arguments (list sym (package-%name package))))
679 ((eq w :external) (pushnew sym syms)))))
681 (let ((internal (package-internal-symbols package))
682 (external (package-external-symbols package)))
684 (add-symbol internal sym)
685 (nuke-symbol external (symbol-name sym))))
688 ;;; Check for name conflict caused by the import and let the user
689 ;;; shadowing-import if there is.
690 (defun import (symbols &optional (package (sane-package)))
692 "Make Symbols accessible as internal symbols in Package. If a symbol
693 is already accessible then it has no effect. If a name conflict
694 would result from the importation, then a correctable error is signalled."
695 (let ((package (find-undeleted-package-or-lose package))
696 (symbols (symbol-listify symbols))
699 (dolist (sym symbols)
700 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
702 (let ((found (member sym syms :test #'string=)))
704 (when (not (eq (car found) sym))
707 ((not (eq s sym)) (push sym cset))
708 ((eq w :inherited) (push sym syms)))))
710 ;; ANSI specifies that this error is correctable.
712 (continue "Import these symbols with Shadowing-Import.")
713 (error 'simple-package-error
716 "Importing these symbols into the ~A package ~
717 causes a name conflict:~%~S"
718 :format-arguments (list (package-%name package) cset))))
719 ;; Add the new symbols to the internal hashtable.
720 (let ((internal (package-internal-symbols package)))
722 (add-symbol internal sym)))
723 ;; If any of the symbols are uninterned, make them be owned by Package.
724 (dolist (sym symbols)
725 (unless (symbol-package sym) (%set-symbol-package sym package)))
726 (shadowing-import cset package)))
728 ;;; If a conflicting symbol is present, unintern it, otherwise just
729 ;;; stick the symbol in.
730 (defun shadowing-import (symbols &optional (package (sane-package)))
732 "Import Symbols into package, disregarding any name conflict. If
733 a symbol of the same name is present, then it is uninterned.
734 The symbols are added to the Package-Shadowing-Symbols."
735 (let* ((package (find-undeleted-package-or-lose package))
736 (internal (package-internal-symbols package)))
737 (dolist (sym (symbol-listify symbols))
738 (multiple-value-bind (s w) (find-symbol (symbol-name sym) package)
739 (unless (and w (not (eq w :inherited)) (eq s sym))
740 (when (or (eq w :internal) (eq w :external))
741 ;; If it was shadowed, we don't want UNINTERN to flame out...
742 (setf (package-%shadowing-symbols package)
743 (remove s (the list (package-%shadowing-symbols package))))
744 (unintern s package))
745 (add-symbol internal sym))
746 (pushnew sym (package-%shadowing-symbols package)))))
749 (defun shadow (symbols &optional (package (sane-package)))
751 "Make an internal symbol in Package with the same name as each of the
752 specified symbols, adding the new symbols to the Package-Shadowing-Symbols.
753 If a symbol with the given name is already present in Package, then
754 the existing symbol is placed in the shadowing symbols list if it is
755 not already present."
756 (let* ((package (find-undeleted-package-or-lose package))
757 (internal (package-internal-symbols package)))
758 (dolist (name (mapcar #'string
759 (if (listp symbols) symbols (list symbols))))
760 (multiple-value-bind (s w) (find-symbol name package)
761 (when (or (not w) (eq w :inherited))
762 (setq s (make-symbol name))
763 (%set-symbol-package s package)
764 (add-symbol internal s))
765 (pushnew s (package-%shadowing-symbols package)))))
768 ;;; Do stuff to use a package, with all kinds of fun name-conflict checking.
769 (defun use-package (packages-to-use &optional (package (sane-package)))
771 "Add all the Packages-To-Use to the use list for Package so that
772 the external symbols of the used packages are accessible as internal
774 (let ((packages (package-listify packages-to-use))
775 (package (find-undeleted-package-or-lose package)))
777 ;; Loop over each package, USE'ing one at a time...
778 (dolist (pkg packages)
779 (unless (member pkg (package-%use-list package))
781 (shadowing-symbols (package-%shadowing-symbols package))
782 (use-list (package-%use-list package)))
784 ;; If the number of symbols already accessible is less than the
785 ;; number to be inherited then it is faster to run the test the
786 ;; other way. This is particularly valuable in the case of
787 ;; a new package USEing Lisp.
789 ((< (+ (package-internal-symbol-count package)
790 (package-external-symbol-count package)
792 (dolist (p use-list res)
793 (incf res (package-external-symbol-count p)))))
794 (package-external-symbol-count pkg))
795 (do-symbols (sym package)
796 (multiple-value-bind (s w)
797 (find-external-symbol (symbol-name sym) pkg)
798 (when (and w (not (eq s sym))
799 (not (member sym shadowing-symbols)))
802 (do-external-symbols (sym p)
803 (multiple-value-bind (s w)
804 (find-external-symbol (symbol-name sym) pkg)
805 (when (and w (not (eq s sym))
806 (not (member (find-symbol (symbol-name sym)
811 (do-external-symbols (sym pkg)
812 (multiple-value-bind (s w)
813 (find-symbol (symbol-name sym) package)
814 (when (and w (not (eq s sym))
815 (not (member s shadowing-symbols)))
820 "Unintern the conflicting symbols in the ~2*~A package."
821 "Use'ing package ~A results in name conflicts for these symbols:~%~S"
822 (package-%name pkg) cset (package-%name package))
823 (dolist (s cset) (moby-unintern s package))))
825 (push pkg (package-%use-list package))
826 (push (package-external-symbols pkg) (cdr (package-tables package)))
827 (push package (package-%used-by-list pkg)))))
830 (defun unuse-package (packages-to-unuse &optional (package (sane-package)))
832 "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE."
833 (let ((package (find-undeleted-package-or-lose package)))
834 (dolist (p (package-listify packages-to-unuse))
835 (setf (package-%use-list package)
836 (remove p (the list (package-%use-list package))))
837 (setf (package-tables package)
838 (delete (package-external-symbols p)
839 (the list (package-tables package))))
840 (setf (package-%used-by-list p)
841 (remove package (the list (package-%used-by-list p)))))
844 (defun find-all-symbols (string-or-symbol)
846 "Return a list of all symbols in the system having the specified name."
847 (let ((string (string string-or-symbol))
849 (maphash (lambda (k v)
851 (multiple-value-bind (s w) (find-symbol string v)
852 (when w (pushnew s res))))
856 ;;;; APROPOS and APROPOS-LIST
858 (defun briefly-describe-symbol (symbol)
861 (when (boundp symbol)
862 (write-string " (bound)"))
863 (when (fboundp symbol)
864 (write-string " (fbound)")))
866 (defun apropos-list (string-designator
871 "Like APROPOS, except that it returns a list of the symbols found instead
873 (if package-designator
874 (let ((package (find-undeleted-package-or-lose package-designator))
875 (string (stringify-name string-designator "APROPOS search"))
877 (do-symbols (symbol package)
878 (when (and (eq (symbol-package symbol) package)
879 (or (not external-only)
880 (eq (find-symbol (symbol-name symbol) package)
882 (search string (symbol-name symbol) :test #'char-equal))
883 (push symbol result)))
885 (mapcan (lambda (package)
886 (apropos-list string-designator package external-only))
887 (list-all-packages))))
889 (defun apropos (string-designator &optional package external-only)
891 "Briefly describe all symbols which contain the specified STRING.
892 If PACKAGE is supplied then only describe symbols present in
893 that package. If EXTERNAL-ONLY then only describe
894 external symbols in the specified package."
895 ;; Implementing this in terms of APROPOS-LIST keeps things simple at the cost
896 ;; of some unnecessary consing; and the unnecessary consing shouldn't be an
897 ;; issue, since this function is is only useful interactively anyway, and
898 ;; we can cons and GC a lot faster than the typical user can read..
899 (dolist (symbol (apropos-list string-designator package external-only))
900 (briefly-describe-symbol symbol))
903 ;;;; final initialization
905 ;;;; The cold loader (GENESIS) makes the data structure in
906 ;;;; *!INITIAL-SYMBOLS*. We grovel over it, making the specified
907 ;;;; packages and interning the symbols. For a description of the
908 ;;;; format of *!INITIAL-SYMBOLS*, see the GENESIS source.
910 (defvar *!initial-symbols*)
914 (setq *in-package-init* t)
916 (/show0 "about to loop over *!INITIAL-SYMBOLS* to make packages")
917 (dolist (spec *!initial-symbols*)
918 (let* ((pkg (apply #'make-package (first spec)))
919 (internal (package-internal-symbols pkg))
920 (external (package-external-symbols pkg)))
921 (/show0 "back from MAKE-PACKAGE, PACKAGE-NAME=..")
922 (/primitive-print (package-name pkg))
924 ;; Put internal symbols in the internal hashtable and set package.
925 (dolist (symbol (second spec))
926 (add-symbol internal symbol)
927 (%set-symbol-package symbol pkg))
929 ;; External symbols same, only go in external table.
930 (dolist (symbol (third spec))
931 (add-symbol external symbol)
932 (%set-symbol-package symbol pkg))
934 ;; Don't set package for imported symbols.
935 (dolist (symbol (fourth spec))
936 (add-symbol internal symbol))
937 (dolist (symbol (fifth spec))
938 (add-symbol external symbol))
940 ;; Put shadowing symbols in the shadowing symbols list.
941 (setf (package-%shadowing-symbols pkg) (sixth spec))
942 ;; Set the package documentation
943 (setf (package-doc-string pkg) (seventh spec))))
945 ;; FIXME: These assignments are also done at toplevel in
946 ;; boot-extensions.lisp. They should probably only be done once.
947 (/show0 "setting up *CL-PACKAGE* and *KEYWORD-PACKAGE*")
948 (setq *cl-package* (find-package "COMMON-LISP"))
949 (setq *keyword-package* (find-package "KEYWORD"))
951 (/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*")
952 (makunbound '*!initial-symbols*) ; (so that it gets GCed)
954 ;; Make some other packages that should be around in the cold load.
955 ;; The COMMON-LISP-USER package is required by the ANSI standard,
956 ;; but not completely specified by it, so in the cross-compilation
957 ;; host Lisp it could contain various symbols, USE-PACKAGEs, or
958 ;; nicknames that we don't want in our target SBCL. For that reason,
959 ;; we handle it specially, not dumping the host Lisp version at
961 (aver (not (find-package "COMMON-LISP-USER")))
962 ;; ..but instead making our own from scratch here.
963 (/show0 "about to MAKE-PACKAGE COMMON-LISP-USER")
964 (make-package "COMMON-LISP-USER"
965 :nicknames '("CL-USER")
967 ;; ANSI encourages us to put extension packages
968 ;; in the USE list of COMMON-LISP-USER.
969 "SB!ALIEN" "SB!ALIEN" "SB!DEBUG"
970 "SB!EXT" "SB!GRAY" "SB!PROFILE"))
972 ;; Now do the *!DEFERRED-USE-PACKAGES*.
973 (/show0 "about to do *!DEFERRED-USE-PACKAGES*")
974 (dolist (args *!deferred-use-packages*)
975 (apply #'use-package args))
977 ;; The Age Of Magic is over, we can behave ANSIly henceforth.
978 (/show0 "about to SETQ *IN-PACKAGE-INIT*")
979 (setq *in-package-init* nil)
981 ;; For the kernel core image wizards, set the package to *CL-PACKAGE*.
983 ;; FIXME: We should just set this to (FIND-PACKAGE
984 ;; "COMMON-LISP-USER") once and for all here, instead of setting it
985 ;; once here and resetting it later.
986 (setq *package* *cl-package*))
989 (/show0 "done with !PACKAGE-COLD-INIT"))
991 (!defun-from-collected-cold-init-forms !package-cold-init)