Workaround for type inference problem
[sbcl.git] / src / code / backq.lisp
blob947a2d6dc8fe5521caa60287e576ccc77838977b
1 ;;;; the backquote reader macro
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.
12 (in-package "SB!IMPL")
14 (/show0 "entering backq.lisp")
16 ;; An unquoting COMMA struct.
17 (defstruct (comma (:constructor unquote (expr &optional (kind 0)))
18 ;; READing unpretty commas requires a default constructor.
19 (:constructor %default-comma-constructor)
20 (:copier nil))
21 (expr nil :read-only t)
22 (kind nil :read-only t :type (member 0 1 2)))
23 #+sb-xc (declaim (freeze-type comma))
25 (defconstant !+comma-dot+ 1)
26 (defconstant !+comma-at+ 2)
27 (defun unquote-nsplice (x) (unquote x !+comma-dot+))
28 (defun unquote-splice (x) (unquote x !+comma-at+))
29 (defun unquote* (list) (mapcar #'unquote list))
30 (defun unquote*-splice (list) (mapcar #'unquote-splice list))
31 (declaim (inline comma-constructor comma-splicing-p))
32 (defun comma-constructor (x)
33 (svref #(unquote unquote-nsplice unquote-splice) (comma-kind x)))
34 (defun comma-splicing-p (comma) (not (zerop (comma-kind comma))))
36 (declaim (inline singleton-p))
37 (defun singleton-p (list)
38 (and (consp list)
39 (null (rest list))))
41 #+sb-xc-host
42 (progn
43 ;; tell the host how to dump it
44 (defmethod make-load-form ((self comma) &optional environment)
45 (declare (ignore environment))
46 (list (comma-constructor self) (list 'quote (comma-expr self))))
47 ;; tell the cross-compiler that it can do :just-dump-it-normally
48 (setf (get 'comma :sb-xc-allow-dumping-instances) t))
50 (declaim (type (and fixnum unsigned-byte) *backquote-depth*))
51 (defvar *backquote-depth* 0 #!+sb-doc "how deep we are into backquotes")
52 (defvar *bq-error* "Comma not inside a backquote.")
54 (/show0 "backq.lisp 50")
56 ;;; the actual character macro
57 (defun backquote-charmacro (stream char)
58 (declare (ignore char))
59 (let* ((expr (let ((*backquote-depth* (1+ *backquote-depth*)))
60 (read stream t nil t)))
61 (result (list 'quasiquote expr)))
62 (if (and (comma-p expr) (comma-splicing-p expr))
63 ;; use RESULT rather than EXPR in the error so it pprints nicely
64 (simple-reader-error
65 stream "~S is not a well-formed backquote expression" result)
66 result)))
68 (/show0 "backq.lisp 64")
70 (defun comma-charmacro (stream char)
71 (declare (ignore char))
72 (unless (> *backquote-depth* 0)
73 (when *read-suppress*
74 (return-from comma-charmacro nil))
75 (simple-reader-error stream *bq-error*))
76 (let ((flag (let ((c (read-char stream)))
77 (case c
78 (#\@ !+comma-at+)
79 (#\. !+comma-dot+)
80 (t (unread-char c stream) 0))))
81 (x (peek-char t stream t nil t)))
82 (when (and (char= x #\)) (eq (get-macro-character x) 'read-right-paren))
83 ;; Easier to figure out than an "unmatched parenthesis".
84 (simple-reader-error stream "Trailing ~A in backquoted expression."
85 (svref #("comma" "comma-dot" "comma-at") flag)))
86 (unquote (let ((*backquote-depth* (1- *backquote-depth*)))
87 (read stream t nil t)) flag)))
89 (/show0 "backq.lisp 83")
91 (declaim (ftype (function (t fixnum boolean) (values t t &optional))
92 qq-template-to-sexpr qq-template-1))
94 ;; A QQ-SUBFORM is a cons whose car is an arbitrary S-expression, and
95 ;; cdr one of {EVAL,QUOTE,NCONC,|Append|} signifying how to treat the car.
96 ;; QUOTE and EVAL mean that a single element should be inserted,
97 ;; literally or after being evaluated; NCONC/Append evaluate and splice.
98 (declaim (inline qq-subform-splicing-p))
99 (defun qq-subform-splicing-p (subform)
100 (case (cdr subform)
101 (|Append| '|Append|)
102 (nconc 'nconc)))
104 (defun expand-quasiquote (thing compiler-p)
105 ;; QQ-TEMPLATE-TO-SEXPR returns the parts of a QQ-SUBFORM as 2 values.
106 (multiple-value-bind (expr operator)
107 (qq-template-to-sexpr thing 0 compiler-p)
108 (ecase operator ; Splicing is illegal at toplevel
109 (eval expr)
110 (quote (list 'quote expr)))))
112 ;; The compiler macro for QUASIQUOTE assumes that it's fine to use
113 ;; the foldable list constructors.
114 (define-compiler-macro quasiquote (thing)
115 (expand-quasiquote thing t))
117 ;; The ordinary macro uses CL-standard list constructors for a few reasons:
118 ;; - It makes COMPILE do slightly less work than COMPILE-FILE
119 ;; - If expanded forms are leaked to the user, it looks nicer.
120 ;; Pending discussion of how to or whether to prettify the value
121 ;; of (MACROEXPAND-1 '`(FOO ,X)) this could be irrelevant.
122 (defmacro quasiquote (thing)
123 (expand-quasiquote thing nil))
125 ;; Convert a quasi-quote template to a Lisp form that when evaluated constructs
126 ;; the template, substituting into the outermost commas. Return two values:
127 ;; the S-expression, and an indicator of how to incorporate it into its parent.
128 (defun qq-template-to-sexpr (expr depth compiler-p)
129 (cond ((not expr) (values nil 'quote))
130 ((listp expr)
131 (qq-template-1 expr (+ (if (eq (car expr) 'quasiquote) 1 0) depth)
132 compiler-p))
133 ((simple-vector-p expr) (qq-template-1 expr depth compiler-p))
134 ((not (comma-p expr)) (values expr 'quote))
135 ((zerop depth)
136 (values (comma-expr expr)
137 (svref #(eval nconc |Append|) (comma-kind expr))))
139 ;; A comma is "pure data" if deeper than the current backquote depth.
140 ;; If its expression interpolates 1 item, reconstruct it using its
141 ;; ordinary constructor, otherwise its multi-constructor.
142 (multiple-value-bind (subexpr operator)
143 (qq-template-to-sexpr (comma-expr expr) (1- depth) compiler-p)
144 (when (eq operator 'quote)
145 (setq subexpr (list 'quote subexpr) operator 'eval))
146 (values (list (cond ((eq operator 'eval) (comma-constructor expr))
147 ((comma-splicing-p expr) 'unquote*-splice)
148 (t 'unquote*))
149 subexpr)
150 operator)))))
152 (/show0 "backq.lisp 139")
154 ;; Find the longest suffix comprised wholly of self-evaluating and/or quoted
155 ;; SUBFORMS. DOTTED-P indicates that the last item represents what was in the
156 ;; CDR of the last cons of the original list. Return the modified SUBFORMS
157 ;; as a proper list, and new DOTTED-P flag. i.e. Conceptually:
158 ;; `(a ,[@]b c d) -> `(a ,[@]b . (c d))
159 ;; `(a ,[@]b c . d) -> `(a ,[@]b . (c . d))
160 (defun qq-fold-suffix (subforms dotted-p vectorp)
161 (labels ((const-tailp (list)
162 (if list
163 (let* ((rest (cdr list))
164 (const-part (const-tailp rest)))
165 (if (and (eq const-part rest) (eq (cdar list) 'quote))
166 list
167 const-part)))))
168 (let ((const-tail (and (cdr subforms) (const-tailp subforms))))
169 (if const-tail
170 (let* ((constants (mapcar #'car const-tail))
171 (new-tail (if dotted-p (apply 'list* constants) constants)))
172 (setq subforms (nconc (ldiff subforms const-tail)
173 (list (cons new-tail 'quote)))
174 dotted-p t)))))
175 ;; If the only splicing operator is in the last element of a proper list,
176 ;; get rid of the splice and make it an improper list.
177 (labels ((convertible-p (list)
178 (if (cdr list)
179 (and (not (qq-subform-splicing-p (car list)))
180 (convertible-p (cdr list)))
181 (qq-subform-splicing-p (car list)))))
182 (when (and (not dotted-p) (not vectorp) (convertible-p subforms))
183 (let ((tail (car (last subforms))))
184 (setq subforms (nconc (nbutlast subforms) (list (list (car tail))))
185 dotted-p t))))
186 (values subforms dotted-p))
188 ;; Map TEMPLATE-TO-SEXPR over INPUT, a list or simple-vector, producing a list
189 ;; as if by MAP. The cdr of the last cons of the input (if a list) may be a
190 ;; non-nil atom. Return a secondary value indicating whether it was or not.
191 ;; The output list never "dots" its last cons, regardless of the input.
192 (defun qq-map-template-to-list (input depth compiler-p)
193 (let ((original input) list dotted-p)
194 (flet ((to-sexpr (x)
195 (multiple-value-call #'cons
196 (qq-template-to-sexpr x depth compiler-p))))
197 (typecase input
198 (cons
199 (loop
200 (push (to-sexpr (pop input)) list)
201 ;; Ensure that QQ-TEMPLATE-TO-SEXPR sees each occurrence of
202 ;; (QUASIQUOTE <form>) as a proper list so that it can
203 ;; bump the depth counter. The oddball case `(a . `(b))
204 ;; would otherwise be seen as not nested `(a quasiquote (b)).
205 (cond ((null input) (return))
206 ((comma-p input) ; (... . ,<expr>)
207 (when (comma-splicing-p input) ; uncaught by reader
208 ;; Actually I don't even know how to get this error
209 (error "~S is not a well-formed backquote expression"
210 original))
211 ;; (A B . ,X) becomes (A B ,@X). It matters only if there
212 ;; are commas in X like (... . ,,@C). Otherwise no effect.
213 (push (to-sexpr (unquote-splice (comma-expr input))) list)
214 (return))
215 ((or (not (listp input)) (eq (car input) 'quasiquote))
216 (push (to-sexpr input) list)
217 (setq dotted-p t)
218 (return))))
219 (setq list (nreverse list)))
220 (simple-vector
221 (setq list (map 'list #'to-sexpr input)))))
222 ;; For lists, find the longest suffix comprised wholly of literals.
223 ;; For vectors without splicing we don't do that because (VECTOR 'A B 'C 'D)
224 ;; is better than (COERCE (LIST* 'A B '(C D)) 'VECTOR) by avoiding a step.
225 ;; But if splicing is required then we're going to construct the interim
226 ;; list no matter what. It could theoretically be avoided by doing:
227 ;; (MULTIPLE-VALUE-CALL #'VECTOR ... (VALUES-LIST <splice>) ...)
228 (if (or (listp original)
229 ;; The target compiler open-codes SOME but the cross-compiler
230 ;; seems not to without (THE LIST) to help it out.
231 (some #'qq-subform-splicing-p (the list list)))
232 (qq-fold-suffix list dotted-p (vectorp input))
233 (values list dotted-p))))
235 ;; Return an expression to quasi-quote INPUT, which is either a list
236 ;; or simple-vector, by recursing over its subexpressions.
237 ;; The expansion is in terms of CL-standard functions for MACROEXPAND,
238 ;; but SBCL-private functions for the compiler-macro.
239 ;; This is mainly for aesthetics. If users expressly macroexpand a sexpr
240 ;; and then compile it, they miss out on the opportunity for the minor
241 ;; advantage provided by the foldable functions, but why would they do that?
242 (defun qq-template-1 (input depth compiler-p)
243 (multiple-value-bind (subforms dot-p)
244 (qq-map-template-to-list input depth compiler-p)
245 (labels ((const-p (subform) ; is SUBFORM constant?
246 ;; This needs to notice only the QQ-SUBFORM kind of QUOTE,
247 ;; but it helps to get EVAL forms whose expression is (QUOTE x).
248 ;; Otherwise, work is deferred to IR1 in processing `(A ,'B C).
249 (or (eq (cdr subform) 'quote) ; either the kind is QUOTE
250 (let ((exp (car subform)))
251 (if (atom exp) ; or it's a self-evaluating atom
252 (atom-const-p exp)
253 ;; or (QUOTE <thing>)
254 (and (eq (car exp) 'quote) (singleton-p (cdr exp)))))))
255 (atom-const-p (atom) ; is known to be an atom
256 (typep atom '(or (not symbol) (member t nil) keyword)))
257 (const-val (subform) ; given that it is known CONST-P
258 (let ((exp (car subform)))
259 (if (or (eq (cdr subform) 'quote) (atom exp))
261 (second exp)))) ; (QUOTE x) in a for-evaluation position
262 (render (subform) ; Return a sexpr that evaluates to SUBFORM
263 ;; For subform kind = QUOTE, wrap it in a QUOTE unless
264 ;; the quoted object is self-evaluating, then elide the QUOTE.
265 (let ((exp (car subform)))
266 (if (and (eq (cdr subform) 'quote)
267 (not (and (atom exp) (atom-const-p exp))))
268 (list 'quote exp)
269 exp)))
270 (normalize-fn (fn-name)
271 (if (or compiler-p (eq fn-name 'nconc))
272 fn-name
273 (ecase fn-name
274 (|Append| 'append)
275 (|List| 'list)
276 (|List*| 'list*)
277 (|Vector| 'vector))))
278 (recurse (list &aux (elt (car list)) (rest (cdr list)))
279 (if (endp rest)
280 (cond ((or dot-p (qq-subform-splicing-p elt))
281 (let ((tail (render elt)))
282 (if (vectorp input)
283 ;; When splicing pieces into a vector,
284 ;; force the tail to be a list.
285 (list (normalize-fn '|Append|) tail nil)
286 tail)))
287 ((const-p elt) (list 'quote (list (const-val elt))))
288 (t (list (normalize-fn '|List|)
289 (render elt)))) ; singleton list
290 (let ((fn (normalize-fn
291 (or (qq-subform-splicing-p elt) '|List*|)))
292 (head (render elt))
293 (tail (recurse rest)))
294 (if (and (listp tail) (eq (car tail) fn))
295 (list* fn head (cdr tail)) ; (F a (F b c)) -> (F a b c)
296 (list fn head tail))))))
297 (let ((vect-p (vectorp input)))
298 ;; If at least one splicing comma, use the recursive algorithm.
299 (if (some #'qq-subform-splicing-p (the list subforms))
300 (let ((x (recurse subforms)))
301 (values (if vect-p (list 'coerce x ''simple-vector) x) 'eval))
302 (let ((fn (cond (vect-p '|Vector|) (dot-p '|List*|) (t '|List|))))
303 (if (every #'const-p subforms)
304 (values (apply fn (mapcar #'const-val subforms)) 'quote)
305 (values (cons (normalize-fn fn)
306 (mapcar #'render subforms)) 'eval))))))))
308 ;;; COMPILE-FILE may treat anything as constant that is part of quoted
309 ;;; structure, including quasi-quoted structure (lp#1026439).
310 ;;; As such, we use foldable versions of the standard sequence constructors
311 ;;; which are otherwise identical to their ordinary counterparts.
312 ;;; Pretty-printing doesn't care about these names, only recognizing QUASIQUOTE.
313 ;;; Generated code looks nicer to me without prepending BACKQ-.
314 ;;; Also note there is no alter-ego of CONS or NCONC.
315 (setf (symbol-function '|Append|) #'append
316 (symbol-function '|List|) #'list
317 (symbol-function '|List*|) #'list*
318 (symbol-function '|Vector|) #'vector)
320 ;;;; initialization
322 ;;; Install BACKQ stuff in the current *READTABLE*.
324 ;;; In the target Lisp, we have to wait to do this until the readtable
325 ;;; has been created. In the cross-compilation host Lisp, we can do
326 ;;; this right away. (You may ask: In the cross-compilation host,
327 ;;; which already has its own implementation of the backquote
328 ;;; readmacro, why do we do this at all? Because the cross-compilation
329 ;;; host might -- as SBCL itself does -- express the backquote
330 ;;; expansion in terms of internal, nonportable functions. By
331 ;;; redefining backquote in terms of functions which are guaranteed to
332 ;;; exist on the target Lisp, we ensure that backquote expansions in
333 ;;; code-generating code work properly.)
335 (defun !backq-cold-init ()
336 (set-macro-character #\` 'backquote-charmacro)
337 (set-macro-character #\, 'comma-charmacro))
338 #-sb-xc (!backq-cold-init)
340 ;;; Since our backquote is installed on the host lisp, and since
341 ;;; developers make mistakes with backquotes and commas too, let's
342 ;;; ensure that we can report errors rather than get an undefined
343 ;;; function condition on SIMPLE-READER-ERROR.
344 #+sb-xc-host ; proper definition happens for the target
345 (defun simple-reader-error (stream format-string &rest format-args)
346 (bug "READER-ERROR on stream ~S: ~?" stream format-string format-args))
348 (/show0 "done with backq.lisp")