Use IOLIB/ in package names
[iolib.git] / src / new-cl / conduits.lisp
blobea24465c41ab33946c116b136af033eb698c9890
1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
2 ;;;
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)
9 ;;; Update Count - 10
10 ;;; Status - Unknown
11 ;;;
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 ;;;; Conduit packages, and package cloning
15 ;;;
16 ;;; tfb 24-Jul-1998 00:41:02, tfb 3-Jul-2000 21:52:48
17 ;;;
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.
22 ;;;
23 ;;; This generalises the stuff in VERIFY-FORM.LISP
24 ;;;
25 ;;; !!! TODO: more of the package operators probably need to be shadowed
26 ;;; Errors should be signalled as subtypes of PACKAGE-ERROR
28 (defpackage :iolib/internal/conduits
29 (:use :common-lisp)
30 #+sb-package-locks
31 (:lock t)
32 ;; redefined CL names
33 (:shadow #:export #:unexport #:defpackage #:delete-package #:rename-package)
34 (:export #:export #:unexport #:defpackage #:delete-package #:rename-package)
35 ;; non-CL thing
36 (:export #:recompute-conduits))
38 (in-package :iolib/internal/conduits)
40 ;;;; Hack to make the HP stuff `work' even when they are not loaded.
41 ;;;
43 ;;; Load HP if we can find it
44 ;;;
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*)
50 '())
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
56 (setf
57 (gethash p org.tfeb.hax.hierarchical-packages:*per-package-alias-table*)
58 new)
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
69 ;;;
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))
82 package/name))
83 ((or string symbol)
84 (let ((found (find-package package/name)))
85 (values (intern (if found
86 (package-name found)
87 (etypecase package/name
88 (string package/name)
89 (symbol (symbol-name package/name))))
90 (find-package :keyword))
91 found)))))
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*)))
97 (if found
98 (pushnew conduit (cdr found))
99 (push (list pack conduit) *package-conduits*)))
100 (let ((found (assoc conduit *conduit-packages*)))
101 (if found
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)
120 (if (find-package p)
121 (list p)
122 nil))
123 pl)))
124 (if (or (null ppl)
125 (null (cdr ppl)))
127 (list ppl))))
128 pa))
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
140 extends
141 extends/including
142 extends/excluding)
143 (flet ((ensure-package (p)
144 (or (find-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))
151 (string d))
153 (ecase state
154 ((:external)
156 ((nil)
157 (error "Symbol name ~S not found in ~S" d p))
158 ((:internal)
159 (error "Symbol ~S internal in ~S" s p))
160 ((:inherited)
161 (error "Symbol ~S not directly present in ~S" s p)))))
162 (import-symbol (s pack)
163 (cl:import (if (eq s 'nil)
164 '(nil)
166 pack))
167 (export-symbol (s pack)
168 (cl:export (if (eq s 'nil)
169 '(nil)
171 pack)))
172 (let ((package (ensure-package package/name)))
173 (dolist (ex extends)
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))
183 (rest ei)))
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))
190 (rest ee))))
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)))))
196 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)
221 (push s exps)))
222 :for interned-symbols := (let ((ints '()))
223 (do-symbols (s from ints)
224 (when (eq (symbol-package s) from)
225 (push s ints))))
226 :when interned-symbols
227 :do (import interned-symbols to)
228 :when shadows
229 :do (shadow shadows to)
230 :when exports
231 :do(export exports to)
232 :when used
233 :do (use-package used to))
234 (loop :with aliases := '()
235 :for f :in froms
236 :for from := (find-package f)
237 :do (loop :for e :in (hp-alias-map from)
238 :when (assoc (first e) aliases
239 :test #'string=)
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))))
246 to))
248 ;;;; Define the basic package operations we need to take over.
249 ;;;
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*))
254 (prog1
255 (cl:export symbol/s package)
256 (recompute-conduits-for package)))
258 (defun unexport (symbol/s &optional (package *package*))
259 (prog1
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
280 conduits.
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
291 (:CLONES package)
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 '()))
296 (dolist (c clauses)
297 (case (first c)
298 (:extend
299 (dolist (e (rest c))
300 (push e excs)))
301 (:extend/including
302 (push (rest c) eics))
303 (:extend/excluding
304 (push (rest c) eecs))
305 (:clone
306 (dolist (e (rest c))
307 (push e cpcs)))
308 (:aliases
309 (loop :for e :in (rest c)
310 :unless (and (consp e)
311 (typep (first e)
312 '(or symbol string))
313 (typep (second e)
314 '(or symbol string))
315 (null (cddr e)))
316 :do
317 (error
318 "Package aliases should be list of (STRING STRING)")
319 :when (assoc (string (first e)) package-aliases
320 :test #'string=)
322 (error "Duplicate package alias ~A" (first e))
323 :do (push (cons (string (first e)) (string (second e)))
324 package-aliases)))
325 (otherwise
326 (push c dpcs))))
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)
334 `(progn
335 (cl:defpackage ,name
336 ,@(nreverse dpcs))
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))))
344 (if found
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))))
352 (cpcs
353 `(progn
354 (cl:defpackage ,name
355 ,@(nreverse dpcs))
356 (eval-when (:compile-toplevel :load-toplevel :execute)
357 (clone-packages-to-package ',cpcs ',name))))
359 `(progn
360 (cl:defpackage ,name
361 #+sb-package-locks
362 (:lock t)
363 ,@(nreverse dpcs))
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*))))
369 (when conduits
370 (error "Trying to delete ~S, but it has conduits ~S"
371 (find-package pack/name) (mapcar #'find-package conduits))))
372 (prog1
373 (progn
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 '()))
383 (prog1
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)))))