Debian Codegraph @

src/lsp/defpackage.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;
6 ;;; THE BOEING COMPANY
7 ;;; BOEING COMPUTER SERVICES
8 ;;; RESEARCH AND TECHNOLOGY
9 ;;; COMPUTER SCIENCE
10 ;;; P.O. BOX 24346, MS 7L-64
11 ;;; SEATTLE, WA 98124-0346
12 ;;;
13 ;;;
14 ;;; Copyright (c) 1990, 1991 The Boeing Company, All Rights Reserved.
15 ;;;
16 ;;; Permission is granted to any individual or institution to use,
17 ;;; copy, modify, and distribute this software, provided that this
18 ;;; complete copyright and permission notice is maintained, intact, in
19 ;;; all copies and supporting documentation and that modifications are
20 ;;; appropriately documented with date, author and description of the
21 ;;; change.
22 ;;;
23 ;;; Stephen L. Nicoud ([email protected]) provides this software "as
24 ;;; is" without express or implied warranty by him or The Boeing
25 ;;; Company.
26 ;;;
27 ;;; This software is distributed in the hope that it will be useful,
28 ;;; but WITHOUT ANY WARRANTY. No author or distributor accepts
29 ;;; responsibility to anyone for the consequences of using it or for
30 ;;; whether it serves any particular purpose or works at all.
31 ;;;
32 ;;; Author: Stephen L. Nicoud
33 ;;;
34 ;;; -----------------------------------------------------------------
35 ;;;
36 ;;; Adapted for X3J13 by Stephen L Nicoud, 91/5/23
37 ;;; Adapted for ECL by Giuseppe Attardi, 6/6/1994.
38 ;;; Partially rewritten by Daniel KochmaƄski, 2017-05-01
39 ;;;
40 ;;; -----------------------------------------------------------------
41
42 (in-package "SYSTEM")
43
44 (defmacro DEFPACKAGE (name &rest options)
45 (declare (type (or symbol string character) name))
46 "DEFPACKAGE - DEFINED-PACKAGE-NAME {OPTION}* [Macro]
47
48 This creates a new package, or modifies an existing one, whose name is
49 DEFINED-PACKAGE-NAME. The DEFINED-PACKAGE-NAME may be a string or a
50 symbol; if it is a symbol, only its print name matters, and not what
51 package, if any, the symbol happens to be in. The newly created or
52 modified package is returned as the value of the DEFPACKAGE form.
53
54 Each standard OPTION is a list of keyword (the name of the option)
55 and associated arguments. No part of a DEFPACKAGE form is
56 evaluated. Except for the :LOCK and :DOCUMENTATION options, more
57 than one option of the same kind may occur within the same
58 DEFPACKAGE form.
59
60 Valid Options:
61 (:documentation string)
62 (:lock boolean)
63 (:nicknames {package-name}*)
64 (:shadow {symbol-name}*)
65 (:shadowing-import-from package-name {symbol-name}*)
66 (:use {package-name}*)
67 (:local-nicknames (local-nickname actual-package-name)*)
68 (:import-from package-name {symbol-name}*)
69 (:intern {symbol-name}*)
70 (:export {symbol-name}*)
71 (:export-from {package-name}*)
72
73 [Note: :EXPORT-FROM, :DOCUMENTATION, :LOCK and :LOCAL-NICKNAMES are
74 extensions to DEFPACKAGE.
75
76 If a symbol is interned in the package being created and if a
77 symbol with the same print name appears as an external symbol
78 of one of the packages in the :EXPORT-FROM option, then the
79 symbol is exported from the package being created.]"
80
81 (flet ((designators (values)
82 (mapcar #'string values)))
83 (let ((nicknames nil)
84 (documentation nil)
85 (size nil)
86 (shadowed-symbol-names nil)
87 (interned-symbol-names nil)
88 (exported-symbol-names nil)
89 (shadowing-imported-from-symbol-names-list nil)
90 (imported-from-symbol-names-list nil)
91 (exported-from-package-names nil)
92 (use nil)
93 (use-p nil)
94 (lock nil)
95 (local-nicknames nil))
96 (dolist (option options)
97 (case (car option)
98 (:nicknames
99 (setf nicknames (append nicknames (designators (rest option)))))
100 (:documentation
101 (when documentation
102 (si:simple-program-error
103 "DEFPACKAGE option :DOCUMENTATION specified more than once."))
104 (setf documentation (second option)))
105 (:use
106 (setf use (append use (designators (rest option)))
107 use-p t))
108 (:shadow
109 (setf shadowed-symbol-names
110 (append shadowed-symbol-names (designators (rest option)))))
111 (:intern
112 (setf interned-symbol-names
113 (append interned-symbol-names (designators (rest option)))))
114 (:export
115 (setf exported-symbol-names
116 (append exported-symbol-names (designators (rest option)))))
117 (:shadowing-import-from
118 (destructuring-bind (package-name . names)
119 (designators (rest option))
120 (let ((assoc (assoc package-name shadowing-imported-from-symbol-names-list
121 :test #'string=)))
122 (if assoc
123 (setf (cdr assoc) (append (cdr assoc) names))
124 (setf shadowing-imported-from-symbol-names-list
125 (acons package-name names shadowing-imported-from-symbol-names-list))))))
126 (:import-from
127 (destructuring-bind (package-name . names)
128 (designators (rest option))
129 (let ((assoc (assoc package-name imported-from-symbol-names-list
130 :test #'string=)))
131 (if assoc
132 (setf (cdr assoc) (append (cdr assoc) names))
133 (setf imported-from-symbol-names-list
134 (acons package-name names imported-from-symbol-names-list))))))
135 (:size
136 ;; Size is ignored for all we care, but standard mandates signalling
137 ;; error if size is passed twice in DEFPACKAGE.
138 (when size
139 (si:simple-program-error
140 "DEFPACKAGE option :SIZE specified more than once."))
141 (setf size (second option)))
142 ;; extensions
143 (:export-from
144 (setf exported-from-package-names
145 (append exported-from-package-names (designators (rest option)))))
146 (:lock
147 (when lock
148 (si:simple-program-error
149 "DEFPACKAGE option :LOCK specified more than once.")
150 (setf lock (second option))))
151 (:local-nicknames
152 (setf local-nicknames
153 (append local-nicknames
154 (mapcar (lambda (spec)
155 (destructuring-bind (nick name) spec
156 (cons nick name)))
157 (rest option)))))
158 ;; unknown
159 (otherwise
160 (cerror "Proceed, ignoring this option."
161 "~s is not a valid DEFPACKAGE option." option))))
162 (check-disjoint `(:intern ,@interned-symbol-names)
163 `(:export ,@exported-symbol-names))
164 (check-disjoint `(:intern ,@interned-symbol-names)
165 `(:import-from
166 ,@(apply #'append (mapcar #'rest imported-from-symbol-names-list)))
167 `(:shadow ,@shadowed-symbol-names)
168 `(:shadowing-import-from
169 ,@(apply #'append (mapcar #'rest shadowing-imported-from-symbol-names-list))))
170 `(eval-when (eval compile load)
171 (si::dodefpackage
172 ,(string name)
173 ',nicknames
174 ,documentation
175 ,(cadr (assoc ':lock options))
176 ',(if use-p use "CL")
177 ',local-nicknames
178 ',shadowed-symbol-names
179 ',interned-symbol-names
180 ',exported-symbol-names
181 ',shadowing-imported-from-symbol-names-list
182 ',imported-from-symbol-names-list
183 ',exported-from-package-names)))))
184
185 (defun check-disjoint (&rest args)
186 (declare (si::c-local))
187 ;; An arg is (:key . set)
188 (do ((list args (cdr list)))
189 ((endp list))
190 (loop
191 with x = (car list)
192 for y in (rest list)
193 for z = (remove-duplicates (intersection (cdr x)(cdr y) :test #'string=))
194 when z do (error 'simple-program-error
195 :format-control "Parameters ~S and ~S must be disjoint ~
196 but have common elements ~% ~S"
197 :format-arguments (list (car x)(car y) z)))))
198
199 (defun dodefpackage
200 (name
201 nicknames
202 documentation
203 lock
204 use
205 local-nicknames
206 shadowed-symbol-names
207 interned-symbol-names
208 exported-symbol-names
209 shadowing-imported-from-symbol-names-list
210 imported-from-symbol-names-list
211 exported-from-package-names)
212 (if (find-package name)
213 (progn ; (rename-package name name)
214 (when nicknames
215 (rename-package name name nicknames))
216 (when use
217 (unuse-package (package-use-list (find-package name)) name)))
218 (make-package name :use nil :nicknames nicknames :local-nicknames local-nicknames))
219 (let ((*package* (find-package name)))
220 (when documentation
221 (setf (documentation *package* t) documentation))
222 (shadow shadowed-symbol-names)
223 (dolist (item shadowing-imported-from-symbol-names-list)
224 (let ((package (find-package (first item))))
225 (dolist (name (rest item))
226 (shadowing-import (find-or-make-symbol name package)))))
227 (use-package use)
228 (dolist (item imported-from-symbol-names-list)
229 (let ((package (find-package (first item))))
230 (dolist (name (rest item))
231 ;; IMPORT can accept a list as argument, hence if we want to
232 ;; import symbol NIL, we have to enclose it in a list.
233 (import (or (find-or-make-symbol name package) (list NIL))))))
234 (mapc #'intern interned-symbol-names)
235 (export (mapcar #'intern exported-symbol-names))
236 (dolist (package exported-from-package-names)
237 (do-external-symbols (symbol (find-package package))
238 (when (nth 1 (multiple-value-list
239 (find-symbol (string symbol))))
240 (export (list (intern (string symbol))))))))
241 (when lock (lock-package name))
242 (find-package name))
243
244 (defun find-or-make-symbol (name package)
245 (declare (si::c-local))
246 (multiple-value-bind (symbol found)
247 (find-symbol name package)
248 (unless found
249 (signal-simple-error 'package-error "INTERN it."
250 "Cannot find symbol ~S in package ~S"
251 (list name package)
252 :package package)
253 (setq symbol (intern name package)))
254 symbol))
255
256 ;;;; ------------------------------------------------------------
257 ;;;; End of File
258 ;;;; ------------------------------------------------------------