package.lisp 32.2 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
;;
;; CAUTION: we must handle the first few packages specially for hot-upgrade.
7
;; asdf/package will be frozen as of ASDF 3
8 9 10 11 12 13 14 15
;; to forever export the same exact symbols.
;; Any other symbol must be import-from'ed
;; and reexported in a different package
;; (alternatively the package may be dropped & replaced by one with a new name).

(defpackage :asdf/package
  (:use :common-lisp)
  (:export
16 17
   #:find-package* #:find-symbol* #:symbol-call
   #:intern* #:unintern* #:export* #:make-symbol*
18 19 20 21 22 23 24 25
   #:symbol-shadowing-p #:home-package-p #:rehome-symbol
   #:symbol-package-name #:standard-common-lisp-symbol-p
   #:reify-package #:unreify-package #:reify-symbol #:unreify-symbol
   #:nuke-symbol-in-package #:nuke-symbol
   #:ensure-package-unused #:delete-package*
   #:fresh-package-name #:rename-package-away #:package-names #:packages-from-names
   #:package-definition-form #:parse-define-package-form
   #:ensure-package #:define-package))
26 27 28 29 30 31

(in-package :asdf/package)

;;;; General purpose package utilities

(eval-when (:load-toplevel :compile-toplevel :execute)
32 33 34 35 36 37 38
  (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))
39 40 41 42 43 44
    "Find a symbol in a package of given string'ified NAME;
unless CL:FIND-SYMBOL, work well with 'modern' case sensitive syntax
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."
45 46
    (block nil
      (let ((package (find-package* package-designator error)))
47
        (when package ;; package error handled by find-package* already
48 49 50 51 52
          (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))))
53 54 55 56 57
  (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))
58
  (defun intern* (name package-designator &optional (error t))
59
    (intern (string name) (find-package* package-designator error)))
60 61 62 63
  (defun export* (name package-designator)
    (let* ((package (find-package* package-designator))
           (symbol (intern* name package)))
      (export (or symbol (list symbol)) package)))
64 65 66 67
  (defun make-symbol* (name)
    (etypecase name
      (string (make-symbol name))
      (symbol (copy-symbol name))))
68 69 70 71 72 73 74 75 76 77 78
  (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))))
79
  (defun symbol-shadowing-p (symbol package)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
80
    (and (member symbol (package-shadowing-symbols package)) t))
81
  (defun home-package-p (symbol package)
82 83 84 85
    (and package (let ((sp (symbol-package symbol)))
                   (and sp (let ((pp (find-package* package)))
                             (and pp (eq sp pp))))))))

86 87

(eval-when (:load-toplevel :compile-toplevel :execute)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
88 89 90
  (defun symbol-package-name (symbol)
    (let ((package (symbol-package symbol)))
      (and package (package-name package))))
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  (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)))
117 118
         (if package (intern* symbol-name package)
             (make-symbol* symbol-name)))))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
(eval-when (:load-toplevel :compile-toplevel :execute)
  (defvar *all-package-happiness* '())
  (defvar *all-package-fishiness* (list t))
  (defvar *package-fishiness* '())
  (defun flush-fishy ()
    (when *package-fishiness*
      (if (null (rest *package-fishiness*))
          (push (first *package-fishiness*) *all-package-happiness*)
          (push (nreverse *package-fishiness*) *all-package-fishiness*))
      (setf *package-fishiness* nil)))
  (defun record-fishy (info)
    ;;(format t "~&FISHY: ~S~%" info)
    (push info *package-fishiness*))
  (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
138
(eval-when (:load-toplevel :compile-toplevel :execute)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  #+(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)
159 160 161
       (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)))
162 163 164 165 166 167 168 169
    #+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
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  (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)
              (shadowing-import (get-dummy-symbol symbol) package)
              (unintern symbol package))))))
  (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)))
197 198 199 200 201 202 203 204 205 206 207 208
  (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))))
209 210 211 212
            (note-package-fishiness
             :rehome-symbol name (package-name old-package)
             (package-name package) old-status (and shadowing t)
             overwritten-symbol-status overwritten-symbol-shadowing-p)
213 214 215
            (when old-package
              (if shadowing
                  (shadowing-import shadowing old-package))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
216
              (unintern symbol old-package))
217 218 219 220 221 222 223 224 225 226
            (cond
              (overwritten-symbol-shadowing-p
               (shadowing-import symbol package))
              (t
               (when overwritten-symbol-status
                 (unintern overwritten-symbol package))
               (import symbol package)))
            (if shadowing
                (shadowing-import symbol old-package)
                (import symbol old-package))
227 228 229 230 231 232
            #+(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)))
