Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / pcl / walk.lisp
blobd6d5016fc7abc2d0e56b6cd8fc91e1efda630cc6
1 ;;;; a simple code walker
2 ;;;;
3 ;;;; The code which implements the macroexpansion environment
4 ;;;; manipulation mechanisms is in the first part of the file, the
5 ;;;; real walker follows it.
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
10 ;;;; This software is derived from software originally released by Xerox
11 ;;;; Corporation. Copyright and release statements follow. Later modifications
12 ;;;; to the software are in the public domain and are provided with
13 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
14 ;;;; information.
16 ;;;; copyright information from original PCL sources:
17 ;;;;
18 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
19 ;;;; All rights reserved.
20 ;;;;
21 ;;;; Use and copying of this software and preparation of derivative works based
22 ;;;; upon this software are permitted. Any distribution of this software or
23 ;;;; derivative works must comply with all applicable United States export
24 ;;;; control laws.
25 ;;;;
26 ;;;; This software is made available AS IS, and Xerox Corporation makes no
27 ;;;; warranty about the software, its performance or its conformity to any
28 ;;;; specification.
30 (in-package "SB!WALKER")
32 ;;;; forward references
34 (defvar *key-to-walker-environment*)
36 ;;;; environment hacking stuff, necessarily SBCL-specific
38 ;;; Here in the original PCL were implementations of the
39 ;;; implementation-specific environment hacking functions for each of
40 ;;; the implementations this walker had been ported to. This
41 ;;; functionality was originally factored out in order to make PCL
42 ;;; portable from one Common Lisp to another. As of 19981107, that
43 ;;; portability was fairly stale and (because of the scarcity of CLTL1
44 ;;; implementations and the strong interdependence of the rest of ANSI
45 ;;; Common Lisp on the CLOS system) fairly irrelevant. It was fairly
46 ;;; thoroughly put out of its misery by WHN in his quest to clean up
47 ;;; the system enough that it can be built from scratch using any ANSI
48 ;;; Common Lisp.
49 ;;;
50 ;;; This code just hacks 'macroexpansion environments'. That is, it is
51 ;;; only concerned with the function binding of symbols in the
52 ;;; environment. The walker needs to be able to tell if the symbol
53 ;;; names a lexical macro or function, and it needs to be able to
54 ;;; build environments which contain lexical macro or function
55 ;;; bindings. It must be able, when walking a MACROLET, FLET or LABELS
56 ;;; form to construct an environment which reflects the bindings
57 ;;; created by that form. Note that the environment created does NOT
58 ;;; have to be sufficient to evaluate the body, merely to walk its
59 ;;; body. This means that definitions do not have to be supplied for
60 ;;; lexical functions, only the fact that that function is bound is
61 ;;; important. For macros, the macroexpansion function must be
62 ;;; supplied.
63 ;;;
64 ;;; This code is organized in a way that lets it work in
65 ;;; implementations that stack cons their environments. That is
66 ;;; reflected in the fact that the only operation that lets a user
67 ;;; build a new environment is a WITH-BODY macro which executes its
68 ;;; body with the specified symbol bound to the new environment. No
69 ;;; code in this walker or in PCL will hold a pointer to these
70 ;;; environments after the body returns. Other user code is free to do
71 ;;; so in implementations where it works, but that code is not
72 ;;; considered portable.
73 ;;;
74 ;;; There are 3 environment hacking tools. One macro,
75 ;;; WITH-AUGMENTED-ENVIRONMENT, which is used to create new
76 ;;; environments, and two functions, ENVIRONMENT-FUNCTION and
77 ;;; ENVIRONMENT-MACRO, which are used to access the bindings of
78 ;;; existing environments
80 ;;; In SBCL, as in CMU CL before it, the environment is represented
81 ;;; with a structure that holds alists for the functional things,
82 ;;; variables, blocks, etc. Except for SYMBOL-MACROLET, only the
83 ;;; SB!C::LEXENV-FUNS slot is relevant. It holds: Alist (Name . What),
84 ;;; where What is either a functional (a local function) or a list
85 ;;; (MACRO . <function>) (a local macro, with the specifier expander.)
86 ;;; Note that Name may be a (SETF <name>) function. Accessors are
87 ;;; defined below, eg (ENV-WALK-FUNCTION ENV).
88 ;;;
89 ;;; If WITH-AUGMENTED-ENVIRONMENT is called from WALKER-ENVIRONMENT-BIND
90 ;;; this code hides the WALKER version of an environment
91 ;;; inside the SB!C::LEXENV structure.
92 ;;;
93 ;;; In CMUCL (and former SBCL), This used to be a list of lists of form
94 ;;; (<gensym-name> MACRO . #<interpreted-function>) in the :functions slot
95 ;;; in a C::LEXENV.
96 ;;; This form was accepted by the compiler, but this was a crude hack,
97 ;;; because the <interpreted-function> was used as a structure to hold the
98 ;;; bits of interest, {function, form, declarations, lexical-variables},
99 ;;; a list, which was not really an interpreted function.
100 ;;; Instead this list was COERCEd to a #<FUNCTION ...>!
102 ;;; Instead, we now use a special sort of "function"-type for that
103 ;;; information, because the functions slot in SB!C::LEXENV is
104 ;;; supposed to have a list of <Name MACRO . #<function> elements.
105 ;;; So, now we hide our bits of interest in the walker-info slot in
106 ;;; our new BOGO-FUN.
108 ;;; MACROEXPAND-1 and SB!INT:EVAL-IN-LEXENV are the only SBCL
109 ;;; functions that get called with the constructed environment
110 ;;; argument.
112 (/show "walk.lisp 108")
114 (defmacro with-augmented-environment
115 ((new-env old-env &key functions macros) &body body)
116 `(let ((,new-env (with-augmented-environment-internal ,old-env
117 ,functions
118 ,macros)))
119 ,@body))
121 ;;; a unique tag to show that we're the intended caller of BOGO-FUN
122 (defvar *bogo-fun-magic-tag*
123 '(:bogo-fun-magic-tag))
125 ;;; The interface of BOGO-FUNs (previously implemented as
126 ;;; FUNCALLABLE-INSTANCEs) is just these two operations, so we can do
127 ;;; them with ordinary closures.
129 ;;; KLUDGE: BOGO-FUNs are sorta weird, and MNA and I have both hacked
130 ;;; on this code without quite figuring out what they're for. (He
131 ;;; changed them to work after some changes in the IR1 interpreter
132 ;;; made functions not be built lazily, and I changed them so that
133 ;;; they don't need FUNCALLABLE-INSTANCE stuff, so that the F-I stuff
134 ;;; can become less general.) There may be further simplifications or
135 ;;; clarifications which could be done. -- WHN 2001-10-19
136 (defun walker-info-to-bogo-fun (walker-info)
137 (lambda (magic-tag &rest rest)
138 (aver (not rest)) ; else someone is using me in an unexpected way
139 (aver (eql magic-tag *bogo-fun-magic-tag*)) ; else ditto
140 walker-info))
141 (defun bogo-fun-to-walker-info (bogo-fun)
142 (declare (type function bogo-fun))
143 (funcall bogo-fun *bogo-fun-magic-tag*))
145 (defun with-augmented-environment-internal (env funs macros)
146 ;; Note: In order to record the correct function definition, we
147 ;; would have to create an interpreted closure, but the
148 ;; WITH-NEW-DEFINITION macro down below makes no distinction between
149 ;; FLET and LABELS, so we have no idea what to use for the
150 ;; environment. So we just blow it off, 'cause anything real we do
151 ;; would be wrong. But we still have to make an entry so we can tell
152 ;; functions from macros -- same for telling variables apart from
153 ;; symbol macros.
154 (let ((lexenv (sb!kernel:coerce-to-lexenv env)))
155 (sb!c::make-lexenv
156 :default lexenv
157 :vars (when (eql (caar macros) *key-to-walker-environment*)
158 (copy-tree (mapcar (lambda (b)
159 (let ((name (car b))
160 (info (cadr b)))
161 (if (eq info :lexical-var)
162 (cons name
163 (if (var-special-p name env)
164 (sb!c::make-global-var
165 :kind :special
166 :%source-name name)
167 (sb!c::make-lambda-var
168 :%source-name name)))
169 b)))
170 (fourth (cadar macros)))))
171 :funs (append (mapcar (lambda (f)
172 (cons (car f)
173 (sb!c::make-functional :lexenv lexenv)))
174 funs)
175 (mapcar (lambda (m)
176 (list* (car m)
177 'sb!c::macro
178 (if (eq (car m)
179 *key-to-walker-environment*)
180 (walker-info-to-bogo-fun (cadr m))
181 (coerce (cadr m) 'function))))
182 macros)))))
184 (defun environment-function (env fn)
185 (when env
186 (let ((entry (assoc fn (sb!c::lexenv-funs env) :test #'equal)))
187 (and entry
188 (sb!c::functional-p (cdr entry))
189 (cdr entry)))))
191 (defun environment-macro (env macro)
192 (when env
193 (let ((entry (assoc macro (sb!c::lexenv-funs env) :test #'eq)))
194 (and entry
195 (eq (cadr entry) 'sb!c::macro)
196 (if (eq macro *key-to-walker-environment*)
197 (values (bogo-fun-to-walker-info (cddr entry)))
198 (values (function-lambda-expression (cddr entry))))))))
200 ;;;; other environment hacking, not so SBCL-specific as the
201 ;;;; environment hacking in the previous section
203 (defmacro with-new-definition-in-environment
204 ((new-env old-env macrolet/flet/labels-form) &body body)
205 (let ((functions (make-symbol "Functions"))
206 (macros (make-symbol "Macros")))
207 `(let ((,functions ())
208 (,macros ()))
209 (ecase (car ,macrolet/flet/labels-form)
210 ((flet labels)
211 (dolist (fn (cadr ,macrolet/flet/labels-form))
212 (push fn ,functions)))
213 ((macrolet)
214 (dolist (mac (cadr ,macrolet/flet/labels-form))
215 (push (list (car mac)
216 (convert-macro-to-lambda (cadr mac)
217 (cddr mac)
218 ,old-env
219 (string (car mac))))
220 ,macros))))
221 (with-augmented-environment
222 (,new-env ,old-env :functions ,functions :macros ,macros)
223 ,@body))))
225 (defun convert-macro-to-lambda (llist body env &optional (name "dummy macro"))
226 (let ((gensym (make-symbol name)))
227 (eval-in-lexenv `(defmacro ,gensym ,llist ,@body)
228 (sb!c::make-restricted-lexenv env))
229 (macro-function gensym)))
231 ;;;; the actual walker
233 ;;; As the walker walks over the code, it communicates information to
234 ;;; itself about the walk. This information includes the walk
235 ;;; function, variable bindings, declarations in effect etc. This
236 ;;; information is inherently lexical, so the walker passes it around
237 ;;; in the actual environment the walker passes to macroexpansion
238 ;;; functions.
239 (defmacro walker-environment-bind ((var env &rest key-args)
240 &body body)
241 `(with-augmented-environment
242 (,var ,env :macros (walker-environment-bind-1 ,env ,.key-args))
243 .,body))
245 (defvar *key-to-walker-environment* (gensym))
247 (defun env-lock (env)
248 (environment-macro env *key-to-walker-environment*))
250 (defun walker-environment-bind-1 (env &key (walk-function nil wfnp)
251 (walk-form nil wfop)
252 (declarations nil decp)
253 (lexical-vars nil lexp))
254 (let ((lock (env-lock env)))
255 (list
256 (list *key-to-walker-environment*
257 (list (if wfnp walk-function (car lock))
258 (if wfop walk-form (cadr lock))
259 (if decp declarations (caddr lock))
260 (if lexp lexical-vars (cadddr lock)))))))
262 (defun env-walk-function (env)
263 (car (env-lock env)))
265 (defun env-walk-form (env)
266 (cadr (env-lock env)))
268 (defun env-declarations (env)
269 (caddr (env-lock env)))
271 (defun env-var-type (var env)
272 (dolist (decl (env-declarations env) t)
273 (when (and (eq 'type (car decl)) (member var (cddr decl) :test 'eq))
274 (return (cadr decl)))))
276 (defun env-lexical-variables (env)
277 (cadddr (env-lock env)))
279 (defun note-declaration (declaration env)
280 (push declaration (caddr (env-lock env))))
282 (defun note-var-binding (thing env)
283 (push (list thing :lexical-var) (cadddr (env-lock env))))
285 (defun var-lexical-p (var env)
286 (let ((entry (member var (env-lexical-variables env) :key #'car :test #'eq)))
287 (when (eq (cadar entry) :lexical-var)
288 (return-from var-lexical-p entry)))
289 ;; if we're finding something in the real lexenv, we don't have a
290 ;; bound declaration and so we specifically don't want to return
291 ;; a special object that declarations can attach to, just the name.
292 (and env (find var (mapcar #'car (sb!c::lexenv-vars env)))))
294 (defun variable-symbol-macro-p (var env)
295 ;; FIXME: crufty return convention
296 (let ((entry (or (member var (env-lexical-variables env) :key #'car :test #'eq)
297 (and env (member var (sb!c::lexenv-vars env) :key #'car :test #'eq)))))
298 (when (and (consp (cdar entry)) (eq (cadar entry) 'sb!sys:macro))
299 (return-from variable-symbol-macro-p entry))
300 (unless entry
301 (when (var-globally-symbol-macro-p var)
302 (list (list* var 'sb!sys:macro (info :variable :macro-expansion var)))))))
304 (defun var-globally-symbol-macro-p (var)
305 (eq (info :variable :kind var) :macro))
307 (defun walked-var-declaration-p (declaration)
308 (member declaration '(sb!pcl::%class sb!pcl::%variable-rebinding special)))
310 (defun %var-declaration (declaration var env)
311 (let ((id (or (var-lexical-p var env) var)))
312 (if (eq 'special declaration)
313 (dolist (decl (env-declarations env))
314 (when (and (eq (car decl) declaration)
315 (or (member var (cdr decl))
316 (and id (member id (cdr decl)))))
317 (return decl)))
318 (dolist (decl (env-declarations env))
319 (when (and (eq (car decl) declaration)
320 (eq (cadr decl) id))
321 (return decl))))))
323 (defun var-declaration (declaration var env)
324 (if (walked-var-declaration-p declaration)
325 (%var-declaration declaration var env)
326 (error "Not a variable declaration the walker cares about: ~S" declaration)))
328 #-sb-xc-host
329 (define-compiler-macro var-declaration (&whole form declaration var env
330 &environment lexenv)
331 (if (sb!xc:constantp declaration lexenv)
332 (let ((decl (constant-form-value declaration lexenv)))
333 (if (walked-var-declaration-p decl)
334 `(%var-declaration ,declaration ,var ,env)
335 form))
336 form))
338 (defun var-special-p (var env)
339 (and (or (var-declaration 'special var env)
340 (var-globally-special-p var))
343 (defun var-globally-special-p (symbol)
344 (eq (info :variable :kind symbol) :special))
347 ;;;; handling of special forms
349 ;;; Here are some comments from the original PCL on the difficulty of
350 ;;; doing this portably across different CLTL1 implementations. This
351 ;;; is no longer directly relevant because this code now only runs on
352 ;;; SBCL, but the comments are retained for culture: they might help
353 ;;; explain some of the design decisions which were made in the code.
355 ;;; and I quote...
357 ;;; The set of special forms is purposely kept very small because
358 ;;; any program analyzing program (read code walker) must have
359 ;;; special knowledge about every type of special form. Such a
360 ;;; program needs no special knowledge about macros...
362 ;;; So all we have to do here is a define a way to store and retrieve
363 ;;; templates which describe how to walk the 24 special forms and we
364 ;;; are all set...
366 ;;; Well, its a nice concept, and I have to admit to being naive
367 ;;; enough that I believed it for a while, but not everyone takes
368 ;;; having only 24 special forms as seriously as might be nice. There
369 ;;; are (at least) 3 ways to lose:
371 ;;; 1 - Implementation x implements a Common Lisp special form as
372 ;;; a macro which expands into a special form which:
373 ;;; - Is a common lisp special form (not likely)
374 ;;; - Is not a common lisp special form (on the 3600 IF --> COND).
376 ;;; * We can save ourselves from this case (second subcase really)
377 ;;; by checking to see whether there is a template defined for
378 ;;; something before we check to see whether we can macroexpand it.
380 ;;; 2 - Implementation x implements a Common Lisp macro as a special form.
382 ;;; * This is a screw, but not so bad, we save ourselves from it by
383 ;;; defining extra templates for the macros which are *likely* to
384 ;;; be implemented as special forms. [Note: As of sbcl-0.6.9, these
385 ;;; extra templates have been deleted, since this is not a problem
386 ;;; in SBCL and we no longer try to make this walker portable
387 ;;; across other possibly-broken CL implementations.]
389 ;;; 3 - Implementation x has a special form which is not on the list of
390 ;;; Common Lisp special forms.
392 ;;; * This is a bad sort of a screw and happens more than I would
393 ;;; like to think, especially in the implementations which provide
394 ;;; more than just Common Lisp (3600, Xerox etc.).
395 ;;; The fix is not terribly satisfactory, but will have to do for
396 ;;; now. There is a hook in get walker-template which can get a
397 ;;; template from the implementation's own walker. That template
398 ;;; has to be converted, and so it may be that the right way to do
399 ;;; this would actually be for that implementation to provide an
400 ;;; interface to its walker which looks like the interface to this
401 ;;; walker.
403 (defmacro get-walker-template-internal (x)
404 `(get ,x 'walker-template))
406 (defmacro define-walker-template (name
407 &optional (template '(nil repeat (eval))))
408 `(setf (get-walker-template-internal ',name) ',template))
410 (defun get-walker-template (x context)
411 (cond ((symbolp x)
412 (get-walker-template-internal x))
413 ((and (listp x) (eq (car x) 'lambda))
414 '(lambda repeat (eval)))
416 ;; FIXME: In an ideal world we would do something similar to
417 ;; COMPILER-ERROR here, replacing the form within the walker
418 ;; with an error-signalling form. This is slightly less
419 ;; pretty, but informative non the less. Best is the enemy of
420 ;; good, etc.
421 (error "Illegal function call in method body:~% ~S"
422 context))))
424 ;;;; the actual templates
426 ;;; ANSI special forms
427 (define-walker-template block (nil nil repeat (eval)))
428 (define-walker-template catch (nil eval repeat (eval)))
429 (define-walker-template declare walk-unexpected-declare)
430 (define-walker-template eval-when (nil quote repeat (eval)))
431 (define-walker-template flet walk-flet)
432 (define-walker-template function (nil call))
433 (define-walker-template go (nil quote))
434 (define-walker-template if walk-if)
435 (define-walker-template labels walk-labels)
436 (define-walker-template lambda walk-lambda)
437 (define-walker-template let walk-let)
438 (define-walker-template let* walk-let*)
439 (define-walker-template load-time-value walk-load-time-value)
440 (define-walker-template locally walk-locally)
441 (define-walker-template macrolet walk-macrolet)
442 (define-walker-template multiple-value-call (nil eval repeat (eval)))
443 (define-walker-template multiple-value-prog1 (nil return repeat (eval)))
444 (define-walker-template multiple-value-setq walk-multiple-value-setq)
445 (define-walker-template multiple-value-bind walk-multiple-value-bind)
446 (define-walker-template progn (nil repeat (eval)))
447 (define-walker-template progv (nil eval eval repeat (eval)))
448 (define-walker-template quote (nil quote))
449 (define-walker-template return-from (nil quote repeat (return)))
450 (define-walker-template setq walk-setq)
451 (define-walker-template symbol-macrolet walk-symbol-macrolet)
452 (define-walker-template tagbody walk-tagbody)
453 (define-walker-template the (nil quote eval))
454 (define-walker-template throw (nil eval eval))
455 (define-walker-template unwind-protect (nil return repeat (eval)))
457 ;;; SBCL-only special forms
458 (define-walker-template truly-the (nil quote eval))
459 ;;; FIXME: maybe we don't need this one any more, given that
460 ;;; NAMED-LAMBDA now expands into (FUNCTION (NAMED-LAMBDA ...))?
461 (define-walker-template named-lambda walk-named-lambda)
463 (defvar *walk-form-expand-macros-p* nil)
465 (defun walk-form (form
466 &optional environment
467 (walk-function
468 (lambda (subform context env)
469 (declare (ignore context env))
470 subform)))
471 (walker-environment-bind (new-env environment :walk-function walk-function)
472 (walk-form-internal form :eval new-env)))
474 ;;; WALK-FORM-INTERNAL is the main driving function for the code
475 ;;; walker. It takes a form and the current context and walks the form
476 ;;; calling itself or the appropriate template recursively.
478 ;;; "It is recommended that a program-analyzing-program process a form
479 ;;; that is a list whose car is a symbol as follows:
481 ;;; 1. If the program has particular knowledge about the symbol,
482 ;;; process the form using special-purpose code. All of the
483 ;;; standard special forms should fall into this category.
484 ;;; 2. Otherwise, if MACRO-FUNCTION is true of the symbol apply
485 ;;; either MACROEXPAND or MACROEXPAND-1 and start over.
486 ;;; 3. Otherwise, assume it is a function call. "
487 (defun walk-form-internal (form context env)
488 (declare (type (member :eval :set) context))
489 ;; First apply the walk-function to perform whatever translation
490 ;; the user wants to this form. If the second value returned
491 ;; by walk-function is T then we don't recurse...
492 (catch form
493 (multiple-value-bind (newform walk-no-more-p)
494 (funcall (env-walk-function env) form context env)
495 (catch newform
496 (cond
497 (walk-no-more-p newform)
498 ((not (eq form newform))
499 (walk-form-internal newform context env))
500 ((and (not (consp newform))
501 (or (eql context :eval)
502 (eql context :set)))
503 (let ((symmac (car (variable-symbol-macro-p newform env))))
504 (if symmac
505 (let* ((newnewform (walk-form-internal (cddr symmac)
506 context
507 env))
508 (resultform
509 (if (eq newnewform (cddr symmac))
510 (if *walk-form-expand-macros-p* newnewform newform)
511 newnewform))
512 (type (env-var-type newform env)))
513 (if (eq t type)
514 resultform
515 `(the ,type ,resultform)))
516 newform)))
517 ((eql context :set) newform)
519 (let* ((fn (car newform))
520 (template (get-walker-template fn newform)))
521 (if template
522 (if (symbolp template)
523 (funcall template newform context env)
524 (walk-template newform template context env))
525 (multiple-value-bind (newnewform macrop)
526 (walker-environment-bind
527 (new-env env :walk-form newform)
528 (%macroexpand-1 newform new-env))
529 (cond
530 (macrop
531 (let ((newnewnewform (walk-form-internal newnewform
532 context
533 env)))
534 (if (eq newnewnewform newnewform)
535 (if *walk-form-expand-macros-p* newnewform newform)
536 newnewnewform)))
537 ((and (symbolp fn)
538 (special-operator-p fn))
539 ;; This shouldn't happen, since this walker is now
540 ;; maintained as part of SBCL, so it should know
541 ;; about all the special forms that SBCL knows
542 ;; about.
543 (bug "unexpected special form ~S" fn))
545 ;; Otherwise, walk the form as if it's just a
546 ;; standard function call using a template for
547 ;; standard function call.
548 (walk-template
549 newnewform '(call repeat (eval)) context env))))))))))))
551 (defun walk-template (form template context env)
552 (if (atom template)
553 (ecase template
554 ((eval function test effect return)
555 (walk-form-internal form :eval env))
556 ((quote nil) form)
557 (set
558 (walk-form-internal form :set env))
559 ((lambda call)
560 (cond ((legal-fun-name-p form)
561 form)
562 (t (walk-form-internal form context env)))))
563 (case (car template)
564 (repeat
565 (walk-template-handle-repeat form
566 (cdr template)
567 ;; For the case where nothing
568 ;; happens after the repeat
569 ;; optimize away the call to
570 ;; LENGTH.
571 (if (null (cddr template))
573 (nthcdr (- (length form)
574 (length
575 (cddr template)))
576 form))
577 context
578 env))
580 (walk-template form
581 (if (if (listp (cadr template))
582 (eval (cadr template))
583 (funcall (cadr template) form))
584 (caddr template)
585 (cadddr template))
586 context
587 env))
588 (remote
589 (walk-template form (cadr template) context env))
590 (otherwise
591 (cond ((atom form) form)
592 (t (recons form
593 (walk-template
594 (car form) (car template) context env)
595 (walk-template
596 (cdr form) (cdr template) context env))))))))
598 (defun walk-template-handle-repeat (form template stop-form context env)
599 (if (eq form stop-form)
600 (walk-template form (cdr template) context env)
601 (walk-template-handle-repeat-1
602 form template (car template) stop-form context env)))
604 (defun walk-template-handle-repeat-1 (form template repeat-template
605 stop-form context env)
606 (cond ((null form) ())
607 ((eq form stop-form)
608 (if (null repeat-template)
609 (walk-template stop-form (cdr template) context env)
610 (error "while handling code walker REPEAT:
611 ~%ran into STOP while still in REPEAT template")))
612 ((null repeat-template)
613 (walk-template-handle-repeat-1
614 form template (car template) stop-form context env))
616 (recons form
617 (walk-template (car form) (car repeat-template) context env)
618 (walk-template-handle-repeat-1 (cdr form)
619 template
620 (cdr repeat-template)
621 stop-form
622 context
623 env)))))
625 (defun walk-repeat-eval (form env)
626 (and form
627 (recons form
628 (walk-form-internal (car form) :eval env)
629 (walk-repeat-eval (cdr form) env))))
631 (defun recons (x car cdr)
632 (if (or (not (eq (car x) car))
633 (not (eq (cdr x) cdr)))
634 (cons car cdr)
637 (defun relist (x &rest args)
638 (if (null args)
640 (relist-internal x args nil)))
642 (defun relist* (x &rest args)
643 (relist-internal x args t))
645 (defun relist-internal (x args *p)
646 (if (null (cdr args))
647 (if *p
648 (car args)
649 (recons x (car args) nil))
650 (recons x
651 (car args)
652 (relist-internal (cdr x) (cdr args) *p))))
654 ;;;; special walkers
656 (defun walk-declarations (body fn env
657 &optional doc-string-p declarations old-body
658 &aux (form (car body)) macrop new-form)
659 (cond ((and (stringp form) ;might be a doc string
660 (cdr body) ;isn't the returned value
661 (null doc-string-p) ;no doc string yet
662 (null declarations)) ;no declarations yet
663 (recons body
664 form
665 (walk-declarations (cdr body) fn env t)))
666 ((and (listp form) (eq (car form) 'declare))
667 ;; We got ourselves a real live declaration. Record it, look
668 ;; for more.
669 (dolist (declaration (cdr form))
670 (let ((type (car declaration))
671 (name (cadr declaration))
672 (args (cddr declaration)))
673 (if (walked-var-declaration-p type)
674 (note-declaration `(,type
675 ,(or (var-lexical-p name env) name)
676 ,.args)
677 env)
678 (note-declaration (sb!c::canonized-decl-spec declaration) env))
679 (push declaration declarations)))
680 (recons body
681 form
682 (walk-declarations
683 (cdr body) fn env doc-string-p declarations)))
684 ((and form
685 (listp form)
686 (null (get-walker-template (car form) form))
687 (progn
688 (multiple-value-setq (new-form macrop)
689 (%macroexpand-1 form env))
690 macrop))
691 ;; This form was a call to a macro. Maybe it expanded
692 ;; into a declare? Recurse to find out.
693 (walk-declarations (recons body new-form (cdr body))
694 fn env doc-string-p declarations
695 (or old-body body)))
697 ;; Now that we have walked and recorded the declarations,
698 ;; call the function our caller provided to expand the body.
699 ;; We call that function rather than passing the real-body
700 ;; back, because we are RECONSING up the new body.
701 (funcall fn (or old-body body) env))))
703 (defun walk-unexpected-declare (form context env)
704 (declare (ignore context env))
705 (warn "encountered ~S ~_in a place where a DECLARE was not expected"
706 form)
707 form)
709 (defun walk-arglist (arglist context env &optional (destructuringp nil)
710 &aux arg)
711 (cond ((null arglist) ())
712 ((symbolp (setq arg (car arglist)))
713 (or (member arg sb!xc:lambda-list-keywords :test #'eq)
714 (note-var-binding arg env))
715 (recons arglist
717 (walk-arglist (cdr arglist)
718 context
720 (and destructuringp
721 (not (member arg sb!xc:lambda-list-keywords))))))
722 ((consp arg)
723 (prog1 (recons arglist
724 (if destructuringp
725 (walk-arglist arg context env destructuringp)
726 (relist* arg
727 (car arg)
728 (walk-form-internal (cadr arg) :eval env)
729 (cddr arg)))
730 (walk-arglist (cdr arglist) context env nil))
731 (if (symbolp (car arg))
732 (note-var-binding (car arg) env)
733 (note-var-binding (cadar arg) env))
734 (or (null (cddr arg))
735 (not (symbolp (caddr arg)))
736 (note-var-binding (caddr arg) env))))
738 (error "can't understand something in the arglist ~S" arglist))))
740 (defun walk-let (form context env)
741 (walker-environment-bind (new-env env)
742 (let* ((let (car form))
743 (bindings (cadr form))
744 (body (cddr form))
745 walked-bindings
746 (walked-body
747 (walk-declarations
748 body
749 (lambda (real-body real-env)
750 (setf walked-bindings
751 (walk-bindings-1 bindings env new-env context))
752 (walk-repeat-eval real-body real-env))
753 new-env)))
754 (relist* form let walked-bindings walked-body))))
756 (defun let*-binding-name (binding)
757 (if (symbolp binding)
758 binding
759 (car binding)))
761 (defun let*-binding-init (binding)
762 (if (or (symbolp binding)
763 (null (cdr binding)))
764 'no-init
765 (cadr binding)))
767 (defun let*-bindings (bindings &aux names inits (seen (make-hash-table)))
768 (dolist (binding (reverse bindings) (values names inits))
769 (let ((name (let*-binding-name binding)))
770 (push (cons name (gethash name seen)) names)
771 (setf (gethash name seen) t)
772 (push (let*-binding-init binding) inits))))
774 (defun walk-let* (form context env)
775 (walker-environment-bind (new-env env)
776 (let ((let* (car form))
777 (bindings (cadr form))
778 (body (cddr form)))
779 (multiple-value-bind (names inits) (let*-bindings bindings)
780 (multiple-value-bind (newbody decls doc) (parse-body body :doc-string-allowed nil)
781 (declare (ignore newbody))
782 (aver (null doc))
783 (labels ((maybe-process-and-munge-declaration (name declaration env)
784 (if (walked-var-declaration-p (car declaration))
785 (case (car declaration)
786 (special (if (find name (cdr declaration))
787 (progn
788 (note-declaration `(special ,(var-lexical-p name env)) env)
789 (cons 'special (remove name (cdr declaration))))
790 declaration))
791 (t (if (eql name (cadr declaration))
792 (progn
793 (note-declaration `(,(car declaration)
794 ,(var-lexical-p name env)
795 ,@(cddr declaration))
796 env)
797 nil)
798 declaration)))
799 declaration))
800 (walk-let*-bindings (bindings names inits)
801 (when bindings
802 (recons bindings
803 (let ((name (car names))
804 (init (car inits)))
805 (prog1
806 (if (eql init 'no-init)
807 (prog1 (car bindings)
808 (note-var-binding (car name) new-env))
809 (prog1
810 (relist (car bindings)
811 (caar bindings)
812 (walk-form-internal init context new-env))
813 (note-var-binding (car name) new-env)))
814 (unless (cdr name)
815 (setf decls (mapcar (lambda (d)
816 (cons 'declare
817 (mapcar (lambda (dd) (maybe-process-and-munge-declaration (car name) dd new-env))
818 (cdr d))))
819 decls)))))
820 (walk-let*-bindings (cdr bindings) (cdr names) (cdr inits))))))
821 (relist* form let*
822 (walk-let*-bindings bindings names inits)
823 (walk-declarations body (lambda (form env) (walk-repeat-eval form env)) new-env))))))))
825 (defun walk-load-time-value (form context env)
826 (destructuring-bind (ltv val &optional read-only-p) form
827 (declare (ignore ltv))
828 (relist form
829 'load-time-value
830 ;; this is wrong: VAL is handled differently depending on
831 ;; whether we're in EVAL, COMPILE or COMPILE-FILE, and
832 ;; (after macroexpansion) is evaluated in the null lexical
833 ;; environment. The macro semantics are the same in each
834 ;; case, but VAR-LEXICAL-P can be tricked into giving the
835 ;; wrong answer.
836 (walk-form-internal val context env)
837 (walk-form-internal read-only-p context env))))
839 (defun walk-locally (form context env)
840 (declare (ignore context))
841 (walker-environment-bind (new-env env)
842 (let* ((locally (car form))
843 (body (cdr form))
844 (walked-body
845 (walk-declarations body #'walk-repeat-eval new-env)))
846 (relist*
847 form locally walked-body))))
849 (defun walk-multiple-value-setq (form context env)
850 (let ((vars (cadr form)))
851 (if (some (lambda (var)
852 (variable-symbol-macro-p var env))
853 vars)
854 (let* ((temps (mapcar (lambda (var)
855 (declare (ignore var))
856 (gensym))
857 vars))
858 (sets (mapcar (lambda (var temp) `(setq ,var ,temp))
859 vars
860 temps))
861 (expanded `(multiple-value-bind ,temps ,(caddr form)
862 ,@sets))
863 (walked (walk-form-internal expanded context env)))
864 (if (eq walked expanded)
865 form
866 walked))
867 (walk-template form '(nil (repeat (set)) eval) context env))))
869 (defun walk-multiple-value-bind (form context old-env)
870 (walker-environment-bind (new-env old-env)
871 (let* ((mvb (car form))
872 (bindings (cadr form))
873 (mv-form (walk-template (caddr form) 'eval context old-env))
874 (body (cdddr form))
875 walked-bindings
876 (walked-body
877 (walk-declarations
878 body
879 (lambda (real-body real-env)
880 (setq walked-bindings
881 (walk-bindings-1 bindings old-env new-env context))
882 (walk-repeat-eval real-body real-env))
883 new-env)))
884 (relist* form mvb walked-bindings mv-form walked-body))))
886 (defun walk-bindings-1 (bindings old-env new-env context)
887 (and bindings
888 (let ((binding (car bindings)))
889 (recons bindings
890 (if (symbolp binding)
891 (prog1 binding
892 (note-var-binding binding new-env))
893 (prog1 (relist binding
894 (car binding)
895 (walk-form-internal
896 (cadr binding) context old-env))
897 (note-var-binding (car binding) new-env)))
898 (walk-bindings-1 (cdr bindings) old-env new-env context)))))
900 (defun walk-lambda (form context old-env)
901 (walker-environment-bind (new-env old-env)
902 (let* ((arglist (cadr form))
903 (body (cddr form))
904 (walked-arglist (walk-arglist arglist context new-env))
905 (walked-body
906 (walk-declarations body #'walk-repeat-eval new-env)))
907 (relist* form
908 (car form)
909 walked-arglist
910 walked-body))))
912 (defun walk-named-lambda (form context old-env)
913 (walker-environment-bind (new-env old-env)
914 (let* ((name (second form))
915 (arglist (third form))
916 (body (cdddr form))
917 (walked-arglist (walk-arglist arglist context new-env))
918 (walked-body
919 (walk-declarations body #'walk-repeat-eval new-env)))
920 (relist* form
921 (car form)
922 name
923 walked-arglist
924 walked-body))))
926 (defun walk-setq (form context env)
927 (if (cdddr form)
928 (let* ((expanded (let ((rforms nil)
929 (tail (cdr form)))
930 (loop (when (null tail) (return (nreverse rforms)))
931 (let ((var (pop tail)) (val (pop tail)))
932 (push `(setq ,var ,val) rforms)))))
933 (walked (walk-repeat-eval expanded env)))
934 (if (eq expanded walked)
935 form
936 `(progn ,@walked)))
937 (let* ((var (cadr form))
938 (val (caddr form))
939 (symmac (car (variable-symbol-macro-p var env))))
940 (if symmac
941 (let* ((type (env-var-type var env))
942 (expanded (if (eq t type)
943 `(setf ,(cddr symmac) ,val)
944 `(setf ,(cddr symmac) (the ,type ,val))))
945 (walked (walk-form-internal expanded context env)))
946 (if (eq expanded walked)
947 form
948 walked))
949 (relist form 'setq
950 (walk-form-internal var :set env)
951 (walk-form-internal val :eval env))))))
953 (defun walk-symbol-macrolet (form context old-env)
954 (declare (ignore context))
955 (let* ((bindings (cadr form))
956 (body (cddr form)))
957 (walker-environment-bind
958 (new-env old-env
959 :lexical-vars
960 (append (mapcar (lambda (binding)
961 `(,(car binding)
962 sb!sys:macro . ,(cadr binding)))
963 bindings)
964 (env-lexical-variables old-env)))
965 (relist* form 'symbol-macrolet bindings
966 (walk-declarations body #'walk-repeat-eval new-env)))))
968 (defun walk-tagbody (form context env)
969 (recons form (car form) (walk-tagbody-1 (cdr form) context env)))
971 (defun walk-tagbody-1 (form context env)
972 (and form
973 (recons form
974 (if (or (symbolp (car form)) (integerp (car form)))
975 (walk-template (car form) 'quote context env)
976 (walk-form-internal (car form) context env))
977 (walk-tagbody-1 (cdr form) context env))))
979 (defun walk-macrolet (form context old-env)
980 (walker-environment-bind (old-env old-env)
981 (walker-environment-bind (macro-env
983 :walk-function (env-walk-function old-env))
984 (labels ((walk-definitions (definitions)
985 (and definitions
986 (let ((definition (car definitions)))
987 (recons definitions
988 (relist* definition
989 (car definition)
990 (walk-arglist (cadr definition)
991 context
992 macro-env
994 (walk-declarations (cddr definition)
995 #'walk-repeat-eval
996 macro-env))
997 (walk-definitions (cdr definitions)))))))
998 (with-new-definition-in-environment (new-env old-env form)
999 (relist* form
1000 (car form)
1001 (walk-definitions (cadr form))
1002 (walk-declarations (cddr form)
1003 #'walk-repeat-eval
1004 new-env)))))))
1006 (defun walk-flet (form context old-env)
1007 (walker-environment-bind (old-env old-env)
1008 (labels ((walk-definitions (definitions)
1009 (if (null definitions)
1011 (recons definitions
1012 (walk-lambda (car definitions) context old-env)
1013 (walk-definitions (cdr definitions))))))
1014 (recons form
1015 (car form)
1016 (recons (cdr form)
1017 (walk-definitions (cadr form))
1018 (with-new-definition-in-environment (new-env old-env form)
1019 (walk-declarations (cddr form)
1020 #'walk-repeat-eval
1021 new-env)))))))
1023 (defun walk-labels (form context old-env)
1024 (walker-environment-bind (old-env old-env)
1025 (with-new-definition-in-environment (new-env old-env form)
1026 (labels ((walk-definitions (definitions)
1027 (if (null definitions)
1029 (recons definitions
1030 (walk-lambda (car definitions) context new-env)
1031 (walk-definitions (cdr definitions))))))
1032 (recons form
1033 (car form)
1034 (recons (cdr form)
1035 (walk-definitions (cadr form))
1036 (walk-declarations (cddr form)
1037 #'walk-repeat-eval
1038 new-env)))))))
1040 (defun walk-if (form context env)
1041 (destructuring-bind (if predicate arm1 &optional arm2) form
1042 (declare (ignore if)) ; should be 'IF
1043 (relist form
1045 (walk-form-internal predicate context env)
1046 (walk-form-internal arm1 context env)
1047 (walk-form-internal arm2 context env))))
1049 ;;;; examples
1052 ;;; Here are some examples of the kinds of things you should be able
1053 ;;; to do with your implementation of the macroexpansion environment
1054 ;;; hacking mechanism.
1056 ;;; WITH-LEXICAL-MACROS is kind of like MACROLET, but it only takes
1057 ;;; names of the macros and actual macroexpansion functions to use to
1058 ;;; macroexpand them. The win about that is that for macros which want
1059 ;;; to wrap several MACROLETs around their body, they can do this but
1060 ;;; have the macroexpansion functions be compiled. See the WITH-RPUSH
1061 ;;; example.
1063 ;;; If the implementation had a special way of communicating the
1064 ;;; augmented environment back to the evaluator that would be totally
1065 ;;; great. It would mean that we could just augment the environment
1066 ;;; then pass control back to the implementations own compiler or
1067 ;;; interpreter. We wouldn't have to call the actual walker. That
1068 ;;; would make this much faster. Since the principal client of this is
1069 ;;; defmethod it would make compiling defmethods faster and that would
1070 ;;; certainly be a win.
1072 (defmacro with-lexical-macros (macros &body body &environment old-env)
1073 (with-augmented-environment (new-env old-env :macros macros)
1074 (walk-form (cons 'progn body) :environment new-env)))
1076 (defun expand-rpush (form env)
1077 (declare (ignore env))
1078 `(push ,(caddr form) ,(cadr form)))
1080 (defmacro with-rpush (&body body)
1081 `(with-lexical-macros ,(list (list 'rpush #'expand-rpush)) ,@body))