package.lisp 19.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
;;;; ---------------------------------------------------------------------------
;;;; 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.
;; asdf/package will be frozen as of 2.27
;; 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 18
   #:find-package* #:find-symbol* #:intern* #:unintern*
   #:symbol-name-package #:package-data
   #:delete-package* #:ensure-package #:define-package))
19 20 21

(in-package :asdf/package)

22 23 24 25 26
(declaim (optimize (speed 0) (safety 3) (debug 3)))

(defmacro DBG (tag &rest exprs)
  "simple debug statement macro:
outputs a tag plus a list of variable and their values, returns the last value"
27
  ;;"if not in debugging mode, just compute and return last value"
28 29 30 31 32 33 34 35 36 37 38 39 40
  #-DBGXXX (declare (ignore tag)) #-DBGXXX (car (last exprs)) #+DBGXXX
  (let ((res (gensym))(f (gensym)))
  `(let (,res (*print-readably* nil))
    (flet ((,f (fmt &rest args) (apply #'format *error-output* fmt args)))
      (fresh-line *standard-output*) (fresh-line *trace-output*) (fresh-line *error-output*)
      (,f "~&~A~%" ,tag)
      ,@(mapcan
         #'(lambda (x)
            `((,f "~&  ~S => " ',x)
              (,f "~{~S~^ ~}~%" (setf ,res (multiple-value-list ,x)))))
         exprs)
      (apply 'values ,res)))))

41 42 43
;;;; General purpose package utilities

(eval-when (:load-toplevel :compile-toplevel :execute)
44 45 46 47 48 49 50
  (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))
51 52 53 54 55 56
    "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."
57 58 59 60 61 62 63 64 65
    (block nil
      (let ((package (find-package* package-designator error)))
        (when package
          (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))))
  (defun intern* (name package-designator &optional (error t))
66
    (intern (string name) (find-package* package-designator error)))
67 68 69 70 71 72 73 74 75 76 77
  (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))))
78 79 80 81 82 83 84 85 86 87 88 89 90 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 117 118 119 120 121 122 123 124
  (defun symbol-shadowing-p (symbol package)
    (member symbol (package-shadowing-symbols package)))
  (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))))
            (when old-package
              (if shadowing
                  (shadowing-import shadowing old-package))
                  (unintern symbol old-package))
            (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))
            #+ccl
            (multiple-value-bind (setf-name foundp)
                (gethash symbol ccl::%setf-function-names%)
              (when foundp
                (let* ((setf-function (fdefinition setf-name))
                       (new-setf-name (ccl::construct-setf-function-name symbol)))
                  (setf (fdefinition new-setf-name) setf-function
                        (gethash symbol ccl::%setf-function-names%) new-setf-name
                        (gethash new-setf-name ccl::%setf-function-name-inverses%) symbol))))
            #+ccl
            (multiple-value-bind (overwritten-setf foundp)
                (gethash overwritten-symbol ccl::%setf-function-names%)
              (when foundp
                (unintern overwritten-setf)))
            (when (eq old-status :external)
              (export symbol old-package))
            (when (eq overwritten-symbol-status :external)
              (export symbol package))))
        (values overwritten-symbol overwritten-symbol-status))))
