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
29 ;;; :USER => those resulting from user code
30 ;;; T => always, even if it's the iteration macro that's suboptimal.
31 (defvar *iterate-warnings* :any)
34 (defmacro iterate (clauses &body body &environment env)
35 (optimize-iterate-form clauses body env))
37 ;;; temporary variable names used by ITERATE expansions
38 (defparameter *iterate-temp-vars-list*
39 '(iterate-temp-1 iterate-temp-2 iterate-temp-3 iterate-temp-4
40 iterate-temp-5 iterate-temp-6 iterate-temp-7 iterate-temp-8))
44 (clauses body iterate-env)
46 ((temp-vars *iterate-temp-vars-list*)
48 (finish-form `(return-from ,block-name))
49 (bound-vars (mapcan #'(lambda (clause)
50 (let ((names (first clause)))
55 iterate-decls generator-decls update-forms bindings leftover-body)
56 (do ((tail bound-vars (cdr tail)))
58 ;; Check for duplicates
59 (when (member (car tail)
61 (warn "Variable appears more than once in ITERATE: ~S" (car tail))))
63 ((get-iterate-temp nil
65 ;; Make temporary var. Note that it is ok to re-use these symbols
66 ;; in each iterate, because they are not used within BODY.
69 (dolist (clause clauses)
71 ((or (not (consp clause))
72 (not (consp (cdr clause))))
73 (warn "bad syntax in ITERATE: clause not of form (var iterator): ~S"
76 (unless (null (cddr clause))
78 "probable parenthesis error in ITERATE clause--more than 2 elements: ~S"
81 (let-body binding-type let-bindings localdecls otherdecls extra-body)
82 (expand-into-let (second clause)
85 ;; We have expanded the generator clause and parsed it into
88 ((vars (first clause))
89 gen-args renamed-vars)
90 (setq vars (if (listp vars)
93 ; VARS is now a (fresh) list of
94 ; all iteration vars bound in
98 ; Already issued a warning
101 ((null (setq let-body (function-lambda-p let-body 1)))
102 ; Not of the expected form
103 (let ((generator (second clause)))
104 (cond ((and (consp generator)
105 (fboundp (car generator)))
106 ; It looks ok--a macro or
107 ; function here--so the guy who
108 ; wrote it just didn't do it in
110 (maybe-warn :definition "could not optimize iterate clause ~S because generator not of form (LET[*] ... (FUNCTION (LAMBDA (finish) ...)))"
112 (t ; Perhaps it's just a
113 ; misspelling? Probably user
116 "Iterate operator in clause ~S is not fboundp."
118 (setq let-body :abort)))
121 ;; We have something of the form #'(LAMBDA (finisharg) ...),
122 ;; possibly with some LET bindings around it. LET-BODY =
123 ;; ((finisharg) ...).
124 (setq let-body (cdr let-body))
125 (setq gen-args (pop let-body))
128 ;; The first transformation we want to perform is
129 ;; "LET-eversion": turn (let* ((generator (let (..bindings..)
130 ;; #'(lambda ...)))) ..body..) into (let* (..bindings..
131 ;; (generator #'(lambda ...))) ..body..). This
132 ;; transformation is valid if nothing in body refers to any
133 ;; of the bindings, something we can ensure by
134 ;; alpha-converting the inner let (substituting new names for
135 ;; each var). Of course, none of those vars can be special,
136 ;; but we already checked for that above.
137 (multiple-value-setq (let-bindings renamed-vars)
138 (rename-let-bindings let-bindings binding-type
139 iterate-env leftover-body #'get-iterate-temp))
140 (setq leftover-body nil)
141 ; If there was any leftover
142 ; from previous, it is now
146 ;; The second transformation is substituting the body of the
147 ;; generator (LAMBDA (finish-arg) . gen-body) for its appearance
148 ;; in the update form (funcall generator #'(lambda ()
149 ;; finish-form)), then simplifying that form. The requirement
150 ;; for this part is that the generator body not refer to any
151 ;; variables that are bound between the generator binding and the
152 ;; appearance in the loop body. The only variables bound in that
153 ;; interval are generator temporaries, which have unique names so
154 ;; are no problem, and the iteration variables remaining for
155 ;; subsequent clauses. We'll discover the story as we walk the
157 (multiple-value-bind (finishdecl other rest)
158 (parse-declarations let-body gen-args)
159 (declare (ignore finishdecl))
160 ; Pull out declares, if any,
161 ; separating out the one(s)
162 ; referring to the finish arg,
163 ; which we will throw away.
165 ; Combine remaining decls with
166 ; decls extracted from the LET,
168 (setq otherdecls (nconc otherdecls other)))
171 ; There are interesting
172 ; declarations, so have to keep
174 `(let nil (declare ,@otherdecls)
179 (t `(progn ,@rest)))))
180 (unless (eq (setq let-body (iterate-transform-body let-body
181 iterate-env renamed-vars
183 finish-form bound-vars clause))
186 ;; Skip the rest if transformation failed. Warning has
187 ;; already been issued.
189 ;; Note possible further optimization: if LET-BODY expanded
190 ;; into (prog1 oldvalue prepare-for-next-iteration), as so
191 ;; many do, then we could in most cases split the PROG1 into
192 ;; two pieces: do the (setq var oldvalue) here, and do the
193 ;; prepare-for-next-iteration at the bottom of the loop.
194 ;; This does a slight optimization of the PROG1 and also
195 ;; rearranges the code in a way that a reasonably clever
196 ;; compiler might detect how to get rid of redundant
197 ;; variables altogether (such as happens with INTERVAL and
198 ;; LIST-TAILS); that would make the whole thing closer to
199 ;; what you might have coded by hand. However, to do this
200 ;; optimization, we need to ensure that (a) the
201 ;; prepare-for-next-iteration refers freely to no vars other
202 ;; than the internal vars we have extracted from the LET, and
203 ;; (b) that the code has no side effects. These are both
204 ;; true for all the iterators defined by this module, but how
205 ;; shall we represent side-effect info and/or tap into the
206 ;; compiler's knowledge of same?
208 ; There were declarations for
209 ; the generator locals--have to
210 ; keep them for later, and
211 ; rename the vars mentioned
219 (let ((head (car decl)))
220 (cons head (if (eq head 'type)
228 ;; Finished analyzing clause now. LET-BODY is the form which, when
229 ;; evaluated, returns updated values for the iteration variable(s)
231 (when (eq let-body :abort)
233 ;; Some punt case: go with the formal semantics: bind a var to
234 ;; the generator, then call it in the update section
236 ((gvar (get-iterate-temp))
237 (generator (second clause)))
242 ;; FIXME: This conditional was here with this
243 ;; comment in old CMU CL PCL. Does Python really
244 ;; think it's unreachable?
245 ;;#-cmu ; Python thinks this is unreachable.
247 ; Have to use this up
248 `(progn ,@(prog1 leftover-body (setq
253 (setq let-body `(funcall ,gvar #'(lambda nil ,finish-form)))))
254 (push (mv-setq (copy-list vars)
259 ;; Pop off the vars we have now bound from the list of vars to
260 ;; watch out for -- we'll bind them right now.
263 (nconc bindings let-bindings
265 ;; There was some computation to do after the
266 ;; bindings--here's our chance.
267 (cons (list (first vars)
268 `(progn ,@extra-body nil))
271 (do ((tail body (cdr tail)))
272 ((not (and (consp tail)
277 ;; TAIL now points at first non-declaration. If there were
278 ;; declarations, pop them off so they appear in the right place
279 (unless (eq tail body)
280 (setq iterate-decls (ldiff body tail))
283 (let* ,bindings ,@(and generator-decls
284 `((declare ,@generator-decls)))
287 (loop ,@(nreverse update-forms)
290 (defun expand-into-let (clause parent-name env)
292 ;; Return values: Body, LET[*], bindings, localdecls, otherdecls, extra
293 ;; body, where BODY is a single form. If multiple forms in a LET, the
294 ;; preceding forms are returned as extra body. Returns :ABORT if it
295 ;; issued a punt warning.
296 (prog ((expansion clause)
297 expandedp binding-type let-bindings let-body)
299 (multiple-value-setq (expansion expandedp)
300 (macroexpand-1 expansion env))
301 (cond ((not (consp expansion))
304 ((symbolp (setq binding-type (first expansion)))
307 (setq let-bindings (second expansion))
308 ; List of variable bindings
309 (setq let-body (cddr expansion))
311 ((and (consp binding-type)
312 (eq (car binding-type)
314 (not (find-if #'(lambda (x)
315 (member x lambda-list-keywords)
317 (setq let-bindings (second binding-type)))
319 (eql (length (second expansion))
320 (length let-bindings))
321 (null (cddr expansion)))
322 ; A simple LAMBDA form can be
324 (setq let-body (cddr binding-type))
325 (setq let-bindings (mapcar #'list let-bindings (second
328 (setq binding-type 'let)
331 ;; Fall thru if not a LET
332 (cond (expandedp ; try expanding again
334 (t ; Boring--return form as the
338 (return (let ((locals (variables-from-let let-bindings))
340 (multiple-value-bind (localdecls otherdecls let-body)
341 (parse-declarations let-body locals)
342 (cond ((setq specials (extract-special-bindings
344 (maybe-warn (cond ((find-if #'variable-globally-special-p
346 ; This could be the fault of a
351 "Couldn't optimize ~S because expansion of ~S binds specials ~(~S ~)"
352 parent-name clause specials)
354 (t (values (cond ((not (consp let-body))
356 ; Null body of LET? unlikely,
357 ; but someone else will likely
360 ((null (cdr let-body))
362 ; A single expression, which we
368 ;; More than one expression. These are forms to
369 ;; evaluate after the bindings but before the
370 ;; generator form is returned. Save them to
371 ;; evaluate in the next convenient place. Note that
372 ;; this is ok, as there is no construct that can
373 ;; cause a LET to return prematurely (without
374 ;; returning also from some surrounding construct).
377 (car (last let-body))))
378 binding-type let-bindings localdecls
379 otherdecls extra-body))))))))
381 (defun variables-from-let (bindings)
383 ;; Return a list of the variables bound in the first argument to LET[*].
384 (mapcar #'(lambda (binding)
390 (defun iterate-transform-body (let-body iterate-env renamed-vars finish-arg
391 finish-form bound-vars clause)
393 ;;; This is the second major transformation for a single iterate clause.
394 ;;; LET-BODY is the body of the iterator after we have extracted its local
395 ;;; variables and declarations. We have two main tasks: (1) Substitute
396 ;;; internal temporaries for occurrences of the LET variables; the alist
397 ;;; RENAMED-VARS specifies this transformation. (2) Substitute evaluation of
398 ;;; FINISH-FORM for any occurrence of (funcall FINISH-ARG). Along the way, we
399 ;;; check for forms that would invalidate these transformations: occurrence of
400 ;;; FINISH-ARG outside of a funcall, and free reference to any element of
401 ;;; BOUND-VARS. CLAUSE & TYPE are the original ITERATE clause and its type
402 ;;; (ITERATE or ITERATE*), for purpose of error messages. On success, we
403 ;;; return the transformed body; on failure, :ABORT.
405 (walk-form let-body iterate-env
406 #'(lambda (form context env)
407 (declare (ignore context))
409 ;; Need to substitute RENAMED-VARS, as well as turn
410 ;; (FUNCALL finish-arg) into the finish form
411 (cond ((symbolp form)
413 (cond ((and (eq form finish-arg)
414 (variable-same-p form env
416 ; An occurrence of the finish
417 ; arg outside of FUNCALL
418 ; context--I can't handle this
419 (maybe-warn :definition "Couldn't optimize iterate form because generator ~S does something with its FINISH arg besides FUNCALL it."
421 (return-from iterate-transform-body
423 ((and (setq renaming (assoc form
426 (variable-same-p form env
428 ; Reference to one of the vars
431 ((and (member form bound-vars)
432 (variable-same-p form env
434 ; FORM is a var that is bound
435 ; in this same ITERATE, or
436 ; bound later in this ITERATE*.
437 ; This is a conflict.
438 (maybe-warn :user "Couldn't optimize iterate form because generator ~S is closed over ~S, in conflict with a subsequent iteration variable."
441 (return-from iterate-transform-body
449 (variable-same-p (second form)
451 ; (FUNCALL finish-arg) =>
453 (unless (null (cddr form))
454 (maybe-warn :definition
455 "Generator for ~S applied its finish arg to > 0 arguments ~S--ignored."
465 ;; Extract the declarations from the head of TAIL and divide them into 2
466 ;; classes: declares about variables in the list LOCALS, and all other
467 ;; declarations. Returns 3 values: those 2 lists plus the remainder of TAIL.
469 (localdecls otherdecls form)
471 (unless (and tail (consp (setq form (car tail)))
474 (return (values localdecls otherdecls tail)))
479 ((inline notinline optimize)
480 ; These don't talk about vars
481 (push decl otherdecls))
482 (t ; Assume all other kinds are
484 (let* ((vars (if (eq (first decl)
488 (l (intersection locals vars))
492 ; None talk about LOCALS
493 (push decl otherdecls))
494 ((null (setq other (set-difference vars l)))
495 ; All talk about LOCALS
496 (push decl localdecls))
498 (let ((head (cons 'type (and (eq (first decl)
500 (list (second decl))))))
501 (push (append head other)
503 (push (append head l)
508 (defun extract-special-bindings (vars decls)
510 ;; Return the subset of VARS that are special, either globally or
511 ;; because of a declaration in DECLS
512 (let ((specials (remove-if-not #'variable-globally-special-p vars)))
516 (setq specials (union specials (intersection vars
520 (defun function-lambda-p (form &optional nargs)
522 ;; If FORM is #'(LAMBDA bindings . body) and bindings is of length
523 ;; NARGS, return the lambda expression
528 (consp (setq form (cdr form)))
530 (consp (setq form (car form)))
533 (consp (setq body (cdr form)))
534 (listp (setq args (car body)))
542 (let-bindings binding-type env leftover-body &optional tempvarfn)
544 ;; Perform the alpha conversion required for "LET eversion" of
545 ;; (LET[*] LET-BINDINGS . body)--rename each of the variables to an
546 ;; internal name. Returns 2 values: a new set of LET bindings and the
547 ;; alist of old var names to new (so caller can walk the body doing
548 ;; the rest of the renaming). BINDING-TYPE is one of LET or LET*.
549 ;; LEFTOVER-BODY is optional list of forms that must be eval'ed
550 ;; before the first binding happens. ENV is the macro expansion
551 ;; environment, in case we have to walk a LET*. TEMPVARFN is a
552 ;; function of no args to return a temporary var; if omitted, we use
556 (values (mapcar #'(lambda (binding)
557 (let ((valueform (cond ((not (consp binding))
561 ((or (eq binding-type
565 ; All bindings are in parallel,
566 ; so none can refer to others
569 ; In a LET*, have to substitute
570 ; vars in the 2nd and
571 ; subsequent initialization
576 (newvar (if tempvarfn
579 (push (cons (if (consp binding)
584 ; Add new variable to the list
585 ; AFTER we have walked the
588 ;; Previous clause had some computation to do
589 ;; after its bindings. Here is the first
590 ;; opportunity to do it
591 (setq valueform `(progn ,@leftover-body
593 (setq leftover-body nil))
594 (list newvar valueform)))
598 (defun rename-variables (form alist env)
600 ;; Walks FORM, renaming occurrences of the key variables in ALIST with
601 ;; their corresponding values. ENV is FORM's environment, so we can
602 ;; make sure we are talking about the same variables.
604 #'(lambda (form context subenv)
605 (declare (ignore context))
607 (cond ((and (symbolp form)
608 (setq pair (assoc form alist))
609 (variable-same-p form subenv env))
617 ;; Produces (MULTIPLE-VALUE-SETQ vars expr), except that I'll optimize some
618 ;; of the simple cases for benefit of compilers that don't, and I don't care
619 ;; what the value is, and I know that the variables need not be set in
620 ;; parallel, since they can't be used free in EXPR
623 ; EXPR is a side-effect
626 ; This is an error, but I'll
627 ; let MULTIPLE-VALUE-SETQ
629 `(multiple-value-setq ,vars ,expr))
634 ;; (mv-setq (a b c) (values x y z)) can be reduced to a parallel setq
635 ;; (psetq returns nil, but I don't care about returned value). Do this
636 ;; even for the single variable case so that we catch (mv-setq (a) (values
640 `(setq ,@(mapcon #'(lambda (tail)
642 (cond ((or (cdr tail)
644 ; One result expression for
647 (t ; More expressions than vars,
648 ; so arrange to evaluate all
650 (cons 'prog1 expr)))))
653 ; Simple one variable case
656 (t ; General case--I know nothing
657 `(multiple-value-setq ,vars ,expr))))
659 (defun variable-same-p (var env1 env2)
660 (eq (variable-lexical-p var env1)
661 (variable-lexical-p var env2)))
663 (defun maybe-warn (type &rest warn-args)
665 ;; Issue a warning about not being able to optimize this thing. TYPE
666 ;; is one of :DEFINITION, meaning the definition is at fault, and
667 ;; :USER, meaning the user's code is at fault.
668 (when (case *iterate-warnings*
670 ((:user) (eq type :user))
672 (apply #'warn warn-args)))
676 ;;; FIXME: If they're only samples, can they be commented out?
680 (&whole whole &key from downfrom to downto above below by type)
683 (error "Can't use both FROM and DOWNFROM in ~S" whole))
684 ((cdr (remove nil (list to downto above below)))
685 (error "Can't use more than one limit keyword in ~S" whole))
688 ((down (or downfrom downto above))
689 (limit (or to downto above below))
690 (inc (cond ((null by)
693 ; Can inline this increment
696 ((from ,(or from downfrom 0))
697 ,@(and limit `((to ,limit)))
700 ,@(and type `((declare (type ,type from ,@(and limit '(to))
705 ,@(cond ((null limit)
706 ; We won't use the FINISH arg.
707 '((declare (ignore finish)))))
708 (prog1 ,(cond (limit ; Test the limit. If ok,
709 ; return current value and
710 ; increment, else quit
711 `(if (,(cond (above '>)
720 (setq from (,(if down
724 ,(or inc 'by))))))))))
726 (defmacro list-elements (list &key (by '#'cdr))
729 (prog1 (if (endp tail)
732 (setq tail (funcall ,by tail))))))
734 (defmacro list-tails (list &key (by '#'cdr))
737 (prog1 (if (endp tail)
740 (setq tail (funcall ,by tail))))))
745 "Generates successive elements of SEQUENCE, with second value being the index. Use (ELEMENTS (THE type arg)) if you care about the type."
747 ((type (and (consp sequence)
752 (sequence-accessor type)
754 (listp (eq type 'list)))
756 ;; If type is given via THE, we may be able to generate a good accessor here
757 ;; for the benefit of implementations that aren't smart about (ELT (THE
758 ;; STRING FOO)). I'm not bothering to keep the THE inside the body,
759 ;; however, since I assume any compiler that would understand (AREF (THE
760 ;; SIMPLE-ARRAY S)) would also understand that (AREF S) is the same when I
761 ;; bound S to (THE SIMPLE-ARRAY foo) and never modified it.
763 ;; If sequence is declared to be a list, it's better to cdr down it, so we
764 ;; have some extra cases here. Normally folks would write LIST-ELEMENTS,
765 ;; but maybe they wanted to get the index for free...
769 '((size (length s)))))
771 (values (cond ,(if listp
775 (,accessor s index)))
776 (t (funcall finish)))
778 (setq index (1+ index))))))))
783 "Generates each time 2 items, the indicator and the value."
784 `(let ((tail ,plist))
786 (values (if (endp tail)
789 (prog1 (if (endp (setq tail (cdr tail)))
792 (setq tail (cdr tail)))))))
794 (defun sequence-accessor (type)
796 ;; returns the function with which most efficiently to make accesses to
797 ;; a sequence of type TYPE.
798 (case (if (consp type)
799 ; e.g., (VECTOR FLOAT *)
802 ((array simple-array vector) 'aref)
803 (simple-vector 'svref)
805 (simple-string 'schar)
807 (simple-bit-vector 'sbit)
810 ;; These "iterators" may be withdrawn
812 (defmacro eachtime (expr)
814 (declare (ignore finish))
817 (defmacro while (expr)
819 (unless ,expr (funcall finish))))
821 (defmacro until (expr)
823 (when ,expr (funcall finish))))
827 (defmacro gathering (clauses &body body &environment env)
828 (or (optimize-gathering-form clauses body env)
829 (simple-expand-gathering-form clauses body env)))
831 (defmacro with-gathering (clauses gather-body &body use-body)
832 "Binds the variables specified in CLAUSES to the result of (GATHERING clauses gather-body) and evaluates the forms in USE-BODY inside that contour."
834 ;; We may optimize this a little better later for those compilers that
835 ;; don't do a good job on (m-v-bind vars (... (values ...)) ...).
836 `(multiple-value-bind ,(mapcar #'car clauses)
837 (gathering ,clauses ,gather-body)
841 simple-expand-gathering-form
843 (declare (ignore env))
845 ;; The "formal semantics" of GATHERING. We use this only in cases that can't
848 ((acc-names (mapcar #'first (if (symbolp clauses)
849 ; Shorthand using anonymous
851 (setq clauses `((*anonymous-gathering-site*
854 (realizer-names (mapcar #'(lambda (binding)
855 (declare (ignore binding))
858 `(multiple-value-call
860 ,(mapcan #'list acc-names realizer-names)
861 (flet ((gather (value &optional (accumulator *anonymous-gathering-site*)
863 (funcall accumulator value)))
865 (values ,@(mapcar #'(lambda (rname)
868 ,@(mapcar #'second clauses))))
870 (defvar *active-gatherers* nil
871 "List of GATHERING bindings currently active during macro expansion)")
873 (defvar *anonymous-gathering-site* nil "Variable used in formal expansion of an abbreviated GATHERING form (one with anonymous gathering site).")
875 (defun optimize-gathering-form (clauses body gathering-env)
877 (acc-info leftover-body top-bindings finish-forms top-decls)
878 (dolist (clause (if (symbolp clauses)
880 `((*anonymous-gathering-site* (,clauses)))
883 (let-body binding-type let-bindings localdecls otherdecls extra-body)
884 (expand-into-let (second clause)
885 'gathering gathering-env)
887 ((acc-var (first clause))
888 renamed-vars accumulator realizer)
889 (when (and (consp let-body)
892 (consp (setq let-body (cdr let-body)))
893 (setq accumulator (function-lambda-p (car let-body)))
894 (consp (setq let-body (cdr let-body)))
895 (setq realizer (function-lambda-p (car let-body)
897 (null (cdr let-body)))
899 ;; Macro returned something of the form
900 ;; (VALUES #'(lambda (value) ...)
901 ;; #'(lambda () ...)),
902 ;; a function to accumulate values and a function to realize the
906 ;; Gatherer expanded into a LET
907 (cond (otherdecls (maybe-warn :definition "Couldn't optimize GATHERING clause ~S because its expansion carries declarations about more than the bound variables: ~S"
909 `(declare ,@otherdecls))
913 ;; The first transformation we want to perform is a
914 ;; variant of "LET-eversion": turn
917 ;; (let (..bindings..)
918 ;; (values #'(lambda ...)
922 ;; (let* (..bindings..
923 ;; (acc #'(lambda ...))
924 ;; (real #'(lambda ...)))
926 ;; This transformation is valid if nothing in body refers
927 ;; to any of the bindings, something we can ensure by
928 ;; alpha-converting the inner let (substituting new names
929 ;; for each var). Of course, none of those vars can be
930 ;; special, but we already checked for that above.
931 (multiple-value-setq (let-bindings renamed-vars)
932 (rename-let-bindings let-bindings binding-type
933 gathering-env leftover-body))
934 (setq top-bindings (nconc top-bindings let-bindings))
935 (setq leftover-body nil)
936 ; If there was any leftover
937 ; from previous, it is now
940 (setq leftover-body (nconc leftover-body extra-body))
941 ; Computation to do after these
943 (push (cons acc-var (rename-and-capture-variables accumulator
944 renamed-vars gathering-env))
946 (setq realizer (rename-variables realizer renamed-vars
948 (push (cond ((null (cdddr realizer))
949 ; Simple (LAMBDA () expr) =>
952 (t ; There could be declarations
953 ; or something, so leave as a
955 (cons 'let (cdr realizer))))
957 (unless (null localdecls)
958 ; Declarations about the LET
959 ; variables also has to
961 (setq top-decls (nconc top-decls (sublis renamed-vars
964 (maybe-warn :definition "Couldn't optimize GATHERING clause ~S because its expansion is not of the form (VALUES #'(LAMBDA ...) #'(LAMBDA () ...))"
969 (expansion `(multiple-value-list ,(second clause))))
970 ; Slow way--bind gensym to the
971 ; macro expansion, and we will
972 ; funcall it in the body
973 (push (list acc-var gs)
975 (push `(funcall (cadr ,gs))
981 (list (list gs (cond (leftover-body
982 `(progn ,@(prog1 leftover-body
983 (setq leftover-body nil))
985 (t expansion))))))))))
986 (setq body (walk-gathering-body body gathering-env acc-info))
987 (cond ((eq body :abort)
988 ; Couldn't finish expansion
990 (t `(let* ,top-bindings
991 ,@(and top-decls `((declare ,@top-decls)))
993 ,(cond ((null (cdr finish-forms))
994 ; just a single value
996 (t `(values ,@(reverse finish-forms)))))))))
998 (defun rename-and-capture-variables (form alist env)
1000 ;; Walks FORM, renaming occurrences of the key variables in ALIST with
1001 ;; their corresponding values, and capturing any other free variables.
1002 ;; Returns a list of the new form and the list of other closed-over
1003 ;; vars. ENV is FORM's environment, so we can make sure we are talking
1004 ;; about the same variables.
1008 #'(lambda (form context subenv)
1009 (declare (ignore context))
1011 (cond ((or (not (symbolp form))
1012 (not (variable-same-p form subenv
1014 ; non-variable or one that has
1017 ((setq pair (assoc form alist))
1021 (pushnew form closed)
1027 (body gathering-env acc-info)
1029 ;; Walk the body of (GATHERING (...) . BODY) in environment GATHERING-ENV.
1030 ;; ACC-INFO is a list of information about each of the gathering "bindings"
1031 ;; in the form, in the form (var gatheringfn freevars env)
1033 ((*active-gatherers* (nconc (mapcar #'car acc-info)
1034 *active-gatherers*)))
1036 ;; *ACTIVE-GATHERERS* tells us what vars are currently legal as GATHER
1037 ;; targets. This is so that when we encounter a GATHER not belonging to us
1038 ;; we can know whether to warn about it.
1044 (declare (ignore context))
1048 ((not (eq (car form)
1050 ; We only care about GATHER
1051 (when (and (eq (car form)
1055 ; Passed as functional--can't
1058 "Can't optimize GATHERING because of reference to #'GATHER."
1060 (return-from walk-gathering-body :abort))
1062 ((setq info (assoc (setq site (if (null (cddr form))
1065 *anonymous-gathering-site*
1068 ; One of ours--expand (GATHER
1069 ; value var). INFO = (var
1070 ; gatheringfn freevars env)
1071 (unless (null (cdddr form))
1072 (warn "Extra arguments (> 2) in ~S discarded." form)
1074 (let ((fn (second info)))
1076 ; Unoptimized case--just call
1077 ; the gatherer. FN is the
1078 ; gensym that we bound to the
1079 ; list of two values returned
1080 ; from the gatherer.
1083 (t ; FN = (lambda (value) ...)
1084 (dolist (s (third info))
1085 (unless (or (variable-same-p s env
1087 (and (variable-special-p
1092 ;; Some var used free in the LAMBDA form has been
1093 ;; rebound between here and the parent GATHERING
1094 ;; form, so can't substitute the lambda. Ok if it's
1095 ;; a special reference both here and in the LAMBDA,
1096 ;; because then it's not closed over.
1097 (maybe-warn :user "Can't optimize GATHERING because the expansion closes over the variable ~S, which is rebound around a GATHER for it."
1099 (return-from walk-gathering-body
1102 ;; Return ((lambda (value) ...) actual-value). In
1103 ;; many cases we could simplify this further by
1104 ;; substitution, but we'd have to be careful (for
1105 ;; example, we would need to alpha-convert any LET
1106 ;; we found inside). Any decent compiler will do it
1108 (list fn (second form))))))
1109 ((and (setq info (member site *active-gatherers*))
1110 (or (eq site '*anonymous-gathering-site*)
1111 (variable-same-p site env (fourth info))))
1112 ; Some other GATHERING will
1113 ; take care of this form, so
1114 ; pass it up for now.
1115 ; Environment check is to make
1116 ; sure nobody shadowed it
1117 ; between here and there
1119 (t ; Nobody's going to handle it
1120 (if (eq site '*anonymous-gathering-site*)
1121 ; More likely that she forgot
1122 ; to mention the site than
1123 ; forget to write an anonymous
1125 (warn "There is no gathering site specified in ~S."
1128 "The site ~S in ~S is not defined in an enclosing GATHERING form."
1130 ; Turn it into something else
1131 ; so we don't warn twice in the
1133 `(%orphaned-gather ,@(cdr form)))))
1134 ((and (symbolp form)
1135 (setq info (assoc form acc-info))
1136 (variable-same-p form env gathering-env))
1137 ; A variable reference to a
1138 ; gather binding from
1140 (maybe-warn :user "Can't optimize GATHERING because site variable ~S is used outside of a GATHER form."
1142 (return-from walk-gathering-body :abort))
1147 ;; FIXME: If these are only samples, can we delete them?
1151 (&key initial-value)
1152 `(let* ((head ,initial-value)
1153 (tail ,(and initial-value `(last head))))
1154 (values #'(lambda (value)
1156 (setq head (setq tail (list value)))
1157 (setq tail (cdr (rplacd tail (list value))))))
1158 #'(lambda nil head))))
1160 (defmacro joining (&key initial-value)
1161 `(let ((result ,initial-value))
1162 (values #'(lambda (value)
1163 (setq result (nconc result value)))
1164 #'(lambda nil result))))
1168 (&key initial-value)
1169 `(let ((result ,initial-value))
1172 (when ,(cond ((and (constantp initial-value)
1173 (not (null (eval initial-value))))
1174 ; Initial value is given and we
1175 ; know it's not NIL, so leave
1176 ; out the null check
1178 (t '(or (null result)
1180 (setq result value)))
1181 #'(lambda nil result))))
1185 (&key initial-value)
1186 `(let ((result ,initial-value))
1189 (when ,(cond ((and (constantp initial-value)
1190 (not (null (eval initial-value))))
1191 ; Initial value is given and we
1192 ; know it's not NIL, so leave
1193 ; out the null check
1195 (t '(or (null result)
1197 (setq result value)))
1198 #'(lambda nil result))))
1200 (defmacro summing (&key (initial-value 0))
1201 `(let ((sum ,initial-value))
1202 (values #'(lambda (value)
1203 (setq sum (+ sum value)))
1204 #'(lambda nil sum))))
1206 ;;; It's easier to read expanded code if PROG1 gets left alone.
1207 (define-walker-template prog1 (nil return sb-walker::repeat (eval)))