1.0.41.45: make.sh command-line parsing
[sbcl.git] / src / code / parse-defmacro.lisp
1 ;;;; the PARSE-DEFMACRO function and related code
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!KERNEL")
13
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
23
24 ;; the default default for unsupplied &OPTIONAL and &KEY args
25 (defvar *default-default*)
26
27 ;;; temps that we introduce and might not reference
28 (defvar *ignorable-vars*)
29 (declaim (type list *ignorable-vars*))
30
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
35                        &key
36                        (anonymousp nil)
37                        (doc-string-allowed t)
38                        ((:environment env-arg-name))
39                        ((:default-default *default-default*))
40                        (error-fun 'error)
41                        (wrap-block t))
42   (multiple-value-bind (forms declarations documentation)
43       (parse-body body :doc-string-allowed doc-string-allowed)
44     (let ((*arg-tests* ())
45           (*user-lets* ())
46           (*system-lets* ())
47           (*ignorable-vars* ())
48           (*env-var* nil))
49       (multiple-value-bind (env-arg-used minimum maximum)
50           (parse-defmacro-lambda-list lambda-list whole-var name context
51                                       :error-fun error-fun
52                                       :anonymousp anonymousp)
53         (values `(let* (,@(nreverse *system-lets*))
54                    #-sb-xc-host
55                    (declare (muffle-conditions sb!ext:code-deletion-note))
56                    ,@(when *ignorable-vars*
57                        `((declare (ignorable ,@*ignorable-vars*))))
58                    ,@*arg-tests*
59                    (let* (,@(when env-arg-used
60                             `((,*env-var* ,env-arg-name)))
61                           ,@(nreverse *user-lets*))
62                      ,@declarations
63                      ,@(if wrap-block
64                            `((block ,(fun-name-block-name name)
65                                ,@forms))
66                            forms)))
67                 `(,@(when (and env-arg-name (not env-arg-used))
68                       `((declare (ignore ,env-arg-name)))))
69                 documentation
70                 minimum
71                 maximum)))))
72
73 (defun parse-defmacro-lambda-list (possibly-dotted-lambda-list
74                                    whole-var
75                                    name
76                                    context
77                                    &key
78                                    error-fun
79                                    anonymousp
80                                    env-illegal
81                                    sublist)
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)
89          (maximum 0)
90          (minimum 0)
91          (keys ())
92          (key-seen nil)
93          (aux-seen nil)
94          (optional-seen nil)
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))
101                           ((atom in-pdll)
102                            (nreverse (if in-pdll
103                                          (list* in-pdll '&rest reversed-result)
104                                          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
116                         :system t
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))
124                      `(if (listp ,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
129                                                         context
130                                                         :error-fun error-fun
131                                                         :sublist t))
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)))
137           (typecase var
138             (list
139              (case now-processing
140                ((:required)
141                 (when restp
142                   (defmacro-error (format nil "required argument after ~A"
143                                           restp)
144                       context name))
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)
150                       minimum (1+ minimum)
151                       maximum (1+ maximum)))
152                ((:optionals)
153                 (normalize-singleton var)
154                 (destructuring-bind
155                       (varname &optional default-form suppliedp-name)
156                     var
157                   (push-optional-binding varname default-form suppliedp-name
158                                          :is-supplied-p `(not (null ,path))
159                                          :path `(car ,path)
160                                          :name name
161                                          :context context
162                                          :error-fun error-fun))
163                 (setq path `(cdr ,path)
164                       maximum (1+ maximum)))
165                ((:keywords)
166                 (normalize-singleton var)
167                 (let* ((keyword-given (consp (car var)))
168                        (variable (if keyword-given
169                                      (cadar var)
170                                      (car var)))
171                        (keyword (if keyword-given
172                                     (caar var)
173                                     (keywordicate variable)))
174                        (default-form (cadr var))
175                        (suppliedp-name (caddr var)))
176                   (push-optional-binding variable default-form suppliedp-name
177                                          :is-supplied-p
178                                          `(keyword-supplied-p ',keyword
179                                                               ,rest-name)
180                                          :path
181                                          `(lookup-keyword ',keyword ,rest-name)
182                                          :name name
183                                          :context context
184                                          :error-fun error-fun)
185                   (push keyword keys)))
186                ((:auxs)
187                 (push-let-binding (car var) (cadr var)))))
188             ((and symbol (not (eql nil)))
189              (case var
190                (&whole
191                 (cond ((cdr rest-of-lambda-list)
192                        (pop rest-of-lambda-list)
193                        (process-sublist (car rest-of-lambda-list)
194                                         "WHOLE-LIST-"
195                                         (if (eq 'define-compiler-macro context)
196                                             compiler-macro-whole
197                                             whole-var)))
198                       (t
199                        (defmacro-error "&WHOLE" context name))))
200                (&environment
201                 (cond (env-illegal
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 ~
207                              lambda-list."))
208                       (env-arg-used
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)
215                              env-arg-used t))
216                       (t
217                        (defmacro-error "&ENVIRONMENT" context name))))
218                ((&rest &body)
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)
224                              restp var)
225                        (process-sublist (car rest-of-lambda-list)
226                                         "REST-LIST-" path))
227                       (t
228                        (defmacro-error (symbol-name var) context name))))
229                (&optional
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))
233                 (when optional-seen
234                   (error "Multiple ~A in ~A lambda list." var context))
235                 (setq now-processing :optionals
236                       optional-seen var))
237                (&key
238                 (when aux-seen
239                   (error "~A after ~A in ~A lambda-list." '&key '&aux context))
240                 (when key-seen
241                   (error "Multiple ~A in ~A lambda-list." '&key context))
242                 (setf now-processing :keywords
243                       rest-name (gensym "KEYWORDS-")
244                       restp var
245                       key-seen var)
246                 (push rest-name *ignorable-vars*)
247                 (push-let-binding rest-name path :system t))
248                (&allow-other-keys
249                 (unless (eq now-processing :keywords)
250                   (error "~A outside ~A section of lambda-list in ~A."
251                          var '&key context))
252                 (when allow-other-keys-p
253                   (error "Multiple ~A in ~A lambda-list." var context))
254                 (setq allow-other-keys-p t))
255                (&aux
256                 (when (eq context 'defsetf)
257                   (error "~A not allowed in a ~A lambda-list." var context))
258                 (when aux-seen
259                   (error "Multiple ~A in ~A lambda-list." '&aux context))
260                 (setq now-processing :auxs
261                       aux-seen var))
262                ;; FIXME: Other lambda list keywords.
263                (t
264                 (case now-processing
265                   ((:required)
266                    (when restp
267                      (defmacro-error (format nil "required argument after ~A"
268                                              restp)
269                          context name))
270                    (push-let-binding var `(car ,path))
271                    (setq minimum (1+ minimum)
272                          maximum (1+ maximum)
273                          path `(cdr ,path)))
274                   ((:optionals)
275                    (push-let-binding var `(car ,path)
276                                      :when `(not (null ,path)))
277                    (setq path `(cdr ,path)
278                          maximum (1+ maximum)))
279                   ((:keywords)
280                    (let ((key (keywordicate var)))
281                      (push-let-binding
282                       var
283                       `(lookup-keyword ,key ,rest-name)
284                       :when `(keyword-supplied-p ,key ,rest-name))
285                      (push key keys)))
286                   ((:auxs)
287                    (push-let-binding var nil))))))
288             (t
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))
298                                         (cdr ,path-0)
299                                         ,path-0)
300                                    path-0)))
301                 (with-unique-names (args)
302                   `(let ((,args ,args-form))
303                      (unless ,(if restp
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
309                                                             ,minimum
310                                                             ,maximum))
311                        ,(if (eq error-fun 'error)
312                             `(arg-count-error ',context ',name ,args
313                                               ',lambda-list ,minimum
314                                               ,explicit-maximum)
315                             `(,error-fun 'arg-count-error
316                                          :kind ',context
317                                          ,@(when name `(:name ',name))
318                                          :args ,args
319                                          :lambda-list ',lambda-list
320                                          :minimum ,minimum
321                                          :maximum ,explicit-maximum))))))
322               *arg-tests*))
323       (when key-seen
324         (with-unique-names (problem info)
325           (push `(multiple-value-bind (,problem ,info)
326                      (verify-keywords ,rest-name
327                                       ',keys
328                                       ',allow-other-keys-p)
329                    (when ,problem
330                      (,error-fun
331                       'defmacro-lambda-list-broken-key-list-error
332                       :kind ',context
333                       ,@(when name `(:name ',name))
334                       :problem ,problem
335                       :info ,info)))
336                 *arg-tests*)))
337       (values env-arg-used minimum explicit-maximum))))
338
339 ;;; We save space in macro definitions by calling this function.
340 (defun arg-count-error (context name args lambda-list minimum maximum)
341   (let (#-sb-xc-host
342         (sb!debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
343     (error 'arg-count-error
344            :kind context
345            :name name
346            :args args
347            :lambda-list lambda-list
348            :minimum minimum
349            :maximum maximum)))
350
351 (defun push-sublist-binding (variable path object name context error-fun)
352   (check-defmacro-arg variable)
353   (let ((var (gensym "TEMP-")))
354     (push `(,variable
355             (let ((,var ,path))
356               (if (listp ,var)
357                 ,var
358                 (,error-fun 'defmacro-bogus-sublist-error
359                             :kind ',context
360                             ,@(when name `(:name ',name))
361                             :object ,var
362                             :lambda-list ',object))))
363           *system-lets*)))
364
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))))
371     (if system
372         (push let-form *system-lets*)
373         (push let-form *user-lets*))))
374
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
386                                        context
387                                        :error-fun error-fun
388                                        :sublist t)))
389         ((symbolp value-var)
390          (push-let-binding value-var path :when suppliedp-name :else init-form))
391         (t
392          (error "illegal optional variable name: ~S" value-var))))
393
394 (defun defmacro-error (problem context name)
395   (error "illegal or ill-formed ~A argument in ~A~@[ ~S~]"
396          problem context name))
397
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)))
403
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)))
411       ((null 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))
416            (values nil nil)))
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))
424           (t
425            (setq unknown-keyword (car remaining))))))
426
427 (defun lookup-keyword (keyword key-list)
428   (do ((remaining key-list (cddr remaining)))
429       ((endp remaining))
430     (when (eq keyword (car remaining))
431       (return (cadr remaining)))))
432
433 (defun keyword-supplied-p (keyword key-list)
434   (do ((remaining key-list (cddr remaining)))
435       ((endp remaining))
436     (when (eq keyword (car remaining))
437       (return t))))