125 126 127 128 129
  (defun symbol-name-package (symbol)
    (cons (symbol-name symbol) (package-name (symbol-package symbol))))
  (defun package-names (package)
    (cons (package-name package) (package-nicknames package)))
  (defun package-data (package-designator &key name-package (error t))
130 131
    (let ((package (find-package* package-designator error)))
      (when package
132 133 134 135
        (labels ((marshall-symbols (symbols)
                   (if name-package (mapcar #'symbol-name-package symbols) symbols))
                 (sort-symbols (symbols)
                   (marshall-symbols (sort symbols #'string<)))
136
                 (sort-packages (packages)
137
                   (sort (mapcar #'package-name packages) #'string<)))
138 139 140 141 142 143 144 145 146 147 148
          (loop :with internal :with external :with inherited
                :for sym :being :the :symbols :in package
                :for status = (nth-value 1 (find-symbol* sym package)) :do
                  (ecase status
                    (:internal (push sym internal))
                    (:external (push sym external))
                    (:inherited (push sym inherited)))
                :finally
                   (return
                     `(:name ,(package-name package)
                       :nicknames ,(package-nicknames package)
149 150 151 152
                       :internal ,(sort-symbols internal)
                       :external ,(sort-symbols external)
                       :inherited ,(sort-symbols inherited)
                       :shadowing ,(sort-symbols (package-shadowing-symbols package))
153 154
                       :use ,(sort-packages (package-use-list package))
                       :used-by ,(sort-packages (package-used-by-list package))))))))))
155 156 157 158 159 160 161

(eval-when (:load-toplevel :compile-toplevel :execute)
  (defun soft-upgrade-p (upgrade)
    (ecase upgrade ((:soft nil) t) (:hard nil)))
  (defun ensure-package-unused (package)
    (loop :for p :in (package-used-by-list package) :do
      (unuse-package package p)))
162
  (defun delete-package* (package)
163 164 165 166
    (let ((p (find-package package)))
      (when p
        (ensure-package-unused p)
        (delete-package package))))
167 168
  (defun ensure-package-fmakunbound (package symbols)
    (loop :for name :in symbols
169
          :for sym = (find-symbol* name package nil)
170 171 172
          :when sym :do (fmakunbound sym)))
  (defun ensure-package-fmakunbound-setf (package symbols)
    (loop :for name :in symbols
173
          :for sym = (find-symbol* name package nil)
174
          :when sym :do #-gcl (fmakunbound `(setf ,sym))))
175 176
  (defun packages-from-names (names)
    (remove-duplicates (remove nil (mapcar #'find-package names)) :from-end t))
177
  (defun ensure-package (name &key
178 179 180 181 182 183
                                upgrade
                                nicknames documentation use
                                shadow shadowing-import-from
                                import-from export intern
                                recycle mix reexport
                                unintern fmakunbound fmakunbound-setf)
184
    (let* ((name (string name))
185
           (nicknames (mapcar #'string nicknames))
186
           (names (cons name nicknames))
187
           (previous (packages-from-names names))
188
           (discarded (cdr previous))
189 190 191 192 193 194
           (package (or (first previous) (make-package name :nicknames nicknames)))
           (recycle (packages-from-names recycle))
           (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
           (inherited (make-hash-table :test 'equal))) ; string to package name
195 196 197 198 199 200
      (labels
          ((ensure-shadowing-import (sym p)
             (let* ((name (string sym))
                    (i (find-symbol* name p)))
               (cond
                 ((gethash name shadowed)
201
                  (unless (eq i (find-symbol* name package))
202 203 204
                    (error "Conflicting shadowings for ~A" name)))
                 (t
                    (setf (gethash name shadowed) t)
205
                    (setf (gethash name imported) t)
206
                    (shadowing-import i package)))))
207 208 209
           (ensure-import (sym p)
             (let* ((name (string sym))
                    (i (find-symbol* name p)))
210
               (multiple-value-bind (x xp) (find-symbol name package)
211
                 (cond
212 213
                   ((gethash name imported)
                    (unless (eq i x)
214
                      (error "Can't import ~S from both ~S and ~S"
215
                             name (package-name (symbol-package x)) (package-name p))))
216 217 218
                   ((gethash name shadowed)
                    (error "Can't both shadow ~S and import it from ~S" name (package-name p)))
                   (t
219
                    (setf (gethash name imported) t)
220 221 222
                    (unless (and xp (eq i x))
                      (when xp (unintern* x p))
                      (import i package)))))))
223 224
           (ensure-mix (sym p)
             (let* ((name (string sym))
225 226 227
                    (sp (string p)))
               (unless (or (gethash name shadowed) (gethash name imported))
                 (let ((ip (gethash name inherited)))
228
                   (cond
229 230
                     ((eq sp ip))
                     (ip
231
                      (remhash name inherited)
232
                      (ensure-shadowing-import name ip))
233
                     (t
234
                      (ensure-inherited sym sp)))))))
235 236
           (ensure-inherited (sym p)
             (let* ((name (string sym))
237 238 239 240
                    (symbol (find-symbol* name p))
                    (sp (symbol-package symbol))
                    (spn (package-name sp))
                    (ipn (gethash name inherited)))
241 242
               (multiple-value-bind (x xp) (find-symbol name package)
                 (cond
243 244
                   (ipn
                    (unless (eq spn ipn)
245
                      (error "Can't inherit ~S from ~S, it is inherited from ~S"
246
                             name spn ipn)))
247
                   ((gethash name imported)
248
                    (unless (eq symbol x)
249 250 251
                      (error "Can't inherit ~S from ~S, it is imported from ~S"
                             name sp (package-name (symbol-package x)))))
                   ((gethash name shadowed)
252
                    (error "Can't inherit ~S from ~S, it is shadowed" name spn))
253
                   (t
254
                    (setf (gethash name inherited) spn)
255 256
                    (when xp
                      (unintern* x package)))))))
257
           (recycle-symbol (name)
258 259 260 261
             (dolist (r recycle (values nil nil))
               (multiple-value-bind (symbol status) (find-symbol name r)
                 (when (and status (eq r (symbol-package symbol)))
                   (return (values symbol r))))))
262
           (symbol-recycled-p (sym)
263
             (member (symbol-package sym) recycle))
264 265 266 267 268 269 270 271 272
           (ensure-symbol (name &optional intern)
             (unless (or (gethash name shadowed)
                         (gethash name imported)
                         (gethash name inherited))
               (multiple-value-bind (recycled previous) (recycle-symbol name)
                 (cond
                   ((eq previous package))
                   ((or (not previous) (not (member (symbol-package recycled) recycle)))
                    (when intern (intern* name package)))
273
                   (t (rehome-symbol recycled package))))))
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
           (ensure-export (name p)
             (multiple-value-bind (symbol status) (find-symbol name p)
               (assert status)
               (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))
             (export sym p))
           (ensure-exported-to-user (name sym u)
             (multiple-value-bind (usym ustat) (find-symbol name u)
               (unless (eq sym usym)
                 (let ((shadowing (member usym (package-shadowing-symbols u))))
                   (block nil
                     (cond
                       ((not shadowing)
                        (unintern usym u))
                       ((symbol-recycled-p usym)
                        (shadowing-import sym u))
                       (t (return)))
                     (when (eq ustat :external)
                       (ensure-exported name sym u))))))))
        (assert (soft-upgrade-p upgrade))
        (setf (documentation package t) documentation)
        (loop :for p :in discarded
              :for n = (remove-if #'(lambda (x) (member x names :test 'equal))
                                  (package-names p))
              :do (if n (rename-package discarded (first n) (rest n))
                      (delete-package* discarded)))
        (rename-package package name nicknames)
        (loop :for p :in (set-difference (package-use-list package) (append mix use))
              :do (unuse-package p package))
        (dolist (name unintern) (unintern* name package nil))
        (loop :for sym :in export :for name = (string sym) :do
          (setf (gethash name exported) t))
        (loop :for p :in reexport :do
          (do-external-symbols (sym p)
            (let ((name (string sym)))
              (export (find-symbol* name package) package) (setf (gethash name exported) t))))
        (do-external-symbols (sym package)
          (unless (gethash (symbol-name sym) exported) (unexport sym package)))
        (loop :for s :in shadow :for name = (string s) :do
          (setf (gethash name shadowed) t)
          (multiple-value-bind (recycled previous) (recycle-symbol name)
            (cond
              ((or (not previous) (not (member (symbol-package recycle) recycle)))
               (ecase (nth-value 1 (find-symbol* name package nil))
                 ((nil :inherited) (shadow name package))
                 ((:internal :external) (shadowing-import (make-symbol name) package))))
              ((eq previous package) (shadow recycled package))
324
              (t (rehome-symbol recycled package)))))
325 326 327 328 329 330
        (loop :for (p . syms) :in shadowing-import-from :do
          (dolist (sym syms) (ensure-shadowing-import sym p)))
        (loop :for p :in mix :do
          (do-external-symbols (sym p) (ensure-mix sym p)))
        (loop :for (p . syms) :in import-from :do
          (dolist (sym syms) (ensure-import sym p)))
331 332 333
        (loop :for p :in use :for sp = (string p) :for pp = (find-package sp) :do
          (do-external-symbols (sym pp) (ensure-inherited sym sp))
          (use-package pp package))
334 335 336 337
        (loop :for name :being :the :hash-keys :of exported :do
          (ensure-symbol name t))
        (dolist (name (append intern fmakunbound fmakunbound-setf))
          (ensure-symbol (string name) t))
338
        (do-symbols (sym package)
339 340 341
          (ensure-symbol (symbol-name sym)))
        (loop :for name :being :the :hash-keys :of exported :do
          (ensure-export name package))
342 343 344 345 346 347 348
        ;; do away with packages with conflicting (nick)names
        ;; note from ASDF 2.26: ECL might not be liking an early fmakunbound (below #-ecl'ed)
        (ensure-package-fmakunbound package fmakunbound)
        (ensure-package-fmakunbound-setf package fmakunbound-setf)
        package))))

(eval-when (:load-toplevel :compile-toplevel :execute)
349
  (defun parse-define-package-form (package clauses)
350 351 352 353
    (loop
      :with use-p = nil :with recycle-p = nil
      :with documentation = nil :with upgrade = nil
      :for (kw . args) :in clauses
354
      :when (eq kw :nicknames) :append args :into nicknames :else
355 356 357 358 359 360
      :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
      :when (eq kw :use) :append args :into use :and :do (setf use-p t) :else
361
      :when (eq kw :shadow) :append args :into shadow :else
362 363
      :when (eq kw :shadowing-import-from) :collect args :into shadowing-import-from :else
      :when (eq kw :import-from) :collect args :into import-from :else
364 365
      :when (eq kw :export) :append args :into export :else
      :when (eq kw :intern) :append args :into intern :else
366
      :when (eq kw :recycle) :append args :into recycle :and :do (setf recycle-p t) :else
367
      :when (eq kw :mix) :append args :into mix :else
368
      :when (eq kw :reexport) :append args :into reexport :else
369 370 371
      :when (eq kw :unintern) :append args :into unintern :else
      :when (eq kw :fmakunbound) :append args :into fmakunbound :else
      :when (eq kw :fmakunbound-setf) :append args :into fmakunbound-setf :else
372 373 374 375 376
      :when (eq kw :upgrade)
        :do (unless (and (consp args) (null (cdr args)) (member (car args) '(:soft :hard)) (null upgrade))
              (error "define-package: bad :upgrade directive"))
            (setf upgrade (car args)) :else
      :do (error "unrecognized define-package keyword ~S" kw)
377
      (progn fmakunbound fmakunbound-setf)
378 379
      :finally (return `(,package
                         :nicknames ,nicknames :documentation ,documentation
380 381 382 383 384 385
                         :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))
                         :mix ,mix :reexport ,reexport :unintern ,unintern
                         ,@(when upgrade `(:upgrade ,upgrade))
386
                         #|:fmakunbound ,fmakunbound :fmakunbound-setf ,fmakunbound-setf|#)))))
387 388 389

(defmacro define-package (package &rest clauses)
  `(eval-when (:compile-toplevel :load-toplevel :execute)
390
     #+(or ecl gcl) (defpackage ,package (:use))
391
     (apply 'ensure-package ',(parse-define-package-form package clauses))))