package.lisp 42.3 KB
Newer Older
1
2
3
4
5
6
;;;; ---------------------------------------------------------------------------
;;;; Handle ASDF package upgrade, including implementation-dependent magic.
;;
;; See https://bugs.launchpad.net/asdf/+bug/485687
;;

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
;; CAUTION: The definition of the UIOP/PACKAGE package MUST NOT CHANGE,
;; NOT NOW, NOT EVER, NOT UNDER ANY CIRCUMSTANCE. NEVER.
;; ... and the same goes for UIOP/PACKAGE-LOCAL-NICKNAMES.
;;
;; The entire point of UIOP/PACKAGE is to address the fact that the CL standard
;; *leaves it unspecified what happens when a package is redefined incompatibly*.
;; For instance, SBCL 1.4.2 will signal a full WARNING when this happens,
;; throwing a wrench in upgrading code with ASDF itself, while continuing to
;; export old symbols it now shouldn't as it also exports new ones,
;; causing problems with code that relies on the new/current exports.
;; CLISP and CCL also exports both sets of symbols, though without any WARNING.
;; ABCL 1.6.1 will plainly ignore the new definition.
;; Other implementations may do whatever they want and change their behavior at any time.
;; ***Using DEFPACKAGE twice with different definitions is nasal-demon territory.***
;;
;; Thus we define UIOP/PACKAGE:DEFINE-PACKAGE with which packages can be defined
;; in an upgrade-friendly way: the new definition is authoritative, and
;; the package will define and export exactly those symbols in the new definition,
;; no more and no fewer, whereas it is well-defined what happens to previous symbols.
;; However, for obvious bootstrap reasons, we cannot use DEFINE-PACKAGE
;; to define UIOP/PACKAGE itself, only DEFPACKAGE.
;; Therefore, unlike the other packages in ASDF, UIOP/PACKAGE is immutable,
;; now and forever. It is frozen for the aeons to come, like the CL package itself,
;; to the same exact state it was defined at its inception, in ASDF 2.27 in 2013.
;; The same goes for UIOP/PACKAGE-LOCAL-NICKNAMES, that we use internally.
;;
;; If you ever must define new symbols in this file, you can and must
;; export them from a different package, possibly defined in the same file,
;; say a package UIOP/PACKAGE* defined at the end of this file with DEFINE-PACKAGE,
;; that might use :import-from to import the symbols from UIOP/PACKAGE,
;; if you must somehow define them in UIOP/PACKAGE.
38

39
(defpackage :uiop/package ;;; THOU SHALT NOT modify this definition, EVER. See explanations above.
40
41
  (:use :common-lisp)
  (:export
42
   #:find-package* #:find-symbol* #:symbol-call
43
   #:intern* #:export* #:import* #:shadowing-import* #:shadow* #:make-symbol* #:unintern*
44
   #:symbol-shadowing-p #:home-package-p
45
46
   #:symbol-package-name #:standard-common-lisp-symbol-p
   #:reify-package #:unreify-package #:reify-symbol #:unreify-symbol
47
   #:nuke-symbol-in-package #:nuke-symbol #:rehome-symbol
48
   #:ensure-package-unused #:delete-package*
49
   #:package-names #:packages-from-names #:fresh-package-name #:rename-package-away
50
   #:package-definition-form #:parse-define-package-form
51
52
   #:ensure-package #:define-package
   ))
53

