1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
24 (in-package "SB-ITERATE")
26 ;;; Are warnings to be issued for iterate/gather forms that aren't optimized?
28 ;;; :USER => those resulting from user code
29 ;;; T => always, even if it's the iteration macro that's suboptimal.
30 (defvar *iterate-warnings* :any)
33 (defmacro iterate (clauses &body body &environment env)
34 (optimize-iterate-form clauses body env))
37 simple-expand-iterate-form
40 ;; Expand ITERATE. This is the "formal semantics" expansion, which we never
43 ((block-name (gensym))
44 (bound-var-lists (mapcar #'(lambda (clause)
45 (let ((names (first clause)))
50 (generator-vars (mapcar #'(lambda (clause)
51 (declare (ignore clause))
56 ,(mapcan #'(lambda (gvar clause var-list)
57 ;; For each clause, bind a generator temp to the clause,
58 ;; then bind the specified var(s).
59 (cons (list gvar (second clause))
60 (copy-list var-list)))
61 generator-vars clauses bound-var-lists)
63 ;; Note bug in formal semantics: there can be declarations in the head
64 ;; of BODY; they go here, rather than inside loop.
67 #'(lambda (var-list gen-var)
68 ;; Set each bound variable (or set of vars) to the result of
69 ;; calling the corresponding generator.
70 `(multiple-value-setq ,var-list
71 (funcall ,gen-var #'(lambda nil (return-from
73 bound-var-lists generator-vars)
76 ;;; temporary variable names used by ITERATE expansions
77 (defparameter *iterate-temp-vars-list*
78 '(iterate-temp-1 iterate-temp-2 iterate-temp-3 iterate-temp-4
79 iterate-temp-5 iterate-temp-6 iterate-temp-7 iterate-temp-8))
83 (clauses body iterate-env)
85 ((temp-vars *iterate-temp-vars-list*)
87 (finish-form `(return-from ,block-name))
88 (bound-vars (mapcan #'(lambda (clause)
89 (let ((names (first clause)))
94 iterate-decls generator-decls update-forms bindings leftover-body)
95 (do ((tail bound-vars (cdr tail)))
97 ;; Check for duplicates
98 (when (member (car tail)
100 (warn "Variable appears more than once in ITERATE: ~S" (car tail))))
102 ((get-iterate-temp nil
104 ;; Make temporary var. Note that it is ok to re-use these symbols
105 ;; in each iterate, because they are not used within BODY.
108 (dolist (clause clauses)
110 ((or (not (consp clause))
111 (not (consp (cdr clause))))
112 (warn "bad syntax in ITERATE: clause not of form (var iterator): ~S"
115 (unless (null (cddr clause))
117 "probable parenthesis error in ITERATE clause--more than 2 elements: ~S"
120 (let-body binding-type let-bindings localdecls otherdecls extra-body)
121 (expand-into-let (second clause)
122 'iterate iterate-env)
124 ;; We have expanded the generator clause and parsed it into
127 ((vars (first clause))
128 gen-args renamed-vars)
129 (setq vars (if (listp vars)
132 ; VARS is now a (fresh) list of
133 ; all iteration vars bound in
136 ((eq let-body :abort)
137 ; Already issued a warning
138 ; about malformedness
140 ((null (setq let-body (function-lambda-p let-body 1)))
141 ; Not of the expected form
142 (let ((generator (second clause)))
143 (cond ((and (consp generator)
144 (fboundp (car generator)))
145 ; It looks ok--a macro or
146 ; function here--so the guy who
147 ; wrote it just didn't do it in
149 (maybe-warn :definition "could not optimize iterate clause ~S because generator not of form (LET[*] ... (FUNCTION (LAMBDA (finish) ...)))"
151 (t ; Perhaps it's just a
152 ; misspelling? Probably user
155 "Iterate operator in clause ~S is not fboundp."
157 (setq let-body :abort)))
160 ;; We have something of the form #'(LAMBDA (finisharg) ...),
161 ;; possibly with some LET bindings around it. LET-BODY =
162 ;; ((finisharg) ...).
163 (setq let-body (cdr let-body))
164 (setq gen-args (pop let-body))
167 ;; The first transformation we want to perform is
168 ;; "LET-eversion": turn (let* ((generator (let (..bindings..)
169 ;; #'(lambda ...)))) ..body..) into (let* (..bindings..
170 ;; (generator #'(lambda ...))) ..body..). This
171 ;; transformation is valid if nothing in body refers to any
172 ;; of the bindings, something we can ensure by
173 ;; alpha-converting the inner let (substituting new names for
174 ;; each var). Of course, none of those vars can be special,
175 ;; but we already checked for that above.
176 (multiple-value-setq (let-bindings renamed-vars)
177 (rename-let-bindings let-bindings binding-type
178 iterate-env leftover-body #'get-iterate-temp))
179 (setq leftover-body nil)
180 ; If there was any leftover
181 ; from previous, it is now
185 ;; The second transformation is substituting the body of the
186 ;; generator (LAMBDA (finish-arg) . gen-body) for its appearance
187 ;; in the update form (funcall generator #'(lambda ()
188 ;; finish-form)), then simplifying that form. The requirement
189 ;; for this part is that the generator body not refer to any
190 ;; variables that are bound between the generator binding and the
191 ;; appearance in the loop body. The only variables bound in that
192 ;; interval are generator temporaries, which have unique names so
193 ;; are no problem, and the iteration variables remaining for
194 ;; subsequent clauses. We'll discover the story as we walk the
196 (multiple-value-bind (finishdecl other rest)
197 (parse-declarations let-body gen-args)
198 (declare (ignore finishdecl))
199 ; Pull out declares, if any,
200 ; separating out the one(s)
201 ; referring to the finish arg,
202 ; which we will throw away.
204 ; Combine remaining decls with
205 ; decls extracted from the LET,
207 (setq otherdecls (nconc otherdecls other)))
210 ; There are interesting
211 ; declarations, so have to keep
213 `(let nil (declare ,@otherdecls)
218 (t `(progn ,@rest)))))
219 (unless (eq (setq let-body (iterate-transform-body let-body
220 iterate-env renamed-vars
222 finish-form bound-vars clause))
225 ;; Skip the rest if transformation failed. Warning has
226 ;; already been issued.
228 ;; Note possible further optimization: if LET-BODY expanded
229 ;; into (prog1 oldvalue prepare-for-next-iteration), as so
230 ;; many do, then we could in most cases split the PROG1 into
231 ;; two pieces: do the (setq var oldvalue) here, and do the
232 ;; prepare-for-next-iteration at the bottom of the loop.
233 ;; This does a slight optimization of the PROG1 and also
234 ;; rearranges the code in a way that a reasonably clever
235 ;; compiler might detect how to get rid of redundant
236 ;; variables altogether (such as happens with INTERVAL and
237 ;; LIST-TAILS); that would make the whole thing closer to
238 ;; what you might have coded by hand. However, to do this
239 ;; optimization, we need to ensure that (a) the
240 ;; prepare-for-next-iteration refers freely to no vars other
241 ;; than the internal vars we have extracted from the LET, and
242 ;; (b) that the code has no side effects. These are both
243 ;; true for all the iterators defined by this module, but how
244 ;; shall we represent side-effect info and/or tap into the
245 ;; compiler's knowledge of same?
247 ; There were declarations for
248 ; the generator locals--have to
249 ; keep them for later, and
250 ; rename the vars mentioned
258 (let ((head (car decl)))
259 (cons head (if (eq head 'type)
267 ;; Finished analyzing clause now. LET-BODY is the form which, when
268 ;; evaluated, returns updated values for the iteration variable(s)
270 (when (eq let-body :abort)
272 ;; Some punt case: go with the formal semantics: bind a var to
273 ;; the generator, then call it in the update section
275 ((gvar (get-iterate-temp))
276 (generator (second clause)))
281 ;; FIXME: This conditional was here with this
282 ;; comment in old CMU CL PCL. Does Python really
283 ;; think it's unreachable?
284 ;;#-cmu ; Python thinks this is unreachable.
286 ; Have to use this up
287 `(progn ,@(prog1 leftover-body (setq
292 (setq let-body `(funcall ,gvar #'(lambda nil ,finish-form)))))
293 (push (mv-setq (copy-list vars)
298 ;; Pop off the vars we have now bound from the list of vars to
299 ;; watch out for -- we'll bind them right now.
302 (nconc bindings let-bindings
304 ;; There was some computation to do after the
305 ;; bindings--here's our chance.
306 (cons (list (first vars)
307 `(progn ,@extra-body nil))
310 (do ((tail body (cdr tail)))
311 ((not (and (consp tail)
316 ;; TAIL now points at first non-declaration. If there were
317 ;; declarations, pop them off so they appear in the right place
318 (unless (eq tail body)
319 (setq iterate-decls (ldiff body tail))
322 (let* ,bindings ,@(and generator-decls
323 `((declare ,@generator-decls)))
326 (loop ,@(nreverse update-forms)
329 (defun expand-into-let (clause parent-name env)
331 ;; Return values: Body, LET[*], bindings, localdecls, otherdecls, extra
332 ;; body, where BODY is a single form. If multiple forms in a LET, the
333 ;; preceding forms are returned as extra body. Returns :ABORT if it
334 ;; issued a punt warning.
335 (prog ((expansion clause)
336 expandedp binding-type let-bindings let-body)
338 (multiple-value-setq (expansion expandedp)
339 (macroexpand-1 expansion env))
340 (cond ((not (consp expansion))
343 ((symbolp (setq binding-type (first expansion)))
346 (setq let-bindings (second expansion))
347 ; List of variable bindings
348 (setq let-body (cddr expansion))
350 ((and (consp binding-type)
351 (eq (car binding-type)
353 (not (find-if #'(lambda (x)
354 (member x lambda-list-keywords)
356 (setq let-bindings (second binding-type)))
358 (eql (length (second expansion))
359 (length let-bindings))
360 (null (cddr expansion)))
361 ; A simple LAMBDA form can be
363 (setq let-body (cddr binding-type))
364 (setq let-bindings (mapcar #'list let-bindings (second
367 (setq binding-type 'let)
370 ;; Fall thru if not a LET
371 (cond (expandedp ; try expanding again
373 (t ; Boring--return form as the
377 (return (let ((locals (variables-from-let let-bindings))
379 (multiple-value-bind (localdecls otherdecls let-body)
380 (parse-declarations let-body locals)
381 (cond ((setq specials (extract-special-bindings
383 (maybe-warn (cond ((find-if #'variable-globally-special-p
385 ; This could be the fault of a
390 "Couldn't optimize ~S because expansion of ~S binds specials ~(~S ~)"
391 parent-name clause specials)
393 (t (values (cond ((not (consp let-body))
395 ; Null body of LET? unlikely,
396 ; but someone else will likely
399 ((null (cdr let-body))
401 ; A single expression, which we
407 ;; More than one expression. These are forms to
408 ;; evaluate after the bindings but before the
409 ;; generator form is returned. Save them to
410 ;; evaluate in the next convenient place. Note that
411 ;; this is ok, as there is no construct that can
412 ;; cause a LET to return prematurely (without
413 ;; returning also from some surrounding construct).
416 (car (last let-body))))
417 binding-type let-bindings localdecls
418 otherdecls extra-body))))))))
420 (defun variables-from-let (bindings)
422 ;; Return a list of the variables bound in the first argument to LET[*].
423 (mapcar #'(lambda (binding)
429 (defun iterate-transform-body (let-body iterate-env renamed-vars finish-arg
430 finish-form bound-vars clause)
432 ;;; This is the second major transformation for a single iterate clause.
433 ;;; LET-BODY is the body of the iterator after we have extracted its local
434 ;;; variables and declarations. We have two main tasks: (1) Substitute
435 ;;; internal temporaries for occurrences of the LET variables; the alist
436 ;;; RENAMED-VARS specifies this transformation. (2) Substitute evaluation of
437 ;;; FINISH-FORM for any occurrence of (funcall FINISH-ARG). Along the way, we
438 ;;; check for forms that would invalidate these transformations: occurrence of
439 ;;; FINISH-ARG outside of a funcall, and free reference to any element of
440 ;;; BOUND-VARS. CLAUSE & TYPE are the original ITERATE clause and its type
441 ;;; (ITERATE or ITERATE*), for purpose of error messages. On success, we
442 ;;; return the transformed body; on failure, :ABORT.
444 (walk-form let-body iterate-env
445 #'(lambda (form context env)
446 (declare (ignore context))
448 ;; Need to substitute RENAMED-VARS, as well as turn
449 ;; (FUNCALL finish-arg) into the finish form
450 (cond ((symbolp form)
452 (cond ((and (eq form finish-arg)
453 (variable-same-p form env
455 ; An occurrence of the finish
456 ; arg outside of FUNCALL
457 ; context--I can't handle this
458 (maybe-warn :definition "Couldn't optimize iterate form because generator ~S does something with its FINISH arg besides FUNCALL it."
460 (return-from iterate-transform-body
462 ((and (setq renaming (assoc form
465 (variable-same-p form env
467 ; Reference to one of the vars
470 ((and (member form bound-vars)
471 (variable-same-p form env
473 ; FORM is a var that is bound
474 ; in this same ITERATE, or
475 ; bound later in this ITERATE*.
476 ; This is a conflict.
477 (maybe-warn :user "Couldn't optimize iterate form because generator ~S is closed over ~S, in conflict with a subsequent iteration variable."
480 (return-from iterate-transform-body
488 (variable-same-p (second form)
490 ; (FUNCALL finish-arg) =>
492 (unless (null (cddr form))
493 (maybe-warn :definition
494 "Generator for ~S applied its finish arg to > 0 arguments ~S--ignored."
504 ;; Extract the declarations from the head of TAIL and divide them into 2
505 ;; classes: declares about variables in the list LOCALS, and all other
506 ;; declarations. Returns 3 values: those 2 lists plus the remainder of TAIL.
508 (localdecls otherdecls form)
510 (unless (and tail (consp (setq form (car tail)))
513 (return (values localdecls otherdecls tail)))
518 ((inline notinline optimize)
519 ; These don't talk about vars
520 (push decl otherdecls))
521 (t ; Assume all other kinds are
523 (let* ((vars (if (eq (first decl)
527 (l (intersection locals vars))
531 ; None talk about LOCALS
532 (push decl otherdecls))
533 ((null (setq other (set-difference vars l)))
534 ; All talk about LOCALS
535 (push decl localdecls))
537 (let ((head (cons 'type (and (eq (first decl)
539 (list (second decl))))))
540 (push (append head other)
542 (push (append head l)
547 (defun extract-special-bindings (vars decls)
549 ;; Return the subset of VARS that are special, either globally or
550 ;; because of a declaration in DECLS
551 (let ((specials (remove-if-not #'variable-globally-special-p vars)))
555 (setq specials (union specials (intersection vars
559 (defun function-lambda-p (form &optional nargs)
561 ;; If FORM is #'(LAMBDA bindings . body) and bindings is of length
562 ;; NARGS, return the lambda expression
567 (consp (setq form (cdr form)))
569 (consp (setq form (car form)))
572 (consp (setq body (cdr form)))
573 (listp (setq args (car body)))
581 (let-bindings binding-type env leftover-body &optional tempvarfn)
583 ;; Perform the alpha conversion required for "LET eversion" of
584 ;; (LET[*] LET-BINDINGS . body)--rename each of the variables to an
585 ;; internal name. Returns 2 values: a new set of LET bindings and the
586 ;; alist of old var names to new (so caller can walk the body doing
587 ;; the rest of the renaming). BINDING-TYPE is one of LET or LET*.
588 ;; LEFTOVER-BODY is optional list of forms that must be eval'ed
589 ;; before the first binding happens. ENV is the macro expansion
590 ;; environment, in case we have to walk a LET*. TEMPVARFN is a
591 ;; function of no args to return a temporary var; if omitted, we use
595 (values (mapcar #'(lambda (binding)
596 (let ((valueform (cond ((not (consp binding))
600 ((or (eq binding-type
604 ; All bindings are in parallel,
605 ; so none can refer to others
608 ; In a LET*, have to substitute
609 ; vars in the 2nd and
610 ; subsequent initialization
615 (newvar (if tempvarfn
618 (push (cons (if (consp binding)
623 ; Add new variable to the list
624 ; AFTER we have walked the
627 ;; Previous clause had some computation to do
628 ;; after its bindings. Here is the first
629 ;; opportunity to do it
630 (setq valueform `(progn ,@leftover-body
632 (setq leftover-body nil))
633 (list newvar valueform)))
637 (defun rename-variables (form alist env)
639 ;; Walks FORM, renaming occurrences of the key variables in ALIST with
640 ;; their corresponding values. ENV is FORM's environment, so we can
641 ;; make sure we are talking about the same variables.
643 #'(lambda (form context subenv)
644 (declare (ignore context))
646 (cond ((and (symbolp form)
647 (setq pair (assoc form alist))
648 (variable-same-p form subenv env))
656 ;; Produces (MULTIPLE-VALUE-SETQ vars expr), except that I'll optimize some
657 ;; of the simple cases for benefit of compilers that don't, and I don't care
658 ;; what the value is, and I know that the variables need not be set in
659 ;; parallel, since they can't be used free in EXPR
662 ; EXPR is a side-effect
665 ; This is an error, but I'll
666 ; let MULTIPLE-VALUE-SETQ
668 `(multiple-value-setq ,vars ,expr))
673 ;; (mv-setq (a b c) (values x y z)) can be reduced to a parallel setq
674 ;; (psetq returns nil, but I don't care about returned value). Do this
675 ;; even for the single variable case so that we catch (mv-setq (a) (values
679 `(setq ,@(mapcon #'(lambda (tail)
681 (cond ((or (cdr tail)
683 ; One result expression for
686 (t ; More expressions than vars,
687 ; so arrange to evaluate all
689 (cons 'prog1 expr)))))
692 ; Simple one variable case
695 (t ; General case--I know nothing
696 `(multiple-value-setq ,vars ,expr))))
698 (defun variable-same-p (var env1 env2)
699 (eq (variable-lexical-p var env1)
700 (variable-lexical-p var env2)))
702 (defun maybe-warn (type &rest warn-args)
704 ;; Issue a warning about not being able to optimize this thing. TYPE
705 ;; is one of :DEFINITION, meaning the definition is at fault, and
706 ;; :USER, meaning the user's code is at fault.
707 (when (case *iterate-warnings*
709 ((:user) (eq type :user))
711 (apply #'warn warn-args)))
715 ;;; FIXME: If they're only samples, can they be commented out?
719 (&whole whole &key from downfrom to downto above below by type)
722 (error "Can't use both FROM and DOWNFROM in ~S" whole))
723 ((cdr (remove nil (list to downto above below)))
724 (error "Can't use more than one limit keyword in ~S" whole))
727 ((down (or downfrom downto above))
728 (limit (or to downto above below))
729 (inc (cond ((null by)
732 ; Can inline this increment
735 ((from ,(or from downfrom 0))
736 ,@(and limit `((to ,limit)))
739 ,@(and type `((declare (type ,type from ,@(and limit '(to))
744 ,@(cond ((null limit)
745 ; We won't use the FINISH arg.
746 '((declare (ignore finish)))))
747 (prog1 ,(cond (limit ; Test the limit. If ok,
748 ; return current value and
749 ; increment, else quit
750 `(if (,(cond (above '>)
759 (setq from (,(if down
763 ,(or inc 'by))))))))))
765 (defmacro list-elements (list &key (by '#'cdr))
768 (prog1 (if (endp tail)
771 (setq tail (funcall ,by tail))))))
773 (defmacro list-tails (list &key (by '#'cdr))
776 (prog1 (if (endp tail)
779 (setq tail (funcall ,by tail))))))
784 "Generates successive elements of SEQUENCE, with second value being the index. Use (ELEMENTS (THE type arg)) if you care about the type."
786 ((type (and (consp sequence)
791 (sequence-accessor type)
793 (listp (eq type 'list)))
795 ;; If type is given via THE, we may be able to generate a good accessor here
796 ;; for the benefit of implementations that aren't smart about (ELT (THE
797 ;; STRING FOO)). I'm not bothering to keep the THE inside the body,
798 ;; however, since I assume any compiler that would understand (AREF (THE
799 ;; SIMPLE-ARRAY S)) would also understand that (AREF S) is the same when I
800 ;; bound S to (THE SIMPLE-ARRAY foo) and never modified it.
802 ;; If sequence is declared to be a list, it's better to cdr down it, so we
803 ;; have some extra cases here. Normally folks would write LIST-ELEMENTS,
804 ;; but maybe they wanted to get the index for free...
808 '((size (length s)))))
810 (values (cond ,(if listp
814 (,accessor s index)))
815 (t (funcall finish)))
817 (setq index (1+ index))))))))
822 "Generates each time 2 items, the indicator and the value."
823 `(let ((tail ,plist))
825 (values (if (endp tail)
828 (prog1 (if (endp (setq tail (cdr tail)))
831 (setq tail (cdr tail)))))))
833 (defun sequence-accessor (type)
835 ;; returns the function with which most efficiently to make accesses to
836 ;; a sequence of type TYPE.
837 (case (if (consp type)
838 ; e.g., (VECTOR FLOAT *)
841 ((array simple-array vector) 'aref)
842 (simple-vector 'svref)
844 (simple-string 'schar)
846 (simple-bit-vector 'sbit)
849 ;; These "iterators" may be withdrawn
851 (defmacro eachtime (expr)
853 (declare (ignore finish))
856 (defmacro while (expr)
858 (unless ,expr (funcall finish))))
860 (defmacro until (expr)
862 (when ,expr (funcall finish))))
866 (defmacro gathering (clauses &body body &environment env)
867 (or (optimize-gathering-form clauses body env)
868 (simple-expand-gathering-form clauses body env)))
870 (defmacro with-gathering (clauses gather-body &body use-body)
871 "Binds the variables specified in CLAUSES to the result of (GATHERING clauses gather-body) and evaluates the forms in USE-BODY inside that contour."
873 ;; We may optimize this a little better later for those compilers that
874 ;; don't do a good job on (m-v-bind vars (... (values ...)) ...).
875 `(multiple-value-bind ,(mapcar #'car clauses)
876 (gathering ,clauses ,gather-body)
880 simple-expand-gathering-form
882 (declare (ignore env))
884 ;; The "formal semantics" of GATHERING. We use this only in cases that can't
887 ((acc-names (mapcar #'first (if (symbolp clauses)
888 ; Shorthand using anonymous
890 (setq clauses `((*anonymous-gathering-site*
893 (realizer-names (mapcar #'(lambda (binding)
894 (declare (ignore binding))
897 `(multiple-value-call
899 ,(mapcan #'list acc-names realizer-names)
900 (flet ((gather (value &optional (accumulator *anonymous-gathering-site*)
902 (funcall accumulator value)))
904 (values ,@(mapcar #'(lambda (rname)
907 ,@(mapcar #'second clauses))))
909 (defvar *active-gatherers* nil
910 "List of GATHERING bindings currently active during macro expansion)")
912 (defvar *anonymous-gathering-site* nil "Variable used in formal expansion of an abbreviated GATHERING form (one with anonymous gathering site).")
914 (defun optimize-gathering-form (clauses body gathering-env)
916 (acc-info leftover-body top-bindings finish-forms top-decls)
917 (dolist (clause (if (symbolp clauses)
919 `((*anonymous-gathering-site* (,clauses)))
922 (let-body binding-type let-bindings localdecls otherdecls extra-body)
923 (expand-into-let (second clause)
924 'gathering gathering-env)
926 ((acc-var (first clause))
927 renamed-vars accumulator realizer)
928 (when (and (consp let-body)
931 (consp (setq let-body (cdr let-body)))
932 (setq accumulator (function-lambda-p (car let-body)))
933 (consp (setq let-body (cdr let-body)))
934 (setq realizer (function-lambda-p (car let-body)
936 (null (cdr let-body)))
938 ;; Macro returned something of the form
939 ;; (VALUES #'(lambda (value) ...)
940 ;; #'(lambda () ...)),
941 ;; a function to accumulate values and a function to realize the
945 ;; Gatherer expanded into a LET
946 (cond (otherdecls (maybe-warn :definition "Couldn't optimize GATHERING clause ~S because its expansion carries declarations about more than the bound variables: ~S"
948 `(declare ,@otherdecls))
952 ;; The first transformation we want to perform is a
953 ;; variant of "LET-eversion": turn
956 ;; (let (..bindings..)
957 ;; (values #'(lambda ...)
961 ;; (let* (..bindings..
962 ;; (acc #'(lambda ...))
963 ;; (real #'(lambda ...)))
965 ;; This transformation is valid if nothing in body refers
966 ;; to any of the bindings, something we can ensure by
967 ;; alpha-converting the inner let (substituting new names
968 ;; for each var). Of course, none of those vars can be
969 ;; special, but we already checked for that above.
970 (multiple-value-setq (let-bindings renamed-vars)
971 (rename-let-bindings let-bindings binding-type
972 gathering-env leftover-body))
973 (setq top-bindings (nconc top-bindings let-bindings))
974 (setq leftover-body nil)
975 ; If there was any leftover
976 ; from previous, it is now
979 (setq leftover-body (nconc leftover-body extra-body))
980 ; Computation to do after these
982 (push (cons acc-var (rename-and-capture-variables accumulator
983 renamed-vars gathering-env))
985 (setq realizer (rename-variables realizer renamed-vars
987 (push (cond ((null (cdddr realizer))
988 ; Simple (LAMBDA () expr) =>
991 (t ; There could be declarations
992 ; or something, so leave as a
994 (cons 'let (cdr realizer))))
996 (unless (null localdecls)
997 ; Declarations about the LET
998 ; variables also has to
1000 (setq top-decls (nconc top-decls (sublis renamed-vars
1003 (maybe-warn :definition "Couldn't optimize GATHERING clause ~S because its expansion is not of the form (VALUES #'(LAMBDA ...) #'(LAMBDA () ...))"
1008 (expansion `(multiple-value-list ,(second clause))))
1009 ; Slow way--bind gensym to the
1010 ; macro expansion, and we will
1011 ; funcall it in the body
1012 (push (list acc-var gs)
1014 (push `(funcall (cadr ,gs))
1020 (list (list gs (cond (leftover-body
1021 `(progn ,@(prog1 leftover-body
1022 (setq leftover-body nil))
1024 (t expansion))))))))))
1025 (setq body (walk-gathering-body body gathering-env acc-info))
1026 (cond ((eq body :abort)
1027 ; Couldn't finish expansion
1029 (t `(let* ,top-bindings
1030 ,@(and top-decls `((declare ,@top-decls)))
1032 ,(cond ((null (cdr finish-forms))
1033 ; just a single value
1035 (t `(values ,@(reverse finish-forms)))))))))
1037 (defun rename-and-capture-variables (form alist env)
1039 ;; Walks FORM, renaming occurrences of the key variables in ALIST with
1040 ;; their corresponding values, and capturing any other free variables.
1041 ;; Returns a list of the new form and the list of other closed-over
1042 ;; vars. ENV is FORM's environment, so we can make sure we are talking
1043 ;; about the same variables.
1047 #'(lambda (form context subenv)
1048 (declare (ignore context))
1050 (cond ((or (not (symbolp form))
1051 (not (variable-same-p form subenv
1053 ; non-variable or one that has
1056 ((setq pair (assoc form alist))
1060 (pushnew form closed)
1066 (body gathering-env acc-info)
1068 ;; Walk the body of (GATHERING (...) . BODY) in environment GATHERING-ENV.
1069 ;; ACC-INFO is a list of information about each of the gathering "bindings"
1070 ;; in the form, in the form (var gatheringfn freevars env)
1072 ((*active-gatherers* (nconc (mapcar #'car acc-info)
1073 *active-gatherers*)))
1075 ;; *ACTIVE-GATHERERS* tells us what vars are currently legal as GATHER
1076 ;; targets. This is so that when we encounter a GATHER not belonging to us
1077 ;; we can know whether to warn about it.
1083 (declare (ignore context))
1087 ((not (eq (car form)
1089 ; We only care about GATHER
1090 (when (and (eq (car form)
1094 ; Passed as functional--can't
1097 "Can't optimize GATHERING because of reference to #'GATHER."
1099 (return-from walk-gathering-body :abort))
1101 ((setq info (assoc (setq site (if (null (cddr form))
1104 *anonymous-gathering-site*
1107 ; One of ours--expand (GATHER
1108 ; value var). INFO = (var
1109 ; gatheringfn freevars env)
1110 (unless (null (cdddr form))
1111 (warn "Extra arguments (> 2) in ~S discarded." form)
1113 (let ((fn (second info)))
1115 ; Unoptimized case--just call
1116 ; the gatherer. FN is the
1117 ; gensym that we bound to the
1118 ; list of two values returned
1119 ; from the gatherer.
1122 (t ; FN = (lambda (value) ...)
1123 (dolist (s (third info))
1124 (unless (or (variable-same-p s env
1126 (and (variable-special-p
1131 ;; Some var used free in the LAMBDA form has been
1132 ;; rebound between here and the parent GATHERING
1133 ;; form, so can't substitute the lambda. Ok if it's
1134 ;; a special reference both here and in the LAMBDA,
1135 ;; because then it's not closed over.
1136 (maybe-warn :user "Can't optimize GATHERING because the expansion closes over the variable ~S, which is rebound around a GATHER for it."
1138 (return-from walk-gathering-body
1141 ;; Return ((lambda (value) ...) actual-value). In
1142 ;; many cases we could simplify this further by
1143 ;; substitution, but we'd have to be careful (for
1144 ;; example, we would need to alpha-convert any LET
1145 ;; we found inside). Any decent compiler will do it
1147 (list fn (second form))))))
1148 ((and (setq info (member site *active-gatherers*))
1149 (or (eq site '*anonymous-gathering-site*)
1150 (variable-same-p site env (fourth info))))
1151 ; Some other GATHERING will
1152 ; take care of this form, so
1153 ; pass it up for now.
1154 ; Environment check is to make
1155 ; sure nobody shadowed it
1156 ; between here and there
1158 (t ; Nobody's going to handle it
1159 (if (eq site '*anonymous-gathering-site*)
1160 ; More likely that she forgot
1161 ; to mention the site than
1162 ; forget to write an anonymous
1164 (warn "There is no gathering site specified in ~S."
1167 "The site ~S in ~S is not defined in an enclosing GATHERING form."
1169 ; Turn it into something else
1170 ; so we don't warn twice in the
1172 `(%orphaned-gather ,@(cdr form)))))
1173 ((and (symbolp form)
1174 (setq info (assoc form acc-info))
1175 (variable-same-p form env gathering-env))
1176 ; A variable reference to a
1177 ; gather binding from
1179 (maybe-warn :user "Can't optimize GATHERING because site variable ~S is used outside of a GATHER form."
1181 (return-from walk-gathering-body :abort))
1186 ;; FIXME: If these are only samples, can we delete them?
1190 (&key initial-value)
1191 `(let* ((head ,initial-value)
1192 (tail ,(and initial-value `(last head))))
1193 (values #'(lambda (value)
1195 (setq head (setq tail (list value)))
1196 (setq tail (cdr (rplacd tail (list value))))))
1197 #'(lambda nil head))))
1199 (defmacro joining (&key initial-value)
1200 `(let ((result ,initial-value))
1201 (values #'(lambda (value)
1202 (setq result (nconc result value)))
1203 #'(lambda nil result))))
1207 (&key initial-value)
1208 `(let ((result ,initial-value))
1211 (when ,(cond ((and (constantp initial-value)
1212 (not (null (eval initial-value))))
1213 ; Initial value is given and we
1214 ; know it's not NIL, so leave
1215 ; out the null check
1217 (t '(or (null result)
1219 (setq result value)))
1220 #'(lambda nil result))))
1224 (&key initial-value)
1225 `(let ((result ,initial-value))
1228 (when ,(cond ((and (constantp initial-value)
1229 (not (null (eval initial-value))))
1230 ; Initial value is given and we
1231 ; know it's not NIL, so leave
1232 ; out the null check
1234 (t '(or (null result)
1236 (setq result value)))
1237 #'(lambda nil result))))
1239 (defmacro summing (&key (initial-value 0))
1240 `(let ((sum ,initial-value))
1241 (values #'(lambda (value)
1242 (setq sum (+ sum value)))
1243 #'(lambda nil sum))))
1245 ;;; It's easier to read expanded code if PROG1 gets left alone.
1246 (define-walker-template prog1 (nil return sb-walker::repeat (eval)))