Put RECONS in SB-INT: and remove alter ego SHARING-CONS
[sbcl.git] / src / pcl / walk.lisp
blobf990d25150c340a489b352c2c8d4dca8ba1d3031
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 (declare (ignorable llist body env name))
227 #+sb-xc-host (error "CONVERT-MACRO-TO-LAMBDA called") ; no EVAL-IN-LEXENV
228 #-sb-xc-host
229 (let ((gensym (make-symbol name)))
230 (eval-in-lexenv `(defmacro ,gensym ,llist ,@body)
231 (sb!c::make-restricted-lexenv env))
232 (macro-function gensym)))
234 ;;;; the actual walker
236 ;;; As the walker walks over the code, it communicates information to
237 ;;; itself about the walk. This information includes the walk
238 ;;; function, variable bindings, declarations in effect etc. This
239 ;;; information is inherently lexical, so the walker passes it around
240 ;;; in the actual environment the walker passes to macroexpansion
241 ;;; functions.
242 (defmacro walker-environment-bind ((var env &rest key-args)
243 &body body)
244 `(with-augmented-environment
245 (,var ,env :macros (walker-environment-bind-1 ,env ,.key-args))
246 .,body))
248 (defvar *key-to-walker-environment* (gensym))
250 (defun env-lock (env)
251 (environment-macro env *key-to-walker-environment*))
253 (defun walker-environment-bind-1 (env &key (walk-function nil wfnp)
254 (walk-form nil wfop)
255 (declarations nil decp)
256 (lexical-vars nil lexp))
257 (let ((lock (env-lock env)))
258 (list
259 (list *key-to-walker-environment*
260 (list (if wfnp walk-function (car lock))
261 (if wfop walk-form (cadr lock))
262 (if decp declarations (caddr lock))
263 (if lexp lexical-vars (cadddr lock)))))))
265 (defun env-walk-function (env)
266 (car (env-lock env)))
268 (defun env-walk-form (env)
269 (cadr (env-lock env)))
271 (defun env-declarations (env)
272 (caddr (env-lock env)))
274 (defun env-var-type (var env)
275 (dolist (decl (env-declarations env) t)
276 (when (and (eq 'type (car decl)) (member var (cddr decl) :test 'eq))
277 (return (cadr decl)))))
279 (defun env-lexical-variables (env)
280 (cadddr (env-lock env)))
282 (defun note-declaration (declaration env)
283 (push declaration (caddr (env-lock env))))
285 (defun note-var-binding (thing env)
286 (push (list thing :lexical-var) (cadddr (env-lock env))))
288 (defun var-lexical-p (var env)
289 (let ((entry (member var (env-lexical-variables env) :key #'car :test #'eq)))
290 (when (eq (cadar entry) :lexical-var)
291 (return-from var-lexical-p entry)))
292 ;; if we're finding something in the real lexenv, we don't have a
293 ;; bound declaration and so we specifically don't want to return
294 ;; a special object that declarations can attach to, just the name.
295 (and env (find var (mapcar #'car (sb!c::lexenv-vars env)))))
297 (defun variable-symbol-macro-p (var env)
298 ;; FIXME: crufty return convention
299 (let ((entry (or (member var (env-lexical-variables env) :key #'car :test #'eq)
300 (and env (member var (sb!c::lexenv-vars env) :key #'car :test #'eq)))))
301 (when (and (consp (cdar entry)) (eq (cadar entry) 'sb!sys:macro))
302 (return-from variable-symbol-macro-p entry))
303 (unless entry
304 (when (var-globally-symbol-macro-p var)
305 (list (list* var 'sb!sys:macro (info :variable :macro-expansion var)))))))
307 (defun var-globally-symbol-macro-p (var)
308 (eq (info :variable :kind var) :macro))
310 (defun walked-var-declaration-p (declaration)
311 (member declaration '(sb!pcl::%class sb!pcl::%variable-rebinding special)))
313 (defun %var-declaration (declaration var env)
314 (let ((id (or (var-lexical-p var env) var)))
315 (if (eq 'special declaration)
316 (dolist (decl (env-declarations env))
317 (when (and (eq (car decl) declaration)
318 (or (member var (cdr decl))
319 (and id (member id (cdr decl)))))
320 (return decl)))
321 (dolist (decl (env-declarations env))
322 (when (and (eq (car decl) declaration)
323 (eq (cadr decl) id))
324 (return decl))))))
326 (defun var-declaration (declaration var env)
327 (if (walked-var-declaration-p declaration)
328 (%var-declaration declaration var env)
329 (error "Not a variable declaration the walker cares about: ~S" declaration)))
331 #-sb-xc-host
332 (define-compiler-macro var-declaration (&whole form declaration var env
333 &environment lexenv)
334 (if (sb!xc:constantp declaration lexenv)
335 (let ((decl (constant-form-value declaration lexenv)))
336 (if (walked-var-declaration-p decl)
337 `(%var-declaration ,declaration ,var ,env)
338 form))
339 form))
341 (defun var-special-p (var env)
342 (and (or (var-declaration 'special var env)
343 (var-globally-special-p var))
346 (defun var-globally-special-p (symbol)
347 (eq (info :variable :kind symbol) :special))
350 ;;;; handling of special forms
352 ;;; Here are some comments from the original PCL on the difficulty of
353 ;;; doing this portably across different CLTL1 implementations. This
354 ;;; is no longer directly relevant because this code now only runs on
355 ;;; SBCL, but the comments are retained for culture: they might help
356 ;;; explain some of the design decisions which were made in the code.
358 ;;; and I quote...
360 ;;; The set of special forms is purposely kept very small because
361 ;;; any program analyzing program (read code walker) must have
362 ;;; special knowledge about every type of special form. Such a
363 ;;; program needs no special knowledge about macros...
365 ;;; So all we have to do here is a define a way to store and retrieve
366 ;;; templates which describe how to walk the 24 special forms and we
367 ;;; are all set...
369 ;;; Well, its a nice concept, and I have to admit to being naive
370 ;;; enough that I believed it for a while, but not everyone takes
371 ;;; having only 24 special forms as seriously as might be nice. There
372 ;;; are (at least) 3 ways to lose:
374 ;;; 1 - Implementation x implements a Common Lisp special form as
375 ;;; a macro which expands into a special form which:
376 ;;; - Is a common lisp special form (not likely)
377 ;;; - Is not a common lisp special form (on the 3600 IF --> COND).
379 ;;; * We can save ourselves from this case (second subcase really)
380 ;;; by checking to see whether there is a template defined for
381 ;;; something before we check to see whether we can macroexpand it.
383 ;;; 2 - Implementation x implements a Common Lisp macro as a special form.
385 ;;; * This is a screw, but not so bad, we save ourselves from it by
386 ;;; defining extra templates for the macros which are *likely* to
387 ;;; be implemented as special forms. [Note: As of sbcl-0.6.9, these
388 ;;; extra templates have been deleted, since this is not a problem
389 ;;; in SBCL and we no longer try to make this walker portable
390 ;;; across other possibly-broken CL implementations.]
392 ;;; 3 - Implementation x has a special form which is not on the list of
393 ;;; Common Lisp special forms.
395 ;;; * This is a bad sort of a screw and happens more than I would
396 ;;; like to think, especially in the implementations which provide
397 ;;; more than just Common Lisp (3600, Xerox etc.).
398 ;;; The fix is not terribly satisfactory, but will have to do for
399 ;;; now. There is a hook in get walker-template which can get a
400 ;;; template from the implementation's own walker. That template
401 ;;; has to be converted, and so it may be that the right way to do
402 ;;; this would actually be for that implementation to provide an
403 ;;; interface to its walker which looks like the interface to this
404 ;;; walker.
406 (defmacro get-walker-template-internal (x)
407 `(get ,x 'walker-template))
409 (defmacro define-walker-template (name
410 &optional (template '(nil repeat (eval))))
411 `(setf (get-walker-template-internal ',name) ',template))
413 (defun get-walker-template (x context)
414 (cond ((symbolp x)
415 (get-walker-template-internal x))
416 ((and (listp x) (eq (car x) 'lambda))
417 '(lambda repeat (eval)))
419 ;; FIXME: In an ideal world we would do something similar to
420 ;; COMPILER-ERROR here, replacing the form within the walker
421 ;; with an error-signalling form. This is slightly less
422 ;; pretty, but informative non the less. Best is the enemy of
423 ;; good, etc.
424 (error "Illegal function call in method body:~% ~S"
425 context))))
427 ;;;; the actual templates
429 ;;; ANSI special forms
430 (define-walker-template block (nil nil repeat (eval)))
431 (define-walker-template catch (nil eval repeat (eval)))
432 (define-walker-template declare walk-unexpected-declare)
433 (define-walker-template eval-when (nil quote repeat (eval)))
434 (define-walker-template flet walk-flet)
435 (define-walker-template function (nil call))
436 (define-walker-template go (nil quote))
437 (define-walker-template if walk-if)
438 (define-walker-template labels walk-labels)
439 (define-walker-template lambda walk-lambda)
440 (define-walker-template let walk-let)
441 (define-walker-template let* walk-let*)
442 (define-walker-template load-time-value walk-load-time-value)
443 (define-walker-template locally walk-locally)
444 (define-walker-template macrolet walk-macrolet)
445 (define-walker-template multiple-value-call (nil eval repeat (eval)))
446 (define-walker-template multiple-value-prog1 (nil return repeat (eval)))
447 (define-walker-template multiple-value-setq walk-multiple-value-setq)
448 (define-walker-template multiple-value-bind walk-multiple-value-bind)
449 (define-walker-template progn (nil repeat (eval)))
450 (define-walker-template progv (nil eval eval repeat (eval)))
451 (define-walker-template quote (nil quote))
452 (define-walker-template return-from (nil quote repeat (return)))
453 (define-walker-template setq walk-setq)
454 (define-walker-template symbol-macrolet walk-symbol-macrolet)
455 (define-walker-template tagbody walk-tagbody)
456 (define-walker-template the (nil quote eval))
457 (define-walker-template throw (nil eval eval))
458 (define-walker-template unwind-protect (nil return repeat (eval)))
460 ;;; SBCL-only special forms
461 (define-walker-template truly-the (nil quote eval))
462 (define-walker-template callable-cast (nil quote quote eval))
463 (define-walker-template sb!kernel:the* (nil quote eval))
464 ;;; FIXME: maybe we don't need this one any more, given that
465 ;;; NAMED-LAMBDA now expands into (FUNCTION (NAMED-LAMBDA ...))?
466 (define-walker-template named-lambda walk-named-lambda)
468 (defvar *walk-form-expand-macros-p* nil)
470 (defun walk-form (form
471 &optional environment
472 (walk-function
473 (lambda (subform context env)
474 (declare (ignore context env))
475 subform)))
476 #!+(and sb-fasteval (host-feature sb-xc))
477 (when (typep environment 'sb!interpreter:basic-env)
478 (setq environment (sb!interpreter:lexenv-from-env environment)))
479 (walker-environment-bind (new-env environment :walk-function walk-function)
480 (walk-form-internal form :eval new-env)))
482 ;;; WALK-FORM-INTERNAL is the main driving function for the code
483 ;;; walker. It takes a form and the current context and walks the form
484 ;;; calling itself or the appropriate template recursively.
486 ;;; "It is recommended that a program-analyzing-program process a form
487 ;;; that is a list whose car is a symbol as follows:
489 ;;; 1. If the program has particular knowledge about the symbol,
490 ;;; process the form using special-purpose code. All of the
491 ;;; standard special forms should fall into this category.
492 ;;; 2. Otherwise, if MACRO-FUNCTION is true of the symbol apply
493 ;;; either MACROEXPAND or MACROEXPAND-1 and start over.
494 ;;; 3. Otherwise, assume it is a function call. "
495 (defun walk-form-internal (form context env)
496 (declare (type (member :eval :set) context))
497 ;; First apply the walk-function to perform whatever translation
498 ;; the user wants to this form. If the second value returned
499 ;; by walk-function is T then we don't recurse...
500 (catch form
501 (with-current-source-form (form)
502 (multiple-value-bind (newform walk-no-more-p)
503 (funcall (env-walk-function env) form context env)
504 (catch newform
505 (cond
506 (walk-no-more-p newform)
507 ((not (eq form newform))
508 (walk-form-internal newform context env))
509 ((and (not (consp newform))
510 (or (eql context :eval)
511 (eql context :set)))
512 (let ((symmac (car (variable-symbol-macro-p newform env))))
513 (if symmac
514 (let* ((newnewform (walk-form-internal (cddr symmac)
515 context
516 env))
517 (resultform
518 (if (eq newnewform (cddr symmac))
519 (if *walk-form-expand-macros-p* newnewform newform)
520 newnewform))
521 (type (env-var-type newform env)))
522 (if (eq t type)
523 resultform
524 `(the ,type ,resultform)))
525 newform)))
526 ((eql context :set) newform)
528 (let* ((fn (car newform))
529 (template (get-walker-template fn newform)))
530 (if template
531 (if (symbolp template)
532 (funcall template newform context env)
533 (walk-template newform template context env))
534 (multiple-value-bind (newnewform macrop)
535 (walker-environment-bind
536 (new-env env :walk-form newform)
537 (%macroexpand-1 newform new-env))
538 (cond
539 (macrop
540 (let ((newnewnewform (walk-form-internal newnewform
541 context
542 env)))
543 (if (eq newnewnewform newnewform)
544 (if *walk-form-expand-macros-p* newnewform newform)
545 newnewnewform)))
546 ((and (symbolp fn)
547 (special-operator-p fn))
548 ;; This shouldn't happen, since this walker is now
549 ;; maintained as part of SBCL, so it should know
550 ;; about all the special forms that SBCL knows
551 ;; about.
552 (bug "unexpected special form ~S" fn))
554 ;; Otherwise, walk the form as if it's just a
555 ;; standard function call using a template for
556 ;; standard function call.
557 (walk-template
558 newnewform '(call repeat (eval)) context env)))))))))))))
560 (defun walk-template (form template context env)
561 (if (atom template)
562 (ecase template
563 ((eval function test effect return)
564 (walk-form-internal form :eval env))
565 ((quote nil) form)
566 (set
567 (walk-form-internal form :set env))
568 ((lambda call)
569 (cond ((legal-fun-name-p form)
570 form)
571 (t (walk-form-internal form context env)))))
572 (case (car template)
573 (repeat
574 (walk-template-handle-repeat form
575 (cdr template)
576 ;; For the case where nothing
577 ;; happens after the repeat
578 ;; optimize away the call to
579 ;; LENGTH.
580 (if (null (cddr template))
582 (nthcdr (- (length form)
583 (length
584 (cddr template)))
585 form))
586 context
587 env))
589 (walk-template form
590 (if (if (listp (cadr template))
591 (eval (cadr template))
592 (funcall (cadr template) form))
593 (caddr template)
594 (cadddr template))
595 context
596 env))
597 (remote
598 (walk-template form (cadr template) context env))
599 (otherwise
600 (cond ((atom form) form)
601 (t (recons form
602 (walk-template
603 (car form) (car template) context env)
604 (walk-template
605 (cdr form) (cdr template) context env))))))))
607 (defun walk-template-handle-repeat (form template stop-form context env)
608 (if (eq form stop-form)
609 (walk-template form (cdr template) context env)
610 (walk-template-handle-repeat-1
611 form template (car template) stop-form context env)))
613 (defun walk-template-handle-repeat-1 (form template repeat-template
614 stop-form context env)
615 (cond ((null form) ())
616 ((eq form stop-form)
617 (if (null repeat-template)
618 (walk-template stop-form (cdr template) context env)
619 (error "while handling code walker REPEAT:
620 ~%ran into STOP while still in REPEAT template")))
621 ((null repeat-template)
622 (walk-template-handle-repeat-1
623 form template (car template) stop-form context env))
625 (recons form
626 (walk-template (car form) (car repeat-template) context env)
627 (walk-template-handle-repeat-1 (cdr form)
628 template
629 (cdr repeat-template)
630 stop-form
631 context
632 env)))))
634 (defun walk-repeat-eval (form env)
635 (and form
636 (recons form
637 (walk-form-internal (car form) :eval env)
638 (walk-repeat-eval (cdr form) env))))
640 (defun relist (x &rest args)
641 (if (null args)
643 (relist-internal x args nil)))
645 (defun relist* (x &rest args)
646 (relist-internal x args t))
648 (defun relist-internal (x args *p)
649 (if (null (cdr args))
650 (if *p
651 (car args)
652 (recons x (car args) nil))
653 (recons x
654 (car args)
655 (relist-internal (cdr x) (cdr args) *p))))
657 ;;;; special walkers
659 (defun walk-declarations (body fn env
660 &optional doc-string-p declarations old-body
661 &aux (form (car body)))
662 (cond ((and (stringp form) ;might be a doc string
663 (cdr body) ;isn't the returned value
664 (null doc-string-p) ;no doc string yet
665 ;; FIXME: {decl}+ doc {decl}+ is supposed to be valid.
666 (null declarations)) ;no declarations yet
667 (recons body
668 form
669 (walk-declarations (cdr body) fn env t)))
670 ((and (listp form) (eq (car form) 'declare))
671 ;; We got ourselves a real live declaration. Record it, look
672 ;; for more.
673 (dolist (declaration (cdr form))
674 (let ((type (car declaration))
675 (name (cadr declaration))
676 (args (cddr declaration)))
677 (if (walked-var-declaration-p type)
678 (note-declaration `(,type
679 ,(or (var-lexical-p name env) name)
680 ,.args)
681 env)
682 (note-declaration (sb!c::canonized-decl-spec declaration) env))
683 (push declaration declarations)))
684 (recons body
685 form
686 (walk-declarations
687 (cdr body) fn env doc-string-p declarations)))
689 ;; Now that we have walked and recorded the declarations,
690 ;; call the function our caller provided to expand the body.
691 ;; We call that function rather than passing the real-body
692 ;; back, because we are RECONSING up the new body.
693 (funcall fn (or old-body body) env))))
695 (defun walk-unexpected-declare (form context env)
696 (declare (ignore context env))
697 (warn "encountered ~S ~_in a place where a DECLARE was not expected"
698 form)
699 form)
701 (defun walk-arglist (arglist context env &optional (destructuringp nil)
702 &aux arg)
703 (cond ((null arglist) ())
704 ((symbolp (setq arg (car arglist)))
705 (or (member arg sb!xc:lambda-list-keywords :test #'eq)
706 (note-var-binding arg env))
707 (recons arglist
709 (walk-arglist (cdr arglist)
710 context
712 (and destructuringp
713 (not (member arg sb!xc:lambda-list-keywords))))))
714 ((consp arg)
715 (prog1 (recons arglist
716 (if destructuringp
717 (walk-arglist arg context env destructuringp)
718 (relist* arg
719 (car arg)
720 (walk-form-internal (cadr arg) :eval env)
721 (cddr arg)))
722 (walk-arglist (cdr arglist) context env nil))
723 (if (symbolp (car arg))
724 (note-var-binding (car arg) env)
725 (note-var-binding (cadar arg) env))
726 (or (null (cddr arg))
727 (not (symbolp (caddr arg)))
728 (note-var-binding (caddr arg) env))))
730 (error "can't understand something in the arglist ~S" arglist))))
732 (defun walk-let (form context env)
733 (walker-environment-bind (new-env env)
734 (let* ((let (car form))
735 (bindings (cadr form))
736 (body (cddr form))
737 walked-bindings
738 (walked-body
739 (walk-declarations
740 body
741 (lambda (real-body real-env)
742 (setf walked-bindings
743 (walk-bindings-1 bindings env new-env context))
744 (walk-repeat-eval real-body real-env))
745 new-env)))
746 (relist* form let walked-bindings walked-body))))
748 (defun let*-binding-name (binding)
749 (if (symbolp binding)
750 binding
751 (car binding)))
753 (defun let*-binding-init (binding)
754 (if (or (symbolp binding)
755 (null (cdr binding)))
756 'no-init
757 (cadr binding)))
759 (defun let*-bindings (bindings &aux names inits (seen (make-hash-table :test #'eq)))
760 (dolist (binding (reverse bindings) (values names inits))
761 (let ((name (let*-binding-name binding)))
762 (push (cons name (gethash name seen)) names)
763 (setf (gethash name seen) t)
764 (push (let*-binding-init binding) inits))))
766 (defun walk-let* (form context env)
767 (walker-environment-bind (new-env env)
768 (let ((let* (car form))
769 (bindings (cadr form))
770 (body (cddr form)))
771 (multiple-value-bind (names inits) (let*-bindings bindings)
772 (multiple-value-bind (newbody decls doc) (parse-body body nil)
773 (declare (ignore newbody))
774 (aver (null doc))
775 (labels ((maybe-process-and-munge-declaration (name declaration env)
776 (if (walked-var-declaration-p (car declaration))
777 (case (car declaration)
778 (special (if (find name (cdr declaration))
779 (progn
780 (note-declaration `(special ,(var-lexical-p name env)) env)
781 (cons 'special (remove name (cdr declaration))))
782 declaration))
783 (t (if (eql name (cadr declaration))
784 (progn
785 (note-declaration `(,(car declaration)
786 ,(var-lexical-p name env)
787 ,@(cddr declaration))
788 env)
789 nil)
790 declaration)))
791 declaration))
792 (walk-let*-bindings (bindings names inits)
793 (when bindings
794 (recons bindings
795 (let ((name (car names))
796 (init (car inits)))
797 (prog1
798 (if (eql init 'no-init)
799 (prog1 (car bindings)
800 (note-var-binding (car name) new-env))
801 (prog1
802 (relist (car bindings)
803 (caar bindings)
804 (walk-form-internal init context new-env))
805 (note-var-binding (car name) new-env)))
806 (unless (cdr name)
807 (setf decls (mapcar (lambda (d)
808 (cons 'declare
809 (mapcar (lambda (dd) (maybe-process-and-munge-declaration (car name) dd new-env))
810 (cdr d))))
811 decls)))))
812 (walk-let*-bindings (cdr bindings) (cdr names) (cdr inits))))))
813 (relist* form let*
814 (walk-let*-bindings bindings names inits)
815 (walk-declarations body (lambda (form env) (walk-repeat-eval form env)) new-env))))))))
817 (defun walk-load-time-value (form context env)
818 (destructuring-bind (ltv val &optional read-only-p) form
819 (declare (ignore ltv))
820 (relist form
821 'load-time-value
822 ;; this is wrong: VAL is handled differently depending on
823 ;; whether we're in EVAL, COMPILE or COMPILE-FILE, and
824 ;; (after macroexpansion) is evaluated in the null lexical
825 ;; environment. The macro semantics are the same in each
826 ;; case, but VAR-LEXICAL-P can be tricked into giving the
827 ;; wrong answer.
828 (walk-form-internal val context env)
829 (walk-form-internal read-only-p context env))))
831 (defun walk-locally (form context env)
832 (declare (ignore context))
833 (walker-environment-bind (new-env env)
834 (let* ((locally (car form))
835 (body (cdr form))
836 (walked-body
837 (walk-declarations body #'walk-repeat-eval new-env)))
838 (relist*
839 form locally walked-body))))
841 (defun walk-multiple-value-setq (form context env)
842 (let ((vars (cadr form)))
843 (if (some (lambda (var)
844 (variable-symbol-macro-p var env))
845 vars)
846 (let* ((temps (mapcar (lambda (var)
847 (declare (ignore var))
848 (gensym))
849 vars))
850 (sets (mapcar (lambda (var temp) `(setq ,var ,temp))
851 vars
852 temps))
853 (expanded `(multiple-value-bind ,temps ,(caddr form)
854 ,@sets))
855 (walked (walk-form-internal expanded context env)))
856 (if (eq walked expanded)
857 form
858 walked))
859 (walk-template form '(nil (repeat (set)) eval) context env))))
861 (defun walk-multiple-value-bind (form context old-env)
862 (walker-environment-bind (new-env old-env)
863 (let* ((mvb (car form))
864 (bindings (cadr form))
865 (mv-form (walk-template (caddr form) 'eval context old-env))
866 (body (cdddr form))
867 walked-bindings
868 (walked-body
869 (walk-declarations
870 body
871 (lambda (real-body real-env)
872 (setq walked-bindings
873 (walk-bindings-1 bindings old-env new-env context))
874 (walk-repeat-eval real-body real-env))
875 new-env)))
876 (relist* form mvb walked-bindings mv-form walked-body))))
878 (defun walk-bindings-1 (bindings old-env new-env context)
879 (and bindings
880 (let ((binding (car bindings)))
881 (recons bindings
882 (if (symbolp binding)
883 (prog1 binding
884 (note-var-binding binding new-env))
885 (prog1 (relist binding
886 (car binding)
887 (walk-form-internal
888 (cadr binding) context old-env))
889 (note-var-binding (car binding) new-env)))
890 (walk-bindings-1 (cdr bindings) old-env new-env context)))))
892 (defun walk-lambda (form context old-env)
893 (walker-environment-bind (new-env old-env)
894 (let* ((arglist (cadr form))
895 (body (cddr form))
896 (walked-arglist (walk-arglist arglist context new-env))
897 (walked-body
898 (walk-declarations body #'walk-repeat-eval new-env)))
899 (relist* form
900 (car form)
901 walked-arglist
902 walked-body))))
904 (defun walk-named-lambda (form context old-env)
905 (walker-environment-bind (new-env old-env)
906 (let* ((name (second form))
907 (arglist (third form))
908 (body (cdddr form))
909 (walked-arglist (walk-arglist arglist context new-env))
910 (walked-body
911 (walk-declarations body #'walk-repeat-eval new-env)))
912 (relist* form
913 (car form)
914 name
915 walked-arglist
916 walked-body))))
918 (defun walk-setq (form context env)
919 (if (cdddr form)
920 (let* ((expanded (let ((rforms nil)
921 (tail (cdr form)))
922 (loop (when (null tail) (return (nreverse rforms)))
923 (let ((var (pop tail)) (val (pop tail)))
924 (push `(setq ,var ,val) rforms)))))
925 (walked (walk-repeat-eval expanded env)))
926 (if (eq expanded walked)
927 form
928 `(progn ,@walked)))
929 (let* ((var (cadr form))
930 (val (caddr form))
931 (symmac (car (variable-symbol-macro-p var env))))
932 (if symmac
933 (let* ((type (env-var-type var env))
934 (expanded (if (eq t type)
935 `(setf ,(cddr symmac) ,val)
936 `(setf ,(cddr symmac) (the ,type ,val))))
937 (walked (walk-form-internal expanded context env)))
938 (if (eq expanded walked)
939 form
940 walked))
941 (relist form 'setq
942 (walk-form-internal var :set env)
943 (walk-form-internal val :eval env))))))
945 (defun walk-symbol-macrolet (form context old-env)
946 (declare (ignore context))
947 (let* ((bindings (cadr form))
948 (body (cddr form)))
949 (walker-environment-bind
950 (new-env old-env
951 :lexical-vars
952 (append (mapcar (lambda (binding)
953 `(,(car binding)
954 sb!sys:macro . ,(cadr binding)))
955 bindings)
956 (env-lexical-variables old-env)))
957 (relist* form 'symbol-macrolet bindings
958 (walk-declarations body #'walk-repeat-eval new-env)))))
960 (defun walk-tagbody (form context env)
961 (recons form (car form) (walk-tagbody-1 (cdr form) context env)))
963 (defun walk-tagbody-1 (form context env)
964 (and form
965 (recons form
966 (if (or (symbolp (car form)) (integerp (car form)))
967 (walk-template (car form) 'quote context env)
968 (walk-form-internal (car form) context env))
969 (walk-tagbody-1 (cdr form) context env))))
971 (defun walk-macrolet (form context old-env)
972 (walker-environment-bind (old-env old-env)
973 (walker-environment-bind (macro-env
975 :walk-function (env-walk-function old-env))
976 (labels ((walk-definitions (definitions)
977 (and definitions
978 (let ((definition (car definitions)))
979 (recons definitions
980 (relist* definition
981 (car definition)
982 (walk-arglist (cadr definition)
983 context
984 macro-env
986 (walk-declarations (cddr definition)
987 #'walk-repeat-eval
988 macro-env))
989 (walk-definitions (cdr definitions)))))))
990 (with-new-definition-in-environment (new-env old-env form)
991 (relist* form
992 (car form)
993 (walk-definitions (cadr form))
994 (walk-declarations (cddr form)
995 #'walk-repeat-eval
996 new-env)))))))
998 (defun walk-flet (form context old-env)
999 (walker-environment-bind (old-env old-env)
1000 (labels ((walk-definitions (definitions)
1001 (if (null definitions)
1003 (recons definitions
1004 (walk-lambda (car definitions) context old-env)
1005 (walk-definitions (cdr definitions))))))
1006 (recons form
1007 (car form)
1008 (recons (cdr form)
1009 (walk-definitions (cadr form))
1010 (with-new-definition-in-environment (new-env old-env form)
1011 (walk-declarations (cddr form)
1012 #'walk-repeat-eval
1013 new-env)))))))
1015 (defun walk-labels (form context old-env)
1016 (walker-environment-bind (old-env old-env)
1017 (with-new-definition-in-environment (new-env old-env form)
1018 (labels ((walk-definitions (definitions)
1019 (if (null definitions)
1021 (recons definitions
1022 (walk-lambda (car definitions) context new-env)
1023 (walk-definitions (cdr definitions))))))
1024 (recons form
1025 (car form)
1026 (recons (cdr form)
1027 (walk-definitions (cadr form))
1028 (walk-declarations (cddr form)
1029 #'walk-repeat-eval
1030 new-env)))))))
1032 (defun walk-if (form context env)
1033 (destructuring-bind (if predicate arm1 &optional arm2) form
1034 (declare (ignore if)) ; should be 'IF
1035 (relist form
1037 (walk-form-internal predicate context env)
1038 (walk-form-internal arm1 context env)
1039 (walk-form-internal arm2 context env))))
1041 ;;;; examples
1044 ;;; Here are some examples of the kinds of things you should be able
1045 ;;; to do with your implementation of the macroexpansion environment
1046 ;;; hacking mechanism.
1048 ;;; WITH-LEXICAL-MACROS is kind of like MACROLET, but it only takes
1049 ;;; names of the macros and actual macroexpansion functions to use to
1050 ;;; macroexpand them. The win about that is that for macros which want
1051 ;;; to wrap several MACROLETs around their body, they can do this but
1052 ;;; have the macroexpansion functions be compiled. See the WITH-RPUSH
1053 ;;; example.
1055 ;;; If the implementation had a special way of communicating the
1056 ;;; augmented environment back to the evaluator that would be totally
1057 ;;; great. It would mean that we could just augment the environment
1058 ;;; then pass control back to the implementations own compiler or
1059 ;;; interpreter. We wouldn't have to call the actual walker. That
1060 ;;; would make this much faster. Since the principal client of this is
1061 ;;; defmethod it would make compiling defmethods faster and that would
1062 ;;; certainly be a win.
1064 (defmacro with-lexical-macros (macros &body body &environment old-env)
1065 (with-augmented-environment (new-env old-env :macros macros)
1066 (walk-form (cons 'progn body) :environment new-env)))
1068 (defun expand-rpush (form env)
1069 (declare (ignore env))
1070 `(push ,(caddr form) ,(cadr form)))
1072 (defmacro with-rpush (&body body)
1073 `(with-lexical-macros ,(list (list 'rpush #'expand-rpush)) ,@body))