233 234 235 236 237 238 239
                  (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)
                    (unintern setf-symbol (symbol-package setf-symbol)))
240
                  (setf (fdefinition new-setf-symbol) setf-function)
241
                  (set-setf-function-symbol new-setf-symbol symbol kind))))
242
            #+(or clisp clozure)
243
            (multiple-value-bind (overwritten-setf foundp)
244
                (get-setf-function-symbol overwritten-symbol)
245 246 247
              (when foundp
                (unintern overwritten-setf)))
            (when (eq old-status :external)
248
              (export* symbol old-package))
249
            (when (eq overwritten-symbol-status :external)
250
              (export* symbol package))))
251
        (values overwritten-symbol overwritten-symbol-status))))
252 253 254
  (defun ensure-package-unused (package)
    (loop :for p :in (package-used-by-list package) :do
      (unuse-package package p)))
255
  (defun delete-package* (package &key nuke)
256 257
    (let ((p (find-package package)))
      (when p
258
        (when nuke (do-symbols (s p) (when (home-package-p s p) (nuke-symbol s))))
259 260
        (ensure-package-unused p)
        (delete-package package))))
261 262 263 264
  (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))
265 266
  (defun fresh-package-name (&key (prefix :%TO-BE-DELETED)
                               separator
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
267 268
                               (index (random most-positive-fixnum)))
    (loop :for i :from index
269
          :for n = (format nil "~A~@[~A~D~]" prefix (and (plusp i) (or separator "")) i)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
270
          :thereis (and (not (find-package n)) n)))
271
  (defun rename-package-away (p &rest keys &key prefix &allow-other-keys)
272 273 274 275 276
    (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))))
277 278 279 280 281


;;; Communicable representation of symbol and package information

(eval-when (:load-toplevel :compile-toplevel :execute)
282 283 284 285 286 287
  (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)))
288 289 290 291 292 293 294 295
           (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 ()))
296
      (when package
297 298 299 300 301 302 303 304 305 306 307 308
        (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
309
                       ((and shadowing imported)
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
                        (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)
                   (sort names #'string<))
                 (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
330
             ,@(when-relevant :nicknames (and nicknamesp (sort-names nicknames)))
331 332 333 334 335
             (: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)))
336
             ,@(when-relevant :intern (and internp (sort-names intern)))))))))
337

338 339 340

;;; ensure-package, define-package

