Use SB!IMPL as the implementation package for PARSE-BODY
[sbcl.git] / src / code / package.lisp
blobff97121d0fe0b0324463a3f585697233a6fd17b3
1 ;;;; that part of the CMU CL package.lisp file which can run on the
2 ;;;; cross-compilation host
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.
13 (in-package "SB!IMPL")
15 ;;;; the PACKAGE-HASHTABLE structure
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.
33 ;; FIXME: too vaguely named. Should be PACKAGE-HASH-VECTOR.
34 (def!type hash-vector () '(simple-array (unsigned-byte 8) (*)))
36 (def!struct (package-hashtable
37 (:constructor %make-package-hashtable
38 (cells size &aux (free size)))
39 (:copier nil))
40 ;; The general-vector of symbols, with a hash-vector in its last cell.
41 (cells (missing-arg) :type simple-vector)
42 ;; The total number of entries allowed before resizing.
44 ;; FIXME: CAPACITY would be a more descriptive name. (This is
45 ;; related to but not quite the same as HASH-TABLE-SIZE, so calling
46 ;; it SIZE seems somewhat misleading.)
47 (size (missing-arg) :type index)
48 ;; The remaining number of entries that can be made before we have to rehash.
49 (free (missing-arg) :type index)
50 ;; The number of deleted entries.
51 (deleted 0 :type index))
53 ;;;; the PACKAGE structure
55 ;;; Meta: IN-PACKAGE references a string constant, not a package.
56 ;;; But we need this to be a DEF!STRUCT to emit 'package.h'
57 ;;; during first genesis.
58 ;;; KLUDGE: We use DEF!STRUCT to define this not because we need to
59 ;;; manipulate target package objects on the cross-compilation host,
60 ;;; but only because its MAKE-LOAD-FORM function needs to be hooked
61 ;;; into the pre-CLOS DEF!STRUCT MAKE-LOAD-FORM system so that we can
62 ;;; compile things like IN-PACKAGE in warm init before CLOS is set up.
63 ;;; The DEF!STRUCT side effect of defining a new PACKAGE type on the
64 ;;; cross-compilation host is just a nuisance, and in order to avoid
65 ;;; breaking the cross-compilation host, we need to work around it
66 ;;; around by putting the new PACKAGE type (and the PACKAGEP predicate
67 ;;; too..) into SB!XC. -- WHN 20000309
68 (def!struct (sb!xc:package
69 (:constructor %make-package
70 (%name internal-symbols external-symbols))
71 (:make-load-form-fun (lambda (p)
72 (values `(find-undeleted-package-or-lose
73 ',(package-name p))
74 nil)))
75 (:predicate sb!xc:packagep))
76 #!+sb-doc
77 "the standard structure for the description of a package"
78 ;; the name of the package, or NIL for a deleted package
79 (%name nil :type (or simple-string null))
80 ;; nickname strings
81 (%nicknames () :type list)
82 ;; packages used by this package
83 (%use-list () :type list)
84 ;; a simple-vector of the external symbol hashtables for used packages.
85 ;; Derived from %USE-LIST, but maintained separately.
86 (tables #() :type simple-vector)
87 ;; index into TABLES of the table in which an inherited symbol was most
88 ;; recently found. On the next FIND-SYMBOL* operation, the indexed table
89 ;; is tested first.
90 (mru-table-index 0 :type index)
91 ;; packages that use this package
92 (%used-by-list () :type list)
93 ;; PACKAGE-HASHTABLEs of internal & external symbols
94 (internal-symbols nil :type package-hashtable)
95 (external-symbols nil :type package-hashtable)
96 ;; shadowing symbols
97 ;; Todo: dynamically changeover to a PACKAGE-HASHTABLE if list gets long
98 (%shadowing-symbols () :type list)
99 ;; documentation string for this package
100 (doc-string nil :type (or simple-string null))
101 ;; package locking
102 #!+sb-package-locks
103 (lock nil :type boolean)
104 #!+sb-package-locks
105 (%implementation-packages nil :type list)
106 ;; Definition source location
107 (source-location nil :type (or null sb!c:definition-source-location))
108 ;; Local package nicknames.
109 (%local-nicknames nil :type list)
110 (%locally-nicknamed-by nil :type list))
112 ;;;; iteration macros
114 (flet ((expand-iterator (range var body result-form)
115 (multiple-value-bind (forms decls)
116 (parse-body body :doc-string-allowed nil)
117 (with-unique-names (iterator winp next)
118 `(block nil
119 (with-package-iterator (,iterator ,@range)
120 (tagbody
121 ,next
122 (multiple-value-bind (,winp ,var) (,iterator)
123 ;; I don't think this VAR is automatically ignorable.
124 ;; The user should use it, or declare IGNORE/IGNORABLE.
125 ,@decls
126 (if ,winp
127 (tagbody ,@forms (go ,next))
128 (return ,result-form))))))))))
130 (defmacro-mundanely do-symbols ((var &optional
131 (package '*package*)
132 result-form)
133 &body body-decls)
134 #!+sb-doc
135 "DO-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECLARATION}* {TAG | FORM}*
136 Executes the FORMs at least once for each symbol accessible in the given
137 PACKAGE with VAR bound to the current symbol."
138 (expand-iterator `((find-undeleted-package-or-lose ,package)
139 :internal :external :inherited)
140 var body-decls result-form))
142 (defmacro-mundanely do-external-symbols ((var &optional
143 (package '*package*)
144 result-form)
145 &body body-decls)
146 #!+sb-doc
147 "DO-EXTERNAL-SYMBOLS (VAR [PACKAGE [RESULT-FORM]]) {DECL}* {TAG | FORM}*
148 Executes the FORMs once for each external symbol in the given PACKAGE with
149 VAR bound to the current symbol."
150 (expand-iterator `((find-undeleted-package-or-lose ,package) :external)
151 var body-decls result-form))
153 (defmacro-mundanely do-all-symbols ((var &optional
154 result-form)
155 &body body-decls)
156 #!+sb-doc
157 "DO-ALL-SYMBOLS (VAR [RESULT-FORM]) {DECLARATION}* {TAG | FORM}*
158 Executes the FORMs once for each symbol in every package with VAR bound
159 to the current symbol."
160 (expand-iterator '((list-all-packages) :internal :external)
161 var body-decls result-form)))
164 ;;;; WITH-PACKAGE-ITERATOR
166 (defun package-iter-init (access-types pkg-designator-list)
167 (declare (type (integer 1 7) access-types)) ; a nonzero bitmask over types
168 (values (logior (ash access-types 3) #b11) 0 #()
169 (package-listify pkg-designator-list)))
171 (declaim (inline pkg-symbol-valid-p))
172 (defun pkg-symbol-valid-p (x) (not (fixnump x)))
174 ;; The STATE parameter is comprised of 4 packed fields
175 ;; [0:1] = substate {0=internal,1=external,2=inherited,3=initial}
176 ;; [2] = package with inherited symbols has shadowing symbols
177 ;; [3:5] = enabling bits for {internal,external,inherited}
178 ;; [6:] = index into 'package-tables'
180 (defconstant +package-iter-check-shadows+ #b000100)
182 (defun package-iter-step (start-state index sym-vec pkglist)
183 ;; the defknown isn't enough
184 (declare (type fixnum start-state) (type index index)
185 (type simple-vector sym-vec) (type list pkglist))
186 (declare (optimize speed))
187 (labels
188 ((advance (state) ; STATE is the one just completed
189 (case (logand state #b11)
190 ;; Test :INHERITED first because the state repeats for a package
191 ;; as many times as there are packages it uses. There are enough
192 ;; bits to count up to 2^23 packages if fixnums are 30 bits.
194 (when (desired-state-p 2)
195 (let* ((tables (package-tables (this-package)))
196 (next-state (the fixnum (+ state (ash 1 6))))
197 (table-idx (ash next-state -6)))
198 (when (< table-idx (length tables))
199 (return-from advance ; remain in state 2
200 (start next-state (svref tables table-idx))))))
201 (pop pkglist)
202 (advance 3)) ; start on next package
203 (1 ; finished externals, switch to inherited if desired
204 (when (desired-state-p 2)
205 (let ((tables (package-tables (this-package))))
206 (when (plusp (length tables)) ; inherited symbols
207 (return-from advance ; enter state 2
208 (start (if (package-%shadowing-symbols (this-package))
209 (logior 2 +package-iter-check-shadows+) 2)
210 (svref tables 0))))))
211 (advance 2)) ; skip state 2
212 (0 ; finished internals, switch to externals if desired
213 (if (desired-state-p 1) ; enter state 1
214 (start 1 (package-external-symbols (this-package)))
215 (advance 1))) ; skip state 1
216 (t ; initial state
217 (cond ((endp pkglist) ; latch into returning NIL forever more
218 (values 0 0 #() '() nil nil))
219 ((desired-state-p 0) ; enter state 0
220 (start 0 (package-internal-symbols (this-package))))
221 (t (advance 0)))))) ; skip state 0
222 (desired-state-p (target-state)
223 (logtest start-state (ash 1 (+ target-state 3))))
224 (this-package ()
225 (truly-the sb!xc:package (car pkglist)))
226 (start (next-state new-table)
227 (let ((symbols (package-hashtable-cells new-table)))
228 (package-iter-step (logior (mask-field (byte 3 3) start-state)
229 next-state)
230 ;; assert that physical length was nonzero
231 (the index (1- (length symbols)))
232 symbols pkglist))))
233 (declare (inline desired-state-p this-package))
234 (if (zerop index)
235 (advance start-state)
236 (macrolet ((scan (&optional (guard t))
237 `(loop
238 (let ((sym (aref sym-vec (decf index))))
239 (when (and (pkg-symbol-valid-p sym) ,guard)
240 (return (values start-state index sym-vec pkglist sym
241 (aref #(:internal :external :inherited)
242 (logand start-state 3))))))
243 (when (zerop index)
244 (return (advance start-state))))))
245 (declare #-sb-xc-host(optimize (sb!c::insert-array-bounds-checks 0)))
246 (if (logtest start-state +package-iter-check-shadows+)
247 (let ((shadows (package-%shadowing-symbols (this-package))))
248 (scan (not (member sym shadows :test #'string=))))
249 (scan))))))
251 (defmacro-mundanely with-package-iterator ((mname package-list
252 &rest symbol-types)
253 &body body)
254 #!+sb-doc
255 "Within the lexical scope of the body forms, MNAME is defined via macrolet
256 such that successive invocations of (MNAME) will return the symbols, one by
257 one, from the packages in PACKAGE-LIST. SYMBOL-TYPES may be any
258 of :INHERITED :EXTERNAL :INTERNAL."
259 ;; SYMBOL-TYPES should really be named ACCESSIBILITY-TYPES.
260 (when (null symbol-types)
261 (error 'simple-program-error
262 :format-control
263 "At least one of :INTERNAL, :EXTERNAL, or :INHERITED must be supplied."))
264 (dolist (symbol symbol-types)
265 (unless (member symbol '(:internal :external :inherited))
266 (error 'simple-program-error
267 :format-control
268 "~S is not one of :INTERNAL, :EXTERNAL, or :INHERITED."
269 :format-arguments (list symbol))))
270 (with-unique-names (bits index sym-vec pkglist symbol kind)
271 (let ((state (list bits index sym-vec pkglist))
272 (select (logior (if (member :internal symbol-types) 1 0)
273 (if (member :external symbol-types) 2 0)
274 (if (member :inherited symbol-types) 4 0))))
275 `(multiple-value-bind ,state (package-iter-init ,select ,package-list)
276 (let (,symbol ,kind)
277 (macrolet
278 ((,mname ()
279 '(if (eql 0 (multiple-value-setq (,@state ,symbol ,kind)
280 (package-iter-step ,@state)))
282 (values t ,symbol ,kind
283 (car (truly-the list ,pkglist))))))
284 ,@body))))))
286 (defmacro-mundanely with-package-graph ((&key) &body forms)
287 `(flet ((thunk () ,@forms))
288 (declare (dynamic-extent #'thunk))
289 (call-with-package-graph #'thunk)))