X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcode%2Ftarget-package.lisp;h=e7a79f23fed9aa000bca8dd30020a68e616b3ef1;hb=2db3b6b4cb740d5b6512459c223859f747807b09;hp=671bda9ba06ba1bd2c30b5a9dce2d87cb2c8e0ae;hpb=a530bbe337109d898d5b4a001fc8f1afa3b5dc39;p=sbcl.git diff --git a/src/code/target-package.lisp b/src/code/target-package.lisp index 671bda9..e7a79f2 100644 --- a/src/code/target-package.lisp +++ b/src/code/target-package.lisp @@ -8,6 +8,9 @@ ;;;; symbol. A name conflict is said to occur when there would be more ;;;; than one candidate symbol. Any time a name conflict is about to ;;;; occur, a correctable error is signaled. +;;;; +;;;; FIXME: The code contains a lot of type declarations. Are they +;;;; all really necessary? ;;;; This software is part of the SBCL system. See the README file for ;;;; more information. @@ -20,21 +23,10 @@ (in-package "SB!IMPL") -(file-comment - "$Header$") - (!begin-collecting-cold-init-forms) (!cold-init-forms (/show0 "entering !PACKAGE-COLD-INIT")) - -(defvar *default-package-use-list*) -(!cold-init-forms - (setf *default-package-use-list* '("COMMON-LISP"))) -#!+sb-doc -(setf (fdocumentation '*default-package-use-list* 'variable) - "the list of packages to use by default when no :USE argument is supplied - to MAKE-PACKAGE or other package creation forms") ;;;; PACKAGE-HASHTABLE stuff @@ -57,22 +49,26 @@ ;;; the hashtable. (defun make-or-remake-package-hashtable (size &optional - (res (%make-package-hashtable))) - (do ((n (logior (truncate size package-rehash-threshold) 1) - (+ n 2))) - ((positive-primep n) - (setf (package-hashtable-table res) - (make-array n)) - (setf (package-hashtable-hash res) - (make-array n - :element-type '(unsigned-byte 8) - :initial-element 0)) - (let ((size (truncate (* n package-rehash-threshold)))) - (setf (package-hashtable-size res) size) - (setf (package-hashtable-free res) size)) - (setf (package-hashtable-deleted res) 0) - res) - (declare (type fixnum n)))) + res) + (flet ((actual-package-hashtable-size (size) + (loop for n of-type fixnum + from (logior (truncate size package-rehash-threshold) 1) + by 2 + when (positive-primep n) return n))) + (let* ((n (actual-package-hashtable-size size)) + (size (truncate (* n package-rehash-threshold))) + (table (make-array n)) + (hash (make-array n + :element-type '(unsigned-byte 8) + :initial-element 0))) + (if res + (setf (package-hashtable-table res) table + (package-hashtable-hash res) hash + (package-hashtable-size res) size + (package-hashtable-free res) size + (package-hashtable-deleted res) 0) + (setf res (%make-package-hashtable table hash size))) + res))) ;;;; miscellaneous PACKAGE operations @@ -87,57 +83,30 @@ ;;; ANSI says (in the definition of DELETE-PACKAGE) that these, and ;;; most other operations, are unspecified for deleted packages. We ;;; just do the easy thing and signal errors in that case. -(macrolet ((def-frob (ext real) +(macrolet ((def (ext real) `(defun ,ext (x) (,real (find-undeleted-package-or-lose x))))) - (def-frob package-nicknames package-%nicknames) - (def-frob package-use-list package-%use-list) - (def-frob package-used-by-list package-%used-by-list) - (def-frob package-shadowing-symbols package-%shadowing-symbols)) - -(flet ((stuff (table) - (let ((size (the fixnum - (- (the fixnum (package-hashtable-size table)) - (the fixnum - (package-hashtable-deleted table)))))) - (declare (fixnum size)) - (values (the fixnum - (- size - (the fixnum - (package-hashtable-free table)))) - size)))) - (defun package-internal-symbol-count (package) - (stuff (package-internal-symbols package))) - (defun package-external-symbol-count (package) - (stuff (package-external-symbols package)))) + (def package-nicknames package-%nicknames) + (def package-use-list package-%use-list) + (def package-used-by-list package-%used-by-list) + (def package-shadowing-symbols package-%shadowing-symbols)) + +(defun %package-hashtable-symbol-count (table) + (let ((size (the fixnum + (- (package-hashtable-size table) + (package-hashtable-deleted table))))) + (the fixnum + (- size (package-hashtable-free table))))) + +(defun package-internal-symbol-count (package) + (%package-hashtable-symbol-count (package-internal-symbols package))) + +(defun package-external-symbol-count (package) + (%package-hashtable-symbol-count (package-external-symbols package))) -(defvar *package* () ; actually initialized in cold load +(defvar *package* (error "*PACKAGE* should be initialized in cold load!") #!+sb-doc "the current package") ;;; FIXME: should be declared of type PACKAGE, with no NIL init form, ;;; after I get around to cleaning up DOCUMENTATION -;;; -;;; FIXME: Setting *PACKAGE* to a non-PACKAGE value (even a plausible -;;; one, like :CL-USER) makes the system fairly unusable, without -;;; generating useful diagnostics. Is it possible to handle this -;;; situation more gracefully by replacing references to *PACKAGE* -;;; with references to (DEFAULT-PACKAGE) and implementing -;;; DEFAULT-PACKAGE so that it checks for the PACKAGEness of *PACKAGE* -;;; and helps the user to fix any problem (perhaps going through -;;; CERROR)? -;;; Error: An attempt was made to use the *PACKAGE* variable when it was -;;; bound to the illegal (non-PACKAGE) value ~S. This is -;;; forbidden by the ANSI specification and could have made -;;; the system very confused. The *PACKAGE* variable has been -;;; temporarily reset to #. How -;;; would you like to proceed? -;;; NAMED Set *PACKAGE* to ~S (which is the package which is -;;; named by the old illegal ~S value of *PACKAGE*, and -;;; is thus very likely the intended value) and continue -;;; without signalling an error. -;;; ERROR Leave *PACKAGE* set to # -;;; and signal PACKAGE-ERROR to the code which tried to -;;; use the old illegal value of *PACKAGE*. -;;; CONTINUE Leave *PACKAGE* set to # -;;; and continue without signalling an error. ;;; a map from package names to packages (defvar *package-names*) @@ -220,9 +189,7 @@ (sxhash (%sxhash-simple-string (symbol-name symbol))) (h2 (the fixnum (1+ (the fixnum (rem sxhash (the fixnum (- len 2)))))))) - (declare (simple-vector vec) - (type (simple-array (unsigned-byte 8)) hash) - (fixnum len sxhash h2)) + (declare (fixnum len sxhash h2)) (cond ((zerop (the fixnum (package-hashtable-free table))) (make-or-remake-package-hashtable (* (package-hashtable-size table) 2) @@ -236,19 +203,18 @@ (do ((i (rem sxhash len) (rem (+ i h2) len))) ((< (the fixnum (aref hash i)) 2) (if (zerop (the fixnum (aref hash i))) - (decf (the fixnum (package-hashtable-free table))) - (decf (the fixnum (package-hashtable-deleted table)))) + (decf (package-hashtable-free table)) + (decf (package-hashtable-deleted table))) (setf (svref vec i) symbol) (setf (aref hash i) - (entry-hash (length (the simple-string - (symbol-name symbol))) + (entry-hash (length (symbol-name symbol)) sxhash))) (declare (fixnum i))))))) -;;; Find where the symbol named String is stored in Table. Index-Var -;;; is bound to the index, or NIL if it is not present. Symbol-Var -;;; is bound to the symbol. Length and Hash are the length and sxhash -;;; of String. Entry-Hash is the entry-hash of the string and length. +;;; Find where the symbol named STRING is stored in TABLE. INDEX-VAR +;;; is bound to the index, or NIL if it is not present. SYMBOL-VAR +;;; is bound to the symbol. LENGTH and HASH are the length and sxhash +;;; of STRING. ENTRY-HASH is the entry-hash of the string and length. (defmacro with-symbol ((index-var symbol-var table string length sxhash entry-hash) &body forms) @@ -259,9 +225,7 @@ (,len (length ,vec)) (,h2 (1+ (the index (rem (the index ,sxhash) (the index (- ,len 2))))))) - (declare (type (simple-array (unsigned-byte 8) (*)) ,hash) - (simple-vector ,vec) - (type index ,len ,h2)) + (declare (type index ,len ,h2)) (prog ((,index-var (rem (the index ,sxhash) ,len)) ,symbol-var ,ehash) (declare (type (or index null) ,index-var)) @@ -271,8 +235,7 @@ (setq ,symbol-var (svref ,vec ,index-var)) (let* ((,name (symbol-name ,symbol-var)) (,name-len (length ,name))) - (declare (simple-string ,name) - (type index ,name-len)) + (declare (type index ,name-len)) (when (and (= ,name-len ,length) (string= ,string ,name :end1 ,length @@ -288,7 +251,7 @@ DOIT (return (progn ,@forms)))))) -;;; Delete the entry for String in Table. The entry must exist. +;;; Delete the entry for STRING in TABLE. The entry must exist. (defun nuke-symbol (table string) (declare (simple-string string)) (let* ((length (length string)) @@ -300,11 +263,11 @@ (setf (aref (package-hashtable-table table) index) nil) (incf (package-hashtable-deleted table))))) -;;; Enter any new Nicknames for Package into *package-names*. +;;; Enter any new NICKNAMES for PACKAGE into *PACKAGE-NAMES*. ;;; If there is a conflict then give the user a chance to do ;;; something about it. (defun enter-new-nicknames (package nicknames) - (check-type nicknames list) + (declare (type list nicknames)) (dolist (n nicknames) (let* ((n (package-namify n)) (found (gethash n *package-names*))) @@ -329,16 +292,19 @@ (push n (package-%nicknames package))))))) (defun make-package (name &key - (use *default-package-use-list*) + (use '#.*default-package-use-list*) nicknames (internal-symbols 10) (external-symbols 10)) #!+sb-doc - "Makes a new package having the specified Name and Nicknames. The - package will inherit all external symbols from each package in - the use list. :Internal-Symbols and :External-Symbols are + #.(format nil + "Make a new package having the specified NAME, NICKNAMES, and + USE list. :INTERNAL-SYMBOLS and :EXTERNAL-SYMBOLS are estimates for the number of internal and external symbols which - will ultimately be present in the package." + will ultimately be present in the package. The default value of + USE is implementation-dependent, and in this implementation + it is ~S." + *default-package-use-list*) ;; Check for package name conflicts in name and nicknames, then ;; make the package. @@ -386,7 +352,10 @@ (name (string name)) (found (find-package name))) (unless (or (not found) (eq found package)) - (error "A package named ~S already exists." name)) + (error 'simple-package-error + :package name + :format-control "A package named ~S already exists." + :format-arguments (list name))) (remhash (package-%name package) *package-names*) (dolist (n (package-%nicknames package)) (remhash n *package-names*)) @@ -451,17 +420,17 @@ (defun list-all-packages () #!+sb-doc - "Returns a list of all existing packages." + "Return a list of all existing packages." (let ((res ())) - (maphash #'(lambda (k v) - (declare (ignore k)) - (pushnew v res)) + (maphash (lambda (k v) + (declare (ignore k)) + (pushnew v res)) *package-names*) res)) -(defun intern (name &optional (package *package*)) +(defun intern (name &optional (package (sane-package))) #!+sb-doc - "Returns a symbol having the specified name, creating it if necessary." + "Return a symbol having the specified name, creating it if necessary." ;; We just simple-stringify the name and call INTERN*, where the real ;; logic is. (let ((name (if (simple-string-p name) @@ -472,9 +441,9 @@ (length name) (find-undeleted-package-or-lose package)))) -(defun find-symbol (name &optional (package *package*)) +(defun find-symbol (name &optional (package (sane-package))) #!+sb-doc - "Returns the symbol named String in Package. If such a symbol is found + "Return the symbol named String in Package. If such a symbol is found then the second value is :internal, :external or :inherited to indicate how the symbol is accessible. If no symbol is found then both values are NIL." @@ -544,7 +513,7 @@ ;;; If we are uninterning a shadowing symbol, then a name conflict can ;;; result, otherwise just nuke the symbol. -(defun unintern (symbol &optional (package *package*)) +(defun unintern (symbol &optional (package (sane-package))) #!+sb-doc "Makes Symbol no longer present in Package. If Symbol was present then T is returned, otherwise NIL. If Package is Symbol's home @@ -552,7 +521,7 @@ (let* ((package (find-undeleted-package-or-lose package)) (name (symbol-name symbol)) (shadowing-symbols (package-%shadowing-symbols package))) - (declare (list shadowing-symbols) (simple-string name)) + (declare (list shadowing-symbols)) ;; If a name conflict is revealed, give use a chance to shadowing-import ;; one of the accessible symbols. @@ -602,9 +571,9 @@ (t (error "~S is neither a symbol nor a list of symbols." thing)))) -;;; Like UNINTERN, but if symbol is inherited chases down the package -;;; it is inherited from and uninterns it there. Used for -;;; name-conflict resolution. Shadowing symbols are not uninterned +;;; This is like UNINTERN, except if SYMBOL is inherited, it chases +;;; down the package it is inherited from and uninterns it there. Used +;;; for name-conflict resolution. Shadowing symbols are not uninterned ;;; since they do not cause conflicts. (defun moby-unintern (symbol package) (unless (member symbol (package-%shadowing-symbols package)) @@ -620,7 +589,7 @@ (unintern symbol q) (return t)))))))))) -(defun export (symbols &optional (package *package*)) +(defun export (symbols &optional (package (sane-package))) #!+sb-doc "Exports Symbols from Package, checking that no name conflicts result." (let ((package (find-undeleted-package-or-lose package)) @@ -695,7 +664,7 @@ t)) ;;; Check that all symbols are accessible, then move from external to internal. -(defun unexport (symbols &optional (package *package*)) +(defun unexport (symbols &optional (package (sane-package))) #!+sb-doc "Makes Symbols no longer exported from Package." (let ((package (find-undeleted-package-or-lose package)) @@ -718,7 +687,7 @@ ;;; Check for name conflict caused by the import and let the user ;;; shadowing-import if there is. -(defun import (symbols &optional (package *package*)) +(defun import (symbols &optional (package (sane-package))) #!+sb-doc "Make Symbols accessible as internal symbols in Package. If a symbol is already accessible then it has no effect. If a name conflict @@ -758,7 +727,7 @@ ;;; If a conflicting symbol is present, unintern it, otherwise just ;;; stick the symbol in. -(defun shadowing-import (symbols &optional (package *package*)) +(defun shadowing-import (symbols &optional (package (sane-package))) #!+sb-doc "Import Symbols into package, disregarding any name conflict. If a symbol of the same name is present, then it is uninterned. @@ -777,7 +746,7 @@ (pushnew sym (package-%shadowing-symbols package))))) t) -(defun shadow (symbols &optional (package *package*)) +(defun shadow (symbols &optional (package (sane-package))) #!+sb-doc "Make an internal symbol in Package with the same name as each of the specified symbols, adding the new symbols to the Package-Shadowing-Symbols. @@ -797,7 +766,7 @@ t) ;;; Do stuff to use a package, with all kinds of fun name-conflict checking. -(defun use-package (packages-to-use &optional (package *package*)) +(defun use-package (packages-to-use &optional (package (sane-package))) #!+sb-doc "Add all the Packages-To-Use to the use list for Package so that the external symbols of the used packages are accessible as internal @@ -858,9 +827,9 @@ (push package (package-%used-by-list pkg))))) t) -(defun unuse-package (packages-to-unuse &optional (package *package*)) +(defun unuse-package (packages-to-unuse &optional (package (sane-package))) #!+sb-doc - "Remove Packages-To-Unuse from the use list for Package." + "Remove PACKAGES-TO-UNUSE from the USE list for PACKAGE." (let ((package (find-undeleted-package-or-lose package))) (dolist (p (package-listify packages-to-unuse)) (setf (package-%use-list package) @@ -877,18 +846,15 @@ "Return a list of all symbols in the system having the specified name." (let ((string (string string-or-symbol)) (res ())) - (maphash #'(lambda (k v) - (declare (ignore k)) - (multiple-value-bind (s w) (find-symbol string v) - (when w (pushnew s res)))) + (maphash (lambda (k v) + (declare (ignore k)) + (multiple-value-bind (s w) (find-symbol string v) + (when w (pushnew s res)))) *package-names*) res)) ;;;; APROPOS and APROPOS-LIST -;;; KLUDGE: All the APROPOS stuff should probably be byte-compiled, since it's -;;; only likely to be used interactively. -- WHN 19990827 - (defun briefly-describe-symbol (symbol) (fresh-line) (prin1 symbol) @@ -897,24 +863,28 @@ (when (fboundp symbol) (write-string " (fbound)"))) -(defun apropos-list (string-designator &optional package external-only) +(defun apropos-list (string-designator + &optional + package-designator + external-only) #!+sb-doc "Like APROPOS, except that it returns a list of the symbols found instead of describing them." - (if package - (let ((string (stringify-name string-designator "APROPOS search")) - (result nil)) - (do-symbols (symbol package) - (when (and (eq (symbol-package symbol) package) - (or (not external-only) - (eq (find-symbol (symbol-name symbol) package) - :external)) - (search string (symbol-name symbol) :test #'char-equal)) - (push symbol result))) - result) - (mapcan (lambda (package) - (apropos-list string-designator package external-only)) - (list-all-packages)))) + (if package-designator + (let ((package (find-undeleted-package-or-lose package-designator)) + (string (stringify-name string-designator "APROPOS search")) + (result nil)) + (do-symbols (symbol package) + (when (and (eq (symbol-package symbol) package) + (or (not external-only) + (eq (find-symbol (symbol-name symbol) package) + :external)) + (search string (symbol-name symbol) :test #'char-equal)) + (push symbol result))) + result) + (mapcan (lambda (package) + (apropos-list string-designator package external-only)) + (list-all-packages)))) (defun apropos (string-designator &optional package external-only) #!+sb-doc @@ -948,8 +918,8 @@ (let* ((pkg (apply #'make-package (first spec))) (internal (package-internal-symbols pkg)) (external (package-external-symbols pkg))) - (/show0 "back from MAKE-PACKAGE") - #!+sb-show (sb!sys:%primitive print (package-name pkg)) + (/show0 "back from MAKE-PACKAGE, PACKAGE-NAME=..") + (/primitive-print (package-name pkg)) ;; Put internal symbols in the internal hashtable and set package. (dolist (symbol (second spec)) @@ -968,26 +938,35 @@ (add-symbol external symbol)) ;; Put shadowing symbols in the shadowing symbols list. - (setf (package-%shadowing-symbols pkg) (sixth spec)))) + (setf (package-%shadowing-symbols pkg) (sixth spec)) + ;; Set the package documentation + (setf (package-doc-string pkg) (seventh spec)))) + + ;; FIXME: These assignments are also done at toplevel in + ;; boot-extensions.lisp. They should probably only be done once. + (/show0 "setting up *CL-PACKAGE* and *KEYWORD-PACKAGE*") + (setq *cl-package* (find-package "COMMON-LISP")) + (setq *keyword-package* (find-package "KEYWORD")) (/show0 "about to MAKUNBOUND *!INITIAL-SYMBOLS*") (makunbound '*!initial-symbols*) ; (so that it gets GCed) - ;; Make some other packages that should be around in the cold load. The - ;; COMMON-LISP-USER package is required by the ANSI standard, but not - ;; completely specified by it, so in the cross-compilation host Lisp it could - ;; contain various symbols, USE-PACKAGEs, or nicknames that we don't want in - ;; our target SBCL. For that reason, we handle it specially, not dumping the - ;; host Lisp version at genesis time.. - (assert (not (find-package "COMMON-LISP-USER"))) + ;; Make some other packages that should be around in the cold load. + ;; The COMMON-LISP-USER package is required by the ANSI standard, + ;; but not completely specified by it, so in the cross-compilation + ;; host Lisp it could contain various symbols, USE-PACKAGEs, or + ;; nicknames that we don't want in our target SBCL. For that reason, + ;; we handle it specially, not dumping the host Lisp version at + ;; genesis time.. + (aver (not (find-package "COMMON-LISP-USER"))) ;; ..but instead making our own from scratch here. (/show0 "about to MAKE-PACKAGE COMMON-LISP-USER") (make-package "COMMON-LISP-USER" :nicknames '("CL-USER") :use '("COMMON-LISP" - ;; ANSI encourages us to put extension packages in the - ;; USE list of COMMON-LISP-USER. - "SB!ALIEN" "SB!C-CALL" "SB!DEBUG" + ;; ANSI encourages us to put extension packages + ;; in the USE list of COMMON-LISP-USER. + "SB!ALIEN" "SB!ALIEN" "SB!DEBUG" "SB!EXT" "SB!GRAY" "SB!PROFILE")) ;; Now do the *!DEFERRED-USE-PACKAGES*. @@ -999,16 +978,11 @@ (/show0 "about to SETQ *IN-PACKAGE-INIT*") (setq *in-package-init* nil) - ;; FIXME: These assignments are also done at toplevel in - ;; boot-extensions.lisp. They should probably only be done once. - (setq *cl-package* (find-package "COMMON-LISP")) - (setq *keyword-package* (find-package "KEYWORD")) - ;; For the kernel core image wizards, set the package to *CL-PACKAGE*. ;; - ;; FIXME: We should just set this to (FIND-PACKAGE "COMMON-LISP-USER") - ;; once and for all here, instead of setting it once here and resetting - ;; it later. + ;; FIXME: We should just set this to (FIND-PACKAGE + ;; "COMMON-LISP-USER") once and for all here, instead of setting it + ;; once here and resetting it later. (setq *package* *cl-package*)) (!cold-init-forms