1 ;;;; the DEFPACKAGE macro
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;; the list of packages to use by default when no :USE argument is
15 ;;; supplied to MAKE-PACKAGE or other package creation forms
17 ;;; ANSI specifies (1) that MAKE-PACKAGE and DEFPACKAGE use the same
18 ;;; value, and (2) that it (as an implementation-defined value) should
19 ;;; be documented, which we do in the doc string. So for OAOO reasons
20 ;;; we represent this value as a variable only at compile time, and
21 ;;; then use #. readmacro hacks to splice it into the target code as a
23 (eval-when (:compile-toplevel)
24 (defparameter *default-package-use-list*
25 ;; ANSI says this is implementation-defined. So we make it NIL,
26 ;; the way God intended. Anyone who actually wants a random value
27 ;; is free to :USE (PACKAGE-USE-LIST :CL-USER) anyway.:-|
30 (defmacro defpackage (package &rest options)
33 "Defines a new package called PACKAGE. Each of OPTIONS should be one of the
34 following: ~{~&~4T~A~}
35 All options except ~{~A, ~}and :DOCUMENTATION can be used multiple
37 '((:use "{package-name}*")
38 (:export "{symbol-name}*")
39 (:import-from "<package-name> {symbol-name}*")
40 (:shadow "{symbol-name}*")
41 (:shadowing-import-from "<package-name> {symbol-name}*")
42 (:local-nicknames "{local-nickname actual-package-name}*")
43 #!+sb-package-locks (:lock "boolean")
44 #!+sb-package-locks (:implement "{package-name}*")
45 (:documentation "doc-string")
46 (:intern "{symbol-name}*")
48 (:nicknames "{package-name}*"))
49 '(:size #!+sb-package-locks :lock))
54 (shadowing-imports nil)
60 (implement (stringify-package-designators (list package)))
65 (declare (ignore implement-p))
66 (dolist (option options)
67 (unless (consp option)
68 (error 'simple-program-error
69 :format-control "bogus DEFPACKAGE option: ~S"
70 :format-arguments (list option)))
73 (setf nicknames (stringify-package-designators (cdr option))))
76 (append local-nicknames
77 (mapcar (lambda (spec)
78 (destructuring-bind (nick name) spec
79 (cons (stringify-package-designator nick)
80 (stringify-package-designator name))))
84 (error 'simple-program-error
85 :format-control "can't specify :SIZE twice."))
86 ((and (consp (cdr option))
87 (typep (second option) 'unsigned-byte))
88 (setf size (second option)))
92 :format-control ":SIZE is not a positive integer: ~S"
93 :format-arguments (list (second option))))))
95 (let ((new (stringify-string-designators (cdr option))))
96 (setf shadows (append shadows new))))
97 (:shadowing-import-from
98 (let ((package-name (stringify-package-designator (second option)))
99 (names (stringify-string-designators (cddr option))))
100 (let ((assoc (assoc package-name shadowing-imports
103 (setf (cdr assoc) (append (cdr assoc) names))
104 (setf shadowing-imports
105 (acons package-name names shadowing-imports))))))
107 (setf use (append use (stringify-package-designators (cdr option)) )
110 (let ((package-name (stringify-package-designator (second option)))
111 (names (stringify-string-designators (cddr option))))
112 (let ((assoc (assoc package-name imports
115 (setf (cdr assoc) (append (cdr assoc) names))
116 (setf imports (acons package-name names imports))))))
118 (let ((new (stringify-string-designators (cdr option))))
119 (setf interns (append interns new))))
121 (let ((new (stringify-string-designators (cdr option))))
122 (setf exports (append exports new))))
126 (setf implement nil))
127 (let ((new (stringify-package-designators (cdr option))))
128 (setf implement (append implement new)
133 (error 'simple-program-error
134 :format-control "multiple :LOCK options"))
135 (setf lock (coerce (second option) 'boolean)))
138 (error 'simple-program-error
139 :format-control "multiple :DOCUMENTATION options"))
140 (setf doc (coerce (second option) 'simple-string)))
142 (error 'simple-program-error
143 :format-control "bogus DEFPACKAGE option: ~S"
144 :format-arguments (list option)))))
145 (check-disjoint `(:intern ,@interns) `(:export ,@exports))
146 (check-disjoint `(:intern ,@interns)
148 ,@(apply #'append (mapcar #'rest imports)))
150 `(:shadowing-import-from
151 ,@(apply #'append (mapcar #'rest shadowing-imports))))
152 `(eval-when (:compile-toplevel :load-toplevel :execute)
153 (%defpackage ,(stringify-string-designator package) ',nicknames ',size
154 ',shadows ',shadowing-imports ',(if use-p use :default)
155 ',imports ',interns ',exports ',implement ',local-nicknames
157 (sb!c:source-location)))))
159 (defun check-disjoint (&rest args)
160 ;; An arg is (:key . set)
161 (do ((list args (cdr list)))
166 for z = (remove-duplicates (intersection (cdr x)(cdr y) :test #'string=))
167 when z do (error 'simple-program-error
168 :format-control "Parameters ~S and ~S must be disjoint ~
169 but have common elements ~% ~S"
170 :format-arguments (list (car x)(car y) z)))))
172 (defun stringify-string-designator (string-designator)
173 (typecase string-designator
174 (simple-string string-designator)
175 (string (coerce string-designator 'simple-string))
176 (symbol (symbol-name string-designator))
177 (character (string string-designator))
179 (error "~S does not designate a string" string-designator))))
181 (defun stringify-string-designators (string-designators)
182 (mapcar #'stringify-string-designator string-designators))
184 (defun stringify-package-designator (package-designator)
185 (typecase package-designator
186 (simple-string package-designator)
187 (string (coerce package-designator 'simple-string))
188 (symbol (symbol-name package-designator))
189 (character (string package-designator))
190 (package (package-name package-designator))
192 (error "~S does not designate a package" package-designator))))
194 (defun stringify-package-designators (package-designators)
195 (mapcar #'stringify-package-designator package-designators))
197 (defun import-list-symbols (import-list)
199 (dolist (import import-list symbols)
200 (destructuring-bind (package-name &rest symbol-names)
202 (let ((package (find-undeleted-package-or-lose package-name)))
203 (mapcar (lambda (name)
204 (push (find-or-make-symbol name package) symbols))
207 (defun use-list-packages (package package-designators)
208 (cond ((listp package-designators)
209 (mapcar #'find-undeleted-package-or-lose package-designators))
211 ;; :default for an existing package means preserve the
213 (package-use-list package))
215 ;; :default for a new package is the *default-package-use-list*
216 '#.*default-package-use-list*)))
218 (defun update-package (package nicknames source-location
219 shadows shadowing-imports
222 exports implement local-nicknames
224 (declare #!-sb-package-locks
225 (ignore implement lock))
226 (%enter-new-nicknames package nicknames)
227 ;; 1. :shadow and :shadowing-import-from
229 ;; shadows is a list of strings, shadowing-imports is a list of symbols.
230 (shadow shadows package)
231 (shadowing-import shadowing-imports package)
234 ;; use is a list of package objects.
235 (use-package use package)
236 ;; 3. :import-from and :intern
238 ;; imports is a list of symbols. interns is a list of strings.
239 (import imports package)
240 (dolist (intern interns)
241 (intern intern package))
244 ;; exports is a list of strings
245 (export (mapcar (lambda (symbol-name) (intern symbol-name package))
248 ;; Everything was created: update metadata
249 (sb!c:with-source-location (source-location)
250 (setf (package-source-location package) source-location))
251 (setf (package-doc-string package) doc-string)
254 ;; Handle packages this is an implementation package of
255 (dolist (p implement)
256 (add-implementation-package package p))
258 (setf (package-lock package) lock))
259 ;; Local nicknames. Throw out the old ones.
260 (setf (package-%local-nicknames package) nil)
261 (dolist (spec local-nicknames)
262 (add-package-local-nickname (car spec) (cdr spec) package))
265 (declaim (type list *on-package-variance*))
266 (defvar *on-package-variance* '(:warn t)
267 "Specifies behavior when redefining a package using DEFPACKAGE and the
268 definition is in variance with the current state of the package.
270 The value should be of the form:
272 (:WARN [T | packages-names] :ERROR [T | package-names])
274 specifying which packages get which behaviour -- with T signifying the default unless
275 otherwise specified. If default is not specified, :WARN is used.
277 :WARN keeps as much state as possible and causes SBCL to signal a full warning.
279 :ERROR causes SBCL to signal an error when the variant DEFPACKAGE form is executed,
280 with restarts provided for user to specify what action should be taken.
284 (setf *on-package-variance* '(:warn (:swank :swank-backend) :error t))
286 specifies to signal a warning if SWANK package is in variance, and an error otherwise.")
288 (defun note-package-variance (&rest args &key package &allow-other-keys)
289 (let ((pname (package-name package)))
290 (destructuring-bind (&key warn error) *on-package-variance*
291 (let ((what (cond ((and (listp error) (member pname error :test #'string=))
293 ((and (listp warn) (member pname warn :test #'string=))
301 (apply #'error 'sb!kernel::package-at-variance-error args))
303 (apply #'warn 'sb!kernel::package-at-variance args)))))))
305 (defun update-package-with-variance (package name nicknames source-location
306 shadows shadowing-imports
310 implement local-nicknames
312 (unless (string= (the string (package-name package)) name)
313 (error 'simple-package-error
315 :format-control "~A is a nickname for the package ~A"
316 :format-arguments (list name (package-name name))))
317 (let ((no-longer-shadowed
318 (set-difference (package-%shadowing-symbols package)
319 (append shadows shadowing-imports)
321 (when no-longer-shadowed
323 (let ((*package* (find-package :keyword)))
324 (note-package-variance
325 :format-control "~A also shadows the following symbols:~% ~S"
326 :format-arguments (list name no-longer-shadowed)
329 :report "Stop shadowing them by uninterning them."
330 (dolist (sym no-longer-shadowed)
331 (unintern sym package)))
333 :report "Keep shadowing them."))))
334 (let ((no-longer-used (set-difference (package-use-list package) use)))
337 (note-package-variance
338 :format-control "~A also uses the following packages:~% ~A"
339 :format-arguments (list name (mapcar #'package-name no-longer-used))
342 :report "Stop using them."
343 (unuse-package no-longer-used package))
345 :report "Keep using them."))))
347 (do-external-symbols (s package)
348 (push s old-exports))
349 (let ((no-longer-exported (set-difference old-exports exports :test #'string=)))
350 (when no-longer-exported
352 (note-package-variance
353 :format-control "~A also exports the following symbols:~% ~S"
354 :format-arguments (list name no-longer-exported)
357 :report "Unexport them."
358 (unexport no-longer-exported package))
360 :report "Keep exporting them.")))))
361 (let ((old-implements
362 (set-difference (package-implements-list package)
363 (mapcar #'find-undeleted-package-or-lose implement))))
366 (note-package-variance
367 :format-control "~A is also an implementation package for:~% ~{~S~^~% ~}"
368 :format-arguments (list name old-implements)
371 :report "Stop being an implementation package for them."
372 (dolist (p old-implements)
373 (remove-implementation-package package p)))
375 :report "Keep exporting them."))))
376 (update-package package nicknames source-location
377 shadows shadowing-imports
378 use imports interns exports
379 implement local-nicknames
382 (defun %defpackage (name nicknames size shadows shadowing-imports
383 use imports interns exports implement local-nicknames
386 (declare (type simple-string name)
387 (type list nicknames shadows shadowing-imports
388 imports interns exports)
389 (type (or list (member :default)) use)
390 (type (or simple-string null) doc-string)
392 (ignore implement lock))
393 (with-package-graph ()
394 (let* ((existing-package (find-package name))
395 (use (use-list-packages existing-package use))
396 (shadowing-imports (import-list-symbols shadowing-imports))
397 (imports (import-list-symbols imports)))
399 (update-package-with-variance existing-package name
400 nicknames source-location
401 shadows shadowing-imports
402 use imports interns exports
403 implement local-nicknames
405 (let ((package (make-package name
407 :internal-symbols (or size 10)
408 :external-symbols (length exports))))
409 (update-package package
412 shadows shadowing-imports
413 use imports interns exports
414 implement local-nicknames
415 lock doc-string))))))
417 (defun find-or-make-symbol (name package)
418 (multiple-value-bind (symbol how) (find-symbol name package)
422 (with-simple-restart (continue "INTERN it.")
423 (error 'simple-package-error
425 :format-control "no symbol named ~S in ~S"
426 :format-arguments (list name (package-name package))))
427 (intern name package)))))