1 | ;;; defpackage.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2007 Peter Graves |
---|
4 | ;;; $Id: defpackage.lisp 11391 2008-11-15 22:38:34Z vvoutilainen $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | (in-package "SYSTEM") |
---|
33 | |
---|
34 | ;;; Adapted from CMUCL. |
---|
35 | |
---|
36 | (defun designated-package-name (designator) |
---|
37 | (cond ((packagep designator) |
---|
38 | (package-name designator)) |
---|
39 | (t |
---|
40 | (string designator)))) |
---|
41 | |
---|
42 | (defun stringify-names (names) |
---|
43 | (mapcar #'string names)) |
---|
44 | |
---|
45 | (defun check-disjoint (&rest args) |
---|
46 | (let ((rest-args args)) |
---|
47 | (dolist (arg1 args) |
---|
48 | (let ((key1 (car arg1)) |
---|
49 | (set1 (cdr arg1))) |
---|
50 | (setq rest-args (cdr rest-args)) |
---|
51 | (dolist (arg2 rest-args) |
---|
52 | (let* ((key2 (car arg2)) |
---|
53 | (set2 (cdr arg2)) |
---|
54 | (common (remove-duplicates (intersection set1 set2 :test #'string=)))) |
---|
55 | (when common |
---|
56 | (error 'program-error |
---|
57 | :format-control |
---|
58 | "Parameters ~S and ~S must be disjoint, but have common elements: ~S" |
---|
59 | :format-arguments |
---|
60 | (list key1 key2 common))))))))) |
---|
61 | |
---|
62 | (defun ensure-available-symbols (symbols) |
---|
63 | symbols) |
---|
64 | |
---|
65 | (defmacro defpackage (package &rest options) |
---|
66 | (let ((nicknames nil) |
---|
67 | (size nil) |
---|
68 | (shadows nil) |
---|
69 | (shadowing-imports nil) |
---|
70 | (use nil) |
---|
71 | (use-p nil) |
---|
72 | (imports nil) |
---|
73 | (interns nil) |
---|
74 | (exports nil) |
---|
75 | (doc nil)) |
---|
76 | (dolist (option options) |
---|
77 | (unless (consp option) |
---|
78 | (error 'program-error "bad DEFPACKAGE option: ~S" option)) |
---|
79 | (case (car option) |
---|
80 | (:nicknames |
---|
81 | (setq nicknames (stringify-names (cdr option)))) |
---|
82 | (:size |
---|
83 | (cond (size |
---|
84 | (error 'program-error "can't specify :SIZE twice")) |
---|
85 | ((and (consp (cdr option)) |
---|
86 | (typep (second option) 'unsigned-byte)) |
---|
87 | (setq size (second option))) |
---|
88 | (t |
---|
89 | (error 'program-error |
---|
90 | "bad :SIZE, must be a positive integer: ~S" |
---|
91 | (second option))))) |
---|
92 | (:shadow |
---|
93 | (let ((new (stringify-names (cdr option)))) |
---|
94 | (setq shadows (append shadows new)))) |
---|
95 | (:shadowing-import-from |
---|
96 | (let ((package-name (designated-package-name (cadr option))) |
---|
97 | (symbol-names (stringify-names (cddr option)))) |
---|
98 | (let ((assoc (assoc package-name shadowing-imports |
---|
99 | :test #'string=))) |
---|
100 | (if assoc |
---|
101 | (setf (cdr assoc) (append (cdr assoc) symbol-names)) |
---|
102 | (setq shadowing-imports |
---|
103 | (acons package-name symbol-names shadowing-imports)))))) |
---|
104 | (:use |
---|
105 | (let ((new (mapcar #'designated-package-name (cdr option)))) |
---|
106 | (setq use (delete-duplicates (nconc use new) :test #'string=)) |
---|
107 | (setq use-p t))) |
---|
108 | (:import-from |
---|
109 | (let ((package-name (designated-package-name (cadr option))) |
---|
110 | (symbol-names (stringify-names (cddr option)))) |
---|
111 | (let ((assoc (assoc package-name imports |
---|
112 | :test #'string=))) |
---|
113 | (if assoc |
---|
114 | (setf (cdr assoc) (append (cdr assoc) symbol-names)) |
---|
115 | (setq imports (acons package-name symbol-names imports)))))) |
---|
116 | (:intern |
---|
117 | (let ((new (stringify-names (cdr option)))) |
---|
118 | (setq interns (append interns new)))) |
---|
119 | (:export |
---|
120 | (let ((new (stringify-names (cdr option)))) |
---|
121 | (setq exports (append exports new)))) |
---|
122 | (:documentation |
---|
123 | (when doc |
---|
124 | (error 'program-error "can't specify :DOCUMENTATION twice")) |
---|
125 | (setq doc (coerce (cadr option) 'simple-string))) |
---|
126 | (t |
---|
127 | (error 'program-error "bad DEFPACKAGE option: ~S" option)))) |
---|
128 | (check-disjoint `(:intern ,@interns) `(:export ,@exports)) |
---|
129 | (check-disjoint `(:intern ,@interns) |
---|
130 | `(:import-from |
---|
131 | ,@(apply #'append (mapcar #'rest imports))) |
---|
132 | `(:shadow ,@shadows) |
---|
133 | `(:shadowing-import-from |
---|
134 | ,@(apply #'append (mapcar #'rest shadowing-imports)))) |
---|
135 | `(%defpackage ,(string package) ',nicknames ',size |
---|
136 | ',shadows (ensure-available-symbols ',shadowing-imports) |
---|
137 | ',(if use-p use nil) |
---|
138 | (ensure-available-symbols ',imports) ',interns ',exports ',doc))) |
---|