0.7.10.15:
[sbcl.git] / src / code / package.lisp
1 ;;;; that part of the CMU CL package.lisp file which can run on the
2 ;;;; cross-compilation host
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!IMPL")
14 \f
15 ;;;; the PACKAGE-HASHTABLE structure
16
17 ;;; comment from CMU CL:
18 ;;;      Packages are implemented using a special kind of hashtable. It is
19 ;;;   an open hashtable with a parallel 8-bit I-vector of hash-codes. The
20 ;;;   primary purpose of the hash for each entry is to reduce paging by
21 ;;;   allowing collisions and misses to be detected without paging in the
22 ;;;   symbol and pname for an entry. If the hash for an entry doesn't
23 ;;;   match that for the symbol that we are looking for, then we can
24 ;;;   go on without touching the symbol, pname, or even hastable vector.
25 ;;;      It turns out that, contrary to my expectations, paging is a very
26 ;;;   important consideration the design of the package representation.
27 ;;;   Using a similar scheme without the entry hash, the fasloader was
28 ;;;   spending more than half its time paging in INTERN.
29 ;;;      The hash code also indicates the status of an entry. If it zero,
30 ;;;   the entry is unused. If it is one, then it is deleted.
31 ;;;   Double-hashing is used for collision resolution.
32
33 (sb!xc:deftype hash-vector () '(simple-array (unsigned-byte 8) (*)))
34
35 (sb!xc:defstruct (package-hashtable (:constructor %make-package-hashtable ())
36                                     (:copier nil))
37   ;; The g-vector of symbols.
38   ;; FIXME: could just be type SIMPLE-VECTOR, with (MISSING-ARG) default
39   (table nil :type (or simple-vector null))
40   ;; The i-vector of pname hash values.
41   ;; FIXME: could just be type HASH-VECTOR, with (MISSING-ARG) default
42   (hash nil :type (or hash-vector null))
43   ;; The total number of entries allowed before resizing.
44   ;;
45   ;; FIXME: CAPACITY would be a more descriptive name. (This is
46   ;; related to but not quite the same as HASH-TABLE-SIZE, so calling
47   ;; it SIZE seems somewhat misleading.)
48   (size 0 :type index)
49   ;; The remaining number of entries that can be made before we have to rehash.
50   (free 0 :type index)
51   ;; The number of deleted entries.
52   (deleted 0 :type index))
53 \f
54 ;;;; the PACKAGE structure
55
56 ;;; KLUDGE: We use DEF!STRUCT to define this not because we need to
57 ;;; manipulate target package objects on the cross-compilation host,
58 ;;; but only because its MAKE-LOAD-FORM function needs to be hooked
59 ;;; into the pre-CLOS DEF!STRUCT MAKE-LOAD-FORM system so that we can
60 ;;; compile things like IN-PACKAGE in warm init before CLOS is set up.
61 ;;; The DEF!STRUCT side effect of defining a new PACKAGE type on the
62 ;;; cross-compilation host is just a nuisance, and in order to avoid
63 ;;; breaking the cross-compilation host, we need to work around it
64 ;;; around by putting the new PACKAGE type (and the PACKAGEP predicate
65 ;;; too..) into SB!XC. -- WHN 20000309
66 (def!struct (sb!xc:package
67              (:constructor internal-make-package)
68              (:make-load-form-fun (lambda (p)
69                                     (values `(find-undeleted-package-or-lose
70                                               ',(package-name p))
71                                             nil)))
72              (:predicate sb!xc:packagep))
73   #!+sb-doc
74   "the standard structure for the description of a package"
75   ;; the name of the package, or NIL for a deleted package
76   (%name nil :type (or simple-string null))
77   ;; nickname strings
78   (%nicknames () :type list)
79   ;; packages used by this package
80   (%use-list () :type list)
81   ;; a list of all the hashtables for inherited symbols. This is
82   ;; derived from %USE-LIST, but maintained separately from %USE-LIST
83   ;; for some reason. (Perhaps the reason is that when FIND-SYMBOL*
84   ;; hits an inherited symbol, it pulls it to the head of the list.)
85   ;;
86   ;; FIXME: This needs a more-descriptive name
87   ;; (USED-PACKAGE-HASH-TABLES?). It also needs an explanation of why
88   ;; the last entry is NIL. Perhaps it should even just go away and
89   ;; let us do indirection on the fly through %USE-LIST. (If so,
90   ;; benchmark to make sure that performance doesn't get stomped..)
91   ;; (If benchmark performance is important, this should prob'ly
92   ;; become a vector instead of a list.)
93   (tables (list nil) :type list)
94   ;; packages that use this package
95   (%used-by-list () :type list)
96   ;; PACKAGE-HASHTABLEs of internal & external symbols
97   (internal-symbols (missing-arg) :type package-hashtable)
98   (external-symbols (missing-arg) :type package-hashtable)
99   ;; shadowing symbols
100   (%shadowing-symbols () :type list)
101   ;; documentation string for this package
102   (doc-string nil :type (or simple-string null)))
103 \f
104 ;;;; iteration macros
105
106 (defmacro-mundanely do-symbols ((var &optional
107                                      (package '*package*)
108                                      result-form)
109                                 &body body-decls)
110   #!+sb-doc
111   "DO-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECLARATION}* {TAG | FORM}*
112    Executes the FORMs at least once for each symbol accessible in the given
113    PACKAGE with VAR bound to the current symbol."
114   (multiple-value-bind (body decls) (parse-body body-decls nil)
115     (let ((flet-name (gensym "DO-SYMBOLS-")))
116       `(block nil
117          (flet ((,flet-name (,var)
118                   ,@decls
119                   (tagbody ,@body)))
120            (let* ((package (find-undeleted-package-or-lose ,package))
121                   (shadows (package-%shadowing-symbols package)))
122              (flet ((iterate-over-hash-table (table ignore)
123                       (let ((hash-vec (package-hashtable-hash table))
124                             (sym-vec (package-hashtable-table table)))
125                         (declare (type (simple-array (unsigned-byte 8) (*))
126                                        hash-vec)
127                                  (type simple-vector sym-vec))
128                         (dotimes (i (length sym-vec))
129                           (when (>= (aref hash-vec i) 2)
130                             (let ((sym (aref sym-vec i)))
131                               (declare (inline member))
132                               (unless (member sym ignore :test #'string=)
133                                 (,flet-name sym))))))))
134                (iterate-over-hash-table (package-internal-symbols package) nil)
135                (iterate-over-hash-table (package-external-symbols package) nil)
136                (dolist (use (package-%use-list package))
137                  (iterate-over-hash-table (package-external-symbols use)
138                                           shadows)))))
139          (let ((,var nil))
140            (declare (ignorable ,var))
141            ,@decls
142            ,result-form)))))
143
144 (defmacro-mundanely do-external-symbols ((var &optional
145                                               (package '*package*)
146                                               result-form)
147                                          &body body-decls)
148   #!+sb-doc
149   "DO-EXTERNAL-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECL}* {TAG | FORM}*
150    Executes the FORMs once for each external symbol in the given PACKAGE with
151    VAR bound to the current symbol."
152   (multiple-value-bind (body decls) (parse-body body-decls nil)
153     (let ((flet-name (gensym "DO-SYMBOLS-")))
154       `(block nil
155          (flet ((,flet-name (,var)
156                   ,@decls
157                   (tagbody ,@body)))
158            (let* ((package (find-undeleted-package-or-lose ,package))
159                   (table (package-external-symbols package))
160                   (hash-vec (package-hashtable-hash table))
161                   (sym-vec (package-hashtable-table table)))
162              (declare (type (simple-array (unsigned-byte 8) (*))
163                             hash-vec)
164                       (type simple-vector sym-vec))
165              (dotimes (i (length sym-vec))
166                (when (>= (aref hash-vec i) 2)
167                  (,flet-name (aref sym-vec i))))))
168          (let ((,var nil))
169            (declare (ignorable ,var))
170            ,@decls
171            ,result-form)))))
172
173 (defmacro-mundanely do-all-symbols ((var &optional
174                                          result-form)
175                                     &body body-decls)
176   #!+sb-doc
177   "DO-ALL-SYMBOLS (VAR [RESULT-FORM]) {DECLARATION}* {TAG | FORM}*
178    Executes the FORMs once for each symbol in every package with VAR bound
179    to the current symbol."
180   (multiple-value-bind (body decls) (parse-body body-decls nil)
181     (let ((flet-name (gensym "DO-SYMBOLS-")))
182       `(block nil
183          (flet ((,flet-name (,var)
184                   ,@decls
185                   (tagbody ,@body)))
186            (dolist (package (list-all-packages))
187              (flet ((iterate-over-hash-table (table)
188                       (let ((hash-vec (package-hashtable-hash table))
189                             (sym-vec (package-hashtable-table table)))
190                         (declare (type (simple-array (unsigned-byte 8) (*))
191                                        hash-vec)
192                                  (type simple-vector sym-vec))
193                         (dotimes (i (length sym-vec))
194                           (when (>= (aref hash-vec i) 2)
195                             (,flet-name (aref sym-vec i)))))))
196                (iterate-over-hash-table (package-internal-symbols package))
197                (iterate-over-hash-table (package-external-symbols package)))))
198          (let ((,var nil))
199            (declare (ignorable ,var))
200            ,@decls
201            ,result-form)))))
202 \f
203 ;;;; WITH-PACKAGE-ITERATOR
204
205 (defmacro-mundanely with-package-iterator ((mname package-list
206                                                   &rest symbol-types)
207                                            &body body)
208   #!+sb-doc
209   "Within the lexical scope of the body forms, MNAME is defined via macrolet
210    such that successive invocations of (MNAME) will return the symbols,
211    one by one, from the packages in PACKAGE-LIST. SYMBOL-TYPES may be
212    any of :INHERITED :EXTERNAL :INTERNAL."
213   (let* ((packages (gensym))
214          (these-packages (gensym))
215          (ordered-types (let ((res nil))
216                           (dolist (kind '(:inherited :external :internal)
217                                         res)
218                             (when (member kind symbol-types)
219                               (push kind res)))))  ; Order SYMBOL-TYPES.
220          (counter (gensym))
221          (kind (gensym))
222          (hash-vector (gensym))
223          (vector (gensym))
224          (package-use-list (gensym))
225          (init-macro (gensym))
226          (end-test-macro (gensym))
227          (real-symbol-p (gensym))
228          (inherited-symbol-p (gensym))
229          (BLOCK (gensym)))
230     `(let* ((,these-packages ,package-list)
231             (,packages `,(mapcar (lambda (package)
232                                    (if (packagep package)
233                                        package
234                                        ;; Maybe FIND-PACKAGE-OR-DIE?
235                                        (or (find-package package)
236                                            (error 'simple-package-error
237                                                   ;; could be a character
238                                                   :name (string package)
239                                                   :format-control "~@<~S does not name a package ~:>"
240                                                   :format-arguments (list package)))))
241                                  (if (consp ,these-packages)
242                                      ,these-packages
243                                      (list ,these-packages))))
244             (,counter nil)
245             (,kind (car ,packages))
246             (,hash-vector nil)
247             (,vector nil)
248             (,package-use-list nil))
249        ,(if (member :inherited ordered-types)
250             `(setf ,package-use-list (package-%use-list (car ,packages)))
251             `(declare (ignore ,package-use-list)))
252        (macrolet ((,init-macro (next-kind)
253          (declare (optimize (inhibit-warnings 3)))
254          (let ((symbols (gensym)))
255            `(progn
256               (setf ,',kind ,next-kind)
257               (setf ,',counter nil)
258               ,(case next-kind
259                  (:internal
260                   `(let ((,symbols (package-internal-symbols
261                                     (car ,',packages))))
262                      (when ,symbols
263                        (setf ,',vector (package-hashtable-table ,symbols))
264                        (setf ,',hash-vector
265                              (package-hashtable-hash ,symbols)))))
266                  (:external
267                   `(let ((,symbols (package-external-symbols
268                                     (car ,',packages))))
269                      (when ,symbols
270                        (setf ,',vector (package-hashtable-table ,symbols))
271                        (setf ,',hash-vector
272                              (package-hashtable-hash ,symbols)))))
273                  (:inherited
274                   `(let ((,symbols (and ,',package-use-list
275                                         (package-external-symbols
276                                          (car ,',package-use-list)))))
277                      (when ,symbols
278                        (setf ,',vector (package-hashtable-table ,symbols))
279                        (setf ,',hash-vector
280                              (package-hashtable-hash ,symbols)))))))))
281                   (,end-test-macro (this-kind)
282                      `,(let ((next-kind (cadr (member this-kind
283                                                       ',ordered-types))))
284                          (if next-kind
285                              `(,',init-macro ,next-kind)
286                              `(if (endp (setf ,',packages (cdr ,',packages)))
287                                   (return-from ,',BLOCK)
288                                   (,',init-macro ,(car ',ordered-types)))))))
289          (when ,packages
290            ,(when (null symbol-types)
291               (error 'simple-program-error
292                      :format-control
293                      "At least one of :INTERNAL, :EXTERNAL, or ~
294                       :INHERITED must be supplied."))
295            ,(dolist (symbol symbol-types)
296               (unless (member symbol '(:internal :external :inherited))
297                 (error 'program-error
298                        :format-control
299                        "~S is not one of :INTERNAL, :EXTERNAL, or :INHERITED."
300                        :format-argument symbol)))
301            (,init-macro ,(car ordered-types))
302            (flet ((,real-symbol-p (number)
303                     (> number 1)))
304              (macrolet ((,mname ()
305               (declare (optimize (inhibit-warnings 3)))
306               `(block ,',BLOCK
307                  (loop
308                    (case ,',kind
309                      ,@(when (member :internal ',ordered-types)
310                          `((:internal
311                             (setf ,',counter
312                                   (position-if #',',real-symbol-p
313                                                ,',hash-vector
314                                                :start (if ,',counter
315                                                           (1+ ,',counter)
316                                                           0)))
317                             (if ,',counter
318                                 (return-from ,',BLOCK
319                                  (values t (svref ,',vector ,',counter)
320                                          ,',kind (car ,',packages)))
321                                 (,',end-test-macro :internal)))))
322                      ,@(when (member :external ',ordered-types)
323                          `((:external
324                             (setf ,',counter
325                                   (position-if #',',real-symbol-p
326                                                ,',hash-vector
327                                                :start (if ,',counter
328                                                           (1+ ,',counter)
329                                                           0)))
330                             (if ,',counter
331                                 (return-from ,',BLOCK
332                                  (values t (svref ,',vector ,',counter)
333                                          ,',kind (car ,',packages)))
334                                 (,',end-test-macro :external)))))
335                      ,@(when (member :inherited ',ordered-types)
336                          `((:inherited
337                             (flet ((,',inherited-symbol-p (number)
338                                      (when (,',real-symbol-p number)
339                                        (let* ((p (position
340                                                   number ,',hash-vector
341                                                   :start (if ,',counter
342                                                              (1+ ,',counter)
343                                                              0)))
344                                               (s (svref ,',vector p)))
345                                          (eql (nth-value
346                                                1 (find-symbol
347                                                   (symbol-name s)
348                                                   (car ,',packages)))
349                                               :inherited)))))
350                               (setf ,',counter
351                                     (position-if #',',inherited-symbol-p
352                                                  ,',hash-vector
353                                                  :start (if ,',counter
354                                                             (1+ ,',counter)
355                                                             0))))
356                             (cond (,',counter
357                                    (return-from
358                                     ,',BLOCK
359                                     (values t (svref ,',vector ,',counter)
360                                             ,',kind (car ,',packages))
361                                     ))
362                                   (t
363                                    (setf ,',package-use-list
364                                          (cdr ,',package-use-list))
365                                    (cond ((endp ,',package-use-list)
366                                           (setf ,',packages (cdr ,',packages))
367                                           (when (endp ,',packages)
368                                             (return-from ,',BLOCK))
369                                           (setf ,',package-use-list
370                                                 (package-%use-list
371                                                  (car ,',packages)))
372                                           (,',init-macro ,(car
373                                                            ',ordered-types)))
374                                          (t (,',init-macro :inherited)
375                                             (setf ,',counter nil)))))))))))))
376                ,@body)))))))