Debian Codegraph @

src/clos/walk.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: WALKER
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;
6 ;;; *************************************************************************
7 ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
8 ;;; All rights reserved.
9 ;;;
10 ;;; Use and copying of this software and preparation of derivative works
11 ;;; based upon this software are permitted. Any distribution of this
12 ;;; software or derivative works must comply with all applicable United
13 ;;; States export control laws.
14 ;;;
15 ;;; This software is made available AS IS, and Xerox Corporation makes no
16 ;;; warranty about the software, its performance or its conformity to any
17 ;;; specification.
18 ;;;
19 ;;; Any person obtaining a copy of this software is requested to send their
20 ;;; name and post office or electronic mail address to:
21 ;;; CommonLoops Coordinator
22 ;;; Xerox PARC
23 ;;; 3333 Coyote Hill Rd.
24 ;;; Palo Alto, CA 94304
25 ;;; (or send Arpanet mail to [email protected])
26 ;;;
27 ;;; Suggestions, comments and requests for improvements are also welcome.
28 ;;; *************************************************************************
29 ;;;
30 ;;; A simple code walker, based IN PART on: (roll the credits)
31 ;;; Larry Masinter's Masterscope
32 ;;; Moon's Common Lisp code walker
33 ;;; Gary Drescher's code walker
34 ;;; Larry Masinter's simple code walker
35 ;;; .
36 ;;; .
37 ;;; boy, thats fair (I hope).
38 ;;;
39 ;;; For now at least, this code walker really only does what PCL needs it to
40 ;;; do. Maybe it will grow up someday.
41 ;;;
42
43 ;;;
44 ;;; This code walker used to be completely portable. Now it is just "Real
45 ;;; easy to port". This change had to happen because the hack that made it
46 ;;; completely portable kept breaking in different releases of different
47 ;;; Common Lisps, and in addition it never worked entirely anyways. So,
48 ;;; its now easy to port. To port this walker, all you have to write is one
49 ;;; simple macro and two simple functions. These macros and functions are
50 ;;; used by the walker to manipluate the macroexpansion environments of
51 ;;; the Common Lisp it is running in.
52 ;;;
53 ;;; The code which implements the macroexpansion environment manipulation
54 ;;; mechanisms is in the first part of the file, the real walker follows it.
55 ;;;
56
57 ;;;
58 ;;; The user entry points are walk-form and nested-walked-form. In addition,
59 ;;; it is legal for user code to call the variable information functions:
60 ;;; variable-lexical-p, variable-special-p and variable-class. Some users
61 ;;; will need to call define-walker-template, they will have to figure that
62 ;;; out for themselves.
63 ;;;
64
65 (pushnew :walker *features*)
66
67 (defpackage "WALKER"
68 (:export define-walker-template
69 walk-form
70 walk-form-expand-macros-p
71 #-ecl nested-walk-form
72 variable-lexical-p
73 variable-special-p
74 *variable-declarations*
75 variable-declaration
76 macroexpand-all
77 )
78 (:import-from "SI" "GET-SYSPROP" "PUT-SYSPROP"))
79
80 (in-package "WALKER")
81 (declaim (notinline note-lexical-binding walk-bindings-1 walk-let/let*
82 walk-form-internal))
83
84
85 ;;;
86 ;;; On the following pages are implementations of the implementation specific
87 ;;; environment hacking functions.
88 ;;;
89 ;;; This code just hacks 'macroexpansion environments'. That is, it is only
90 ;;; concerned with the function binding of symbols in the environment. The
91 ;;; walker needs to be able to tell if the symbol names a lexical macro or
92 ;;; function, and it needs to be able to build environments which contain
93 ;;; lexical macro or function bindings. It must be able, when walking a
94 ;;; macrolet, flet or labels form to construct an environment which reflects
95 ;;; the bindings created by that form. Note that the environment created
96 ;;; does NOT have to be sufficient to evaluate the body, merely to walk its
97 ;;; body. This means that definitions do not have to be supplied for lexical
98 ;;; functions, only the fact that that function is bound is important. For
99 ;;; macros, the macroexpansion function must be supplied.
100 ;;;
101 ;;; This code is organized in a way that lets it work in implementations that
102 ;;; stack cons their environments. That is reflected in the fact that the
103 ;;; only operation that lets a user build a new environment is a with-body
104 ;;; macro which executes its body with the specified symbol bound to the new
105 ;;; environment. No code in this walker or in PCL will hold a pointer to
106 ;;; these environments after the body returns. Other user code is free to do
107 ;;; so in implementations where it works, but that code is not considered
108 ;;; portable.
109 ;;;
110 ;;; There are 3 environment hacking tools. One macro which is used for
111 ;;; creating new environments, and two functions which are used to access the
112 ;;; bindings of existing environments.
113 ;;;
114 ;;; WITH-AUGMENTED-ENVIRONMENT
115 ;;;
116 ;;; ENVIRONMENT-FUNCTION
117 ;;;
118 ;;; ENVIRONMENT-MACRO
119 ;;;
120
121 (defun unbound-lexical-function (&rest args)
122 (declare (ignore args))
123 (error "The evaluator was called to evaluate a form in a macroexpansion~%~
124 environment constructed by the PCL portable code walker. These~%~
125 environments are only useful for macroexpansion, they cannot be~%~
126 used for evaluation.~%~
127 This error should never occur when using PCL.~%~
128 This most likely source of this error is a program which tries to~%~
129 to use the PCL portable code walker to build its own evaluator."))
130
131
132 ;;;
133 ;;; In Kyoto Common Lisp, the macroexpansion environment is a three element
134 ;;; list. The second element describes lexical functions and macros. The
135 ;;; function entries in this list have the form
136 ;;; (<name> FUNCTION <function-value>)
137 ;;; The macro entries have the form
138 ;;; (<name> MACRO <macro-value>).
139 ;;;
140 ;;;
141
142 (defmacro with-augmented-environment
143 ((new-env old-env &key functions macros) &body body)
144 `(let ((,new-env (with-augmented-environment-internal ,old-env
145 ,functions
146 ,macros)))
147 ,@body))
148
149 (defun with-augmented-environment-internal (env functions macros)
150 (let* ((vars (car env))
151 (funs (cdr env)))
152 (dolist (f functions)
153 (push `(,(car f) function ,#'unbound-lexical-function) funs))
154 (dolist (m macros)
155 (push `(,(car m) macro ,(second m)) funs))
156 (cons vars funs)))
157
158 #+nil
159 (defun environment-function (env fn)
160 (when env
161 (let ((entry (assoc fn (cdr env))))
162 (and entry
163 (eq (second entry) 'FUNCTION)
164 (third entry)))))
165
166 (defun environment-macro (env macro)
167 (declare (si::c-local))
168 (when env
169 (let ((entry (assoc macro (cdr env))))
170 (and entry
171 (eq (second entry) 'MACRO)
172 (third entry)))))
173
174
175
176 (defmacro with-new-definition-in-environment
177 ((new-env old-env macrolet/flet/labels-form) &body body)
178 (let* ((functions (make-symbol "Functions"))
179 (macros (make-symbol "Macros")))
180 `(let ((,functions ())
181 (,macros ()))
182 (ecase (car ,macrolet/flet/labels-form)
183 ((FLET LABELS)
184 (dolist (fn (second ,macrolet/flet/labels-form))
185 (push fn ,functions)))
186 ((MACROLET)
187 (dolist (mac (second ,macrolet/flet/labels-form))
188 (push (list (car mac)
189 (convert-macro-to-lambda (second mac)
190 (cddr mac)
191 (string (car mac))))
192 ,macros))))
193 (with-augmented-environment
194 (,new-env ,old-env :functions ,functions :macros ,macros)
195 ,@body))))
196
197 (defun convert-macro-to-lambda (llist body &optional (name "Dummy Macro"))
198 (declare (si::c-local))
199 (let ((gensym (make-symbol name)))
200 (eval `(defmacro ,gensym ,llist ,@body))
201 (macro-function gensym)))
202
203 ;;;
204 ;;; Now comes the real walker.
205 ;;;
206 ;;; As the walker walks over the code, it communicates information to itself
207 ;;; about the walk. This information includes the walk function, variable
208 ;;; bindings, declarations in effect etc. This information is inherently
209 ;;; lexical, so the walker passes it around in the actual environment the
210 ;;; walker passes to macroexpansion functions. This is what makes the
211 ;;; nested-walk-form facility work properly.
212 ;;;
213 (defmacro walker-environment-bind ((var env &rest key-args)
214 &body body)
215 `(with-augmented-environment
216 (,var ,env :macros (walker-environment-bind-1 ,env ,.key-args))
217 .,body))
218
219 (defvar *key-to-walker-environment* (gensym))
220
221 (defun env-lock (env)
222 (declare (si::c-local))
223 (environment-macro env *key-to-walker-environment*))
224
225 (defun walker-environment-bind-1 (env &key (walk-function nil wfnp)
226 (walk-form nil wfop)
227 (declarations nil decp)
228 (lexical-variables nil lexp))
229 (declare (si::c-local))
230 (let ((lock (env-lock env)))
231 (list
232 (list *key-to-walker-environment*
233 (list (if wfnp walk-function (car lock))
234 (if wfop walk-form (second lock))
235 (if decp declarations (third lock))
236 (if lexp lexical-variables (fourth lock)))))))
237
238 (defun env-walk-function (env)
239 (declare (si::c-local))
240 (first (env-lock env)))
241
242 (defun env-walk-form (env)
243 (declare (si::c-local))
244 (second (env-lock env)))
245
246 (defun env-declarations (env)
247 (declare (si::c-local))
248 (third (env-lock env)))
249
250 (defun env-lexical-variables (env)
251 (declare (si::c-local))
252 (fourth (env-lock env)))
253
254
255 (defun note-declaration (declaration env)
256 (declare (si::c-local))
257 (push declaration (third (env-lock env))))
258
259 (defun note-lexical-binding (thing env)
260 (push (list thing :LEXICAL-VAR) (fourth (env-lock env))))
261
262 (defun VARIABLE-LEXICAL-P (var env)
263 (declare (si::c-local))
264 (let ((entry (member var (env-lexical-variables env) :key #'car)))
265 (when (eq (cadar entry) :LEXICAL-VAR)
266 entry)))
267
268 (defun variable-symbol-macro-p (var env)
269 (declare (si::c-local))
270 (let ((entry (member var (env-lexical-variables env) :key #'car)))
271 (when (eq (cadar entry) :macro)
272 entry)))
273
274 (defvar *VARIABLE-DECLARATIONS* '(SPECIAL TYPE)) ; Beppe
275
276 (defun VARIABLE-DECLARATION (declaration var env)
277 (if (not (member declaration *variable-declarations*))
278 (error "~S is not a recognized variable declaration." declaration)
279 (let ((id (or (variable-lexical-p var env) var)))
280 (dolist (decl (env-declarations env))
281 (when (and (eq (car decl) declaration)
282 (or (eq (second decl) id)
283 (and (eq 'TYPE (car decl))
284 (member var (cddr decl) :test #'eq)))) ; Beppe
285 (return decl))))))
286
287 (defun VARIABLE-SPECIAL-P (var env)
288 (or (not (null (variable-declaration 'SPECIAL var env)))
289 (variable-globally-special-p var)))
290
291 ;;;
292 ;;; VARIABLE-GLOBALLY-SPECIAL-P is used to ask if a variable has been
293 ;;; declared globally special. Any particular CommonLisp implementation
294 ;;; should customize this function accordingly and send their customization
295 ;;; back.
296 ;;;
297 ;;; The default version of variable-globally-special-p is probably pretty
298 ;;; slow, so it uses *globally-special-variables* as a cache to remember
299 ;;; variables that it has already figured out are globally special.
300 ;;;
301 ;;; This would need to be reworked if an unspecial declaration got added to
302 ;;; Common Lisp.
303 ;;;
304 ;;; Common Lisp nit:
305 ;;; variable-globally-special-p should be defined in Common Lisp.
306 ;;;
307
308 (defun variable-globally-special-p (symbol) (si:specialp symbol))
309
310
311 ;;
312 ;;;;;; Handling of special forms (the infamous 24).
313 ;;
314 ;;;
315 ;;; and I quote...
316 ;;;
317 ;;; The set of special forms is purposely kept very small because
318 ;;; any program analyzing program (read code walker) must have
319 ;;; special knowledge about every type of special form. Such a
320 ;;; program needs no special knowledge about macros...
321 ;;;
322 ;;; So all we have to do here is a define a way to store and retrieve
323 ;;; templates which describe how to walk the 24 special forms and we are all
324 ;;; set...
325 ;;;
326 ;;; Well, its a nice concept, and I have to admit to being naive enough that
327 ;;; I believed it for a while, but not everyone takes having only 24 special
328 ;;; forms as seriously as might be nice. There are (at least) 3 ways to
329 ;;; lose:
330 ;;
331 ;;; 1 - Implementation x implements a Common Lisp special form as a macro
332 ;;; which expands into a special form which:
333 ;;; - Is a common lisp special form (not likely)
334 ;;; - Is not a common lisp special form (on the 3600 IF --> COND).
335 ;;;
336 ;;; * We can safe ourselves from this case (second subcase really) by
337 ;;; checking to see if there is a template defined for something
338 ;;; before we check to see if we we can macroexpand it.
339 ;;;
340 ;;; 2 - Implementation x implements a Common Lisp macro as a special form.
341 ;;;
342 ;;; * This is a screw, but not so bad, we save ourselves from it by
343 ;;; defining extra templates for the macros which are *likely* to
344 ;;; be implemented as special forms. (DO, DO* ...)
345 ;;;
346 ;;; 3 - Implementation x has a special form which is not on the list of
347 ;;; Common Lisp special forms.
348 ;;;
349 ;;; * This is a bad sort of a screw and happens more than I would like
350 ;;; to think, especially in the implementations which provide more
351 ;;; than just Common Lisp (3600, Xerox etc.).
352 ;;; The fix is not terribly staisfactory, but will have to do for
353 ;;; now. There is a hook in get walker-template which can get a
354 ;;; template from the implementation's own walker. That template
355 ;;; has to be converted, and so it may be that the right way to do
356 ;;; this would actually be for that implementation to provide an
357 ;;; interface to its walker which looks like the interface to this
358 ;;; walker.
359 ;;;
360
361 (eval-when (#-cross compile load eval)
362
363 (defmacro get-walker-template-internal (x) ;Has to be inside eval-when because
364 `(get-sysprop ,x 'WALKER-TEMPLATE)) ;Golden Common Lisp doesn't hack
365 ;compile time definition of macros
366 ;right for setf.
367
368 (defmacro define-walker-template
369 (name &optional (template '(NIL REPEAT (EVAL))))
370 `(eval-when (load eval)
371 (put-sysprop ',name 'WALKER-TEMPLATE ',template)))
372 )
373
374 (defun get-walker-template (x)
375 (cond ((symbolp x)
376 (or (get-walker-template-internal x)
377 (get-implementation-dependent-walker-template x)))
378 ((and (listp x) (eq (car x) 'LAMBDA))
379 '(LAMBDA REPEAT (EVAL)))
380 (t
381 (error "Can't get template for ~S" x))))
382
383 (defun get-implementation-dependent-walker-template (x)
384 (declare (ignore x))
385 ())
386
387
388 ;;
389 ;;;;;; The actual templates
390 ;;
391
392 (define-walker-template BLOCK (NIL NIL REPEAT (EVAL)))
393 (define-walker-template CATCH (NIL EVAL REPEAT (EVAL)))
394 (define-walker-template COMPILER-LET walk-compiler-let)
395 (define-walker-template DECLARE walk-unexpected-declare)
396 (define-walker-template EVAL-WHEN (NIL QUOTE REPEAT (EVAL)))
397 (define-walker-template FLET walk-flet)
398 (define-walker-template FUNCTION (NIL CALL))
399 (define-walker-template GO (NIL QUOTE))
400 (define-walker-template IF walk-if)
401 (define-walker-template LABELS walk-labels)
402 (define-walker-template LAMBDA walk-lambda)
403 (define-walker-template LET walk-let)
404 (define-walker-template LET* walk-let*)
405 (define-walker-template LOCALLY walk-locally)
406 (define-walker-template MACROLET walk-macrolet)
407 (define-walker-template MULTIPLE-VALUE-CALL (NIL EVAL REPEAT (EVAL)))
408 (define-walker-template MULTIPLE-VALUE-PROG1 (NIL RETURN REPEAT (EVAL)))
409 (define-walker-template MULTIPLE-VALUE-SETQ walk-multiple-value-setq)
410 (define-walker-template MULTIPLE-VALUE-BIND walk-multiple-value-bind)
411 (define-walker-template PROGN (NIL REPEAT (EVAL)))
412 (define-walker-template PROGV (NIL EVAL EVAL REPEAT (EVAL)))
413 (define-walker-template QUOTE (NIL QUOTE))
414 (define-walker-template RETURN-FROM (NIL QUOTE REPEAT (RETURN)))
415 (define-walker-template SETQ walk-setq)
416 (define-walker-template SYMBOL-MACROLET walk-symbol-macrolet)
417 (define-walker-template TAGBODY walk-tagbody)
418 (define-walker-template THE (NIL QUOTE EVAL))
419 (define-walker-template THROW (NIL EVAL EVAL))
420 (define-walker-template UNWIND-PROTECT (NIL RETURN REPEAT (EVAL)))
421
422 ;;; The new special form.
423 ;(define-walker-template pcl::LOAD-TIME-EVAL (NIL EVAL))
424
425 ;;;
426 ;;; And the extra templates...
427 ;;;
428 #+ecl
429 (define-walker-template DOTIMES walk-dotimes/dolist)
430 #+ecl
431 (define-walker-template DOLIST walk-dotimes/dolist)
432 #+ecl
433 (define-walker-template WHEN walk-when/unless)
434 #+ecl
435 (define-walker-template UNLESS walk-when/unless)
436 (define-walker-template DO walk-do)
437 (define-walker-template DO* walk-do*)
438 (define-walker-template PROG walk-prog)
439 (define-walker-template PROG* walk-prog*)
440 (define-walker-template COND (NIL REPEAT ((TEST REPEAT (EVAL)))))
441 (define-walker-template ext::lambda-block walk-named-lambda) ;Not really right, but
442 ;we don't hack block
443 ;names anyways.
444 #+ecl
445 (define-walker-template ffi::c-inline walk-c-inline)
446
447
448 ;;; Controls whether macros are expanded by walk-form
449 (defvar WALK-FORM-EXPAND-MACROS-P nil)
450
451 (defun macroexpand-all (form &optional environment)
452 (let ((walk-form-expand-macros-p t))
453 (walk-form form environment)))
454
455 (defun WALK-FORM (form
456 &optional environment
457 (walk-function
458 #'(lambda (subform context env)
459 (declare (ignore context env))
460 subform)))
461 (walker-environment-bind (new-env environment :walk-function walk-function)
462 (walk-form-internal form :eval new-env)))
463
464 ;;;
465 ;;; nested-walk-form provides an interface that allows nested macros, each
466 ;;; of which must walk their body to just do one walk of the body of the
467 ;;; inner macro. That inner walk is done with a walk function which is the
468 ;;; composition of the two walk functions.
469 ;;;
470 ;;; This facility works by having the walker annotate the environment that
471 ;;; it passes to macroexpand-1 to know which form is being macroexpanded.
472 ;;; If then the &whole argument to the macroexpansion function is eq to
473 ;;; the env-walk-form of the environment, nested-walk-form can be certain
474 ;;; that there are no intervening layers and that a nested walk is alright.
475 ;;;
476 ;;; There are some semantic problems with this facility. In particular, if
477 ;;; the outer walk function returns T as its walk-no-more-p value, this will
478 ;;; prevent the inner walk function from getting a chance to walk the subforms
479 ;;; of the form. This is almost never what you want, since it destroys the
480 ;;; equivalence between this nested-walk-form function and two seperate
481 ;;; walk-forms.
482 ;;;
483 #-ecl
484 (defun NESTED-WALK-FORM (whole
485 form
486 &optional environment
487 (walk-function
488 #'(lambda (subform context env)
489 (declare (ignore context env))
490 subform)))
491 (if (eq whole (env-walk-form environment))
492 (let ((outer-walk-function (env-walk-function environment)))
493 (throw whole
494 (walk-form
495 form
496 environment
497 #'(lambda (f c e)
498 ;; First loop to make sure the inner walk function
499 ;; has done all it wants to do with this form.
500 ;; Basically, what we are doing here is providing
501 ;; the same contract walk-form-internal normally
502 ;; provides to the inner walk function.
503 (let*((inner-result nil)
504 (inner-no-more-p nil)
505 (outer-result nil)
506 (outer-no-more-p nil))
507 (loop
508 (multiple-value-setq (inner-result inner-no-more-p)
509 (funcall walk-function f c e))
510 (cond (inner-no-more-p (return))
511 ((not (eq inner-result f)))
512 ((not (consp inner-result)) (return))
513 ((get-walker-template (car inner-result)) (return))
514 (t
515 (multiple-value-bind (expansion macrop)
516 (walker-environment-bind
517 (new-env e :walk-form inner-result)
518 (macroexpand-1 inner-result new-env))
519 (if macrop
520 (setq inner-result expansion)
521 (return)))))
522 (setq f inner-result))
523 (multiple-value-setq (outer-result outer-no-more-p)
524 (funcall outer-walk-function
525 inner-result
526 c
527 e))
528 (values outer-result
529 (and inner-no-more-p outer-no-more-p)))))))
530 (walk-form form environment walk-function)))
531
532 ;;;
533 ;;; WALK-FORM-INTERNAL is the main driving function for the code walker. It
534 ;;; takes a form and the current context and walks the form calling itself or
535 ;;; the appropriate template recursively.
536 ;;;
537 ;;; "It is recommended that a program-analyzing-program process a form
538 ;;; that is a list whose car is a symbol as follows:
539 ;;;
540 ;;; 1. If the program has particular knowledge about the symbol,
541 ;;; process the form using special-purpose code. All of the
542 ;;; standard special forms should fall into this category.
543 ;;; 2. Otherwise, if macro-function is true of the symbol apply
544 ;;; either macroexpand or macroexpand-1 and start over.
545 ;;; 3. Otherwise, assume it is a function call. "
546 ;;;
547
548 (defun walk-form-internal (form context env
549 &aux fn template)
550 ;; First apply the walk-function to perform whatever translation
551 ;; the user wants to this form. If the second value returned
552 ;; by walk-function is T then we don't recurse...
553 (catch form
554 (multiple-value-bind (newform walk-no-more-p)
555 (funcall (env-walk-function env) form context env)
556 (catch newform
557 (cond (walk-no-more-p newform)
558 ((not (eq form newform))
559 (walk-form-internal newform context env))
560 ((not (consp newform))
561 (let ((symmac (car (variable-symbol-macro-p newform env))))
562 (if symmac
563 (let ((newnewform (walk-form-internal (cddr symmac)
564 context env)))
565 (if (eq newnewform (cddr symmac))
566 (if walk-form-expand-macros-p newnewform newform)
567 newnewform))
568 newform)))
569 ((setq template (get-walker-template (setq fn (car newform))))
570 (if (symbolp template)
571 (funcall template newform context env)
572 (walk-template newform template context env)))
573 (t
574 (multiple-value-bind (newnewform macrop)
575 (walker-environment-bind (new-env env :walk-form newform)
576 (macroexpand-1 newform new-env))
577 (cond
578 (macrop
579 (let ((newnewnewform
580 (walk-form-internal newnewform context env)))
581 (if (eq newnewnewform newnewform)
582 (if walk-form-expand-macros-p newnewform newform)
583 newnewnewform)))
584 ((and (symbolp fn)
585 (not (fboundp fn))
586 (special-operator-p fn))
587 (error
588 "~S is a special form, not defined in the CommonLisp.~%~
589 manual This code walker doesn't know how to walk it.~%~
590 Define a template for this special form and try again."
591 fn))
592 (t
593 ;; Otherwise, walk the form as if its just a standard
594 ;; functioncall using a template for standard function
595 ;; call.
596 (walk-template
597 newnewform '(CALL REPEAT (EVAL)) context env))))))))))
598
599 (defun walk-template (form template context env)
600 (declare (si::c-local))
601 (if (atom template)
602 (ecase template
603 ((EVAL FUNCTION TEST EFFECT RETURN)
604 (walk-form-internal form :EVAL env))
605 ((QUOTE NIL) form)
606 (SET
607 (walk-form-internal form :SET env))
608 ((LAMBDA CALL)
609 (cond ((or (symbolp form)
610 (and (listp form)
611 (= (length form) 2)
612 (eq (car form) 'SETF))) form)
613 (t (walk-form-internal form context env)))))
614 (case (car template)
615 (REPEAT
616 (walk-template-handle-repeat form
617 (cdr template)
618 ;; For the case where nothing happens
619 ;; after the repeat optimize out the
620 ;; call to length.
621 (if (null (cddr template))
622 ()
623 (nthcdr (- (length form)
624 (length
625 (cddr template)))
626 form))
627 context
628 env))
629 (IF
630 (walk-template form
631 (if (if (listp (second template))
632 (eval (second template))
633 (funcall (second template) form))
634 (third template)
635 (fourth template))
636 context
637 env))
638 (REMOTE
639 (walk-template form (second template) context env))
640 (otherwise
641 (cond ((atom form) form)
642 (t (recons form
643 (walk-template
644 (car form) (car template) context env)
645 (walk-template
646 (cdr form) (cdr template) context env))))))))
647
648 (defun walk-template-handle-repeat (form template stop-form context env)
649 (declare (si::c-local))
650 (if (eq form stop-form)
651 (walk-template form (cdr template) context env)
652 (walk-template-handle-repeat-1 form
653 template
654 (car template)
655 stop-form
656 context
657 env)))
658
659 (defun walk-template-handle-repeat-1 (form template repeat-template
660 stop-form context env)
661 (declare (si::c-local))
662 (cond ((null form) ())
663 ((eq form stop-form)
664 (if (null repeat-template)
665 (walk-template stop-form (cdr template) context env)
666 (error "While handling repeat:~%~
667 Ran into stop while still in repeat template.")))
668 ((null repeat-template)
669 (walk-template-handle-repeat-1
670 form template (car template) stop-form context env))
671 (t
672 (recons form
673 (walk-template (car form) (car repeat-template) context env)
674 (walk-template-handle-repeat-1 (cdr form)
675 template
676 (cdr repeat-template)
677 stop-form
678 context
679 env)))))
680
681 (defun walk-repeat-eval (form env)
682 (and form
683 (recons form
684 (walk-form-internal (car form) :eval env)
685 (walk-repeat-eval (cdr form) env))))
686
687 (defun recons (x car cdr)
688 (if (or (not (eq (car x) car))
689 (not (eq (cdr x) cdr)))
690 (cons car cdr)
691 x))
692
693 (defun relist (x &rest args)
694 (declare (si::c-local))
695 (if (null args)
696 nil
697 (relist-internal x args nil)))
698
699 (defun relist* (x &rest args)
700 (declare (si::c-local))
701 (relist-internal x args 'T))
702
703 (defun relist-internal (x args *p)
704 (declare (si::c-local))
705 (if (null (cdr args))
706 (if *p (car args) (recons x (car args) nil))
707 (recons x
708 (car args)
709 (relist-internal (cdr x) (cdr args) *p))))
710
711
712 ;;
713 ;;;;;; Special walkers
714 ;;
715
716 (defun walk-declarations (body fn env
717 &optional doc-string-p declarations old-body
718 &aux (form (car body)) macrop new-form)
719 (declare (si::c-local))
720 (cond ((and (stringp form) ;might be a doc string
721 (cdr body) ;isn't the returned value
722 (null doc-string-p) ;no doc string yet
723 (null declarations)) ;no declarations yet
724 (recons body
725 form
726 (walk-declarations (cdr body) fn env t)))
727 ((and (listp form) (eq (car form) 'DECLARE))
728 ;; Got ourselves a real live declaration. Record it, look for more.
729 (dolist (declaration (cdr form))
730 (let*((type (car declaration))
731 (name (second declaration))
732 (args (cddr declaration)))
733 (if (member type *variable-declarations*)
734 (note-declaration `(,type
735 ,(or (variable-lexical-p name env) name)
736 ,.args)
737 env)
738 (note-declaration declaration env))
739 (push declaration declarations)))
740 (recons body
741 form
742 (walk-declarations
743 (cdr body) fn env doc-string-p declarations)))
744 ((and form
745 (listp form)
746 (null (get-walker-template (car form)))
747 (progn
748 (multiple-value-setq (new-form macrop)
749 (macroexpand-1 form env))
750 macrop))
751 ;; This form was a call to a macro. Maybe it expanded
752 ;; into a declare? Recurse to find out.
753 (walk-declarations (recons body new-form (cdr body))
754 fn env doc-string-p declarations
755 (or old-body body)))
756 (t
757 ;; Now that we have walked and recorded the declarations,
758 ;; call the function our caller provided to expand the body.
759 ;; We call that function rather than passing the real-body
760 ;; back, because we are RECONSING up the new body.
761 (funcall fn (or old-body body) env))))
762
763
764 (defun walk-unexpected-declare (form context env)
765 (declare (ignore context env)
766 (si::c-local))
767 (warn "Encountered declare ~S in a place where a declare was not expected."
768 form)
769 form)
770
771 (defun walk-arglist (arglist context env &optional (destructuringp nil)
772 &aux arg)
773 (declare (si::c-local))
774 (cond ((null arglist) ())
775 ((symbolp (setq arg (car arglist)))
776 (or (member arg lambda-list-keywords)
777 (note-lexical-binding arg env))
778 (recons arglist
779 arg
780 (walk-arglist (cdr arglist)
781 context
782 env
783 (and destructuringp
784 (not (member arg
785 lambda-list-keywords))))))
786 ((consp arg)
787 (prog1 (recons arglist
788 (if destructuringp
789 (walk-arglist arg context env destructuringp)
790 (relist* arg
791 (car arg)
792 (walk-form-internal (second arg) :eval env)
793 (cddr arg)))
794 (walk-arglist (cdr arglist) context env nil))
795 (if (symbolp (car arg))
796 (note-lexical-binding (car arg) env)
797 (note-lexical-binding (cadar arg) env))
798 (or (null (cddr arg))
799 (not (symbolp (third arg)))
800 (note-lexical-binding (third arg) env))))
801 (t
802 (error "Can't understand something in the arglist ~S" arglist))))
803
804 (defun walk-let (form context env)
805 (walk-let/let* form context env nil))
806
807 (defun walk-let* (form context env)
808 (walk-let/let* form context env t))
809
810 (defun walk-prog (form context env)
811 (walk-prog/prog* form context env nil))
812
813 (defun walk-prog* (form context env)
814 (walk-prog/prog* form context env t))
815
816 (defun walk-do (form context env)
817 (walk-do/do* form context env nil))
818
819 (defun walk-do* (form context env)
820 (walk-do/do* form context env t))
821
822 (defun walk-let/let* (form context old-env sequentialp)
823 (walker-environment-bind (new-env old-env)
824 (let* ((let/let* (car form))
825 (bindings (second form))
826 (body (cddr form))
827 (walked-bindings
828 (walk-bindings-1 bindings
829 old-env
830 new-env
831 context
832 sequentialp))
833 (walked-body
834 (walk-declarations body #'walk-repeat-eval new-env)))
835 (relist*
836 form let/let* walked-bindings walked-body))))
837
838 (defun walk-locally (form context env)
839 (declare (ignore context))
840 (let* ((locally (car form))
841 (body (cdr form))
842 (walked-body
843 (walk-declarations body #'walk-repeat-eval env)))
844 (relist*
845 form locally walked-body)))
846
847 (defun walk-prog/prog* (form context old-env sequentialp)
848 (walker-environment-bind (new-env old-env)
849 (let* ((possible-block-name (second form))
850 (blocked-prog (and (symbolp possible-block-name)
851 (not (eq possible-block-name 'nil)))))
852 (multiple-value-bind (let/let* block-name bindings body)
853 (if blocked-prog
854 (values (car form) (cadr form) (caddr form) (cdddr form))
855 (values (car form) nil (cadr form) (cddr form)))
856 (let* ((walked-bindings
857 (walk-bindings-1 bindings
858 old-env
859 new-env
860 context
861 sequentialp))
862 (walked-body
863 (walk-declarations
864 body
865 #'(lambda (real-body real-env)
866 (walk-tagbody-1 real-body context real-env))
867 new-env)))
868 (if block-name
869 (relist*
870 form let/let* block-name walked-bindings walked-body)
871 (relist*
872 form let/let* walked-bindings walked-body)))))))
873
874 (defun walk-do/do* (form context old-env sequentialp)
875 (walker-environment-bind (new-env old-env)
876 (let* ((do/do* (car form))
877 (bindings (second form))
878 (end-test (third form))
879 (body (cdddr form))
880 (walked-bindings (walk-bindings-1 bindings
881 old-env
882 new-env
883 context
884 sequentialp))
885 (walked-body
886 (walk-declarations body #'walk-repeat-eval new-env)))
887 (relist* form
888 do/do*
889 (walk-bindings-2 bindings walked-bindings context new-env)
890 (walk-template end-test '(TEST REPEAT (EVAL)) context new-env)
891 walked-body))))
892
893 #+ecl
894 (defun walk-dotimes/dolist (form context old-env)
895 (walker-environment-bind (new-env old-env)
896 (let* ((dotimes/dolist (car form))
897 (bindings (second form))
898 (body (cddr form))
899 ; This is a hack. We tread BINDINGS as we
900 ; would in a DO/DO* loop.
901 (walked-bindings (walk-bindings-1 bindings
902 old-env
903 new-env
904 context
905 t))
906 (walked-body
907 (walk-declarations body #'walk-repeat-eval new-env)))
908 (relist* form
909 dotimes/dolist
910 (walk-bindings-2 bindings walked-bindings context new-env)
911 walked-body))))
912
913 (defun walk-multiple-value-setq (form context env)
914 (let ((vars (cadr form)))
915 (if (some #'(lambda (var)
916 (variable-symbol-macro-p var env))
917 vars)
918 (let* ((temps (mapcar #'(lambda (var) (declare (ignore var)) (gensym)) vars))
919 (sets (mapcar #'(lambda (var temp) `(setq ,var ,temp)) vars temps))
920 (expanded `(multiple-value-bind ,temps
921 ,(caddr form)
922 ,@sets))
923 (walked (walk-form-internal expanded context env)))
924 (if (eq walked expanded)
925 form
926 walked))
927 (walk-template form '(nil (repeat (set)) eval) context env))))
928
929 (defun walk-multiple-value-bind (form context old-env)
930 (walker-environment-bind (new-env old-env)
931 (let* ((mvb (car form))
932 (bindings (second form))
933 (mv-form (walk-template (third form) 'EVAL context old-env))
934 (body (cdddr form))
935 walked-bindings
936 (walked-body
937 (walk-declarations
938 body
939 #'(lambda (real-body real-env)
940 (setq walked-bindings
941 (walk-bindings-1 bindings
942 old-env
943 new-env
944 context
945 nil))
946 (walk-repeat-eval real-body real-env))
947 new-env)))
948 (relist* form mvb walked-bindings mv-form walked-body))))
949
950 (defun walk-bindings-1 (bindings old-env new-env context sequentialp)
951 (and bindings
952 (let ((binding (car bindings)))
953 (recons bindings
954 (if (symbolp binding)
955 (prog1 binding
956 (note-lexical-binding binding new-env))
957 (prog1 (relist* binding
958 (car binding)
959 (walk-form-internal (second binding)
960 context
961 (if sequentialp
962 new-env
963 old-env))
964 (cddr binding)) ;save cddr for DO/DO*
965 ;it is the next value
966 ;form. Don't walk it
967 ;now though.
968 (note-lexical-binding (car binding) new-env)))
969 (walk-bindings-1 (cdr bindings)
970 old-env
971 new-env
972 context
973 sequentialp)))))
974
975 (defun walk-bindings-2 (bindings walked-bindings context env)
976 (declare (si::c-local))
977 (and bindings
978 (let ((binding (car bindings))
979 (walked-binding (car walked-bindings)))
980 (recons bindings
981 (if (symbolp binding)
982 binding
983 (relist* binding
984 (car walked-binding)
985 (second walked-binding)
986 (walk-template (cddr binding)
987 '(EVAL)
988 context
989 env)))
990 (walk-bindings-2 (cdr bindings)
991 (cdr walked-bindings)
992 context
993 env)))))
994
995 (defun walk-lambda (form context old-env)
996 (walker-environment-bind (new-env old-env)
997 (let* ((arglist (second form))
998 (body (cddr form))
999 (walked-arglist (walk-arglist arglist context new-env))
1000 (walked-body
1001 (walk-declarations body #'walk-repeat-eval new-env)))
1002 (relist* form
1003 (car form)
1004 walked-arglist
1005 walked-body))))
1006
1007 (defun walk-named-lambda (form context old-env)
1008 (walker-environment-bind (new-env old-env)
1009 (let* ((name (second form))
1010 (arglist (third form))
1011 (body (cdddr form))
1012 (walked-arglist (walk-arglist arglist context new-env))
1013 (walked-body
1014 (walk-declarations body #'walk-repeat-eval new-env)))
1015 (relist* form
1016 (car form)
1017 name
1018 walked-arglist
1019 walked-body))))
1020
1021 (defun walk-setq (form context env)
1022 (if (cdddr form)
1023 (let* ((expanded (let* ((rforms nil)
1024 (tail (cdr form)))
1025 (loop (when (null tail) (return (nreverse rforms)))
1026 (let ((var (pop tail)) (val (pop tail)))
1027 (push `(setq ,var ,val) rforms)))))
1028 (walked (walk-repeat-eval expanded env)))
1029 (if (eq expanded walked)
1030 form
1031 `(progn ,@walked)))
1032 (let* ((var (cadr form))
1033 (val (caddr form))
1034 (symmac (car (variable-symbol-macro-p var env))))
1035 (if symmac
1036 (let* ((expanded `(setf ,(cddr symmac) ,val))
1037 (walked (walk-form-internal expanded context env)))
1038 (if (eq expanded walked)
1039 form
1040 walked))
1041 (relist form 'setq
1042 (walk-form-internal var :set env)
1043 (walk-form-internal val :eval env))))))
1044
1045 (defun walk-symbol-macrolet (form context old-env)
1046 (declare (ignore context))
1047 (let* ((bindings (second form)))
1048 (walker-environment-bind
1049 (new-env old-env
1050 :lexical-variables
1051 (append (mapcar #'(lambda (binding)
1052 `(,(first binding)
1053 :macro . ,(second binding)))
1054 bindings)
1055 (env-lexical-variables old-env)))
1056 (relist* form 'SYMBOL-MACROLET bindings
1057 (walk-repeat-eval (cddr form) new-env)))))
1058
1059 (defun walk-tagbody (form context env)
1060 (recons form (car form) (walk-tagbody-1 (cdr form) context env)))
1061
1062 (defun walk-tagbody-1 (form context env)
1063 (declare (si::c-local))
1064 (and form
1065 (recons form
1066 (walk-form-internal (car form)
1067 (if (symbolp (car form)) 'QUOTE context)
1068 env)
1069 (walk-tagbody-1 (cdr form) context env))))
1070
1071 (defun walk-compiler-let (form context old-env)
1072 (declare (ignore context))
1073 (let* ((vars ())
1074 (vals ()))
1075 (dolist (binding (second form))
1076 (cond ((symbolp binding) (push binding vars) (push nil vals))
1077 (t
1078 (push (car binding) vars)
1079 (push (eval (second binding)) vals))))
1080 (relist* form
1081 (car form)
1082 (second form)
1083 (progv vars vals (walk-repeat-eval (cddr form) old-env)))))
1084
1085 (defun walk-macrolet (form context old-env)
1086 (walker-environment-bind (macro-env
1087 nil
1088 :walk-function (env-walk-function old-env))
1089 (labels ((walk-definitions (definitions)
1090 (and definitions
1091 (let ((definition (car definitions)))
1092 (recons definitions
1093 (relist* definition
1094 (car definition)
1095 (walk-arglist (second definition)
1096 context
1097 macro-env
1098 t)
1099 (walk-declarations (cddr definition)
1100 #'walk-repeat-eval
1101 macro-env))
1102 (walk-definitions (cdr definitions)))))))
1103 (with-new-definition-in-environment (new-env old-env form)
1104 (relist* form
1105 (car form)
1106 (walk-definitions (second form))
1107 (walk-declarations (cddr form)
1108 #'walk-repeat-eval
1109 new-env))))))
1110
1111 (defun walk-flet (form context old-env)
1112 (labels ((walk-definitions (definitions)
1113 (if (null definitions)
1114 ()
1115 (recons definitions
1116 (walk-lambda (car definitions) context old-env)
1117 (walk-definitions (cdr definitions))))))
1118 (recons form
1119 (car form)
1120 (recons (cdr form)
1121 (walk-definitions (second form))
1122 (with-new-definition-in-environment (new-env old-env form)
1123 (walk-declarations (cddr form)
1124 #'walk-repeat-eval
1125 new-env))))))
1126
1127 (defun walk-labels (form context old-env)
1128 (with-new-definition-in-environment (new-env old-env form)
1129 (labels ((walk-definitions (definitions)
1130 (if (null definitions)
1131 ()
1132 (recons definitions
1133 (walk-lambda (car definitions) context new-env)
1134 (walk-definitions (cdr definitions))))))
1135 (recons form
1136 (car form)
1137 (recons (cdr form)
1138 (walk-definitions (second form))
1139 (walk-declarations (cddr form)
1140 #'walk-repeat-eval
1141 new-env))))))
1142
1143 (defun walk-if (form context env)
1144 (let*((predicate (second form))
1145 (arm1 (third form))
1146 (arm2
1147 (if (cddddr form)
1148 (progn
1149 (warn "In the form:~%~S~%~
1150 IF only accepts three arguments, you are using ~D.~%~
1151 It is true that some Common Lisps support this, but ~
1152 it is not~%~
1153 truly legal Common Lisp. For now, this code ~
1154 walker is interpreting ~%~
1155 the extra arguments as extra else clauses. ~
1156 Even if this is what~%~
1157 you intended, you should fix your source code."
1158 form
1159 (length (cdr form)))
1160 (cons 'PROGN (cdddr form)))
1161 (fourth form))))
1162 (relist form
1163 'IF
1164 (walk-form-internal predicate context env)
1165 (walk-form-internal arm1 context env)
1166 (walk-form-internal arm2 context env))))
1167
1168 #+ecl
1169 (defun walk-when/unless (form context env)
1170 (relist* form
1171 (first form)
1172 (walk-form-internal (second form) context env) ; predicate
1173 (walk-repeat-eval (cddr form) env)))
1174
1175 #+ecl
1176 (defun walk-c-inline (form context env)
1177 (declare (ignore context))
1178 (relist* form
1179 (first form)
1180 (walk-repeat-eval (second form) env) ; arguments
1181 (cddr form))) ; types and flags of the form
1182
1183 ;;;
1184 ;;; Tests tests tests
1185 ;;;
1186
1187 #|
1188 ;;;
1189 ;;; Here are some examples of the kinds of things you should be able to do
1190 ;;; with your implementation of the macroexpansion environment hacking
1191 ;;; mechanism.
1192 ;;;
1193 ;;; with-lexical-macros is kind of like macrolet, but it only takes names
1194 ;;; of the macros and actual macroexpansion functions to use to macroexpand
1195 ;;; them. The win about that is that for macros which want to wrap several
1196 ;;; macrolets around their body, they can do this but have the macroexpansion
1197 ;;; functions be compiled. See the WITH-RPUSH example.
1198 ;;;
1199 ;;; If the implementation had a special way of communicating the augmented
1200 ;;; environment back to the evaluator that would be totally great. It would
1201 ;;; mean that we could just augment the environment then pass control back
1202 ;;; to the implementations own compiler or interpreter. We wouldn't have
1203 ;;; to call the actual walker. That would make this much faster. Since the
1204 ;;; principal client of this is defmethod it would make compiling defmethods
1205 ;;; faster and that would certainly be a win.
1206 ;;;
1207 (defmacro with-lexical-macros (macros &body body &environment old-env)
1208 (with-augmented-environment (new-env old-env :macros macros)
1209 (walk-form (cons 'PROGN body) new-env)))
1210
1211 (defun expand-rpush (form env)
1212 `(push ,(third form) ,(second form)))
1213
1214 (defmacro with-rpush (&body body)
1215 `(with-lexical-macros ,(list (list 'RPUSH #'expand-rpush)) ,@body))
1216
1217
1218 ;;;
1219 ;;; Unfortunately, I don't have an automatic tester for the walker.
1220 ;;; Instead there is this set of test cases with a description of
1221 ;;; how each one should go.
1222 ;;;
1223 (defmacro take-it-out-for-a-test-walk (form)
1224 `(progn
1225 (terpri)
1226 (terpri)
1227 (let ((copy-of-form (copy-tree ',form))
1228 (result (walk-form ',form nil
1229 '(lambda (x y env)
1230 (format t "~&Form: ~S ~3T Context: ~A" x y)
1231 (when (symbolp x)
1232 (let ((lexical (variable-lexical-p x env))
1233 (special (variable-special-p x env)))
1234 (when lexical
1235 (format t ";~3T")
1236 (format t "lexically bound"))
1237 (when special
1238 (format t ";~3T")
1239 (format t "declared special"))
1240 (when (boundp x)
1241 (format t ";~3T")
1242 (format t "bound: ~S " (eval x)))))
1243 x))))
1244 (cond ((not (equal result copy-of-form))
1245 (format t "~%Warning: Result not EQUAL to copy of start."))
1246 ((not (eq result ',form))
1247 (format t "~%Warning: Result not EQ to copy of start.")))
1248 (pprint result)
1249 result)))
1250
1251 (defmacro foo (&rest ignore) ''GLOBAL-FOO)
1252
1253 (defmacro bar (&rest ignore) ''GLOBAL-BAR)
1254
1255 (take-it-out-for-a-test-walk (list arg1 arg2 arg3))
1256 (take-it-out-for-a-test-walk (list (cons 1 2) (list 3 4 5)))
1257
1258 (take-it-out-for-a-test-walk (progn (foo) (bar 1)))
1259
1260 (take-it-out-for-a-test-walk (block block-name a b c))
1261 (take-it-out-for-a-test-walk (block block-name (list a) b c))
1262
1263 (take-it-out-for-a-test-walk (catch catch-tag (list a) b c))
1264 ;;;
1265 ;;; This is a fairly simple macrolet case. While walking the body of the
1266 ;;; macro, x should be lexically bound. In the body of the macrolet form
1267 ;;; itself, x should not be bound.
1268 ;;;
1269 (take-it-out-for-a-test-walk
1270 (macrolet ((foo (x) (list x) ''INNER))
1271 x
1272 (foo 1)))
1273
1274 ;;;
1275 ;;; A slightly more complex macrolet case. In the body of the macro x
1276 ;;; should not be lexically bound. In the body of the macrolet form itself
1277 ;;; x should be bound. Note that THIS CASE WILL CAUSE AN ERROR when it
1278 ;;; tries to macroexpand the call to foo.
1279 ;;;
1280 (take-it-out-for-a-test-walk
1281 (let ((x 1))
1282 (macrolet ((foo () (list x) ''INNER))
1283 x
1284 (foo))))
1285
1286 ;;;
1287 ;;; A truly hairy use of compiler-let and macrolet. In the body of the
1288 ;;; macro x should not be lexically bound. In the body of the macrolet
1289 ;;; itself x should not be lexically bound. But the macro should expand
1290 ;;; into 1.
1291 ;;;
1292 (take-it-out-for-a-test-walk
1293 (compiler-let ((x 1))
1294 (let ((x 2))
1295 (macrolet ((foo () x))
1296 x
1297 (foo)))))
1298
1299
1300 (take-it-out-for-a-test-walk
1301 (flet ((foo (x) (list x y))
1302 (bar (x) (list x y)))
1303 (foo 1)))
1304
1305 (take-it-out-for-a-test-walk
1306 (let ((y 2))
1307 (flet ((foo (x) (list x y))
1308 (bar (x) (list x y)))
1309 (foo 1))))
1310
1311 (take-it-out-for-a-test-walk
1312 (labels ((foo (x) (bar x))
1313 (bar (x) (foo x)))
1314 (foo 1)))
1315
1316 (take-it-out-for-a-test-walk
1317 (flet ((foo (x) (foo x)))
1318 (foo 1)))
1319
1320 (take-it-out-for-a-test-walk
1321 (flet ((foo (x) (foo x)))
1322 (flet ((bar (x) (foo x)))
1323 (bar 1))))
1324
1325 (take-it-out-for-a-test-walk (compiler-let ((a 1) (b 2)) (foo a) b))
1326 (take-it-out-for-a-test-walk (prog () (declare (special a b))))
1327 (take-it-out-for-a-test-walk (let (a b c)
1328 (declare (special a b))
1329 (foo a) b c))
1330 (take-it-out-for-a-test-walk (let (a b c)
1331 (declare (special a) (special b))
1332 (foo a) b c))
1333 (take-it-out-for-a-test-walk (let (a b c)
1334 (declare (special a))
1335 (declare (special b))
1336 (foo a) b c))
1337 (take-it-out-for-a-test-walk (let (a b c)
1338 (declare (special a))
1339 (declare (special b))
1340 (let ((a 1))
1341 (foo a) b c)))
1342 (take-it-out-for-a-test-walk (eval-when ()
1343 a
1344 (foo a)))
1345 (take-it-out-for-a-test-walk (eval-when (eval when load)
1346 a
1347 (foo a)))
1348
1349 (take-it-out-for-a-test-walk (multiple-value-bind (a b) (foo a b) (list a b)))
1350 (take-it-out-for-a-test-walk (multiple-value-bind (a b)
1351 (foo a b)
1352 (declare (special a))
1353 (list a b)))
1354 (take-it-out-for-a-test-walk (progn (function foo)))
1355 (take-it-out-for-a-test-walk (progn a b (go a)))
1356 (take-it-out-for-a-test-walk (if a b c))
1357 (take-it-out-for-a-test-walk (if a b))
1358 (take-it-out-for-a-test-walk ((lambda (a b) (list a b)) 1 2))
1359 (take-it-out-for-a-test-walk ((lambda (a b) (declare (special a)) (list a b))
1360 1 2))
1361 (take-it-out-for-a-test-walk (let ((a a) (b a) (c b)) (list a b c)))
1362 (take-it-out-for-a-test-walk (let* ((a a) (b a) (c b)) (list a b c)))
1363 (take-it-out-for-a-test-walk (let ((a a) (b a) (c b))
1364 (declare (special a b))
1365 (list a b c)))
1366 (take-it-out-for-a-test-walk (let* ((a a) (b a) (c b))
1367 (declare (special a b))
1368 (list a b c)))
1369 (take-it-out-for-a-test-walk (let ((a 1) (b 2))
1370 (foo bar)
1371 (declare (special a))
1372 (foo a b)))
1373 (take-it-out-for-a-test-walk (multiple-value-call #'foo a b c))
1374 (take-it-out-for-a-test-walk (multiple-value-prog1 a b c))
1375 (take-it-out-for-a-test-walk (progn a b c))
1376 (take-it-out-for-a-test-walk (progv vars vals a b c))
1377 (take-it-out-for-a-test-walk (quote a))
1378 (take-it-out-for-a-test-walk (return-from block-name a b c))
1379 (take-it-out-for-a-test-walk (setq a 1))
1380 (take-it-out-for-a-test-walk (setq a (foo 1) b (bar 2) c 3))
1381 (take-it-out-for-a-test-walk (tagbody a b c (go a)))
1382 (take-it-out-for-a-test-walk (the foo (foo-form a b c)))
1383 (take-it-out-for-a-test-walk (throw tag-form a))
1384 (take-it-out-for-a-test-walk (unwind-protect (foo a b) d e f))
1385
1386 (defmacro flet-1 (a b) ''OUTER)
1387 (defmacro labels-1 (a b) ''OUTER)
1388
1389 (take-it-out-for-a-test-walk
1390 (flet ((flet-1 (a b) () (flet-1 a b) (list a b)))
1391 (flet-1 1 2)
1392 (foo 1 2)))
1393 (take-it-out-for-a-test-walk
1394 (labels ((label-1 (a b) () (label-1 a b)(list a b)))
1395 (label-1 1 2)
1396 (foo 1 2)))
1397 (take-it-out-for-a-test-walk (macrolet ((macrolet-1 (a b) (list a b)))
1398 (macrolet-1 a b)
1399 (foo 1 2)))
1400
1401 (take-it-out-for-a-test-walk (macrolet ((foo (a) `(inner-foo-expanded ,a)))
1402 (foo 1)))
1403
1404 (take-it-out-for-a-test-walk (progn (bar 1)
1405 (macrolet ((bar (a)
1406 `(inner-bar-expanded ,a)))
1407 (bar 2))))
1408
1409 (take-it-out-for-a-test-walk (progn (bar 1)
1410 (macrolet ((bar (s)
1411 (bar s)
1412 `(inner-bar-expanded ,s)))
1413 (bar 2))))
1414
1415 (take-it-out-for-a-test-walk (cond (a b)
1416 ((foo bar) a (foo a))))
1417
1418
1419 (let ((the-lexical-variables ()))
1420 (walk-form '(let ((a 1) (b 2))
1421 #'(lambda (x) (list a b x y)))
1422 ()
1423 #'(lambda (form context env)
1424 (when (and (symbolp form)
1425 (variable-lexical-p form env))
1426 (push form the-lexical-variables))
1427 form))
1428 (or (and (= (length the-lexical-variables) 3)
1429 (member 'a the-lexical-variables)
1430 (member 'b the-lexical-variables)
1431 (member 'x the-lexical-variables))
1432 (error "Walker didn't do lexical variables of a closure properly.")))
1433
1434 |#