1 ;;;; An interpreting EVAL
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!EVAL")
14 ;; (declaim (optimize (speed 3) (debug 1) (safety 1)))
16 ;;; Values used for marking specials/macros/etc in environments.
17 (defvar *special
* (gensym "SPECIAL"))
18 (defvar *macro
* (gensym "MACRO"))
19 (defvar *symbol-macro
* (gensym "SYMBOL-MACRO"))
20 (defvar *not-present
* (gensym "NOT-PRESENT"))
22 (define-condition interpreted-program-error
(program-error simple-condition sb
!impl
::encapsulated-condition
)
24 (:report
(lambda (condition stream
)
25 (if (slot-boundp condition
'condition
)
27 (format stream
"Error evaluating a form:~% ~A"
28 (sb!impl
::encapsulated-condition condition
)))
29 (format stream
"Error evaluating a form:~% ~?"
30 (simple-condition-format-control condition
)
31 (simple-condition-format-arguments condition
))))))
33 ;;; ANSI defines that program syntax errors should be of type
34 ;;; PROGRAM-ERROR. Therefore...
35 (define-condition arg-count-program-error
(sb!kernel
::arg-count-error
39 ;;; FIXME: This macro is not clearly better than plain destructuring-bind.
41 ;;; First of all, it's ridiculous that the error message says
42 ;;; "error while parsing arguments to PROGRAM-DESTRUCTURING-BIND".
43 ;;; The user doesn't care what the macro was that parsed the arguments
44 ;;; to the special operator. It should instead say
45 ;;; "... while parsing arguments to special operator <foo>"
47 ;;; Second, it is naive to think that existence of this macro suffices
48 ;;; to always signal an INTEPRETED-PROGRAM-ERROR and not just ERROR.
49 ;;; e.g. (LET ((X 1)) . JUNK) binds the &BODY variable to the non-list JUNK.
50 ;;; To fix the general problem, every use of DOLIST and other things
51 ;;; would have to be replaced by something like SB-PCL::DOLIST-CAREFULLY.
52 ;;; Similarly for ((&REST BINDINGS) &BODY BODY) wherein it's not even
53 ;;; obvious that BINDINGS is enforced by the macro to be a list. [lp#1469275]
55 ;; OAOOM? (see destructuring-bind.lisp)
56 (defmacro program-destructuring-bind
(lambda-list arg-list
&body body
)
57 ;; Not wrapping ARG-LIST in (THE LIST) is better than what DESTRUCTURING-BIND
58 ;; does, because this gives a more descriptive message if you pass a non-list
59 ;; to the form handler, like (IF . 3) will say that 3 does not match the
60 ;; list (TEST IF-TRUE &OPTIONAL IF-FALSE) rather than just "3 is not a list".
61 ;; For the sake of compatibility, DESTRUCTURING-BIND signals TYPE-ERROR
62 ;; in that situation, which is less than ideal.
64 ;; (:EVAL) is a dummy context. We don't have enough information to
65 ;; show the operator name without using debugger internals to get the stack frame.
66 ;; It would be easier to make the name an argument to this macro.
67 `(sb!int
:binding
* ,(sb!c
::expand-ds-bind lambda-list arg-list t nil
'(:eval
))
70 (defun ip-error (format-control &rest format-arguments
)
71 (error 'interpreted-program-error
72 :format-control format-control
73 :format-arguments format-arguments
))
75 (defmacro nconc-2
(a b
)
81 (progn (setf (cdr (last ,tmp
)) ,tmp2
) ,tmp
)
84 ;;; Construct a compiler LEXENV from the same data that's used for
85 ;;; creating an interpreter ENV. This is needed for example when
86 ;;; passing the environment to macroexpanders or when compiling an
87 ;;; interpreted function.
88 (defun fabricate-new-native-environment (old-lexenv new-funs new-expanders
89 new-vars new-symbol-expansions
91 (labels ((to-native-funs (binding)
92 ;; Non-macroexpander function entries are irrelevant for
93 ;; the LEXENV. If we're using the LEXENV for
94 ;; macro-expansion any references to local non-macro
95 ;; function bindings are undefined behaviour. If we're
96 ;; compiling an interpreted function, a lexical environment
97 ;; with non-macro functions will be too hairy to compile.
98 (if (eq (cdr binding
) *macro
*)
101 (cdr (assoc (car binding
) new-expanders
))))
104 (to-native-vars (binding)
105 ;; And likewise for symbol macros.
106 (if (eq (cdr binding
) *symbol-macro
*)
109 (cdr (assoc (car binding
) new-symbol-expansions
))))
112 (let ((lexenv (sb!c
::internal-make-lexenv
113 (nconc-2 (mapcar #'to-native-funs new-funs
)
114 (sb!c
::lexenv-funs old-lexenv
))
115 (nconc-2 (mapcar #'to-native-vars new-vars
)
116 (sb!c
::lexenv-vars old-lexenv
))
118 (sb!c
::lexenv-handled-conditions old-lexenv
)
119 (sb!c
::lexenv-disabled-package-locks old-lexenv
)
120 (sb!c
::lexenv-policy old-lexenv
) ; = (OR %POLICY *POLICY*)
121 (sb!c
::lexenv-user-data old-lexenv
))))
122 (dolist (declaration declarations
)
123 (unless (consp declaration
)
124 (ip-error "malformed declaration specifier ~S in ~S"
125 declaration
(cons 'declare declarations
)))
126 (case (car declaration
)
128 (setf (sb!c
::lexenv-%policy lexenv
)
129 (copy-structure (sb!c
::lexenv-%policy lexenv
)))
130 (dolist (element (cdr declaration
))
131 (multiple-value-bind (quality value
)
132 (if (not (consp element
)) ; FIXME: OAOOM w/'proclaim'
134 (program-destructuring-bind (quality value
)
136 (values quality value
)))
138 ((sb!c
::policy-quality-name-p quality
)
139 (sb!c
::alter-policy
(sb!c
::lexenv-%policy lexenv
)
141 (t (warn "ignoring unknown optimization quality ~S in ~S"
142 quality
(cons 'declare declarations
)))))))
144 (setf (sb!c
::lexenv-handled-conditions lexenv
)
145 (sb!c
::process-muffle-conditions-decl
147 (sb!c
::lexenv-handled-conditions lexenv
))))
149 (setf (sb!c
::lexenv-handled-conditions lexenv
)
150 (sb!c
::process-unmuffle-conditions-decl
152 (sb!c
::lexenv-handled-conditions lexenv
))))
153 ((disable-package-locks sb
!ext
:enable-package-locks
)
154 (setf (sb!c
::lexenv-disabled-package-locks lexenv
)
155 (sb!c
::process-package-lock-decl
157 (sb!c
::lexenv-disabled-package-locks lexenv
))))))
161 (:constructor %make-env
162 (parent vars funs expanders symbol-expansions
163 tags blocks declarations native-lexenv
)))
174 (defun make-env (&key parent vars funs expanders
175 symbol-expansions tags blocks declarations
)
177 (append vars
(env-vars parent
))
178 (append funs
(env-funs parent
))
179 (append expanders
(env-expanders parent
))
180 (append symbol-expansions
(env-symbol-expansions parent
))
181 (nconc-2 tags
(env-tags parent
))
182 (nconc-2 blocks
(env-blocks parent
))
184 (fabricate-new-native-environment (env-native-lexenv parent
)
186 vars symbol-expansions
189 (defun make-null-environment ()
190 (%make-env nil nil nil nil nil nil nil nil
191 (sb!c
::internal-make-lexenv
193 nil nil nil nil nil nil nil
197 ;;; Augment ENV with a special or lexical variable binding
198 (declaim (inline push-var
))
199 (defun push-var (name value env
)
200 (push (cons name value
) (env-vars env
))
201 (push (cons name
:bogus
) (sb!c
::lexenv-vars
(env-native-lexenv env
))))
203 ;;; Augment ENV with a local function binding
204 (declaim (inline push-fun
))
205 (defun push-fun (name value calling-env body-env
)
207 (let ((sb!c
:*lexenv
* (env-native-lexenv calling-env
)))
208 (program-assert-symbol-home-package-unlocked
209 :eval name
"binding ~A as a local function")))
210 (push (cons name value
) (env-funs body-env
))
211 (push (cons name
:bogus
) (sb!c
::lexenv-funs
(env-native-lexenv body-env
))))
213 (sb!int
:def
!method print-object
((env env
) stream
)
214 (print-unreadable-object (env stream
:type t
:identity t
)))
216 (macrolet ((define-get-binding (name accessor
&key
(test '#'eq
))
217 ;; A macro, sadly, because an inline function here is
219 `(defmacro ,name
(symbol env
)
220 `(assoc ,symbol
(,',accessor
,env
) :test
,',test
))))
221 (define-get-binding get-binding env-vars
)
222 (define-get-binding get-fbinding env-funs
:test
#'equal
)
223 (define-get-binding get-expander-binding env-expanders
)
224 (define-get-binding get-symbol-expansion-binding env-symbol-expansions
)
225 (define-get-binding get-tag-binding env-tags
:test
#'eql
)
226 (define-get-binding get-block-binding env-blocks
))
228 ;;; Return a list of all symbols that are declared special in the
229 ;;; declarations listen in DECLS.
230 (defun declared-specials (decls)
231 (let ((specials nil
))
233 (when (eql (car decl
) 'special
)
234 (dolist (var (cdr decl
))
235 (push var specials
))))
238 ;;; Given a list of variables that should be marked as special in an
239 ;;; environment, return the appropriate binding forms to be given
241 (defun special-bindings (specials env
)
242 (mapcar #'(lambda (var)
243 (let ((sb!c
:*lexenv
* (env-native-lexenv env
)))
244 (program-assert-symbol-home-package-unlocked
245 :eval var
"declaring ~A special"))
246 (cons var
*special
*))
249 ;;; Return true if SYMBOL has been declared special either globally
250 ;;; or is in the DECLARED-SPECIALS list.
251 (defun specialp (symbol declared-specials
)
252 (let ((type (sb!int
:info
:variable
:kind symbol
)))
255 ;; Horrible place for this, but it works.
256 (ip-error "Can't bind constant symbol: ~S" symbol
))
259 (ip-error "Can't bind a global variable: ~S" symbol
))
260 ((eq type
:special
) t
)
261 ((member symbol declared-specials
:test
#'eq
)
265 (defun binding-name (binding)
266 (if (consp binding
) (first binding
) binding
))
267 (defun binding-value (binding)
268 (if (consp binding
) (second binding
) nil
))
269 (defun supplied-p-parameter (spec)
270 (if (consp spec
) (third spec
) nil
))
271 (defun keyword-name (spec)
273 (if (consp (first spec
))
274 (second (first spec
))
277 (defun keyword-key (spec)
279 (if (consp (first spec
))
281 (intern (symbol-name (first spec
)) "KEYWORD"))
282 (intern (symbol-name spec
) "KEYWORD")))
283 (defun keyword-default-value (spec)
284 (if (consp spec
) (second spec
) nil
))
286 ;;; Given a list of ARGUMENTS and a LAMBDA-LIST, return two values:
287 ;;; * An alist[*] mapping the required parameters of the function to
288 ;;; the corresponding argument values
289 ;;; * An alist mapping the keyword, optional and rest parameters of
290 ;;; the function to the corresponding argument values (if supplied)
291 ;;; or to the parameter's default expression (if not). Supplied-p
292 ;;; parameters and aux variables are handled in a similar manner.
294 ;;; For example given the argument list of (1 2) and the lambda-list of
295 ;;; (A &OPTIONAL (B A) (C (1+ A))), we'd return the values
296 ;;; (A . '1) and ((B . '2) (C . (1+ A))).
298 ;;; Used only for implementing calls to interpreted functions.
299 (defun parse-arguments (arguments lambda-list
)
300 (multiple-value-bind (llks required optional rest keyword aux
)
301 ;; FIXME: shouldn't this just pass ":silent t" ?
302 (handler-bind ((style-warning #'muffle-warning
))
303 (sb!int
:parse-lambda-list lambda-list
))
304 (let* ((original-arguments arguments
)
305 (rest-p (not (null rest
)))
307 (keyword-p (sb!int
:ll-kwds-keyp llks
))
308 (allow-other-keys-p (sb!int
:ll-kwds-allowp llks
))
309 (arguments-present (length arguments
))
310 (required-length (length required
))
311 (optional-length (length optional
))
312 (non-keyword-arguments (+ required-length optional-length
))
313 (optionals-present (- (min non-keyword-arguments arguments-present
)
315 (keywords-present-p (> arguments-present non-keyword-arguments
))
316 (let-like-bindings nil
)
317 (let*-like-bindings nil
))
319 ((< arguments-present required-length
)
320 (ip-error "~@<Too few arguments in ~S to satisfy lambda list ~S.~:@>"
321 arguments lambda-list
))
322 ((and (not (or rest-p keyword-p
)) keywords-present-p
)
323 (ip-error "~@<Too many arguments in ~S to satisfy lambda list ~S.~:@>"
324 arguments lambda-list
))
325 ((and keyword-p keywords-present-p
326 (oddp (- arguments-present non-keyword-arguments
)))
327 (ip-error "~@<Odd number of &KEY arguments in ~S for ~S.~:@>"
328 arguments lambda-list
)))
329 (dotimes (i required-length
)
330 (push (cons (pop required
) (pop arguments
)) let-like-bindings
))
331 (do ((optionals-parsed 0 (1+ optionals-parsed
)))
333 (let ((this-optional (pop optional
))
334 (supplied-p (< optionals-parsed optionals-present
)))
335 (push (cons (binding-name this-optional
)
337 (list 'quote
(pop arguments
))
338 (binding-value this-optional
)))
340 (when (supplied-p-parameter this-optional
)
341 (push (cons (supplied-p-parameter this-optional
)
342 (list 'quote supplied-p
))
343 let
*-like-bindings
))))
344 (let ((keyword-plist arguments
))
346 (push (cons rest
(list 'quote keyword-plist
)) let
*-like-bindings
))
348 (unless (or allow-other-keys-p
349 (getf keyword-plist
:allow-other-keys
))
350 (loop for
(key value
) on keyword-plist by
#'cddr doing
351 (when (and (not (eq key
:allow-other-keys
))
352 (not (member key keyword
:key
#'keyword-key
)))
353 (ip-error "~@<Unknown &KEY argument ~S in ~S for ~S.~:@>"
354 key original-arguments lambda-list
))))
355 (dolist (keyword-spec keyword
)
356 (let ((supplied (getf keyword-plist
(keyword-key keyword-spec
)
358 (push (cons (keyword-name keyword-spec
)
359 (if (eq supplied
*not-present
*)
360 (keyword-default-value keyword-spec
)
361 (list 'quote supplied
)))
363 (when (supplied-p-parameter keyword-spec
)
364 (push (cons (supplied-p-parameter keyword-spec
)
365 (list 'quote
(not (eq supplied
*not-present
*))))
366 let
*-like-bindings
))))))
370 (let ((this-aux (pop aux
)))
371 (push (cons (binding-name this-aux
)
372 (binding-value this-aux
))
373 let
*-like-bindings
))))
374 (values (nreverse let-like-bindings
) (nreverse let
*-like-bindings
)))))
376 ;;; Evaluate LET*-like (sequential) bindings.
378 ;;; Given an alist of BINDINGS, evaluate the value form of the first
379 ;;; binding in ENV, generate an augmented environment with a binding
380 ;;; of the variable to the value in ENV, and then evaluate the next
381 ;;; binding form. Once all binding forms have been handled, END-ACTION
382 ;;; is funcalled with the final environment.
384 ;;; SPECIALS is a list of variables that have a bound special declaration.
385 ;;; These variables (and those that have been declaimed as special) are
386 ;;; bound as special variables.
387 (defun eval-next-let*-binding
(bindings specials env end-action
)
388 (flet ((maybe-eval (exp)
389 ;; Pick off the easy (QUOTE x) case which is very common
390 ;; due to function calls. (see PARSE-ARGUMENTS)
391 (if (and (consp exp
) (eq (car exp
) 'quote
))
395 (let* ((binding-name (car (car bindings
)))
396 (binding-value (cdr (car bindings
)))
397 (new-env (make-env :parent env
)))
398 (if (specialp binding-name specials
)
401 (list (maybe-eval binding-value
))
402 ;; Mark the variable as special in this environment
403 (push-var binding-name
*special
* new-env
)
404 (eval-next-let*-binding
405 (cdr bindings
) specials new-env end-action
))
407 (push-var binding-name
(maybe-eval binding-value
) new-env
)
408 (eval-next-let*-binding
409 (cdr bindings
) specials new-env end-action
))))
410 (funcall end-action env
))))
412 ;;; Create a new environment based on OLD-ENV by adding the variable
413 ;;; bindings in BINDINGS to it, and call FUNCTION with the new environment
414 ;;; as the only parameter. DECLARATIONS are the declarations that were
415 ;;; in a source position where bound declarations for the bindings could
418 ;;; FREE-SPECIALS-P controls whether all special declarations should
419 ;;; end cause the variables to be marked as special in the environment
420 ;;; (when true), or only bound declarations (when false). Basically
421 ;;; it'll be T when handling a LET, and NIL when handling a call to an
422 ;;; interpreted function.
423 (defun call-with-new-env (old-env bindings declarations
424 free-specials-p function
)
425 (let* ((specials (declared-specials declarations
))
427 (dynamic-values nil
))
428 ;; To check for package-lock violations
429 (special-bindings specials old-env
)
430 (flet ((generate-binding (binding)
431 (if (specialp (car binding
) specials
)
432 ;; If the variable being bound is globally special or
433 ;; there's a bound special declaration for it, record it
434 ;; in DYNAMIC-VARS / -VALUES separately:
435 ;; * To handle the case of FREE-SPECIALS-P == T more
437 ;; * The dynamic variables will be bound with PROGV just
440 (push (car binding
) dynamic-vars
)
441 (push (cdr binding
) dynamic-values
)
443 ;; Otherwise it's a lexical binding, and the value
444 ;; will be recorded in the environment.
446 (let ((new-env (make-env
448 :vars
(mapcan #'generate-binding bindings
)
449 :declarations declarations
)))
450 (dolist (special (if free-specials-p specials dynamic-vars
))
451 (push-var special
*special
* new-env
))
453 (progv dynamic-vars dynamic-values
454 (funcall function new-env
))
455 ;; When there are no specials, the PROGV would be a no-op,
456 ;; but it's better to elide it completely, since the
457 ;; funcall is then in tail position.
458 (funcall function new-env
))))))
460 ;;; Create a new environment based on OLD-ENV by binding the argument
461 ;;; list ARGUMENTS to LAMBDA-LIST, and call FUNCTION with the new
462 ;;; environment as argument. DECLARATIONS are the declarations that
463 ;;; were in a source position where bound declarations for the
464 ;;; bindings could be introduced.
465 (defun call-with-new-env-full-parsing
466 (old-env lambda-list arguments declarations function
)
467 (multiple-value-bind (let-like-bindings let
*-like-binding
)
468 (parse-arguments arguments lambda-list
)
469 (let ((specials (declared-specials declarations
))
470 var-specials free-specials
)
471 ;; Separate the bound and free special declarations
472 (dolist (special specials
)
473 (if (or (member special let-like-bindings
:key
#'car
)
474 (member special let
*-like-binding
:key
#'car
))
475 (push special var-specials
)
476 (push special free-specials
)))
477 ;; First introduce the required parameters into the environment
478 ;; with CALL-WITH-NEW-ENV
480 old-env let-like-bindings declarations nil
482 ;; Then deal with optionals / keywords / etc.
483 (eval-next-let*-binding
484 let
*-like-binding var-specials env
486 ;; And now that we have evaluated all the
487 ;; initialization forms for the bindings, add the free
488 ;; special declarations to the environment. To see why
489 ;; this is the right thing to do (instead of passing
490 ;; FREE-SPECIALS-P == T to CALL-WITH-NEW-ENV),
493 ;; (eval '(let ((*a* 1))
494 ;; (declare (special *a*))
496 ;; (funcall (lambda (&optional (b *a*))
497 ;; (declare (special *a*))
498 ;; (values b *a*))))))
500 ;; *A* should be special in the body of the lambda, but
501 ;; not when evaluating the default value of B.
502 (dolist (special free-specials
)
503 (push-var special
*special
* env
))
504 (funcall function env
))))))))
506 ;;; Set the VALUE of the binding (either lexical or special) of the
507 ;;; variable named by SYMBOL in the environment ENV.
508 (defun set-variable (symbol value env
)
509 (let ((binding (get-binding symbol env
)))
512 ((eq (cdr binding
) *special
*)
513 (setf (symbol-value symbol
) value
))
514 ((eq (cdr binding
) *symbol-macro
*)
515 (error "Tried to set a symbol-macrolet!"))
516 (t (setf (cdr binding
) value
)))
517 (case (sb!int
:info
:variable
:kind symbol
)
518 (:macro
(error "Tried to set a symbol-macrolet!"))
519 (:alien
(let ((type (sb!int
:info
:variable
:alien-info symbol
)))
520 (setf (sb!alien
::%heap-alien type
) value
)))
522 (let ((type (sb!c
::info
:variable
:type symbol
)))
524 (let ((type-specifier (type-specifier type
)))
525 (unless (typep value type-specifier
)
528 :expected-type type-specifier
))))
529 (setf (symbol-value symbol
) value
)))))))
531 ;;; Retrieve the value of the binding (either lexical or special) of
532 ;;; the variable named by SYMBOL in the environment ENV. For symbol
533 ;;; macros the expansion is returned instead.
534 (defun get-variable (symbol env
)
535 (let ((binding (get-binding symbol env
)))
538 ((eq (cdr binding
) *special
*)
539 (values (symbol-value symbol
) :variable
))
540 ((eq (cdr binding
) *symbol-macro
*)
541 (values (cdr (get-symbol-expansion-binding symbol env
))
543 (t (values (cdr binding
) :variable
)))
544 (case (sb!int
:info
:variable
:kind symbol
)
545 (:macro
(values (macroexpand-1 symbol
) :expansion
))
546 (:alien
(values (sb!alien-internals
:alien-value symbol
) :variable
))
547 (t (values (symbol-value symbol
) :variable
))))))
549 ;;; Retrieve the function/macro binding of the symbol NAME in
550 ;;; environment ENV. The second return value will be :MACRO for macro
551 ;;; bindings, :FUNCTION for function bindings.
552 (defun get-function (name env
)
553 (let ((binding (get-fbinding name env
)))
556 ((eq (cdr binding
) *macro
*)
557 (values (cdr (get-expander-binding name env
)) :macro
))
558 (t (values (cdr binding
) :function
)))
560 ((and (symbolp name
) (macro-function name
))
561 (values (macro-function name
) :macro
))
562 (t (values (%coerce-name-to-fun name
) :function
))))))
564 ;;; Return true if EXP is a lambda form.
567 ((lambda sb
!int
:named-lambda
) t
)))
569 ;;; Split off the declarations (and the docstring, if
570 ;;; DOC-STRING-ALLOWED is true) from the actual forms of BODY.
571 ;;; Returns three values: the cons in BODY containing the first
572 ;;; non-header subform, the docstring, and a list of the declarations.
574 ;;; FIXME: The name of this function is somewhat misleading. It's not
575 ;;; used just for parsing the headers from lambda bodies, but for all
576 ;;; special forms that have attached declarations.
577 (defun parse-lambda-headers (body &key doc-string-allowed
)
578 (loop with documentation
= nil
579 with declarations
= nil
582 ((and doc-string-allowed
(stringp (car form
)))
583 (if (cdr form
) ; CLHS 3.4.11
585 (ip-error "~@<Duplicate doc string ~S.~:@>" (car form
))
586 (setf documentation
(car form
)))
587 (return (values form documentation declarations
))))
588 ((and (consp (car form
)) (eql (caar form
) 'declare
))
589 (setf declarations
(append declarations
(cdar form
))))
590 (t (return (values form documentation declarations
))))
591 finally
(return (values nil documentation declarations
))))
593 ;;; Create an interpreted function from the lambda-form EXP evaluated
594 ;;; in the environment ENV.
595 (defun eval-lambda (exp env
)
598 (multiple-value-bind (body documentation declarations
)
599 (parse-lambda-headers (cddr exp
) :doc-string-allowed t
)
600 (make-interpreted-function :lambda-list
(second exp
)
602 :documentation documentation
603 :source-location
(sb!c
::make-definition-source-location
)
604 :declarations declarations
)))
605 ((sb!int
:named-lambda
)
606 (multiple-value-bind (body documentation declarations
)
607 (parse-lambda-headers (cdddr exp
) :doc-string-allowed t
)
608 (make-interpreted-function :name
(second exp
)
609 :lambda-list
(third exp
)
611 :documentation documentation
612 :source-location
(sb!c
::make-definition-source-location
)
613 :declarations declarations
)))))
615 (defun eval-progn (body env
)
616 (let ((previous-exp nil
))
619 (%eval previous-exp env
))
620 (setf previous-exp exp
))
621 ;; Preserve tail call
622 (%eval previous-exp env
)))
624 (defun eval-if (body env
)
625 (program-destructuring-bind (test if-true
&optional if-false
) body
628 (%eval if-false env
))))
630 (defun eval-let (body env
)
631 (program-destructuring-bind (bindings &body body
) body
632 ;; First evaluate the bindings in parallel
633 (let ((bindings (mapcar
635 (cons (binding-name binding
)
636 (%eval
(binding-value binding
) env
)))
638 (multiple-value-bind (body documentation declarations
)
639 (parse-lambda-headers body
:doc-string-allowed nil
)
640 (declare (ignore documentation
))
641 ;; Then establish them into the environment, and evaluate the
643 (call-with-new-env env bindings declarations t
645 (eval-progn body env
)))))))
647 (defun eval-let* (body old-env
)
648 (program-destructuring-bind (bindings &body body
) body
649 (multiple-value-bind (body documentation declarations
)
650 (parse-lambda-headers body
:doc-string-allowed nil
)
651 (declare (ignore documentation
))
652 ;; First we separate the special declarations into bound and
653 ;; free declarations.
654 (let ((specials (declared-specials declarations
))
655 var-specials free-specials
)
656 (dolist (special specials
)
657 (if (member special bindings
:key
#'binding-name
)
658 (push special var-specials
)
659 (push special free-specials
)))
660 (let ((env (make-env :parent old-env
661 :declarations declarations
)))
662 ;; Then we establish the bindings into the environment
664 (eval-next-let*-binding
665 (mapcar #'(lambda (binding)
666 (cons (binding-name binding
)
667 (binding-value binding
)))
671 ;; Now that we're done evaluating the bindings, add the
672 ;; free special declarations. See also
673 ;; CALL-WITH-NEW-ENV-FULL-PARSING.
674 (dolist (special free-specials
)
675 (push-var special
*special
* env
))
676 (eval-progn body env
))))))))
678 ;; Return a named local function in the environment ENV, made from the
679 ;; definition form FUNCTION-DEF.
680 (defun eval-local-function-def (function-def env
)
681 (program-destructuring-bind (name lambda-list
&body local-body
) function-def
682 (multiple-value-bind (local-body documentation declarations
)
683 (parse-lambda-headers local-body
:doc-string-allowed t
)
684 (%eval
`#'(sb!int
:named-lambda
,name
,lambda-list
688 (declare ,@declarations
)
689 (block ,(cond ((consp name
) (second name
))
694 (defun eval-flet (body env
)
695 (program-destructuring-bind ((&rest local-functions
) &body body
) body
696 (multiple-value-bind (body documentation declarations
)
697 (parse-lambda-headers body
:doc-string-allowed nil
)
698 (declare (ignore documentation
))
699 (let* ((specials (declared-specials declarations
))
700 (new-env (make-env :parent env
701 :vars
(special-bindings specials env
)
702 :declarations declarations
)))
703 (dolist (function-def local-functions
)
704 (push-fun (car function-def
)
705 ;; Evaluate the function definitions in ENV.
706 (eval-local-function-def function-def env
)
707 ;; Do package-lock checks in ENV.
709 ;; But add the bindings to the child environment.
711 (eval-progn body new-env
)))))
713 (defun eval-labels (body old-env
)
714 (program-destructuring-bind ((&rest local-functions
) &body body
) body
715 (multiple-value-bind (body documentation declarations
)
716 (parse-lambda-headers body
:doc-string-allowed nil
)
717 (declare (ignore documentation
))
718 ;; Create a child environment, evaluate the function definitions
719 ;; in it, and add them into the same environment.
720 (let ((env (make-env :parent old-env
721 :declarations declarations
)))
722 (dolist (function-def local-functions
)
723 (push-fun (car function-def
)
724 (eval-local-function-def function-def env
)
727 ;; And then add an environment for the body of the LABELS. A
728 ;; separate environment from the one where we added the
729 ;; functions to is needed, since any special variable
730 ;; declarations need to be in effect in the body, but not in
731 ;; the bodies of the local functions.
732 (let* ((specials (declared-specials declarations
))
733 (new-env (make-env :parent env
734 :vars
(special-bindings specials env
))))
735 (eval-progn body new-env
))))))
737 ;; Return a local macro-expander in the environment ENV, made from the
738 ;; definition form FUNCTION-DEF.
739 (defun eval-local-macro-def (function-def env
)
740 (program-destructuring-bind (name lambda-list
&body local-body
) function-def
741 (%eval
(sb!int
:make-macro-lambda nil
; the lambda is anonymous.
742 lambda-list local-body
746 (defun eval-macrolet (body env
)
747 (program-destructuring-bind ((&rest local-functions
) &body body
) body
748 (flet ((generate-fbinding (macro-def)
749 (cons (car macro-def
) *macro
*))
750 (generate-mbinding (macro-def)
751 (let ((name (car macro-def
))
752 (sb!c
:*lexenv
* (env-native-lexenv env
)))
754 (program-assert-symbol-home-package-unlocked
755 :eval name
"binding ~A as a local macro"))
756 (cons name
(eval-local-macro-def macro-def env
)))))
757 (multiple-value-bind (body documentation declarations
)
758 (parse-lambda-headers body
:doc-string-allowed nil
)
759 (declare (ignore documentation
))
760 (let* ((specials (declared-specials declarations
))
761 (new-env (make-env :parent env
762 :vars
(special-bindings specials env
)
763 :funs
(mapcar #'generate-fbinding
765 :expanders
(mapcar #'generate-mbinding
767 :declarations declarations
)))
768 (eval-progn body new-env
))))))
770 (defun eval-symbol-macrolet (body env
)
771 (program-destructuring-bind ((&rest bindings
) &body body
) body
772 (flet ((generate-binding (binding)
773 (cons (car binding
) *symbol-macro
*))
774 (generate-sm-binding (binding)
775 (let ((name (car binding
))
776 (sb!c
:*lexenv
* (env-native-lexenv env
)))
777 (when (or (boundp name
)
778 (eq (sb!int
:info
:variable
:kind name
) :macro
))
779 (program-assert-symbol-home-package-unlocked
780 :eval name
"binding ~A as a local symbol-macro"))
781 (cons name
(second binding
)))))
782 (multiple-value-bind (body documentation declarations
)
783 (parse-lambda-headers body
:doc-string-allowed nil
)
784 (declare (ignore documentation
))
785 (let ((specials (declared-specials declarations
)))
786 (dolist (binding bindings
)
787 (when (specialp (binding-name binding
) specials
)
788 (ip-error "~@<Can't bind SYMBOL-MACROLET of special ~
790 (binding-name binding
)))))
791 (let* ((specials (declared-specials declarations
))
792 (new-env (make-env :parent env
793 :vars
(nconc-2 (mapcar #'generate-binding
795 (special-bindings specials env
))
796 :symbol-expansions
(mapcar
797 #'generate-sm-binding
799 :declarations declarations
)))
800 (eval-progn body new-env
))))))
802 (defun eval-progv (body env
)
803 (program-destructuring-bind (vars vals
&body body
) body
804 (progv (%eval vars env
) (%eval vals env
)
805 (eval-progn body env
))))
807 (defun eval-function (body env
)
808 (program-destructuring-bind (name) body
810 ;; LAMBDAP assumes that the argument is a cons, so we need the
811 ;; initial symbol case, instead of relying on the fall-through
812 ;; case that has the same function body.
813 ((symbolp name
) (nth-value 0 (get-function name env
)))
814 ((lambdap name
) (eval-lambda name env
))
815 (t (nth-value 0 (get-function name env
))))))
817 (defun eval-eval-when (body env
)
818 (program-destructuring-bind ((&rest situation
) &body body
) body
819 ;; FIXME: check that SITUATION only contains valid situations
820 (if (or (member :execute situation
)
821 (member 'eval situation
))
822 (eval-progn body env
))))
824 (defun eval-quote (body env
)
825 (declare (ignore env
))
826 (program-destructuring-bind (object) body
829 (defun eval-setq (pairs env
)
830 (when (oddp (length pairs
))
831 (ip-error "~@<Odd number of args to SETQ: ~S~:@>" (cons 'setq pairs
)))
833 (loop for
(var new-val
) on pairs by
#'cddr do
835 (multiple-value-bind (expansion type
) (get-variable var env
)
839 (%eval
(list 'setf expansion new-val
) env
)))
841 (setf last
(set-variable var
(%eval new-val env
)
843 (unbound-variable (c)
845 (setf last
(setf (symbol-value var
)
846 (%eval new-val env
))))))
849 (defun eval-multiple-value-call (body env
)
850 (program-destructuring-bind (function-form &body forms
) body
851 (%apply
(%eval function-form env
)
852 (loop for form in forms
853 nconc
(multiple-value-list (%eval form env
))))))
855 (defun eval-multiple-value-prog1 (body env
)
856 (program-destructuring-bind (first-form &body forms
) body
857 (multiple-value-prog1 (%eval first-form env
)
858 (eval-progn forms env
))))
860 (defun eval-catch (body env
)
861 (program-destructuring-bind (tag &body forms
) body
862 (catch (%eval tag env
)
863 (eval-progn forms env
))))
865 (defun eval-tagbody (body old-env
)
866 (let ((env (make-env :parent old-env
))
871 (flet ((go-to-tag (tag)
872 (setf target-tag tag
)
874 ;; For each tag, store a trampoline function into the environment
875 ;; and the location in the body into the TAGS alist.
876 (do ((form body
(cdr form
)))
878 (when (atom (car form
))
879 (when (assoc (car form
) tags
)
880 (ip-error "The tag :A appears more than once in a tagbody."))
881 (push (cons (car form
) (cdr form
)) tags
)
882 (push (cons (car form
) #'go-to-tag
) (env-tags env
)))))
883 ;; And then evaluate the forms in the body, starting from the
887 ;; The trampoline has set the TARGET-TAG. Restart evaluation of
888 ;; the body from the location in body that matches the tag.
889 (setf start
(cdr (assoc target-tag tags
)))
892 (when (not (atom form
))
893 (%eval form env
))))))
895 (defun eval-go (body env
)
896 (program-destructuring-bind (tag) body
897 (let ((target (get-tag-binding tag env
)))
899 ;; Call the GO-TO-TAG trampoline
900 (funcall (cdr target
) tag
)
901 (ip-error "~@<Attempt to GO to nonexistent tag: ~S~:@>" tag
)))))
903 (defun eval-block (body old-env
)
904 (flet ((return-from-eval-block (&rest values
)
905 (return-from eval-block
(values-list values
))))
906 (program-destructuring-bind (name &body body
) body
907 (unless (symbolp name
)
908 (ip-error "~@<The block name ~S is not a symbol.~:@>" name
))
910 :blocks
(list (cons name
#'return-from-eval-block
))
912 (eval-progn body env
)))))
914 (defun eval-return-from (body env
)
915 (program-destructuring-bind (name &optional result
) body
916 (let ((target (get-block-binding name env
)))
918 (multiple-value-call (cdr target
) (%eval result env
))
919 (ip-error "~@<Return for unknown block: ~S~:@>" name
)))))
921 (defun eval-the (body env
)
922 (program-destructuring-bind (value-type form
) body
923 (declare (ignore value-type
))
924 ;; FIXME: We should probably check the types here, even though
925 ;; the consequences of the values not being of the asserted types
926 ;; are formally undefined.
929 (defun eval-unwind-protect (body env
)
930 (program-destructuring-bind (protected-form &body cleanup-forms
) body
931 (unwind-protect (%eval protected-form env
)
932 (eval-progn cleanup-forms env
))))
934 (defun eval-throw (body env
)
935 (program-destructuring-bind (tag result-form
) body
936 (throw (%eval tag env
)
937 (%eval result-form env
))))
939 (defun eval-load-time-value (body env
)
940 (program-destructuring-bind (form &optional read-only-p
) body
941 (declare (ignore read-only-p
))
944 (defun eval-locally (body env
)
945 (multiple-value-bind (body documentation declarations
)
946 (parse-lambda-headers body
:doc-string-allowed nil
)
947 (declare (ignore documentation
))
948 (let* ((specials (declared-specials declarations
))
949 (new-env (if (or specials declarations
)
950 (make-env :parent env
951 :vars
(special-bindings specials env
)
952 :declarations declarations
)
954 (eval-progn body new-env
))))
956 (defun eval-args (args env
)
957 (mapcar #'(lambda (arg) (%eval arg env
)) args
))
959 ;;; The expansion of SB-SYS:WITH-PINNED-OBJECTS on GENCGC uses some
960 ;;; VOPs which can't be reasonably implemented in the interpreter. So
961 ;;; we special-case the macro.
962 (defun eval-with-pinned-objects (args env
)
963 (program-destructuring-bind (values &body body
) args
965 (eval-progn body env
)
966 (sb!sys
:with-pinned-objects
((car values
))
967 (eval-with-pinned-objects (cons (cdr values
) body
) env
)))))
969 (define-condition macroexpand-hook-type-error
(type-error)
971 (:report
(lambda (condition stream
)
972 (format stream
"The value of *MACROEXPAND-HOOK* is not a designator for a compiled function: ~A"
973 (type-error-datum condition
)))))
975 (defvar *eval-dispatch-functions
* nil
)
977 ;;; Dispatch to the appropriate EVAL-FOO function based on the contents of EXP.
978 (declaim (inline %%eval
))
979 (defun %%eval
(exp env
)
982 ;; CLHS 3.1.2.1.1 Symbols as Forms
983 (multiple-value-bind (value kind
) (get-variable exp env
)
986 (:expansion
(%eval value env
)))))
987 ;; CLHS 3.1.2.1.3 Self-Evaluating Objects
989 ;; CLHS 3.1.2.1.2 Conses as Forms
992 ;; CLHS 3.1.2.1.2.1 Special Forms
993 ((block) (eval-block (cdr exp
) env
))
994 ((catch) (eval-catch (cdr exp
) env
))
995 ((eval-when) (eval-eval-when (cdr exp
) env
))
996 ((flet) (eval-flet (cdr exp
) env
))
997 ((function) (eval-function (cdr exp
) env
))
998 ((go) (eval-go (cdr exp
) env
))
999 ((if) (eval-if (cdr exp
) env
))
1000 ((labels) (eval-labels (cdr exp
) env
))
1001 ((let) (eval-let (cdr exp
) env
))
1002 ((let*) (eval-let* (cdr exp
) env
))
1003 ((load-time-value) (eval-load-time-value (cdr exp
) env
))
1004 ((locally) (eval-locally (cdr exp
) env
))
1005 ((macrolet) (eval-macrolet (cdr exp
) env
))
1006 ((multiple-value-call) (eval-multiple-value-call (cdr exp
) env
))
1007 ((multiple-value-prog1) (eval-multiple-value-prog1 (cdr exp
) env
))
1008 ((progn) (eval-progn (cdr exp
) env
))
1009 ((progv) (eval-progv (cdr exp
) env
))
1010 ((quote) (eval-quote (cdr exp
) env
))
1011 ((return-from) (eval-return-from (cdr exp
) env
))
1012 ((setq) (eval-setq (cdr exp
) env
))
1013 ((symbol-macrolet) (eval-symbol-macrolet (cdr exp
) env
))
1014 ((tagbody) (eval-tagbody (cdr exp
) env
))
1015 ((the) (eval-the (cdr exp
) env
))
1016 ((throw) (eval-throw (cdr exp
) env
))
1017 ((unwind-protect) (eval-unwind-protect (cdr exp
) env
))
1019 ((truly-the) (eval-the (cdr exp
) env
))
1020 ;; Not a special form, but a macro whose expansion wouldn't be
1021 ;; handled correctly by the evaluator.
1022 ((sb!sys
:with-pinned-objects
) (eval-with-pinned-objects (cdr exp
) env
))
1024 (let ((dispatcher (getf *eval-dispatch-functions
* (car exp
))))
1027 (funcall dispatcher exp env
))
1028 ;; CLHS 3.1.2.1.2.4 Lambda Forms
1029 ((and (consp (car exp
)) (eq (caar exp
) 'lambda
))
1030 (interpreted-apply (eval-function (list (car exp
)) env
)
1031 (eval-args (cdr exp
) env
)))
1033 (multiple-value-bind (function kind
) (get-function (car exp
) env
)
1035 ;; CLHS 3.1.2.1.2.3 Function Forms
1036 (:function
(%apply function
(eval-args (cdr exp
) env
)))
1037 ;; CLHS 3.1.2.1.2.2 Macro Forms
1039 (let ((hook *macroexpand-hook
*))
1040 ;; Having an interpreted function as the
1041 ;; macroexpander hook could cause an infinite
1043 (unless (compiled-function-p
1046 (symbol (symbol-function hook
))))
1047 (error 'macroexpand-hook-type-error
1049 :expected-type
'compiled-function
))
1050 (%eval
(funcall hook
1053 (env-native-lexenv env
))
1056 (defun %eval
(exp env
)
1059 ;; Dynamically binding *EVAL-LEVEL* will prevent tail call
1060 ;; optimization. So only do it when its value will be used for
1061 ;; printing debug output.
1062 (let ((*eval-level
* (1+ *eval-level
*)))
1063 (let ((*print-circle
* t
))
1064 (format t
"~&~vA~S~%" *eval-level
* "" `(%eval
,exp
)))
1068 (defun %apply
(fun args
)
1070 (interpreted-function (interpreted-apply fun args
))
1071 (function (apply fun args
))
1072 (symbol (apply fun args
))))
1074 (defun interpreted-apply (fun args
)
1075 (let ((lambda-list (interpreted-function-lambda-list fun
))
1076 (env (interpreted-function-env fun
))
1077 (body (interpreted-function-body fun
))
1078 (declarations (interpreted-function-declarations fun
)))
1079 (call-with-new-env-full-parsing
1080 env lambda-list args declarations
1082 (eval-progn body env
)))))
1084 ;;; We need separate conditions for the different *-TOO-COMPLEX-ERRORs to
1085 ;;; avoid spuriously triggering the handler in EVAL-IN-NATIVE-ENVIRONMENT
1088 ;;; (let ((sb-ext:*evaluator-mode* :interpret))
1089 ;;; (let ((fun (eval '(let ((a 1)) (lambda () a)))))
1090 ;;; (eval `(compile nil ,fun))))
1092 ;;; FIXME: should these be exported?
1093 (define-condition interpreter-environment-too-complex-error
(simple-error)
1095 (define-condition compiler-environment-too-complex-error
(simple-error)
1098 ;;; Try to compile an interpreted function. If the environment
1099 ;;; contains local functions or lexical variables we'll punt on
1101 (defun prepare-for-compile (function)
1102 (let ((env (interpreted-function-env function
)))
1103 (when (or (env-tags env
)
1105 (find-if-not #'(lambda (x) (eq x
*macro
*))
1106 (env-funs env
) :key
#'cdr
)
1107 (find-if-not #'(lambda (x) (eq x
*symbol-macro
*))
1110 (error 'interpreter-environment-too-complex-error
1112 "~@<Lexical environment of ~S is too complex to compile.~:@>"
1116 `(sb!int
:named-lambda
,(interpreted-function-name function
)
1117 ,(interpreted-function-lambda-list function
)
1118 (declare ,@(interpreted-function-declarations function
))
1119 ,@(interpreted-function-body function
))
1120 (env-native-lexenv env
))))
1122 ;;; Convert a compiler LEXENV to an interpreter ENV. This is needed
1123 ;;; for EVAL-IN-LEXENV.
1124 (defun make-env-from-native-environment (lexenv)
1125 (let ((native-funs (sb!c
::lexenv-funs lexenv
))
1126 (native-vars (sb!c
::lexenv-vars lexenv
)))
1127 (flet ((is-macro (thing)
1128 (and (consp thing
) (eq (car thing
) 'sb
!sys
:macro
))))
1129 (when (or (sb!c
::lexenv-blocks lexenv
)
1130 (sb!c
::lexenv-cleanup lexenv
)
1131 (sb!c
::lexenv-lambda lexenv
)
1132 (sb!c
::lexenv-tags lexenv
)
1133 (sb!c
::lexenv-type-restrictions lexenv
)
1134 (find-if-not #'is-macro native-funs
:key
#'cdr
)
1135 (find-if-not #'is-macro native-vars
:key
#'cdr
))
1136 (error 'compiler-environment-too-complex-error
1138 "~@<Lexical environment is too complex to evaluate in: ~S~:@>"
1141 (flet ((make-binding (native)
1142 (cons (car native
) *symbol-macro
*))
1143 (make-sm-binding (native)
1144 (cons (car native
) (cddr native
)))
1145 (make-fbinding (native)
1146 (cons (car native
) *macro
*))
1147 (make-mbinding (native)
1148 (cons (car native
) (cddr native
))))
1150 (mapcar #'make-binding native-vars
)
1151 (mapcar #'make-fbinding native-funs
)
1152 (mapcar #'make-mbinding native-funs
)
1153 (mapcar #'make-sm-binding native-vars
)
1159 (defun eval-in-environment (form env
)
1162 (defun eval-in-native-environment (form lexenv
)
1164 ((sb!impl
::eval-error
1166 (error 'interpreted-program-error
1167 :condition
(sb!int
:encapsulated-condition condition
)
1169 (sb!c
:with-compiler-error-resignalling
1171 (let ((env (make-env-from-native-environment lexenv
)))
1173 (compiler-environment-too-complex-error (condition)
1174 (declare (ignore condition
))
1175 (sb!int
:style-warn
'lexical-environment-too-complex
1176 :form form
:lexenv lexenv
)
1177 (sb!int
:simple-eval-in-lexenv form lexenv
))))))