341
(eval-when (:load-toplevel :compile-toplevel :execute)
342 343 344 345 346 347
  (defun ensure-package (name &key
                                nicknames documentation use
                                shadow shadowing-import-from
                                import-from export intern
                                recycle mix reexport
                                unintern)
348
    #+(or gcl2.6 genera) (declare (ignore documentation))
349 350 351
    (macrolet ((when-fishy (&body body) `(when-package-fishiness ,@body))
               (fishy (&rest info) `(note-package-fishiness ,@info)))
      (let* ((package-name (string name))
352
             (nicknames (mapcar #'string nicknames))
353
             (names (cons package-name nicknames))
354 355 356
             (previous (packages-from-names names))
             (discarded (cdr previous))
             (to-delete ())
357
             (package (or (first previous) (make-package package-name :nicknames nicknames)))
358 359 360 361 362 363 364 365 366 367 368 369 370
             (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))
             (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)))
371
        (when-fishy (record-fishy package-name))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
372
        (labels
373
            ((ensure-shadowing-import (name p)
374
               (let ((import-me (find-symbol* name p)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
375 376 377
                 (multiple-value-bind (existing status) (find-symbol name package)
                   (cond
                     ((gethash name shadowed)
378
                      (unless (eq import-me existing)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
379 380 381 382 383 384
                        (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))
385
                                       (eq existing import-me)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
386 387
                                       (symbol-shadowing-p existing package)))
                        (fishy :shadowing-import
388
                               name (package-name p) (symbol-package-name import-me)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
389
                               (and status (symbol-package-name existing)) status))
390
                      (shadowing-import import-me package))))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
391 392
             (ensure-import (sym p)
               (let* ((name (string sym))
393
                      (import-me (find-symbol* name p)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
394 395 396
                 (multiple-value-bind (existing status) (find-symbol name package)
                   (cond
                     ((gethash name imported)
397
                      (unless (eq import-me existing)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
398 399 400 401 402 403
                        (error "Can't import ~S from both ~S and ~S"
                               name (package-name (symbol-package existing)) (package-name p))))
                     ((gethash name shadowed)
                      (error "Can't both shadow ~S and import it from ~S" name (package-name p)))
                     (t
                      (setf (gethash name imported) t)
404
                      (unless (and status (eq import-me existing))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
405
                        (when status
406
                          (fishy :import name (package-name p) (symbol-package-name import-me)
407 408
                                 (and status (symbol-package-name existing)) status)
                          (unintern* existing package))
409
                        (import import-me package)))))))
410 411 412 413 414 415
             (ensure-mix (name symbol p)
               (unless (gethash name shadowed)
                 (multiple-value-bind (existing status) (find-symbol name package)
                   (let* ((sp (symbol-package symbol))
                          (im (gethash name imported))
                          (in (gethash name inherited)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
416
                     (cond
417 418 419 420 421
                       ((or (null status)
                            (and status (eq symbol existing))
                            (and in (eq sp (first in))))
                        (ensure-inherited name symbol p t))
                       (in
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
422
                        (remhash name inherited)
423 424 425 426
                        (ensure-shadowing-import name (second in)))
                       (im
                         (error "Imported symbol ~S conflicts with inherited symbol ~S in ~S"
                                existing symbol (package-name package)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
427
                       (t
428 429
                        (ensure-inherited name symbol p t)))))))
             (ensure-inherited (name symbol p mix)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
430 431
               (multiple-value-bind (existing status) (find-symbol name package)
                 (let* ((sp (symbol-package symbol))
432
                        (in (gethash name inherited))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
433
                        (xp (and status (symbol-package existing))))
434 435 436 437
                   (when (null sp)
                     (fishy :import-uninterned name (package-name p) mix)
                     (import symbol p)
                     (setf sp (package-name p)))
438
                   (cond
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
439
                     ((gethash name shadowed))
440 441 442 443 444 445
                     (in
                      (unless (equal sp (first in))
                        (if mix
                            (ensure-shadowing-import name (second in))
                            (error "Can't inherit ~S from ~S, it is inherited from ~S"
                                   name (package-name sp) (package-name (first in))))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
446 447 448 449
                     ((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))))
450
                     (t
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
451
                      (setf (gethash name inherited) (list sp p))
452
                      (when (and status (not (eq sp xp)))
453 454 455 456 457
                        (let ((shadowing (symbol-shadowing-p existing package)))
                          (fishy :inherited name (package-name p) (package-name sp)
                                 (package-name xp))
                          (if shadowing (ensure-shadowing-import name p)
                              (unintern* existing package)))))))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
458
             (recycle-symbol (name)
459 460 461 462 463 464 465 466 467 468 469
               (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.
                            (fishy :recycled-duplicate name (package-name foundp) (package-name r)))
                           (t
                            (setf recycled symbol foundp r)))))))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
             (symbol-recycled-p (sym)
               (member (symbol-package sym) recycle))
             (ensure-symbol (name &optional intern)
               (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)
                     (cond
                       ((and status (eq existing recycled) (eq previous package)))
                       (previous
                        (rehome-symbol recycled package))
                       ((and status (eq package (symbol-package existing))))
                       (t
                        (when status
486 487
                          (fishy :ensure-symbol name
                                 (reify-package (symbol-package existing) package)
488 489
                                 status intern)
                          (unintern existing))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
490 491 492 493 494 495 496 497 498
                        (when intern
                          (intern* name package))))))))
             (ensure-export (name p)
               (multiple-value-bind (symbol status) (find-symbol* name p)
                 (unless (eq status :external)
                   (ensure-exported name symbol p))))
             (ensure-exported (name sym p)
               (dolist (u (package-used-by-list p))
                 (ensure-exported-to-user name sym u))
499
               (export* sym p))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
500 501 502 503
             (ensure-exported-to-user (name sym u)
               (multiple-value-bind (usym ustat) (find-symbol name u)
                 (unless (and ustat (eq sym usym))
                   (let ((accessible
504 505 506 507 508 509 510 511 512 513 514
                           (or (null ustat)
                               (let ((shadowing (symbol-shadowing-p usym u))
                                     (recycled (symbol-recycled-p usym)))
                                 (unless (and shadowing (not recycled))
                                   (fishy :ensure-export name (symbol-package-name sym)
                                          (package-name u)
                                          (and ustat (symbol-package-name usym)) ustat shadowing)
                                   (if (or (eq ustat :inherited) shadowing)
                                       (shadowing-import sym u)
                                       (unintern usym u))
                                   t)))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
515 516
                     (when (and accessible (eq ustat :external))
                       (ensure-exported name sym u)))))))
517 518
          #-(or gcl2.6 genera)
          (when documentation (setf (documentation package t) documentation))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
519
          (loop :for p :in (set-difference (package-use-list package) (append mix use))
520
                :do (fishy :use (package-names p)) (unuse-package p package))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
521 522 523 524
          (loop :for p :in discarded
                :for n = (remove-if #'(lambda (x) (member x names :test 'equal))
                                    (package-names p))
                :do (fishy :nickname (package-names p))
525 526 527
                    (cond (n (rename-package p (first n) (rest n)))
                          (t (rename-package-away p)
                             (push p to-delete))))
528
          (rename-package package package-name nicknames)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
529 530 531 532
          (dolist (name unintern)
            (multiple-value-bind (existing status) (find-symbol name package)
              (when status
                (unless (eq status :inherited)
533 534
                  (fishy :unintern name (symbol-package-name existing) status)
                  (unintern* name package nil)))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
          (dolist (name export)
            (setf (gethash name exported) t))
          (dolist (p reexport)
            (do-external-symbols (sym p)
              (setf (gethash (string sym) exported) t)))
          (do-external-symbols (sym package)
            (let ((name (symbol-name sym)))
              (unless (gethash name exported)
                (fishy :over-exported name (symbol-package-name sym))
                (unexport sym package))))
          (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)
                (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)))
                       (fishy :shadow-imported name (symbol-package-name existing) status shadowing)
                       (shadowing-import dummy package)
                       (import dummy package)))))))
            (shadow name package))
562 563 564
          (loop :for (p . syms) :in shadowing-import-from
                :for pp = (find-package* p) :do
                  (dolist (sym syms) (ensure-shadowing-import (string sym) pp)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
565
          (dolist (p mix)
566
            (do-external-symbols (sym p) (ensure-mix (symbol-name sym) sym p)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
567 568 569
          (loop :for (p . syms) :in import-from :do
            (dolist (sym syms) (ensure-import sym p)))
          (dolist (p (append use mix))
570
            (do-external-symbols (sym p) (ensure-inherited (string sym) sym p nil))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
571 572
            (use-package p package))
          (loop :for name :being :the :hash-keys :of exported :do
573
            (ensure-symbol name t)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
574 575
            (ensure-export name package))
          (dolist (name intern)
576
            (ensure-symbol name t))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
577 578 579
          (do-symbols (sym package)
            (ensure-symbol (symbol-name sym)))
          (map () 'delete-package* to-delete)
580
          (flush-fishy)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
581
          package)))))
582 583

(eval-when (:load-toplevel :compile-toplevel :execute)
584
  (defun parse-define-package-form (package clauses)
585 586
    (loop
      :with use-p = nil :with recycle-p = nil
587
      :with documentation = nil
588
      :for (kw . args) :in clauses
589
      :when (eq kw :nicknames) :append args :into nicknames :else
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
590 591 592 593 594
        :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
595
      :when (eq kw :use) :append args :into use :and :do (setf use-p t) :else
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
596 597 598 599 600 601 602 603 604 605
        :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 :unintern) :append args :into unintern :else
                          :do (error "unrecognized define-package keyword ~S" kw)
606 607
      :finally (return `(,package
                         :nicknames ,nicknames :documentation ,documentation
608 609 610 611
                         :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))
612
                         :mix ,mix :reexport ,reexport :unintern ,unintern)))))
613 614

(defmacro define-package (package &rest clauses)
615 616 617 618 619 620
  (let ((ensure-form
          `(apply 'ensure-package ',(parse-define-package-form package clauses))))
    `(progn
       #+clisp
       (eval-when (:compile-toplevel :load-toplevel :execute)
         ,ensure-form)
621
       #+(or clisp ecl gcl) (defpackage ,package (:use))
622 623
       (eval-when (:compile-toplevel :load-toplevel :execute)
         ,ensure-form))))
624

625
;;;; Final tricks to keep various implementations happy.
626 627 628
;; We want most such tricks in common-lisp.lisp,
;; but these need to be done before the define-package form there,
;; that we nevertheless want to be the very first form.
629
(eval-when (:load-toplevel :compile-toplevel :execute)
630
  #+allegro ;; We need to disable autoloading BEFORE any mention of package ASDF.
631 632
  (setf excl::*autoload-package-name-alist*
        (remove "asdf" excl::*autoload-package-name-alist*
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
                :test 'equalp :key 'car))
  #+gcl
  ;; Debian's GCL 2.7 has bugs with compiling multiple-value stuff,
  ;; but can run ASDF 2.011. GCL 2.6 has even more issues.
  (cond
    ((or (< system::*gcl-major-version* 2)
         (and (= system::*gcl-major-version* 2)
              (< system::*gcl-minor-version* 6)))
     (error "GCL 2.6 or later required to use ASDF"))
    ((and (= system::*gcl-major-version* 2)
          (= system::*gcl-minor-version* 6))
     (pushnew 'ignorable pcl::*variable-declarations-without-argument*)
     (pushnew :gcl2.6 *features*))
    (t
     (pushnew :gcl2.7 *features*))))