1 ;;;; the PARSE-DEFMACRO function and related code
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!KERNEL")
14 ;;; variables for accumulating the results of parsing a DEFMACRO. (Declarations
15 ;;; in DEFMACRO are the reason this isn't as easy as it sounds.)
16 (defvar *arg-tests* nil) ; tests that do argument counting at expansion time
17 (declaim (type list *arg-tests*))
18 (defvar *system-lets* nil) ; LET bindings done to allow lambda-list parsing
19 (declaim (type list *system-lets*))
20 (defvar *user-lets* nil) ; LET bindings that the user has explicitly supplied
21 (declaim (type list *user-lets*))
22 (defvar *env-var* nil) ; &ENVIRONMENT variable name
24 ;; the default default for unsupplied &OPTIONAL and &KEY args
25 (defvar *default-default* nil)
27 ;;; temps that we introduce and might not reference
28 (defvar *ignorable-vars*)
29 (declaim (type list *ignorable-vars*))
31 ;;; Return, as multiple values, a body, possibly a declare form to put
32 ;;; where this code is inserted, the documentation for the parsed
33 ;;; body, and bounds on the number of arguments.
34 (defun parse-defmacro (lambda-list arg-list-name body name error-kind
37 (doc-string-allowed t)
38 ((:environment env-arg-name))
39 ((:default-default *default-default*))
41 (multiple-value-bind (forms declarations documentation)
42 (parse-body body doc-string-allowed)
43 (let ((*arg-tests* ())
48 (multiple-value-bind (env-arg-used minimum maximum)
49 (parse-defmacro-lambda-list lambda-list arg-list-name name
50 error-kind error-fun (not anonymousp)
52 (values `(let* (,@(when env-arg-used
53 `((,*env-var* ,env-arg-name)))
54 ,@(nreverse *system-lets*))
55 ,@(when *ignorable-vars*
56 `((declare (ignorable ,@*ignorable-vars*))))
58 (let* ,(nreverse *user-lets*)
61 `(,@(when (and env-arg-name (not env-arg-used))
62 `((declare (ignore ,env-arg-name)))))
67 ;;; partial reverse-engineered documentation:
68 ;;; TOPLEVEL is true for calls through PARSE-DEFMACRO from DEFSETF and
69 ;;; DESTRUCTURING-BIND, false otherwise.
71 (defun parse-defmacro-lambda-list (possibly-dotted-lambda-list
79 (let* (;; PATH is a sort of pointer into the part of the lambda list we're
80 ;; considering at this point in the code. PATH-0 is the root of the
81 ;; lambda list, which is the initial value of PATH.
85 (path path-0) ; (will change below)
86 (now-processing :required)
91 ;; ANSI specifies that dotted lists are "treated exactly as if the
92 ;; parameter name that ends the list had appeared preceded by &rest."
93 ;; We force this behavior by transforming dotted lists into ordinary
94 ;; lists with explicit &REST elements.
95 (lambda-list (do ((in-pdll possibly-dotted-lambda-list (cdr in-pdll))
96 (reversed-result nil))
99 (list* in-pdll '&rest reversed-result)
101 (push (car in-pdll) reversed-result)))
102 rest-name restp allow-other-keys-p env-arg-used)
103 (when (member '&whole (rest lambda-list))
104 (error "&WHOLE may only appear first in ~S lambda-list." error-kind))
105 (do ((rest-of-args lambda-list (cdr rest-of-args)))
106 ((null rest-of-args))
107 (macrolet ((process-sublist (var sublist-name path)
108 (once-only ((var var))
110 (let ((sub-list-name (gensym ,sublist-name)))
111 (push-sub-list-binding sub-list-name ,path ,var
112 name error-kind error-fun)
113 (parse-defmacro-lambda-list ,var sub-list-name name
114 error-kind error-fun))
115 (push-let-binding ,var ,path nil)))))
116 (let ((var (car rest-of-args)))
122 (defmacro-error "required argument after &REST/&BODY"
124 (process-sublist var "SUBLIST-" `(car ,path))
125 (setq path `(cdr ,path)
127 maximum (1+ maximum)))
129 (destructuring-bind (varname &optional initform supplied-p)
131 (push-optional-binding varname initform supplied-p
132 `(not (null ,path)) `(car ,path)
133 name error-kind error-fun))
134 (setq path `(cdr ,path)
135 maximum (1+ maximum)))
137 (let* ((keyword-given (consp (car var)))
138 (variable (if keyword-given
141 (keyword (if keyword-given
143 (keywordicate variable)))
144 (supplied-p (caddr var)))
145 (push-optional-binding variable (cadr var) supplied-p
146 `(keyword-supplied-p ',keyword
148 `(lookup-keyword ',keyword
150 name error-kind error-fun)
151 (push keyword keys)))
153 (push-let-binding (car var) (cadr var) nil))))
154 ((and symbol (not (eql nil)))
157 (cond ((cdr rest-of-args)
158 (setq rest-of-args (cdr rest-of-args))
159 (process-sublist (car rest-of-args)
160 "WHOLE-LIST-" arg-list-name))
162 (defmacro-error "&WHOLE" error-kind name))))
165 (error "&ENVIRONMENT is not valid with ~S." error-kind))
167 (error "&ENVIRONMENT is only valid at top level of ~
170 (error "Repeated &ENVIRONMENT.")))
171 (cond ((and (cdr rest-of-args) (symbolp (cadr rest-of-args)))
172 (setq rest-of-args (cdr rest-of-args))
173 (check-defmacro-arg (car rest-of-args))
174 (setq *env-var* (car rest-of-args))
175 (setq env-arg-used t))
177 (defmacro-error "&ENVIRONMENT" error-kind name))))
179 (cond ((and (not restp) (cdr rest-of-args))
180 (setq rest-of-args (cdr rest-of-args))
182 (process-sublist (car rest-of-args) "REST-LIST-" path))
184 (defmacro-error (symbol-name var) error-kind name))))
186 (setq now-processing :optionals))
188 (setq now-processing :keywords)
189 (setq rest-name (gensym "KEYWORDS-"))
190 (push rest-name *ignorable-vars*)
193 (push-let-binding rest-name path t))
195 (setq allow-other-keys-p t))
197 (setq now-processing :auxs))
198 ;; FIXME: Other lambda list keywords.
203 (defmacro-error "required argument after &REST/&BODY"
205 (push-let-binding var `(car ,path) nil)
206 (setq minimum (1+ minimum)
210 (push-let-binding var `(car ,path) nil `(not (null ,path)))
211 (setq path `(cdr ,path)
212 maximum (1+ maximum)))
214 (let ((key (keywordicate var)))
215 (push-let-binding var
216 `(lookup-keyword ,key ,rest-name)
220 (push-let-binding var nil nil))))))
222 (error "non-symbol in lambda-list: ~S" var))))))
223 (let (;; common subexpression, suitable for passing to functions
224 ;; which expect a MAXIMUM argument regardless of whether
225 ;; there actually is a maximum number of arguments
226 ;; (expecting MAXIMUM=NIL when there is no maximum)
227 (explicit-maximum (and (not restp) maximum)))
228 (unless (and restp (zerop minimum))
229 (push `(unless ,(if restp
230 ;; (If RESTP, then the argument list might be
231 ;; dotted, in which case ordinary LENGTH won't
233 `(list-of-length-at-least-p ,path-0 ,minimum)
234 `(proper-list-of-length-p ,path-0 ,minimum ,maximum))
235 ,(if (eq error-fun 'error)
236 `(arg-count-error ',error-kind ',name ,path-0
237 ',lambda-list ,minimum
239 `(,error-fun 'arg-count-error
241 ,@(when name `(:name ',name))
243 :lambda-list ',lambda-list
245 :maximum ,explicit-maximum)))
248 (let ((problem (gensym "KEY-PROBLEM-"))
249 (info (gensym "INFO-")))
250 (push `(multiple-value-bind (,problem ,info)
251 (verify-keywords ,rest-name
253 ',allow-other-keys-p)
256 'defmacro-lambda-list-broken-key-list-error
258 ,@(when name `(:name ',name))
262 (values env-arg-used minimum explicit-maximum))))
264 ;;; We save space in macro definitions by calling this function.
265 (defun arg-count-error (error-kind name args lambda-list minimum maximum)
267 (sb!debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
268 (error 'arg-count-error
272 :lambda-list lambda-list
276 (defun push-sub-list-binding (variable path object name error-kind error-fun)
277 (check-defmacro-arg variable)
278 (let ((var (gensym "TEMP-")))
283 (,error-fun 'defmacro-bogus-sublist-error
285 ,@(when name `(:name ',name))
287 :lambda-list ',object))))
290 (defun push-let-binding (variable path systemp &optional condition
291 (init-form *default-default*))
292 (check-defmacro-arg variable)
293 (let ((let-form (if condition
294 `(,variable (if ,condition ,path ,init-form))
295 `(,variable ,path))))
297 (push let-form *system-lets*)
298 (push let-form *user-lets*))))
300 (defun push-optional-binding (value-var init-form supplied-var condition path
301 name error-kind error-fun)
303 (setq supplied-var (gensym "SUPPLIEDP-")))
304 (push-let-binding supplied-var condition t)
305 (cond ((consp value-var)
306 (let ((whole-thing (gensym "OPTIONAL-SUBLIST-")))
307 (push-sub-list-binding whole-thing
308 `(if ,supplied-var ,path ,init-form)
309 value-var name error-kind error-fun)
310 (parse-defmacro-lambda-list value-var whole-thing name
311 error-kind error-fun)))
313 (push-let-binding value-var path nil supplied-var init-form))
315 (error "illegal optional variable name: ~S" value-var))))
317 (defun defmacro-error (problem kind name)
318 (error "illegal or ill-formed ~A argument in ~A~@[ ~S~]"
321 (defun check-defmacro-arg (arg)
322 (when (or (and *env-var* (eq arg *env-var*))
323 (member arg *system-lets* :key #'car)
324 (member arg *user-lets* :key #'car))
325 (error "variable ~S occurs more than once" arg)))
327 ;;; Determine whether KEY-LIST is a valid list of keyword/value pairs.
328 ;;; Do not signal the error directly, 'cause we don't know how it
329 ;;; should be signaled.
330 (defun verify-keywords (key-list valid-keys allow-other-keys)
331 (do ((already-processed nil)
332 (unknown-keyword nil)
333 (remaining key-list (cddr remaining)))
335 (if (and unknown-keyword
336 (not allow-other-keys)
337 (not (lookup-keyword :allow-other-keys key-list)))
338 (values :unknown-keyword (list unknown-keyword valid-keys))
340 (cond ((not (and (consp remaining) (listp (cdr remaining))))
341 (return (values :dotted-list key-list)))
342 ((null (cdr remaining))
343 (return (values :odd-length key-list)))
344 ((or (eq (car remaining) :allow-other-keys)
345 (member (car remaining) valid-keys))
346 (push (car remaining) already-processed))
348 (setq unknown-keyword (car remaining))))))
350 (defun lookup-keyword (keyword key-list)
351 (do ((remaining key-list (cddr remaining)))
353 (when (eq keyword (car remaining))
354 (return (cadr remaining)))))
356 (defun keyword-supplied-p (keyword key-list)
357 (do ((remaining key-list (cddr remaining)))
359 (when (eq keyword (car remaining))