1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
3 ;;; File - conduit-packages.lisp
4 ;;; Description - Conduit packages, and package cloning
5 ;;; Author - Tim Bradshaw (tfb at lostwithiel)
6 ;;; Created On - Thu Sep 14 21:40:18 2000
7 ;;; Last Modified On - Tue Apr 30 15:10:56 2002
8 ;;; Last Modified By - Tim Bradshaw (tfb at lostwithiel)
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 ;;;; Conduit packages, and package cloning
16 ;;; tfb 24-Jul-1998 00:41:02, tfb 3-Jul-2000 21:52:48
18 ;;; Copyright 1998-2002 Tim Bradshaw. This code may be used for any
19 ;;; purpose whatsoever by anyone. It has no warranty whatsoever. I
20 ;;; would appreciate acknowledgement if you use it in anger, and I
21 ;;; would also very much appreciate any feedback or bug fixes.
23 ;;; This generalises the stuff in VERIFY-FORM.LISP
25 ;;; !!! TODO: more of the package operators probably need to be shadowed
26 ;;; Errors should be signalled as subtypes of PACKAGE-ERROR
28 (cl:defpackage
:iolib.internal.conduits
33 (:shadow
#:export
#:unexport
#:defpackage
#:delete-package
#:rename-package
)
34 (:export
#:export
#:unexport
#:defpackage
#:delete-package
#:rename-package
)
36 (:export
#:recompute-conduits
))
38 (in-package :iolib.internal.conduits
)
40 ;;;; Hack to make the HP stuff `work' even when they are not loaded.
43 ;;; Load HP if we can find it
46 (defun hp-alias-map (p)
47 (declare (ignorable p
))
48 #+org.tfeb.hax.hierarchical-packages
49 (gethash p org.tfeb.hax.hierarchical-packages
:*per-package-alias-table
*)
52 (defun (setf hp-alias-map
) (new p
)
53 ;; This one should never be called if HP is not loaded.
54 (declare (ignorable new p
))
55 #+org.tfeb.hax.hierarchical-packages
57 (gethash p org.tfeb.hax.hierarchical-packages
:*per-package-alias-table
*)
59 #-org.tfeb.hax.hierarchical-packages
60 (error "No hierarchical packages, so aliases will not work"))
62 (defun delete-hp-alias-map (p)
63 (declare (ignorable p
))
64 #+org.tfeb.hax.hierarchical-packages
65 (remhash p org.tfeb.hax.hierarchical-packages
:*per-package-alias-table
*))
68 ;;;; Conduit implementation
71 (defvar *conduit-package-descriptions
* '())
72 (defvar *package-conduits
* '())
73 (defvar *conduit-packages
* '())
75 (defun canonicalise-package-name (package/name
)
76 ;; Return a keyword, being the canonical name of the package.
77 ;; Second value is the package named, if it exists.
78 ;; maybe this should not use KEYWORD but our own secret package.
79 (etypecase package
/name
80 (package (values (intern (package-name package
/name
)
81 (find-package :keyword
))
84 (let ((found (find-package package
/name
)))
85 (values (intern (if found
87 (etypecase package
/name
89 (symbol (symbol-name package
/name
))))
90 (find-package :keyword
))
93 (defun note-conduit (pack conduit
)
94 (let ((pack (canonicalise-package-name pack
))
95 (conduit (canonicalise-package-name conduit
)))
96 (let ((found (assoc pack
*package-conduits
*)))
98 (pushnew conduit
(cdr found
))
99 (push (list pack conduit
) *package-conduits
*)))
100 (let ((found (assoc conduit
*conduit-packages
*)))
102 (pushnew pack
(cdr found
))
103 (push (list conduit pack
) *conduit-packages
*)))))
105 (defun recompute-conduits-for (pack &optional
(chain '()))
106 (let ((pack (canonicalise-package-name pack
)))
107 (when (member pack chain
)
108 (error "Circular conduits: ~S occurs in ~S" pack chain
))
109 (dolist (conduit (cdr (assoc pack
*package-conduits
*)))
110 (apply #'make-package-conduit-package
111 (assoc conduit
*conduit-package-descriptions
*))
112 (recompute-conduits-for conduit
(cons pack chain
)))
113 (find-package pack
)))
115 (defun clean-package-alist (pa)
116 ;; return a cleaned package alist: no nulls, no singletons, no nonexistent
117 ;; packages. Just blindly cons a new list here.
118 (mapcan #'(lambda (pl)
119 (let ((ppl (mapcan #'(lambda (p)
130 (defun recompute-conduits ()
131 "Clean up the lists of conduits, and recompute all conduit packages
132 to make them consistent"
133 (setf *package-conduits
* (clean-package-alist *package-conduits
*)
134 *conduit-packages
* (clean-package-alist *conduit-packages
*))
135 (dolist (pd *package-conduits
* (values))
136 (recompute-conduits-for (car pd
))))
139 (defun make-package-conduit-package (package/name
&key
143 (flet ((ensure-package (p)
145 ;; might want to be able to continue
146 (error "No package named ~S" p
)))
147 (ensure-external-symbol (d p
)
148 (multiple-value-bind (s state
)
149 (find-symbol (etypecase d
150 (symbol (symbol-name d
))
157 (error "Symbol name ~S not found in ~S" d p
))
159 (error "Symbol ~S internal in ~S" s p
))
161 (error "Symbol ~S not directly present in ~S" s p
)))))
162 (import-symbol (s pack
)
163 (cl:import
(if (eq s
'nil
)
167 (export-symbol (s pack
)
168 (cl:export
(if (eq s
'nil
)
172 (let ((package (ensure-package package
/name
)))
174 (note-conduit ex package
)
175 (do-external-symbols (s (ensure-package ex
))
176 (import-symbol s package
)
177 (export-symbol s package
)))
178 (dolist (ei extends
/including
)
179 (let ((p (ensure-package (first ei
))))
180 (note-conduit p package
)
181 (dolist (s (mapcar #'(lambda (sd)
182 (ensure-external-symbol sd p
))
184 (import-symbol s package
)
185 (export-symbol s package
))))
186 (dolist (ee extends
/excluding
)
187 (let* ((p (ensure-package (first ee
)))
188 (es (mapcar #'(lambda (sd)
189 (ensure-external-symbol sd p
))
191 (note-conduit p package
)
192 (do-external-symbols (s p
)
193 (unless (member s es
)
194 (import-symbol s package
)
195 (export-symbol s package
)))))
198 ;;; Cloning. Unlike conduits, cloning is a static operation: making a
199 ;;; clone of a package says to copy its state at a given moment and
200 ;;; then ignore any further changes. Redefining a clone package will
201 ;;; only pick up some of the changes - in particular symbols which
202 ;;; have been unexported from the cloned packages will not get
203 ;;; unexported and so on.
205 ;;; It may or may not make sense to clone multiple packages, this
206 ;;; function `supports' that because it's kind of implicit in the way
207 ;;; DEFPACKAGE works that you might get multiple packages.
209 ;;; It's not clear if any of this behaviour is right.
212 (defun clone-packages-to-package (froms to
)
213 (let ((to (or (find-package to
)
214 (make-package to
:use
'()))))
215 (loop :for f
:in froms
216 :for from
:= (find-package f
)
217 :for used
:= (package-use-list from
)
218 :for shadows
:= (package-shadowing-symbols from
)
219 :for exports
:= (let ((exps '()))
220 (do-external-symbols (s from exps
)
222 :for interned-symbols
:= (let ((ints '()))
223 (do-symbols (s from ints
)
224 (when (eq (symbol-package s
) from
)
226 :when interned-symbols
227 :do
(import interned-symbols to
)
229 :do
(shadow shadows to
)
231 :do
(export exports to
)
233 :do
(use-package used to
))
234 (loop :with aliases
:= '()
236 :for from
:= (find-package f
)
237 :do
(loop :for e
:in
(hp-alias-map from
)
238 :when
(assoc (first e
) aliases
241 (error "Duplicate package alias when cloning ~A" (first e
))
242 :do
(push e aliases
))
243 :finally
(when aliases
244 ;; Make sure we only call this if there were aliases
245 (setf (hp-alias-map to
) (nreverse aliases
))))
248 ;;;; Define the basic package operations we need to take over.
250 ;;; !!! Others may need to be added here. I think that UNINTERN is OK,
251 ;;; but I'm not sure about others.
253 (defun export (symbol/s
&optional
(package *package
*))
255 (cl:export symbol
/s package
)
256 (recompute-conduits-for package
)))
258 (defun unexport (symbol/s
&optional
(package *package
*))
260 (cl:unexport symbol
/s package
)
261 (recompute-conduits-for package
)))
263 (defmacro defpackage
(name &body clauses
) ;+++export
264 "Define a package. See CL:DEFPACKAGE for tha basics.
265 In addition, this version of DEFPACKAGE can define a `conduit package':
266 that you can use as a conduit to extend existing packages.
267 This works by importing symbols from the existing packages and
268 then reexporting them. The syntax is as DEFPACKAGE, wiht the addition
269 of three new clauses:
270 (:EXTEND package) takes package and reexports all its symbols;
271 (:EXTEND/INCLUDING package . syms/names) reexports only syms/names;
272 (:EXTEND/EXCLUDING package . syms/names) reexports all *but* syms/names.
273 When defining a conduit package no packages are :USEd by default.
275 If hierarchical packages are loaded when conduits is built (yes, I know)
276 Then you can also say
277 (:ALIASES (name realname) ...)
278 Which will cause name to be a shorthand for realname when the package
279 Being defined is the current package. Aliases are not inherited from
282 This version of DEFPACKAGE also support `cloning' packages: making another
283 package which is `just like' an existing package. This means that all the
284 internal, exported and shadowing symbols in the clone will be the same as
285 those in the cloned package, but any additional things defined by DEFPACKAGE
286 will also take effect. This allows you to essentially make a copy of
287 a package which you can then use to define new functionality without
288 interning a lot of things in the original package. Cloning is a static
289 operation - packages do not know who their clones are, and no attempt is made
290 to keep clones up to date. Cloning is done by the clause
292 Cloning is not compatible with extending (this is checked).
293 As with extending you probably want to specify (:USE) when cloning."
294 (let ((dpcs '()) (excs '()) (eics ()) (eecs '()) (cpcs '())
295 (package-aliases '()))
302 (push (rest c
) eics
))
304 (push (rest c
) eecs
))
309 (loop :for e
:in
(rest c
)
310 :unless
(and (consp e
)
318 "Package aliases should be list of (STRING STRING)")
319 :when
(assoc (string (first e
)) package-aliases
322 (error "Duplicate package alias ~A" (first e
))
323 :do
(push (cons (string (first e
)) (string (second e
)))
327 (when (and cpcs
(or excs eics eecs
))
328 (error "Cloning is not compatible with extending"))
329 (when (and cpcs package-aliases
)
330 (error "Cloning is not compatible with package aliases"))
331 (unless (find :use dpcs
:key
'car
)
332 (push (list :use
) dpcs
))
333 (cond ((or excs eics eecs package-aliases
)
337 ;; need always to do this because defpackage is always done.
338 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
339 (let* ((cn (canonicalise-package-name ',name
))
340 (found (assoc cn
*conduit-package-descriptions
*))
341 (descr '(:extends
,(nreverse excs
)
342 :extends
/including
,(nreverse eics
)
343 :extends
/excluding
,(nreverse eecs
))))
345 (setf (cdr found
) descr
)
346 (push (cons cn descr
) *conduit-package-descriptions
*))
347 (apply #'make-package-conduit-package cn descr
))
348 ,@(when package-aliases
349 `((setf (hp-alias-map (find-package ',name
))
350 ',(nreverse package-aliases
))))
351 (recompute-conduits-for ',name
))))
356 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
357 (clone-packages-to-package ',cpcs
',name
))))
364 (recompute-conduits-for ',name
))))))
366 (defun delete-package (pack/name
)
367 (let ((name (canonicalise-package-name pack
/name
)))
368 (let ((conduits (cdr (assoc name
*package-conduits
*))))
370 (error "Trying to delete ~S, but it has conduits ~S"
371 (find-package pack
/name
) (mapcar #'find-package conduits
))))
374 (delete-hp-alias-map (find-package pack
/name
))
375 (cl:delete-package pack
/name
))
376 ;; NAME can occur in *CONDUIT-PACKAGES* if it was a conduit.
377 ;; NAME can occur in *PACKAGE-CONDUITS* if it had conduits
378 ;; (there will not now be any)
379 (setf *conduit-packages
* (delete name
*conduit-packages
* :key
#'car
)
380 *package-conduits
* (delete name
*package-conduits
* :key
#'car
)))))
382 (defun rename-package (pack/name new-name
&optional
(nicknames '()))
384 (cl:rename-package pack
/name new-name nicknames
)
385 (let ((name (canonicalise-package-name pack
/name
))
386 (new-name (canonicalise-package-name new-name
)))
387 (dolist (c *conduit-packages
*)
388 (nsubstitute new-name name c
))
389 (dolist (p *package-conduits
*)
390 (nsubstitute new-name name p
)))))