54
(in-package :uiop/package)
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
;;; package local nicknames feature.
;;; This can't be deferred until common-lisp.lisp, where most such features are set.
;;; ABCL and CCL already define this feature appropriately.
;;; Seems to be unconditionally present for SBCL, ACL, and CLASP
;;; Don't know about ECL, or others
(eval-when (:load-toplevel :compile-toplevel :execute)
  ;; ABCL pushes :package-local-nicknames without UIOP interfering,
  ;; and Lispworks will do so
  #+(or sbcl clasp)
  (pushnew :package-local-nicknames *features*)
  #+allegro
  (let ((fname (find-symbol (symbol-name '#:add-package-local-nickname) '#:excl)))
    (when (and fname (fboundp fname))
      (pushnew :package-local-nicknames *features*))))

;;; THOU SHALT NOT modify this definition, EVER, *EXCEPT* to add a new implementation.
;; If you somehow need to modify the API in any way,
;; you will need to create another, differently named, and just as immutable package.
#+package-local-nicknames
(defpackage :uiop/package-local-nicknames
  (:use :cl)
  (:import-from
   #+allegro #:excl
   #+sbcl #:sb-ext
   #+(or clasp abcl ecl) #:ext
   #+ccl #:ccl
   #+lispworks #:hcl
   #-(or allegro sbcl clasp abcl ccl lispworks ecl)
   (error "Don't know from which package this lisp supplies the local-package-nicknames API.")
   #:remove-package-local-nickname #:package-local-nicknames #:add-package-local-nickname)
  (:export
   #:add-package-local-nickname #:remove-package-local-nickname #:package-local-nicknames))

89
90
91
;;;; General purpose package utilities

(eval-when (:load-toplevel :compile-toplevel :execute)
92
93
94
95
96
97
98
  (defun find-package* (package-designator &optional (error t))
    (let ((package (find-package package-designator)))
      (cond
        (package package)
        (error (error "No package named ~S" (string package-designator)))
        (t nil))))
  (defun find-symbol* (name package-designator &optional (error t))
99
    "Find a symbol in a package of given string'ified NAME;
100
unlike CL:FIND-SYMBOL, work well with 'modern' case sensitive syntax
101
102
103
104
by letting you supply a symbol or keyword for the name;
also works well when the package is not present.
If optional ERROR argument is NIL, return NIL instead of an error
when the symbol is not found."
105
106
    (block nil
      (let ((package (find-package* package-designator error)))
107
        (when package ;; package error handled by find-package* already
108
109
110
111
112
          (multiple-value-bind (symbol status) (find-symbol (string name) package)
            (cond
              (status (return (values symbol status)))
              (error (error "There is no symbol ~S in package ~S" name (package-name package))))))
        (values nil nil))))
113
114
115
116
117
  (defun symbol-call (package name &rest args)
    "Call a function associated with symbol of given name in given package,
with given ARGS. Useful when the call is read before the package is loaded,
or when loading the package is optional."
    (apply (find-symbol* name package) args))
118
  (defun intern* (name package-designator &optional (error t))
119
    (intern (string name) (find-package* package-designator error)))
120
121
122
123
  (defun export* (name package-designator)
    (let* ((package (find-package* package-designator))
           (symbol (intern* name package)))
      (export (or symbol (list symbol)) package)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
124
125
126
127
128
  (defun import* (symbol package-designator)
    (import (or symbol (list symbol)) (find-package* package-designator)))
  (defun shadowing-import* (symbol package-designator)
    (shadowing-import (or symbol (list symbol)) (find-package* package-designator)))
  (defun shadow* (name package-designator)
129
    (shadow (list (string name)) (find-package* package-designator)))
130
131
132
133
  (defun make-symbol* (name)
    (etypecase name
      (string (make-symbol name))
      (symbol (copy-symbol name))))
134
135
136
137
138
139
140
141
142
143
144
  (defun unintern* (name package-designator &optional (error t))
    (block nil
      (let ((package (find-package* package-designator error)))
        (when package
          (multiple-value-bind (symbol status) (find-symbol* name package error)
            (cond
              (status (unintern symbol package)
                      (return (values symbol status)))
              (error (error "symbol ~A not present in package ~A"
                            (string symbol) (package-name package))))))
        (values nil nil))))
145
  (defun symbol-shadowing-p (symbol package)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
146
    (and (member symbol (package-shadowing-symbols package)) t))
147
  (defun home-package-p (symbol package)
148
149
150
151
    (and package (let ((sp (symbol-package symbol)))
                   (and sp (let ((pp (find-package* package)))
                             (and pp (eq sp pp))))))))

152
153

(eval-when (:load-toplevel :compile-toplevel :execute)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
154
155
156
  (defun symbol-package-name (symbol)
    (let ((package (symbol-package symbol)))
      (and package (package-name package))))
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  (defun standard-common-lisp-symbol-p (symbol)
    (multiple-value-bind (sym status) (find-symbol* symbol :common-lisp nil)
      (and (eq sym symbol) (eq status :external))))
  (defun reify-package (package &optional package-context)
    (if (eq package package-context) t
        (etypecase package
          (null nil)
          ((eql (find-package :cl)) :cl)
          (package (package-name package)))))
  (defun unreify-package (package &optional package-context)
    (etypecase package
      (null nil)
      ((eql t) package-context)
      ((or symbol string) (find-package package))))
  (defun reify-symbol (symbol &optional package-context)
    (etypecase symbol
      ((or keyword (satisfies standard-common-lisp-symbol-p)) symbol)
      (symbol (vector (symbol-name symbol)
                      (reify-package (symbol-package symbol) package-context)))))
  (defun unreify-symbol (symbol &optional package-context)
    (etypecase symbol
      (symbol symbol)
      ((simple-vector 2)
       (let* ((symbol-name (svref symbol 0))
              (package-foo (svref symbol 1))
              (package (unreify-package package-foo package-context)))
183
184
         (if package (intern* symbol-name package)
             (make-symbol* symbol-name)))))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
185

186
187
188
189
190
(eval-when (:load-toplevel :compile-toplevel :execute)
  (defvar *all-package-happiness* '())
  (defvar *all-package-fishiness* (list t))
  (defun record-fishy (info)
    ;;(format t "~&FISHY: ~S~%" info)
191
    (push info *all-package-fishiness*))
192
193
194
195
196
  (defmacro when-package-fishiness (&body body)
    `(when *all-package-fishiness* ,@body))
  (defmacro note-package-fishiness (&rest info)
    `(when-package-fishiness (record-fishy (list ,@info)))))

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
197
(eval-when (:load-toplevel :compile-toplevel :execute)
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  #+(or clisp clozure)
  (defun get-setf-function-symbol (symbol)
    #+clisp (let ((sym (get symbol 'system::setf-function)))
              (if sym (values sym :setf-function)
                  (let ((sym (get symbol 'system::setf-expander)))
                    (if sym (values sym :setf-expander)
                        (values nil nil)))))
    #+clozure (gethash symbol ccl::%setf-function-names%))
  #+(or clisp clozure)
  (defun set-setf-function-symbol (new-setf-symbol symbol &optional kind)
    #+clisp (assert (member kind '(:setf-function :setf-expander)))
    #+clozure (assert (eq kind t))
    #+clisp
    (cond
      ((null new-setf-symbol)
       (remprop symbol 'system::setf-function)
       (remprop symbol 'system::setf-expander))
      ((eq kind :setf-function)
       (setf (get symbol 'system::setf-function) new-setf-symbol))
      ((eq kind :setf-expander)
218
219
220
       (setf (get symbol 'system::setf-expander) new-setf-symbol))
      (t (error "invalid kind of setf-function ~S for ~S to be set to ~S"
                kind symbol new-setf-symbol)))
221
222
223
224
225
226
227
228
    #+clozure
    (progn
      (gethash symbol ccl::%setf-function-names%) new-setf-symbol
      (gethash new-setf-symbol ccl::%setf-function-name-inverses%) symbol))
  #+(or clisp clozure)
  (defun create-setf-function-symbol (symbol)
    #+clisp (system::setf-symbol symbol)
    #+clozure (ccl::construct-setf-function-name symbol))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  (defun set-dummy-symbol (symbol reason other-symbol)
    (setf (get symbol 'dummy-symbol) (cons reason other-symbol)))
  (defun make-dummy-symbol (symbol)
    (let ((dummy (copy-symbol symbol)))
      (set-dummy-symbol dummy 'replacing symbol)
      (set-dummy-symbol symbol 'replaced-by dummy)
      dummy))
  (defun dummy-symbol (symbol)
    (get symbol 'dummy-symbol))
  (defun get-dummy-symbol (symbol)
    (let ((existing (dummy-symbol symbol)))
      (if existing (values (cdr existing) (car existing))
          (make-dummy-symbol symbol))))
  (defun nuke-symbol-in-package (symbol package-designator)
    (let ((package (find-package* package-designator))
          (name (symbol-name symbol)))
      (multiple-value-bind (sym stat) (find-symbol name package)
        (when (and (member stat '(:internal :external)) (eq symbol sym))
          (if (symbol-shadowing-p symbol package)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
248
249
              (shadowing-import* (get-dummy-symbol symbol) package)
              (unintern* symbol package))))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
250
251
252
253
254
255
  (defun nuke-symbol (symbol &optional (packages (list-all-packages)))
    #+(or clisp clozure)
    (multiple-value-bind (setf-symbol kind)
        (get-setf-function-symbol symbol)
      (when kind (nuke-symbol setf-symbol)))
    (loop :for p :in packages :do (nuke-symbol-in-package symbol p)))
256
257
258
259
260
261
262
263
264
265
266
267
  (defun rehome-symbol (symbol package-designator)
    "Changes the home package of a symbol, also leaving it present in its old home if any"
    (let* ((name (symbol-name symbol))
           (package (find-package* package-designator))
           (old-package (symbol-package symbol))
           (old-status (and old-package (nth-value 1 (find-symbol name old-package))))
           (shadowing (and old-package (symbol-shadowing-p symbol old-package) (make-symbol name))))
      (multiple-value-bind (overwritten-symbol overwritten-symbol-status) (find-symbol name package)
        (unless (eq package old-package)
          (let ((overwritten-symbol-shadowing-p
                  (and overwritten-symbol-status
                       (symbol-shadowing-p overwritten-symbol package))))
268
            (note-package-fishiness
269
270
271
             :rehome-symbol name
             (when old-package (package-name old-package)) old-status (and shadowing t)
             (package-name package) overwritten-symbol-status overwritten-symbol-shadowing-p)
272
273
            (when old-package
              (if shadowing
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
274
275
                  (shadowing-import* shadowing old-package))
              (unintern* symbol old-package))
276
277
            (cond
              (overwritten-symbol-shadowing-p
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
278
               (shadowing-import* symbol package))
279
280
              (t
               (when overwritten-symbol-status
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
281
282
                 (unintern* overwritten-symbol package))
               (import* symbol package)))
283
            (if shadowing
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
284
285
                (shadowing-import* symbol old-package)
                (import* symbol old-package))
286
287
288
289
290
291
            #+(or clisp clozure)
            (multiple-value-bind (setf-symbol kind)
                (get-setf-function-symbol symbol)
              (when kind
                (let* ((setf-function (fdefinition setf-symbol))
                       (new-setf-symbol (create-setf-function-symbol symbol)))
292
293
294
295
296
297
                  (note-package-fishiness
                   :setf-function
                   name (package-name package)
                   (symbol-name setf-symbol) (symbol-package-name setf-symbol)
                   (symbol-name new-setf-symbol) (symbol-package-name new-setf-symbol))
                  (when (symbol-package setf-symbol)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
298
                    (unintern* setf-symbol (symbol-package setf-symbol)))
299
                  (setf (fdefinition new-setf-symbol) setf-function)
300
                  (set-setf-function-symbol new-setf-symbol symbol kind))))
301
            #+(or clisp clozure)
302
            (multiple-value-bind (overwritten-setf foundp)
303
                (get-setf-function-symbol overwritten-symbol)
304
305
306
              (when foundp
                (unintern overwritten-setf)))
            (when (eq old-status :external)
307
              (export* symbol old-package))
308
            (when (eq overwritten-symbol-status :external)
309
              (export* symbol package))))
310
        (values overwritten-symbol overwritten-symbol-status))))
311
312
313
  (defun ensure-package-unused (package)
    (loop :for p :in (package-used-by-list package) :do
      (unuse-package package p)))
314
  (defun delete-package* (package &key nuke)
315
316
    (let ((p (find-package package)))
      (when p
317
        (when nuke (do-symbols (s p) (when (home-package-p s p) (nuke-symbol s))))
318
319
        (ensure-package-unused p)
        (delete-package package))))
320
321
322
323
  (defun package-names (package)
    (cons (package-name package) (package-nicknames package)))
  (defun packages-from-names (names)
    (remove-duplicates (remove nil (mapcar #'find-package names)) :from-end t))
324
325
  (defun fresh-package-name (&key (prefix :%TO-BE-DELETED)
                               separator
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
326
327
                               (index (random most-positive-fixnum)))
    (loop :for i :from index
328
          :for n = (format nil "~A~@[~A~D~]" prefix (and (plusp i) (or separator "")) i)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
329
          :thereis (and (not (find-package n)) n)))
330
  (defun rename-package-away (p &rest keys &key prefix &allow-other-keys)
331
332
333
334
335
    (let ((new-name
            (apply 'fresh-package-name
                   :prefix (or prefix (format nil "__~A__" (package-name p))) keys)))
      (record-fishy (list :rename-away (package-names p) new-name))
      (rename-package p new-name))))
336
337
338
339
340


;;; Communicable representation of symbol and package information

(eval-when (:load-toplevel :compile-toplevel :execute)
341
342
343
344
345
346
  (defun package-definition-form (package-designator
                                  &key (nicknamesp t) (usep t)
                                    (shadowp t) (shadowing-import-p t)
                                    (exportp t) (importp t) internp (error t))
    (let* ((package (or (find-package* package-designator error)
                        (return-from package-definition-form nil)))
347
348
349
350
351
352
353
354
           (name (package-name package))
           (nicknames (package-nicknames package))
           (use (mapcar #'package-name (package-use-list package)))
           (shadow ())
           (shadowing-import (make-hash-table :test 'equal))
           (import (make-hash-table :test 'equal))
           (export ())
           (intern ()))
355
      (when package
356
357
358
359
360
361
362
363
364
365
366
367
        (loop :for sym :being :the :symbols :in package
              :for status = (nth-value 1 (find-symbol* sym package)) :do
                (ecase status
                  ((nil :inherited))
                  ((:internal :external)
                   (let* ((name (symbol-name sym))
                          (external (eq status :external))
                          (home (symbol-package sym))
                          (home-name (package-name home))
                          (imported (not (eq home package)))
                          (shadowing (symbol-shadowing-p sym package)))
                     (cond
368
                       ((and shadowing imported)
369
370
371
372
373
374
375
376
377
378
379
                        (push name (gethash home-name shadowing-import)))
                       (shadowing
                        (push name shadow))
                       (imported
                        (push name (gethash home-name import))))
                     (cond
                       (external
                        (push name export))
                       (imported)
                       (t (push name intern)))))))
        (labels ((sort-names (names)
380
                   (sort (copy-list names) #'string<))
381
382
383
384
385
386
387
388
                 (table-keys (table)
                   (loop :for k :being :the :hash-keys :of table :collect k))
                 (when-relevant (key value)
                   (when value (list (cons key value))))
                 (import-options (key table)
                   (loop :for i :in (sort-names (table-keys table))
                         :collect `(,key ,i ,@(sort-names (gethash i table))))))
          `(defpackage ,name
389
             ,@(when-relevant :nicknames (and nicknamesp (sort-names nicknames)))
390
391
392
393
394
             (:use ,@(and usep (sort-names use)))
             ,@(when-relevant :shadow (and shadowp (sort-names shadow)))
             ,@(import-options :shadowing-import-from (and shadowing-import-p shadowing-import))
             ,@(import-options :import-from (and importp import))
             ,@(when-relevant :export (and exportp (sort-names export)))
395
             ,@(when-relevant :intern (and internp (sort-names intern)))))))))
396

397
398

;;; ensure-package, define-package
399
(eval-when (:load-toplevel :compile-toplevel :execute)
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
  (defun ensure-shadowing-import (name to-package from-package shadowed imported)
    (check-type name string)
    (check-type to-package package)
    (check-type from-package package)
    (check-type shadowed hash-table)
    (check-type imported hash-table)
    (let ((import-me (find-symbol* name from-package)))
      (multiple-value-bind (existing status) (find-symbol name to-package)
        (cond
          ((gethash name shadowed)
           (unless (eq import-me existing)
             (error "Conflicting shadowings for ~A" name)))
          (t
           (setf (gethash name shadowed) t)
           (setf (gethash name imported) t)
           (unless (or (null status)
                       (and (member status '(:internal :external))
                            (eq existing import-me)
                            (symbol-shadowing-p existing to-package)))
             (note-package-fishiness
              :shadowing-import name
              (package-name from-package)
              (or (home-package-p import-me from-package) (symbol-package-name import-me))
              (package-name to-package) status
424
              (and status (or (home-package-p existing to-package) (symbol-package-name existing)))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
           (shadowing-import* import-me to-package))))))
  (defun ensure-imported (import-me into-package &optional from-package)
    (check-type import-me symbol)
    (check-type into-package package)
    (check-type from-package (or null package))
    (let ((name (symbol-name import-me)))
      (multiple-value-bind (existing status) (find-symbol name into-package)
        (cond
          ((not status)
           (import* import-me into-package))
          ((eq import-me existing))
          (t
           (let ((shadowing-p (symbol-shadowing-p existing into-package)))
             (note-package-fishiness
              :ensure-imported name
              (and from-package (package-name from-package))
              (or (home-package-p import-me from-package) (symbol-package-name import-me))
              (package-name into-package)
              status
              (and status (or (home-package-p existing into-package) (symbol-package-name existing)))
              shadowing-p)
             (cond
               ((or shadowing-p (eq status :inherited))
                (shadowing-import* import-me into-package))
               (t
                (unintern* existing into-package)
                (import* import-me into-package))))))))
    (values))
453
454
455
456
457
458
  (defun ensure-import (name to-package from-package shadowed imported)
    (check-type name string)
    (check-type to-package package)
    (check-type from-package package)
    (check-type shadowed hash-table)
    (check-type imported hash-table)
459
460
461
462
    (multiple-value-bind (import-me import-status) (find-symbol name from-package)
      (when (null import-status)
        (note-package-fishiness
         :import-uninterned name (package-name from-package) (package-name to-package))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
463
        (setf import-me (intern* name from-package)))
464
465
      (multiple-value-bind (existing status) (find-symbol name to-package)
        (cond
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
466
467
          ((and imported (gethash name imported))
           (unless (and status (eq import-me existing))
468
469
470
471
472
             (error "Can't import ~S from both ~S and ~S"
                    name (package-name (symbol-package existing)) (package-name from-package))))
          ((gethash name shadowed)
           (error "Can't both shadow ~S and import it from ~S" name (package-name from-package)))
          (t
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
473
474
           (setf (gethash name imported) t))))
      (ensure-imported import-me to-package from-package)))
475
476
477
478
479
  (defun ensure-inherited (name symbol to-package from-package mixp shadowed imported inherited)
    (check-type name string)
    (check-type symbol symbol)
    (check-type to-package package)
    (check-type from-package package)
480
    (check-type mixp (member nil t)) ; no cl:boolean on Genera
481
482
483
484
485
486
487
488
489
490
491
    (check-type shadowed hash-table)
    (check-type imported hash-table)
    (check-type inherited hash-table)
    (multiple-value-bind (existing status) (find-symbol name to-package)
      (let* ((sp (symbol-package symbol))
             (in (gethash name inherited))
             (xp (and status (symbol-package existing))))
        (when (null sp)
          (note-package-fishiness
           :import-uninterned name
           (package-name from-package) (package-name to-package) mixp)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
492
          (import* symbol from-package)
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
          (setf sp (package-name from-package)))
        (cond
          ((gethash name shadowed))
          (in
           (unless (equal sp (first in))
             (if mixp
                 (ensure-shadowing-import name to-package (second in) shadowed imported)
                 (error "Can't inherit ~S from ~S, it is inherited from ~S"
                        name (package-name sp) (package-name (first in))))))
          ((gethash name imported)
           (unless (eq symbol existing)
             (error "Can't inherit ~S from ~S, it is imported from ~S"
                    name (package-name sp) (package-name xp))))
          (t
           (setf (gethash name inherited) (list sp from-package))
           (when (and status (not (eq sp xp)))
             (let ((shadowing (symbol-shadowing-p existing to-package)))
               (note-package-fishiness
                :inherited name
                (package-name from-package)
                (or (home-package-p symbol from-package) (symbol-package-name symbol))
                (package-name to-package)
                (or (home-package-p existing to-package) (symbol-package-name existing)))
               (if shadowing (ensure-shadowing-import name to-package from-package shadowed imported)
                   (unintern* existing to-package)))))))))
  (defun ensure-mix (name symbol to-package from-package shadowed imported inherited)
    (check-type name string)
    (check-type symbol symbol)
    (check-type to-package package)
    (check-type from-package package)
    (check-type shadowed hash-table)
    (check-type imported hash-table)
    (check-type inherited hash-table)
    (unless (gethash name shadowed)
      (multiple-value-bind (existing status) (find-symbol name to-package)
        (let* ((sp (symbol-package symbol))
               (im (gethash name imported))
               (in (gethash name inherited)))
          (cond
            ((or (null status)
                 (and status (eq symbol existing))
                 (and in (eq sp (first in))))
             (ensure-inherited name symbol to-package from-package t shadowed imported inherited))
            (in
             (remhash name inherited)
             (ensure-shadowing-import name to-package (second in) shadowed imported))
            (im
540
             (error "Symbol ~S import from ~S~:[~; actually ~:[uninterned~;~:*from ~S~]~] conflicts with existing symbol in ~S~:[~; actually ~:[uninterned~;from ~:*~S~]~]"
541
542
543
544
545
546
                    name (package-name from-package)
                    (home-package-p symbol from-package) (symbol-package-name symbol)
                    (package-name to-package)
                    (home-package-p existing to-package) (symbol-package-name existing)))
            (t
             (ensure-inherited name symbol to-package from-package t shadowed imported inherited)))))))
547

548
  (defun recycle-symbol (name recycle exported)
549
550
551
552
553
554
    ;; Takes a symbol NAME (a string), a list of package designators for RECYCLE
    ;; packages, and a hash-table of names (strings) of symbols scheduled to be
    ;; EXPORTED from the package being defined. It returns two values, the
    ;; symbol found (if any, or else NIL), and a boolean flag indicating whether
    ;; a symbol was found. The caller (DEFINE-PACKAGE) will then do the
    ;; re-homing of the symbol, etc.
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
    (check-type name string)
    (check-type recycle list)
    (check-type exported hash-table)
    (when (gethash name exported) ;; don't bother recycling private symbols
      (let (recycled foundp)
        (dolist (r recycle (values recycled foundp))
          (multiple-value-bind (symbol status) (find-symbol name r)
            (when (and status (home-package-p symbol r))
              (cond
                (foundp
                 ;; (nuke-symbol symbol)) -- even simple variable names like O or C will do that.
                 (note-package-fishiness :recycled-duplicate name (package-name foundp) (package-name r)))
                (t
                 (setf recycled symbol foundp r)))))))))
  (defun symbol-recycled-p (sym recycle)
    (check-type sym symbol)
    (check-type recycle list)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
572
    (and (member (symbol-package sym) recycle) t))
573
574
575
  (defun ensure-symbol (name package intern recycle shadowed imported inherited exported)
    (check-type name string)
    (check-type package package)
576
    (check-type intern (member nil t)) ; no cl:boolean on Genera
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
    (check-type shadowed hash-table)
    (check-type imported hash-table)
    (check-type inherited hash-table)
    (unless (or (gethash name shadowed)
                (gethash name imported)
                (gethash name inherited))
      (multiple-value-bind (existing status)
          (find-symbol name package)
        (multiple-value-bind (recycled previous) (recycle-symbol name recycle exported)
          (cond
            ((and status (eq existing recycled) (eq previous package)))
            (previous
             (rehome-symbol recycled package))
            ((and status (eq package (symbol-package existing))))
            (t
             (when status
               (note-package-fishiness
                :ensure-symbol name
                (reify-package (symbol-package existing) package)
                status intern)
               (unintern existing))
             (when intern
               (intern* name package))))))))
600
  (declaim (ftype (function (t t t &optional t) t) ensure-exported))
601
602
603
604
605
  (defun ensure-exported-to-user (name symbol to-package &optional recycle)
    (check-type name string)
    (check-type symbol symbol)
    (check-type to-package package)
    (check-type recycle list)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
606
    (assert (equal name (symbol-name symbol)))
607
608
609
610
611
612
613
614
615
616
617
618
619
    (multiple-value-bind (existing status) (find-symbol name to-package)
      (unless (and status (eq symbol existing))
        (let ((accessible
                (or (null status)
                    (let ((shadowing (symbol-shadowing-p existing to-package))
                          (recycled (symbol-recycled-p existing recycle)))
                      (unless (and shadowing (not recycled))
                        (note-package-fishiness
                         :ensure-export name (symbol-package-name symbol)
                         (package-name to-package)
                         (or (home-package-p existing to-package) (symbol-package-name existing))
                         status shadowing)
                        (if (or (eq status :inherited) shadowing)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
620
                            (shadowing-import* symbol to-package)
621
622
623
624
625
626
627
                            (unintern existing to-package))
                        t)))))
          (when (and accessible (eq status :external))
            (ensure-exported name symbol to-package recycle))))))
  (defun ensure-exported (name symbol from-package &optional recycle)
    (dolist (to-package (package-used-by-list from-package))
      (ensure-exported-to-user name symbol to-package recycle))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
628
629
    (unless (eq from-package (symbol-package symbol))
      (ensure-imported symbol from-package))
630
631
632
633
634
    (export* name from-package))
  (defun ensure-export (name from-package &optional recycle)
    (multiple-value-bind (symbol status) (find-symbol* name from-package)
      (unless (eq status :external)
        (ensure-exported name symbol from-package recycle))))
635

636
637
638
  #+package-local-nicknames
  (defun install-package-local-nicknames (destination-package new-nicknames)
    ;; First, remove all package-local nicknames. (We'll reinstall any desired ones later.)
639
640
641
    (dolist (pair-to-remove (uiop/package-local-nicknames:package-local-nicknames destination-package))
      (uiop/package-local-nicknames:remove-package-local-nickname
       (string (car pair-to-remove)) destination-package))
642
643
    ;; Then, install all desired nicknames.
    (loop :for (nickname package) :in new-nicknames
644
645
646
647
          :do (uiop/package-local-nicknames:add-package-local-nickname
               (string nickname)
               (find-package package)
               destination-package)))
648

649
650
651
652
653
  (defun ensure-package (name &key
                                nicknames documentation use
                                shadow shadowing-import-from
                                import-from export intern
                                recycle mix reexport
654
                                unintern local-nicknames)
655
    #+genera (declare (ignore documentation))
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
    (let* ((package-name (string name))
           (nicknames (mapcar #'string nicknames))
           (names (cons package-name nicknames))
           (previous (packages-from-names names))
           (discarded (cdr previous))
           (to-delete ())
           (package (or (first previous) (make-package package-name :nicknames nicknames)))
           (recycle (packages-from-names recycle))
           (use (mapcar 'find-package* use))
           (mix (mapcar 'find-package* mix))
           (reexport (mapcar 'find-package* reexport))
           (shadow (mapcar 'string shadow))
           (export (mapcar 'string export))
           (intern (mapcar 'string intern))
           (unintern (mapcar 'string unintern))
671
           (local-nicknames (mapcar #'(lambda (pair) (mapcar 'string pair)) local-nicknames))
672
673
674
675
676
           (shadowed (make-hash-table :test 'equal)) ; string to bool
           (imported (make-hash-table :test 'equal)) ; string to bool
           (exported (make-hash-table :test 'equal)) ; string to bool
           ;; string to list home package and use package:
           (inherited (make-hash-table :test 'equal)))
677
678
      #-package-local-nicknames
      (declare (ignore local-nicknames)) ; if not supported
679
      (when-package-fishiness (record-fishy package-name))
680
      ;; if supported, put package documentation
681
      #-genera
682
      (when documentation (setf (documentation package t) documentation))
683
      ;; remove unwanted packages from use list
684
685
686
      (loop :for p :in (set-difference (package-use-list package) (append mix use))
            :do (note-package-fishiness :over-use name (package-names p))
                (unuse-package p package))
687
      ;; mark unwanted packages for deletion
688
689
690
691
692
693
694
      (loop :for p :in discarded
            :for n = (remove-if #'(lambda (x) (member x names :test 'equal))
                                (package-names p))
            :do (note-package-fishiness :nickname name (package-names p))
                (cond (n (rename-package p (first n) (rest n)))
                      (t (rename-package-away p)
                         (push p to-delete))))
695
      ;; give package its desired name
696
      (rename-package package package-name nicknames)
697
698
      ;; Handle local nicknames
      #+package-local-nicknames
699
      (install-package-local-nicknames package local-nicknames)
700
701
702
703
704
705
706
      (dolist (name unintern)
        (multiple-value-bind (existing status) (find-symbol name package)
          (when status
            (unless (eq status :inherited)
              (note-package-fishiness
               :unintern (package-name package) name (symbol-package-name existing) status)
              (unintern* name package nil)))))
707
      ;; handle exports
708
709
      (dolist (name export)
        (setf (gethash name exported) t))
710
      ;; handle reexportss
711
712
713
      (dolist (p reexport)
        (do-external-symbols (sym p)
          (setf (gethash (string sym) exported) t)))
714
      ;; unexport symbols not listed in (re)export
715
716
717
718
719
720
721
      (do-external-symbols (sym package)
        (let ((name (symbol-name sym)))
          (unless (gethash name exported)
            (note-package-fishiness
             :over-export (package-name package) name
             (or (home-package-p sym package) (symbol-package-name sym)))
            (unexport sym package))))
722
      ;; handle explicitly listed shadowed ssymbols
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
      (dolist (name shadow)
        (setf (gethash name shadowed) t)
        (multiple-value-bind (existing status) (find-symbol name package)
          (multiple-value-bind (recycled previous) (recycle-symbol name recycle exported)
            (let ((shadowing (and status (symbol-shadowing-p existing package))))
              (cond
                ((eq previous package))
                (previous
                 (rehome-symbol recycled package))
                ((or (member status '(nil :inherited))
                     (home-package-p existing package)))
                (t
                 (let ((dummy (make-symbol name)))
                   (note-package-fishiness
                    :shadow-imported (package-name package) name
                    (symbol-package-name existing) status shadowing)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
739
740
741
                   (shadowing-import* dummy package)
                   (import* dummy package)))))))
        (shadow* name package))
742
      ;; handle shadowing imports
743
744
745
      (loop :for (p . syms) :in shadowing-import-from
            :for pp = (find-package* p) :do
              (dolist (sym syms) (ensure-shadowing-import (string sym) package pp shadowed imported)))
746
      ;; handle mixed packages
747
748
749
      (loop :for p :in mix
            :for pp = (find-package* p) :do
              (do-external-symbols (sym pp) (ensure-mix (symbol-name sym) sym package pp shadowed imported inherited)))
750
      ;; handle import-from packages
751
752
753
      (loop :for (p . syms) :in import-from
            :for pp = (find-package p) :do
              (dolist (sym syms) (ensure-import (symbol-name sym) package pp shadowed imported)))
754
      ;; handle use-list and mix
755
756
757
758
759
760
      (dolist (p (append use mix))
        (do-external-symbols (sym p) (ensure-inherited (string sym) sym package p nil shadowed imported inherited))
        (use-package p package))
      (loop :for name :being :the :hash-keys :of exported :do
        (ensure-symbol name package t recycle shadowed imported inherited exported)
        (ensure-export name package recycle))
761
      ;; intern dessired symbols
762
763
764
765
      (dolist (name intern)
        (ensure-symbol name package t recycle shadowed imported inherited exported))
      (do-symbols (sym package)
        (ensure-symbol (symbol-name sym) package nil recycle shadowed imported inherited exported))
766
      ;; delete now-deceased packages
767
768
      (map () 'delete-package* to-delete)
      package)))
769

770

771
(eval-when (:load-toplevel :compile-toplevel :execute)
772
  (defun parse-define-package-form (package clauses)
773
774
    (loop
      :with use-p = nil :with recycle-p = nil
775
      :with documentation = nil
776
      :for (kw . args) :in clauses
777
      :when (eq kw :nicknames) :append args :into nicknames :else
778
779
780
781
782
      :when (eq kw :documentation)
        :do (cond
              (documentation (error "define-package: can't define documentation twice"))
              ((or (atom args) (cdr args)) (error "define-package: bad documentation"))
              (t (setf documentation (car args)))) :else
783
      :when (eq kw :use) :append args :into use :and :do (setf use-p t) :else
784
785
786
787
788
789
790
791
792
793
794
795
796
      :when (eq kw :shadow) :append args :into shadow :else
      :when (eq kw :shadowing-import-from) :collect args :into shadowing-import-from :else
      :when (eq kw :import-from) :collect args :into import-from :else
      :when (eq kw :export) :append args :into export :else
      :when (eq kw :intern) :append args :into intern :else
      :when (eq kw :recycle) :append args :into recycle :and :do (setf recycle-p t) :else
      :when (eq kw :mix) :append args :into mix :else
      :when (eq kw :reexport) :append args :into reexport :else
      :when (eq kw :use-reexport) :append args :into use :and :append args :into reexport
        :and :do (setf use-p t) :else
      :when (eq kw :mix-reexport) :append args :into mix :and :append args :into reexport
        :and :do (setf use-p t) :else
      :when (eq kw :unintern) :append args :into unintern :else
797
798
799
800
801
802
803
      :when (eq kw :local-nicknames)
        :if (symbol-call '#:uiop '#:featurep :package-local-nicknames)
          :append args :into local-nicknames
        :else
          :do (error ":LOCAL-NICKAMES option is not supported on this lisp implementation.")
        :end
      :else
804
        :do (error "unrecognized define-package keyword ~S" kw)
805
806
807
808
809
810
      :finally (return `(',package
                         :nicknames ',nicknames :documentation ',documentation
                         :use ',(if use-p use '(:common-lisp))
                         :shadow ',shadow :shadowing-import-from ',shadowing-import-from
                         :import-from ',import-from :export ',export :intern ',intern
                         :recycle ',(if recycle-p recycle (cons package nicknames))
811
812
                         :mix ',mix :reexport ',reexport :unintern ',unintern
                         :local-nicknames ',local-nicknames)))))
813
814

(defmacro define-package (package &rest clauses)
815
  "DEFINE-PACKAGE takes a PACKAGE and a number of CLAUSES, of the form
816
817
\(KEYWORD . ARGS\).
DEFINE-PACKAGE supports the following keywords:
818
819
USE, SHADOW, SHADOWING-IMPORT-FROM, IMPORT-FROM, EXPORT, INTERN, NICKNAMES,
DOCUMENTATION -- as per CL:DEFPACKAGE.
820
821
822
823
824
825
826
827
828
829
RECYCLE -- Recycle the package's exported symbols from the specified packages,
in order.  For every symbol scheduled to be exported by the DEFINE-PACKAGE,
either through an :EXPORT option or a :REEXPORT option, if the symbol exists in
one of the :RECYCLE packages, the first such symbol is re-homed to the package
being defined.
For the sake of idempotence, it is important that the package being defined
should appear in first position if it already exists, and even if it doesn't,
ahead of any package that is not going to be deleted afterwards and never
created again. In short, except for special cases, always make it the first
package on the list if the list is not empty.
830
MIX -- Takes a list of package designators.  MIX behaves like
831
832
\(:USE PKG1 PKG2 ... PKGn\) but additionally uses :SHADOWING-IMPORT-FROM to
resolve conflicts in favor of the first found symbol.  It may still yield
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
833
an error if there is a conflict with an explicitly :IMPORT-FROM symbol.
834
835
836
REEXPORT -- Takes a list of package designators.  For each package, p, in the list,
export symbols with the same name as those exported from p.  Note that in the case
of shadowing, etc. the symbols with the same name may not be the same symbols.
837
838
839
840
UNINTERN -- Remove symbols here from PACKAGE.
LOCAL-NICKNAMES -- If the host implementation supports package local nicknames
\(check for the :PACKAGE-LOCAL-NICKNAMES feature\), then this should be a list of
nickname and package name pairs.  Using this option will cause an error if the
841
842
843
host CL implementation does not support it.
USE-REEXPORT, MIX-REEXPORT -- Use or mix the specified packages as per the USE or
MIX directives, and reexport their contents as per the REEXPORT directive."
844
  (let ((ensure-form
845
         `(prog1
846
              (funcall 'ensure-package ,@(parse-define-package-form package clauses))
847
            #+sbcl (setf (sb-impl::package-source-location (find-package ',package))
848
                         (sb-c:source-location)))))
849
    `(progn
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
850
       #+(or clasp ecl gcl mkcl) (defpackage ,package (:use))
851
852
       (eval-when (:compile-toplevel :load-toplevel :execute)
         ,ensure-form))))
853
854
855
856
857

;; This package, unlike UIOP/PACKAGE, is allowed to evolve and acquire new symbols or drop old ones.
(define-package :uiop/package*
  (:use-reexport :uiop/package
                 #+package-local-nicknames :uiop/package-local-nicknames))