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