0.8.15.7
[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 (def!type hash-vector () '(simple-array (unsigned-byte 8) (*)))
34
35 (def!struct (package-hashtable
36              (:constructor %make-package-hashtable
37                            (table hash size &aux (free size)))
38              (:copier nil))
39   ;; The g-vector of symbols.
40   (table (missing-arg) :type simple-vector)
41   ;; The i-vector of pname hash values.
42   (hash (missing-arg) :type hash-vector)
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 (missing-arg) :type index)
49   ;; The remaining number of entries that can be made before we have to rehash.
50   (free (missing-arg) :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-base-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-base-string null))
103   ;; package locking
104   #!+sb-package-locks
105   (lock nil :type boolean)
106   #!+sb-package-locks
107   (%implementation-packages nil :type list))
108 \f
109 ;;;; iteration macros
110
111 (defmacro-mundanely do-symbols ((var &optional
112                                      (package '*package*)
113                                      result-form)
114                                 &body body-decls)
115   #!+sb-doc
116   "DO-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECLARATION}* {TAG | FORM}*
117    Executes the FORMs at least once for each symbol accessible in the given
118    PACKAGE with VAR bound to the current symbol."
119   (multiple-value-bind (body decls)
120       (parse-body body-decls :doc-string-allowed nil)
121     (let ((flet-name (gensym "DO-SYMBOLS-")))
122       `(block nil
123          (flet ((,flet-name (,var)
124                   ,@decls
125                   (tagbody ,@body)))
126            (let* ((package (find-undeleted-package-or-lose ,package))
127                   (shadows (package-%shadowing-symbols package)))
128              (flet ((iterate-over-hash-table (table ignore)
129                       (let ((hash-vec (package-hashtable-hash table))
130                             (sym-vec (package-hashtable-table table)))
131                         (dotimes (i (length sym-vec))
132                           (when (>= (aref hash-vec i) 2)
133                             (let ((sym (aref sym-vec i)))
134                               (declare (inline member))
135                               (unless (member sym ignore :test #'string=)
136                                 (,flet-name sym))))))))
137                (iterate-over-hash-table (package-internal-symbols package) nil)
138                (iterate-over-hash-table (package-external-symbols package) nil)
139                (dolist (use (package-%use-list package))
140                  (iterate-over-hash-table (package-external-symbols use)
141                                           shadows)))))
142          (let ((,var nil))
143            (declare (ignorable ,var))
144            ,@decls
145            ,result-form)))))
146
147 (defmacro-mundanely do-external-symbols ((var &optional
148                                               (package '*package*)
149                                               result-form)
150                                          &body body-decls)
151   #!+sb-doc
152   "DO-EXTERNAL-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECL}* {TAG | FORM}*
153    Executes the FORMs once for each external symbol in the given PACKAGE with
154    VAR bound to the current symbol."
155   (multiple-value-bind (body decls)
156       (parse-body body-decls :doc-string-allowed nil)
157     (let ((flet-name (gensym "DO-SYMBOLS-")))
158       `(block nil
159          (flet ((,flet-name (,var)
160                   ,@decls
161                   (tagbody ,@body)))
162            (let* ((package (find-undeleted-package-or-lose ,package))
163                   (table (package-external-symbols package))
164                   (hash-vec (package-hashtable-hash table))
165                   (sym-vec (package-hashtable-table table)))
166              (dotimes (i (length sym-vec))
167                (when (>= (aref hash-vec i) 2)
168                  (,flet-name (aref sym-vec i))))))
169          (let ((,var nil))
170            (declare (ignorable ,var))
171            ,@decls
172            ,result-form)))))
173
174 (defmacro-mundanely do-all-symbols ((var &optional
175                                          result-form)
176                                     &body body-decls)
177   #!+sb-doc
178   "DO-ALL-SYMBOLS (VAR [RESULT-FORM]) {DECLARATION}* {TAG | FORM}*
179    Executes the FORMs once for each symbol in every package with VAR bound
180    to the current symbol."
181   (multiple-value-bind (body decls)
182       (parse-body body-decls :doc-string-allowed nil)
183     (let ((flet-name (gensym "DO-SYMBOLS-")))
184       `(block nil
185          (flet ((,flet-name (,var)
186                   ,@decls
187                   (tagbody ,@body)))
188            (dolist (package (list-all-packages))
189              (flet ((iterate-over-hash-table (table)
190                       (let ((hash-vec (package-hashtable-hash table))
191                             (sym-vec (package-hashtable-table table)))
192                         (dotimes (i (length sym-vec))
193                           (when (>= (aref hash-vec i) 2)
194                             (,flet-name (aref sym-vec i)))))))
195                (iterate-over-hash-table (package-internal-symbols package))
196                (iterate-over-hash-table (package-external-symbols package)))))
197          (let ((,var nil))
198            (declare (ignorable ,var))
199            ,@decls
200            ,result-form)))))
201 \f
202 ;;;; WITH-PACKAGE-ITERATOR
203
204 (defmacro-mundanely with-package-iterator ((mname package-list
205                                                   &rest symbol-types)
206                                            &body body)
207   #!+sb-doc
208   "Within the lexical scope of the body forms, MNAME is defined via macrolet
209    such that successive invocations of (MNAME) will return the symbols,
210    one by one, from the packages in PACKAGE-LIST. SYMBOL-TYPES may be
211    any of :INHERITED :EXTERNAL :INTERNAL."
212   (let* ((packages (gensym))
213          (these-packages (gensym))
214          (ordered-types (let ((res nil))
215                           (dolist (kind '(:inherited :external :internal)
216                                         res)
217                             (when (member kind symbol-types)
218                               (push kind res)))))  ; Order SYMBOL-TYPES.
219          (counter (gensym))
220          (kind (gensym))
221          (hash-vector (gensym))
222          (vector (gensym))
223          (package-use-list (gensym))
224          (init-macro (gensym))
225          (end-test-macro (gensym))
226          (real-symbol-p (gensym))
227          (inherited-symbol-p (gensym))
228          (BLOCK (gensym)))
229     `(let* ((,these-packages ,package-list)
230             (,packages `,(mapcar (lambda (package)
231                                    (if (packagep package)
232                                        package
233                                        ;; Maybe FIND-PACKAGE-OR-DIE?
234                                        (or (find-package package)
235                                            (error 'simple-package-error
236                                                   ;; could be a character
237                                                   :name (string package)
238                                                   :format-control "~@<~S does not name a package ~:>"
239                                                   :format-arguments (list package)))))
240                                  (if (consp ,these-packages)
241                                      ,these-packages
242                                      (list ,these-packages))))
243             (,counter nil)
244             (,kind (car ,packages))
245             (,hash-vector nil)
246             (,vector nil)
247             (,package-use-list nil))
248        ,(if (member :inherited ordered-types)
249             `(setf ,package-use-list (package-%use-list (car ,packages)))
250             `(declare (ignore ,package-use-list)))
251        (macrolet ((,init-macro (next-kind)
252          (declare (optimize (inhibit-warnings 3)))
253          (let ((symbols (gensym)))
254            `(progn
255               (setf ,',kind ,next-kind)
256               (setf ,',counter nil)
257               ,(case next-kind
258                  (:internal
259                   `(let ((,symbols (package-internal-symbols
260                                     (car ,',packages))))
261                      (when ,symbols
262                        (setf ,',vector (package-hashtable-table ,symbols))
263                        (setf ,',hash-vector
264                              (package-hashtable-hash ,symbols)))))
265                  (:external
266                   `(let ((,symbols (package-external-symbols
267                                     (car ,',packages))))
268                      (when ,symbols
269                        (setf ,',vector (package-hashtable-table ,symbols))
270                        (setf ,',hash-vector
271                              (package-hashtable-hash ,symbols)))))
272                  (:inherited
273                   `(let ((,symbols (and ,',package-use-list
274                                         (package-external-symbols
275                                          (car ,',package-use-list)))))
276                      (when ,symbols
277                        (setf ,',vector (package-hashtable-table ,symbols))
278                        (setf ,',hash-vector
279                              (package-hashtable-hash ,symbols)))))))))
280                   (,end-test-macro (this-kind)
281                      `,(let ((next-kind (cadr (member this-kind
282                                                       ',ordered-types))))
283                          (if next-kind
284                              `(,',init-macro ,next-kind)
285                              `(if (endp (setf ,',packages (cdr ,',packages)))
286                                   (return-from ,',BLOCK)
287                                   (,',init-macro ,(car ',ordered-types)))))))
288          (when ,packages
289            ,(when (null symbol-types)
290               (error 'simple-program-error
291                      :format-control
292                      "At least one of :INTERNAL, :EXTERNAL, or ~
293                       :INHERITED must be supplied."))
294            ,(dolist (symbol symbol-types)
295               (unless (member symbol '(:internal :external :inherited))
296                 (error 'program-error
297                        :format-control
298                        "~S is not one of :INTERNAL, :EXTERNAL, or :INHERITED."
299                        :format-argument symbol)))
300            (,init-macro ,(car ordered-types))
301            (flet ((,real-symbol-p (number)
302                     (> number 1)))
303              (macrolet ((,mname ()
304               (declare (optimize (inhibit-warnings 3)))
305               `(block ,',BLOCK
306                  (loop
307                    (case ,',kind
308                      ,@(when (member :internal ',ordered-types)
309                          `((:internal
310                             (setf ,',counter
311                                   (position-if #',',real-symbol-p
312                                                (the hash-vector ,',hash-vector)
313                                                :start (if ,',counter
314                                                           (1+ ,',counter)
315                                                           0)))
316                             (if ,',counter
317                                 (return-from ,',BLOCK
318                                  (values t (svref ,',vector ,',counter)
319                                          ,',kind (car ,',packages)))
320                                 (,',end-test-macro :internal)))))
321                      ,@(when (member :external ',ordered-types)
322                          `((:external
323                             (setf ,',counter
324                                   (position-if #',',real-symbol-p
325                                                (the hash-vector ,',hash-vector)
326                                                :start (if ,',counter
327                                                           (1+ ,',counter)
328                                                           0)))
329                             (if ,',counter
330                                 (return-from ,',BLOCK
331                                  (values t (svref ,',vector ,',counter)
332                                          ,',kind (car ,',packages)))
333                                 (,',end-test-macro :external)))))
334                      ,@(when (member :inherited ',ordered-types)
335                          `((:inherited
336                             (flet ((,',inherited-symbol-p (number)
337                                      (when (,',real-symbol-p number)
338                                        (let* ((p (position
339                                                   number
340                                                   (the hash-vector
341                                                     ,',hash-vector)
342                                                   :start (if ,',counter
343                                                              (1+ ,',counter)
344                                                              0)))
345                                               (s (svref ,',vector p)))
346                                          (eql (nth-value
347                                                1 (find-symbol
348                                                   (symbol-name s)
349                                                   (car ,',packages)))
350                                               :inherited)))))
351                               (setf ,',counter
352                                     (when ,',hash-vector
353                                       (position-if #',',inherited-symbol-p
354                                                    (the hash-vector
355                                                      ,',hash-vector)
356                                                    :start (if ,',counter
357                                                               (1+ ,',counter)
358                                                               0)))))
359                             (cond (,',counter
360                                    (return-from
361                                     ,',BLOCK
362                                     (values t (svref ,',vector ,',counter)
363                                             ,',kind (car ,',packages))
364                                     ))
365                                   (t
366                                    (setf ,',package-use-list
367                                          (cdr ,',package-use-list))
368                                    (cond ((endp ,',package-use-list)
369                                           (setf ,',packages (cdr ,',packages))
370                                           (when (endp ,',packages)
371                                             (return-from ,',BLOCK))
372                                           (setf ,',package-use-list
373                                                 (package-%use-list
374                                                  (car ,',packages)))
375                                           (,',init-macro ,(car
376                                                            ',ordered-types)))
377                                          (t (,',init-macro :inherited)
378                                             (setf ,',counter nil)))))))))))))
379                ,@body)))))))