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