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*) ; tests that do argument counting at expansion time
17 (declaim (type list *arg-tests*))
18 (defvar *system-lets*) ; LET bindings done to allow lambda-list parsing
19 (declaim (type list *system-lets*))
20 (defvar *user-lets*) ; LET bindings that the user has explicitly supplied
21 (declaim (type list *user-lets*))
22 (defvar *env-var*) ; &ENVIRONMENT variable name
24 ;; the default default for unsupplied &OPTIONAL and &KEY args
25 (defvar *default-default*)
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 whole-var body name context
37 (doc-string-allowed t)
38 ((:environment env-arg-name))
39 ((:default-default *default-default*))
42 (multiple-value-bind (forms declarations documentation)
43 (parse-body body :doc-string-allowed doc-string-allowed)
44 (let ((*arg-tests* ())
49 (multiple-value-bind (env-arg-used minimum maximum)
50 (parse-defmacro-lambda-list lambda-list whole-var name context
52 :anonymousp anonymousp)
53 (values `(let* (,@(nreverse *system-lets*))
55 (declare (muffle-conditions sb!ext:code-deletion-note))
56 ,@(when *ignorable-vars*
57 `((declare (ignorable ,@*ignorable-vars*))))
59 (let* (,@(when env-arg-used
60 `((,*env-var* ,env-arg-name)))
61 ,@(nreverse *user-lets*))
64 `((block ,(fun-name-block-name name)
67 `(,@(when (and env-arg-name (not env-arg-used))
68 `((declare (ignore ,env-arg-name)))))
73 (defun parse-defmacro-lambda-list (possibly-dotted-lambda-list
82 (let* (;; PATH is a sort of pointer into the part of the lambda list we're
83 ;; considering at this point in the code. PATH-0 is the root of the
84 ;; lambda list, which is the initial value of PATH.
85 (path-0 (if (or anonymousp sublist) whole-var `(cdr ,whole-var)))
86 (path path-0) ; will change below
87 (compiler-macro-whole (gensym "CMACRO-&WHOLE"))
88 (now-processing :required)
95 ;; ANSI specifies that dotted lists are "treated exactly as if the
96 ;; parameter name that ends the list had appeared preceded by &REST."
97 ;; We force this behavior by transforming dotted lists into ordinary
98 ;; lists with explicit &REST elements.
99 (lambda-list (do ((in-pdll possibly-dotted-lambda-list (cdr in-pdll))
100 (reversed-result nil))
102 (nreverse (if in-pdll
103 (list* in-pdll '&rest reversed-result)
105 (push (car in-pdll) reversed-result)))
106 rest-name restp allow-other-keys-p env-arg-used)
107 (when (member '&whole (rest lambda-list))
108 (error "&WHOLE may only appear first in ~S lambda-list." context))
109 ;; Special case compiler-macros: if car of the form is FUNCALL,
110 ;; skip over it for destructuring, pretending cdr of the form is
111 ;; the actual form. Save original for &WHOLE.
112 (when (and (not sublist) (eq context 'define-compiler-macro))
113 (push-let-binding compiler-macro-whole whole-var :system t)
114 (push compiler-macro-whole *ignorable-vars*)
115 (push-let-binding whole-var whole-var
117 :when `(not (eq 'funcall (car ,whole-var)))
118 ;; Do we need to SETF too?
119 :else `(setf ,whole-var (cdr ,whole-var))))
120 (do ((rest-of-lambda-list lambda-list (cdr rest-of-lambda-list)))
121 ((null rest-of-lambda-list))
122 (macrolet ((process-sublist (var kind path)
123 (once-only ((var var))
125 (let ((sublist-name (gensym ,kind)))
126 (push-sublist-binding sublist-name ,path ,var
127 name context error-fun)
128 (parse-defmacro-lambda-list ,var sublist-name name
132 (push-let-binding ,var ,path))))
133 (normalize-singleton (var)
134 `(when (null (cdr ,var))
135 (setf (cdr ,var) (list *default-default*)))))
136 (let ((var (car rest-of-lambda-list)))
142 (defmacro-error (format nil "required argument after ~A"
145 (when (process-sublist var "REQUIRED-" `(car ,path))
146 ;; Note &ENVIRONMENT from DEFSETF sublist
147 (aver (eq context 'defsetf))
148 (setf env-arg-used t))
149 (setq path `(cdr ,path)
151 maximum (1+ maximum)))
153 (normalize-singleton var)
155 (varname &optional default-form suppliedp-name)
157 (push-optional-binding varname default-form suppliedp-name
158 :is-supplied-p `(not (null ,path))
162 :error-fun error-fun))
163 (setq path `(cdr ,path)
164 maximum (1+ maximum)))
166 (normalize-singleton var)
167 (let* ((keyword-given (consp (car var)))
168 (variable (if keyword-given
171 (keyword (if keyword-given
173 (keywordicate variable)))
174 (default-form (cadr var))
175 (suppliedp-name (caddr var)))
176 (push-optional-binding variable default-form suppliedp-name
178 `(keyword-supplied-p ',keyword
181 `(lookup-keyword ',keyword ,rest-name)
184 :error-fun error-fun)
185 (push keyword keys)))
187 (push-let-binding (car var) (cadr var)))))
188 ((and symbol (not (eql nil)))
191 (cond ((cdr rest-of-lambda-list)
192 (pop rest-of-lambda-list)
193 (process-sublist (car rest-of-lambda-list)
195 (if (eq 'define-compiler-macro context)
199 (defmacro-error "&WHOLE" context name))))
202 (error "&ENVIRONMENT is not valid with ~S." context))
203 ;; DEFSETF explicitly allows &ENVIRONMENT, and we get
204 ;; it here in a sublist.
205 ((and sublist (neq context 'defsetf))
206 (error "&ENVIRONMENT is only valid at top level of ~
209 (error "Repeated &ENVIRONMENT.")))
210 (cond ((and (cdr rest-of-lambda-list)
211 (symbolp (cadr rest-of-lambda-list)))
212 (setq rest-of-lambda-list (cdr rest-of-lambda-list))
213 (check-defmacro-arg (car rest-of-lambda-list))
214 (setq *env-var* (car rest-of-lambda-list)
217 (defmacro-error "&ENVIRONMENT" context name))))
219 (cond ((or key-seen aux-seen)
220 (error "~A after ~A in ~A"
221 var (or key-seen aux-seen) context))
222 ((and (not restp) (cdr rest-of-lambda-list))
223 (setq rest-of-lambda-list (cdr rest-of-lambda-list)
225 (process-sublist (car rest-of-lambda-list)
228 (defmacro-error (symbol-name var) context name))))
230 (when (or key-seen aux-seen restp)
231 (error "~A after ~A in ~A lambda-list."
232 var (or key-seen aux-seen restp) context))
234 (error "Multiple ~A in ~A lambda list." var context))
235 (setq now-processing :optionals
239 (error "~A after ~A in ~A lambda-list." '&key '&aux context))
241 (error "Multiple ~A in ~A lambda-list." '&key context))
242 (setf now-processing :keywords
243 rest-name (gensym "KEYWORDS-")
246 (push rest-name *ignorable-vars*)
247 (push-let-binding rest-name path :system t))
249 (unless (eq now-processing :keywords)
250 (error "~A outside ~A section of lambda-list in ~A."
252 (when allow-other-keys-p
253 (error "Multiple ~A in ~A lambda-list." var context))
254 (setq allow-other-keys-p t))
256 (when (eq context 'defsetf)
257 (error "~A not allowed in a ~A lambda-list." var context))
259 (error "Multiple ~A in ~A lambda-list." '&aux context))
260 (setq now-processing :auxs
262 ;; FIXME: Other lambda list keywords.
267 (defmacro-error (format nil "required argument after ~A"
270 (push-let-binding var `(car ,path))
271 (setq minimum (1+ minimum)
275 (push-let-binding var `(car ,path)
276 :when `(not (null ,path)))
277 (setq path `(cdr ,path)
278 maximum (1+ maximum)))
280 (let ((key (keywordicate var)))
283 `(lookup-keyword ,key ,rest-name)
284 :when `(keyword-supplied-p ,key ,rest-name))
287 (push-let-binding var nil))))))
289 (error "non-symbol in lambda-list: ~S" var))))))
290 (let (;; common subexpression, suitable for passing to functions
291 ;; which expect a MAXIMUM argument regardless of whether
292 ;; there actually is a maximum number of arguments
293 ;; (expecting MAXIMUM=NIL when there is no maximum)
294 (explicit-maximum (and (not restp) maximum)))
295 (unless (and restp (zerop minimum))
296 (push (let ((args-form (if (eq 'define-compiler-macro context)
297 `(if (eq 'funcall (car ,whole-var))
301 (with-unique-names (args)
302 `(let ((,args ,args-form))
304 ;; (If RESTP, then the argument list
305 ;; might be dotted, in which case
306 ;; ordinary LENGTH won't work.)
307 `(list-of-length-at-least-p ,args ,minimum)
308 `(proper-list-of-length-p ,args
311 ,(if (eq error-fun 'error)
312 `(arg-count-error ',context ',name ,args
313 ',lambda-list ,minimum
315 `(,error-fun 'arg-count-error
317 ,@(when name `(:name ',name))
319 :lambda-list ',lambda-list
321 :maximum ,explicit-maximum))))))
324 (with-unique-names (problem info)
325 (push `(multiple-value-bind (,problem ,info)
326 (verify-keywords ,rest-name
328 ',allow-other-keys-p)
331 'defmacro-lambda-list-broken-key-list-error
333 ,@(when name `(:name ',name))
337 (values env-arg-used minimum explicit-maximum))))
339 ;;; We save space in macro definitions by calling this function.
340 (defun arg-count-error (context name args lambda-list minimum maximum)
342 (sb!debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
343 (error 'arg-count-error
347 :lambda-list lambda-list
351 (defun push-sublist-binding (variable path object name context error-fun)
352 (check-defmacro-arg variable)
353 (let ((var (gensym "TEMP-")))
358 (,error-fun 'defmacro-bogus-sublist-error
360 ,@(when name `(:name ',name))
362 :lambda-list ',object))))
365 (defun push-let-binding (variable form
366 &key system when (else *default-default*))
367 (check-defmacro-arg variable)
368 (let ((let-form (if when
369 `(,variable (if ,when ,form ,else))
370 `(,variable ,form))))
372 (push let-form *system-lets*)
373 (push let-form *user-lets*))))
375 (defun push-optional-binding (value-var init-form suppliedp-name
376 &key is-supplied-p path name context error-fun)
377 (unless suppliedp-name
378 (setq suppliedp-name (gensym "SUPPLIEDP-")))
379 (push-let-binding suppliedp-name is-supplied-p :system t)
380 (cond ((consp value-var)
381 (let ((whole-thing (gensym "OPTIONAL-SUBLIST-")))
382 (push-sublist-binding whole-thing
383 `(if ,suppliedp-name ,path ,init-form)
384 value-var name context error-fun)
385 (parse-defmacro-lambda-list value-var whole-thing name
390 (push-let-binding value-var path :when suppliedp-name :else init-form))
392 (error "illegal optional variable name: ~S" value-var))))
394 (defun defmacro-error (problem context name)
395 (error "illegal or ill-formed ~A argument in ~A~@[ ~S~]"
396 problem context name))
398 (defun check-defmacro-arg (arg)
399 (when (or (and *env-var* (eq arg *env-var*))
400 (member arg *system-lets* :key #'car)
401 (member arg *user-lets* :key #'car))
402 (error "variable ~S occurs more than once" arg)))
404 ;;; Determine whether KEY-LIST is a valid list of keyword/value pairs.
405 ;;; Do not signal the error directly, 'cause we don't know how it
406 ;;; should be signaled.
407 (defun verify-keywords (key-list valid-keys allow-other-keys)
408 (do ((already-processed nil)
409 (unknown-keyword nil)
410 (remaining key-list (cddr remaining)))
412 (if (and unknown-keyword
413 (not allow-other-keys)
414 (not (lookup-keyword :allow-other-keys key-list)))
415 (values :unknown-keyword (list unknown-keyword valid-keys))
417 (cond ((not (and (consp remaining) (listp (cdr remaining))))
418 (return (values :dotted-list key-list)))
419 ((null (cdr remaining))
420 (return (values :odd-length key-list)))
421 ((or (eq (car remaining) :allow-other-keys)
422 (member (car remaining) valid-keys))
423 (push (car remaining) already-processed))
425 (setq unknown-keyword (car remaining))))))
427 (defun lookup-keyword (keyword key-list)
428 (do ((remaining key-list (cddr remaining)))
430 (when (eq keyword (car remaining))
431 (return (cadr remaining)))))
433 (defun keyword-supplied-p (keyword key-list)
434 (do ((remaining key-list (cddr remaining)))
436 (when (eq keyword (car remaining))