47f308680d3ec02e0baf3a84a426767cfeb70299
[sbcl.git] / src / cold / with-stuff.lisp
1 ;;;; code to tweak compilation environment, used to set up
2 ;;;; for different phases of cross-compilation
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB-COLD")
14
15 ;;; a helper macro for WITH-ADDITIONAL-NICKNAME and WITHOUT-SOME-NICKNAME
16 (defmacro with-given-nicknames ((package-designator nicknames) &body body)
17   (let ((p (gensym "P"))
18         (n (gensym "N"))
19         (o (gensym "O")))
20     `(let* ((,p ,package-designator) ; PACKAGE-DESIGNATOR, evaluated only once
21             (,n ,nicknames) ; NICKNAMES, evaluated only once
22             (,o (package-nicknames ,p))) ; old package nicknames
23        (rename-package-carefully ,p (package-name ,p) ,n)
24        (unwind-protect
25            (progn ,@body)
26          (unless (nicknames= ,n (package-nicknames ,p))
27            ;; This probably didn't happen on purpose, and it's not clear anyway
28            ;; what we should do when it did happen, so die noisily:
29            (error "package nicknames changed within WITH-GIVEN-NICKNAMES: ~
30                    expected ~S, found ~S"
31                   ,n
32                   (package-nicknames ,p)))
33          (rename-package-carefully ,p (package-name ,p) ,o)))))
34
35 ;;; Execute BODY with NICKNAME added as a nickname for PACKAGE-DESIGNATOR.
36 (defmacro with-additional-nickname ((package-designator nickname) &body body)
37   (let ((p (gensym "P"))
38         (n (gensym "N")))
39     `(let* ((,p ,package-designator) ; PACKAGE-DESIGNATOR, evaluated only once
40             (,n ,nickname)) ; NICKNAME, evaluated only once
41        (if (find-package ,n)
42          (error "~S is already a package name." ,n)
43          (with-given-nicknames (,p (cons ,n (package-nicknames ,p)))
44            ,@body)))))
45
46 ;;; Execute BODY with NICKNAME removed as a nickname for PACKAGE-DESIGNATOR.
47 (defmacro without-given-nickname ((package-designator nickname) &body body)
48   (let ((p (gensym "P"))
49         (n (gensym "N"))
50         (o (gensym "O")))
51     `(let* ((,p ,package-designator) ; PACKAGE-DESIGNATOR, evaluated only once
52             (,n ,nickname) ; NICKNAME, evaluated only once
53             (,o (package-nicknames ,p))) ; old package nicknames
54        (if (find ,n ,o :test #'string=)
55          (with-given-nicknames (,p (remove ,n ,o :test #'string=))
56            ,@body)
57          (error "~S is not a nickname for ~S." ,n ,p)))))
58
59 ;;; a helper function for WITH-NICKNAME: Are two collections of package
60 ;;; nicknames the same?
61 (defun nicknames= (x y)
62   (equal (sort (mapcar #'string x) #'string<)
63          (sort (mapcar #'string y) #'string<)))
64 (compile 'nicknames=)
65
66 ;;; helper functions for WITH-ADDITIONAL-NICKNAMES and WITHOUT-GIVEN-NICKNAMES
67 (defun %with-additional-nickname (package-designator nickname body-fn)
68   (with-additional-nickname (package-designator nickname)
69     (funcall body-fn)))
70 (defun %without-given-nickname (package-designator nickname body-fn)
71   (without-given-nickname (package-designator nickname)
72     (funcall body-fn)))
73 (defun %multi-nickname-magic (nd-list single-nn-fn body-fn)
74   (labels ((multi-nd (nd-list body-fn) ; multiple nickname descriptors
75              (if (null nd-list)
76                (funcall body-fn)
77                (single-nd (first nd-list)
78                           (lambda ()
79                             (multi-nd (rest nd-list) body-fn)))))
80            (single-nd (nd body-fn) ; single nickname descriptor
81              (destructuring-bind (package-descriptor nickname-list) nd
82                (multi-nn package-descriptor nickname-list body-fn)))
83            (multi-nn (nn-list package-descriptor body-fn) ; multiple nicknames
84              (if (null nn-list)
85                (funcall body-fn)
86                (funcall single-nn-fn
87                         (first nn-list)
88                         package-descriptor
89                         (lambda ()
90                           (multi-nn package-descriptor
91                                     (rest nn-list)
92                                     body-fn))))))
93     (multi-nd nd-list body-fn)))
94 (compile '%with-additional-nickname)
95 (compile '%without-given-nickname)
96 (compile '%multi-nickname-magic)
97
98 ;;; This is like WITH-ADDITIONAL-NICKNAME and WITHOUT-GIVEN-NICKNAMES,
99 ;;; except working on arbitrary lists of nickname descriptors instead
100 ;;; of single nickname/package pairs.
101 ;;;
102 ;;; A nickname descriptor is a list of the form
103 ;;;   PACKAGE-DESIGNATOR NICKNAME*
104 (defmacro with-additional-nicknames (nickname-descriptor-list &body body)
105   `(%multi-nickname-magic ,nickname-descriptor-list
106                           #'%with-additional-nickname
107                           (lambda () ,@body)))
108 (defmacro without-given-nicknames (nickname-descriptor-list &body body)
109   `(%multi-nickname-magic ,nickname-descriptor-list
110                           #'%without-additional-nickname
111                           (lambda () ,@body)))