ca43691e37327a7da5c45134a0f260c9fbf01acb
[sbcl.git] / src / code / full-eval.lisp
1 ;;;; An interpreting EVAL
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!EVAL")
13
14 ;; (declaim (optimize (speed 3) (debug 1) (safety 1)))
15
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"))
21
22 (define-condition interpreted-program-error (program-error simple-condition sb!impl::encapsulated-condition)
23   ()
24   (:report (lambda (condition stream)
25              (if (slot-boundp condition 'condition)
26                  (progn
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))))))
32
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
36                                            program-error)
37   ())
38
39 (defun arg-count-program-error (datum &rest arguments)
40   (declare (ignore datum))
41   (apply #'error 'arg-count-program-error arguments))
42
43 ;; OAOOM? (see destructuring-bind.lisp)
44 (defmacro program-destructuring-bind (lambda-list arg-list &body body)
45   (let ((arg-list-name (gensym "ARG-LIST-")))
46     (multiple-value-bind (body local-decls)
47         (sb!kernel:parse-defmacro lambda-list arg-list-name body nil
48                                   'program-destructuring-bind
49                                   :anonymousp t
50                                   :doc-string-allowed nil
51                                   :wrap-block nil
52                                   :error-fun 'arg-count-program-error)
53       `(let ((,arg-list-name ,arg-list))
54          ,@local-decls
55          ,body))))
56
57 (defun ip-error (format-control &rest format-arguments)
58   (error 'interpreted-program-error
59          :format-control format-control
60          :format-arguments format-arguments))
61
62 (defmacro nconc-2 (a b)
63   (let ((tmp (gensym))
64         (tmp2 (gensym)))
65     `(let ((,tmp ,a)
66            (,tmp2 ,b))
67        (if ,tmp
68            (progn (setf (cdr (last ,tmp)) ,tmp2) ,tmp)
69            ,tmp2))))
70
71 ;;; Construct a compiler LEXENV from the same data that's used for
72 ;;; creating an interpreter ENV. This is needed for example when
73 ;;; passing the environment to macroexpanders or when compiling an
74 ;;; interpreted function.
75 (defun fabricate-new-native-environment (old-lexenv new-funs new-expanders
76                                          new-vars new-symbol-expansions
77                                          declarations)
78   (labels ((to-native-funs (binding)
79              ;; Non-macroexpander function entries are irrelevant for
80              ;; the LEXENV. If we're using the LEXENV for
81              ;; macro-expansion any references to local non-macro
82              ;; function bindings are undefined behaviour. If we're
83              ;; compiling an interpreted function, a lexical environment
84              ;; with non-macro functions will be too hairy to compile.
85              (if (eq (cdr binding) *macro*)
86                  (cons (car binding)
87                        (cons 'sb!sys:macro
88                              (cdr (assoc (car binding) new-expanders))))
89                  (cons (car binding)
90                        :bogus)))
91            (to-native-vars (binding)
92              ;; And likewise for symbol macros.
93              (if (eq (cdr binding) *symbol-macro*)
94                  (cons (car binding)
95                        (cons 'sb!sys:macro
96                              (cdr (assoc (car binding) new-symbol-expansions))))
97                  (cons (car binding)
98                        :bogus))))
99     (let ((lexenv (sb!c::internal-make-lexenv
100                    (nconc-2 (mapcar #'to-native-funs new-funs)
101                             (sb!c::lexenv-funs old-lexenv))
102                    (nconc-2 (mapcar #'to-native-vars new-vars)
103                             (sb!c::lexenv-vars old-lexenv))
104                    nil nil nil nil nil
105                    (sb!c::lexenv-handled-conditions old-lexenv)
106                    (sb!c::lexenv-disabled-package-locks old-lexenv)
107                    (sb!c::lexenv-policy old-lexenv)
108                    (sb!c::lexenv-user-data old-lexenv))))
109       (dolist (declaration declarations)
110         (unless (consp declaration)
111           (ip-error "malformed declaration specifier ~S in ~S"
112                     declaration (cons 'declare declarations)))
113         (case (car declaration)
114           ((optimize)
115            (dolist (element (cdr declaration))
116              (multiple-value-bind (quality value)
117                  (if (not (consp element))
118                      (values element 3)
119                      (program-destructuring-bind (quality value)
120                          element
121                        (values quality value)))
122                (if (sb!c::policy-quality-name-p quality)
123                    (push (cons quality value)
124                          (sb!c::lexenv-%policy lexenv))
125                    (warn "ignoring unknown optimization quality ~
126                                       ~S in ~S" quality
127                                       (cons 'declare declarations))))))
128           (sb!ext:muffle-conditions
129            (setf (sb!c::lexenv-handled-conditions lexenv)
130                  (sb!c::process-muffle-conditions-decl
131                   declaration
132                   (sb!c::lexenv-handled-conditions lexenv))))
133           (sb!ext:unmuffle-conditions
134            (setf (sb!c::lexenv-handled-conditions lexenv)
135                  (sb!c::process-unmuffle-conditions-decl
136                   declaration
137                   (sb!c::lexenv-handled-conditions lexenv))))
138           ((sb!ext:disable-package-locks sb!ext:enable-package-locks)
139            (setf (sb!c::lexenv-disabled-package-locks lexenv)
140                  (sb!c::process-package-lock-decl
141                   declaration
142                   (sb!c::lexenv-disabled-package-locks lexenv))))))
143       lexenv)))
144
145 (defstruct (env
146              (:constructor %make-env
147                            (parent vars funs expanders symbol-expansions
148                             tags blocks declarations native-lexenv)))
149   parent
150   vars
151   funs
152   expanders
153   symbol-expansions
154   tags
155   blocks
156   declarations
157   native-lexenv)
158
159 (defun make-env (&key parent vars funs expanders
160                  symbol-expansions tags blocks declarations)
161   (%make-env parent
162              (append vars (env-vars parent))
163              (append funs (env-funs parent))
164              (append expanders (env-expanders parent))
165              (append symbol-expansions (env-symbol-expansions parent))
166              (nconc-2 tags (env-tags parent))
167              (nconc-2 blocks (env-blocks parent))
168              declarations
169              (fabricate-new-native-environment (env-native-lexenv parent)
170                                                funs expanders
171                                                vars symbol-expansions
172                                                declarations)))
173
174 (defun make-null-environment ()
175   (%make-env nil nil nil nil nil nil nil nil
176              (sb!c::internal-make-lexenv
177               nil nil
178               nil nil nil nil nil nil nil
179               sb!c::*policy*
180               nil)))
181
182 ;;; Augment ENV with a special or lexical variable binding
183 (declaim (inline push-var))
184 (defun push-var (name value env)
185   (push (cons name value) (env-vars env))
186   (push (cons name :bogus) (sb!c::lexenv-vars (env-native-lexenv env))))
187
188 ;;; Augment ENV with a local function binding
189 (declaim (inline push-fun))
190 (defun push-fun (name value calling-env body-env)
191   (when (fboundp name)
192     (let ((sb!c:*lexenv* (env-native-lexenv calling-env)))
193       (program-assert-symbol-home-package-unlocked
194        :eval name "binding ~A as a local function")))
195   (push (cons name value) (env-funs body-env))
196   (push (cons name :bogus) (sb!c::lexenv-funs (env-native-lexenv body-env))))
197
198 (sb!int:def!method print-object ((env env) stream)
199   (print-unreadable-object (env stream :type t :identity t)))
200
201 (macrolet ((define-get-binding (name accessor &key (test '#'eq))
202              ;; A macro, sadly, because an inline function here is
203              ;; "too hairy"
204              `(defmacro ,name (symbol env)
205                 `(assoc ,symbol (,',accessor ,env) :test ,',test))))
206   (define-get-binding get-binding env-vars)
207   (define-get-binding get-fbinding env-funs :test #'equal)
208   (define-get-binding get-expander-binding env-expanders)
209   (define-get-binding get-symbol-expansion-binding env-symbol-expansions)
210   (define-get-binding get-tag-binding env-tags :test #'eql)
211   (define-get-binding get-block-binding env-blocks))
212
213 ;;; Return a list of all symbols that are declared special in the
214 ;;; declarations listen in DECLS.
215 (defun declared-specials (decls)
216   (let ((specials nil))
217     (dolist (decl decls)
218       (when (eql (car decl) 'special)
219         (dolist (var (cdr decl))
220           (push var specials))))
221     specials))
222
223 ;;; Given a list of variables that should be marked as special in an
224 ;;; environment, return the appropriate binding forms to be given
225 ;;; to MAKE-ENV.
226 (defun special-bindings (specials env)
227   (mapcar #'(lambda (var)
228               (let ((sb!c:*lexenv* (env-native-lexenv env)))
229                 (program-assert-symbol-home-package-unlocked
230                  :eval var "declaring ~A special"))
231               (cons var *special*))
232           specials))
233
234 ;;; Return true if SYMBOL has been declared special either globally
235 ;;; or is in the DECLARED-SPECIALS list.
236 (defun specialp (symbol declared-specials)
237   (let ((type (sb!int:info :variable :kind symbol)))
238     (cond
239       ((eq type :constant)
240        ;; Horrible place for this, but it works.
241        (ip-error "Can't bind constant symbol: ~S" symbol))
242       ((eq type :global)
243        ;; Ditto...
244        (ip-error "Can't bind a global variable: ~S" symbol))
245       ((eq type :special) t)
246       ((member symbol declared-specials :test #'eq)
247        t)
248       (t nil))))
249
250 (defun binding-name (binding)
251   (if (consp binding) (first binding) binding))
252 (defun binding-value (binding)
253   (if (consp binding) (second binding) nil))
254 (defun supplied-p-parameter (spec)
255   (if (consp spec) (third spec) nil))
256 (defun keyword-name (spec)
257   (if (consp spec)
258       (if (consp (first spec))
259           (second (first spec))
260           (first spec))
261       spec))
262 (defun keyword-key (spec)
263   (if (consp spec)
264       (if (consp (first spec))
265           (first (first spec))
266           (intern (symbol-name (first spec)) "KEYWORD"))
267       (intern (symbol-name spec) "KEYWORD")))
268 (defun keyword-default-value (spec)
269   (if (consp spec) (second spec) nil))
270
271 ;;; Given a list of ARGUMENTS and a LAMBDA-LIST, return two values:
272 ;;;   * An alist[*] mapping the required parameters of the function to
273 ;;;     the corresponding argument values
274 ;;;   * An alist mapping the keyword, optional and rest parameters of
275 ;;;     the function to the corresponding argument values (if supplied)
276 ;;;     or to the parameter's default expression (if not). Supplied-p
277 ;;;     parameters and aux variables are handled in a similar manner.
278 ;;;
279 ;;; For example given the argument list of (1 2) and the lambda-list of
280 ;;; (A &OPTIONAL (B A) (C (1+ A))), we'd return the values
281 ;;; (A . '1) and ((B . '2) (C . (1+ A))).
282 ;;;
283 ;;; Used only for implementing calls to interpreted functions.
284 (defun parse-arguments (arguments lambda-list)
285   (multiple-value-bind (required optional rest-p rest keyword-p
286                         keyword allow-other-keys-p aux-p aux)
287       (handler-bind ((style-warning #'muffle-warning))
288         (sb!int:parse-lambda-list lambda-list))
289     (let* ((original-arguments arguments)
290            (arguments-present (length arguments))
291            (required-length (length required))
292            (optional-length (length optional))
293            (non-keyword-arguments (+ required-length optional-length))
294            (optionals-present (- (min non-keyword-arguments arguments-present)
295                                  required-length))
296            (keywords-present-p (> arguments-present non-keyword-arguments))
297            (let-like-bindings nil)
298            (let*-like-bindings nil))
299       (cond
300         ((< arguments-present required-length)
301          (ip-error "~@<Too few arguments in ~S to satisfy lambda list ~S.~:@>"
302                    arguments lambda-list))
303         ((and (not (or rest-p keyword-p)) keywords-present-p)
304          (ip-error "~@<Too many arguments in ~S to satisfy lambda list ~S.~:@>"
305                    arguments lambda-list))
306         ((and keyword-p keywords-present-p
307               (oddp (- arguments-present non-keyword-arguments)))
308          (ip-error "~@<Odd number of &KEY arguments in ~S for ~S.~:@>"
309                    arguments lambda-list)))
310       (dotimes (i required-length)
311         (push (cons (pop required) (pop arguments)) let-like-bindings))
312       (do ((optionals-parsed 0 (1+ optionals-parsed)))
313           ((null optional))
314         (let ((this-optional (pop optional))
315               (supplied-p (< optionals-parsed optionals-present)))
316           (push (cons (binding-name this-optional)
317                       (if supplied-p
318                           (list 'quote (pop arguments))
319                           (binding-value this-optional)))
320                 let*-like-bindings)
321           (when (supplied-p-parameter this-optional)
322             (push (cons (supplied-p-parameter this-optional)
323                         (list 'quote supplied-p))
324                   let*-like-bindings))))
325       (let ((keyword-plist arguments))
326         (when rest-p
327           (push (cons rest (list 'quote keyword-plist)) let*-like-bindings))
328         (when keyword-p
329           (unless (or allow-other-keys-p
330                       (getf keyword-plist :allow-other-keys))
331             (loop for (key value) on keyword-plist by #'cddr doing
332                   (when (and (not (eq key :allow-other-keys))
333                              (not (member key keyword :key #'keyword-key)))
334                     (ip-error "~@<Unknown &KEY argument ~S in ~S for ~S.~:@>"
335                               key original-arguments lambda-list))))
336           (dolist (keyword-spec keyword)
337             (let ((supplied (getf keyword-plist (keyword-key keyword-spec)
338                                   *not-present*)))
339               (push (cons (keyword-name keyword-spec)
340                           (if (eq supplied *not-present*)
341                               (keyword-default-value keyword-spec)
342                               (list 'quote supplied)))
343                     let*-like-bindings)
344               (when (supplied-p-parameter keyword-spec)
345                 (push (cons (supplied-p-parameter keyword-spec)
346                             (list 'quote (not (eq supplied *not-present*))))
347                       let*-like-bindings))))))
348       (when aux-p
349         (do ()
350             ((null aux))
351           (let ((this-aux (pop aux)))
352             (push (cons (binding-name this-aux)
353                         (binding-value this-aux))
354                   let*-like-bindings))))
355       (values (nreverse let-like-bindings) (nreverse let*-like-bindings)))))
356
357 ;;; Evaluate LET*-like (sequential) bindings.
358 ;;;
359 ;;; Given an alist of BINDINGS, evaluate the value form of the first
360 ;;; binding in ENV, bind the variable to the value in ENV, and then
361 ;;; evaluate the next binding form. Once all binding forms have been
362 ;;; handled, END-ACTION is funcalled.
363 ;;;
364 ;;; SPECIALS is a list of variables that have a bound special declaration.
365 ;;; These variables (and those that have been declaimed as special) are
366 ;;; bound as special variables.
367 (defun eval-next-let*-binding (bindings specials env end-action)
368   (flet ((maybe-eval (exp)
369            ;; Pick off the easy (QUOTE x) case which is very common
370            ;; due to function calls.  (see PARSE-ARGUMENTS)
371            (if (and (consp exp) (eq (car exp) 'quote))
372                (second exp)
373                (%eval exp env))))
374     (if bindings
375         (let* ((binding-name (car (car bindings)))
376                (binding-value (cdr (car bindings))))
377           (if (specialp binding-name specials)
378               (progv
379                   (list binding-name)
380                   (list (maybe-eval binding-value))
381                 ;; Mark the variable as special in this environment
382                 (push-var binding-name *special* env)
383                 (eval-next-let*-binding (cdr bindings)
384                                         specials env end-action))
385               (progn
386                 (push-var binding-name (maybe-eval binding-value) env)
387                 (eval-next-let*-binding (cdr bindings)
388                                         specials env end-action))))
389         (funcall end-action))))
390
391 ;;; Create a new environment based on OLD-ENV by adding the variable
392 ;;; bindings in BINDINGS to it, and call FUNCTION with the new environment
393 ;;; as the only parameter. DECLARATIONS are the declarations that were
394 ;;; in a source position where bound declarations for the bindings could
395 ;;; be introduced.
396 ;;;
397 ;;; FREE-SPECIALS-P controls whether all special declarations should
398 ;;; end cause the variables to be marked as special in the environment
399 ;;; (when true), or only bound declarations (when false). Basically
400 ;;; it'll be T when handling a LET, and NIL when handling a call to an
401 ;;; interpreted function.
402 (defun call-with-new-env (old-env bindings declarations
403                           free-specials-p function)
404   (let* ((specials (declared-specials declarations))
405          (dynamic-vars nil)
406          (dynamic-values nil))
407     ;; To check for package-lock violations
408     (special-bindings specials old-env)
409     (flet ((generate-binding (binding)
410              (if (specialp (car binding) specials)
411                  ;; If the variable being bound is globally special or
412                  ;; there's a bound special declaration for it, record it
413                  ;; in DYNAMIC-VARS / -VALUES separately:
414                  ;;   * To handle the case of FREE-SPECIALS-P == T more
415                  ;;     cleanly.
416                  ;;   * The dynamic variables will be bound with PROGV just
417                  ;;     before funcalling
418                  (progn
419                    (push (car binding) dynamic-vars)
420                    (push (cdr binding) dynamic-values)
421                    nil)
422                  ;; Otherwise it's a lexical binding, and the value
423                  ;; will be recorded in the environment.
424                  (list binding))))
425       (let ((new-env (make-env
426                       :parent old-env
427                       :vars (mapcan #'generate-binding bindings)
428                       :declarations declarations)))
429         (dolist (special (if free-specials-p specials dynamic-vars))
430           (push-var special *special* new-env))
431         (if dynamic-vars
432             (progv dynamic-vars dynamic-values
433               (funcall function new-env))
434             ;; When there are no specials, the PROGV would be a no-op,
435             ;; but it's better to elide it completely, since the
436             ;; funcall is then in tail position.
437             (funcall function new-env))))))
438
439 ;;; Create a new environment based on OLD-ENV by binding the argument
440 ;;; list ARGUMENTS to LAMBDA-LIST, and call FUNCTION with the new
441 ;;; environment as argument. DECLARATIONS are the declarations that
442 ;;; were in a source position where bound declarations for the
443 ;;; bindings could be introduced.
444 (defun call-with-new-env-full-parsing
445     (old-env lambda-list arguments declarations function)
446   (multiple-value-bind (let-like-bindings let*-like-binding)
447       (parse-arguments arguments lambda-list)
448     (let ((specials (declared-specials declarations))
449           var-specials free-specials)
450       ;; Separate the bound and free special declarations
451       (dolist (special specials)
452         (if (or (member special let-like-bindings :key #'car)
453                 (member special let*-like-binding :key #'car))
454             (push special var-specials)
455             (push special free-specials)))
456       ;; First introduce the required parameters into the environment
457       ;; with CALL-WITH-NEW-ENV
458       (call-with-new-env
459        old-env let-like-bindings declarations nil
460        #'(lambda (env)
461            ;; Then deal with optionals / keywords / etc.
462            (eval-next-let*-binding
463             let*-like-binding var-specials env
464             #'(lambda ()
465                 ;; And now that we have evaluated all the
466                 ;; initialization forms for the bindings, add the free
467                 ;; special declarations to the environment. To see why
468                 ;; this is the right thing to do (instead of passing
469                 ;; FREE-SPECIALS-P == T to CALL-WITH-NEW-ENV),
470                 ;; consider:
471                 ;;
472                 ;;   (eval '(let ((*a* 1))
473                 ;;     (declare (special *a*))
474                 ;;     (let ((*a* 2))
475                 ;;       (funcall (lambda (&optional (b *a*))
476                 ;;                  (declare (special *a*))
477                 ;;                  (values b *a*))))))
478                 ;;
479                 ;; *A* should be special in the body of the lambda, but
480                 ;; not when evaluating the default value of B.
481                 (dolist (special free-specials)
482                   (push-var special *special* env))
483                 (funcall function env))))))))
484
485 ;;; Set the VALUE of the binding (either lexical or special) of the
486 ;;; variable named by SYMBOL in the environment ENV.
487 (defun set-variable (symbol value env)
488   (let ((binding (get-binding symbol env)))
489     (if binding
490         (cond
491           ((eq (cdr binding) *special*)
492            (setf (symbol-value symbol) value))
493           ((eq (cdr binding) *symbol-macro*)
494            (error "Tried to set a symbol-macrolet!"))
495           (t (setf (cdr binding) value)))
496         (case (sb!int:info :variable :kind symbol)
497           (:macro (error "Tried to set a symbol-macrolet!"))
498           (:alien (let ((type (sb!int:info :variable :alien-info symbol)))
499                     (setf (sb!alien::%heap-alien type) value)))
500           (t
501            (let ((type (sb!c::info :variable :type symbol)))
502              (when type
503                (let ((type-specifier (sb!kernel:type-specifier type)))
504                  (unless (typep value type-specifier)
505                    (error 'type-error
506                           :datum value
507                           :expected-type type-specifier))))
508              (setf (symbol-value symbol) value)))))))
509
510 ;;; Retrieve the value of the binding (either lexical or special) of
511 ;;; the variable named by SYMBOL in the environment ENV. For symbol
512 ;;; macros the expansion is returned instead.
513 (defun get-variable (symbol env)
514   (let ((binding (get-binding symbol env)))
515     (if binding
516         (cond
517           ((eq (cdr binding) *special*)
518            (values (symbol-value symbol) :variable))
519           ((eq (cdr binding) *symbol-macro*)
520            (values (cdr (get-symbol-expansion-binding symbol env))
521                    :expansion))
522           (t (values (cdr binding) :variable)))
523         (case (sb!int:info :variable :kind symbol)
524           (:macro (values (macroexpand-1 symbol) :expansion))
525           (:alien (values (sb!alien-internals:alien-value symbol) :variable))
526           (t (values (symbol-value symbol) :variable))))))
527
528 ;;; Retrieve the function/macro binding of the symbol NAME in
529 ;;; environment ENV. The second return value will be :MACRO for macro
530 ;;; bindings, :FUNCTION for function bindings.
531 (defun get-function (name env)
532   (let ((binding (get-fbinding name env)))
533     (if binding
534         (cond
535           ((eq (cdr binding) *macro*)
536            (values (cdr (get-expander-binding name env)) :macro))
537           (t (values (cdr binding) :function)))
538         (cond
539           ((and (symbolp name) (macro-function name))
540            (values (macro-function name) :macro))
541           (t (values (%coerce-name-to-fun name) :function))))))
542
543 ;;; Return true if EXP is a lambda form.
544 (defun lambdap (exp)
545   (case (car exp)
546     ((lambda sb!int:named-lambda) t)))
547
548 ;;; Split off the declarations (and the docstring, if
549 ;;; DOC-STRING-ALLOWED is true) from the actual forms of BODY.
550 ;;; Returns three values: the cons in BODY containing the first
551 ;;; non-header subform, the docstring, and a list of the declarations.
552 ;;;
553 ;;; FIXME: The name of this function is somewhat misleading. It's not
554 ;;; used just for parsing the headers from lambda bodies, but for all
555 ;;; special forms that have attached declarations.
556 (defun parse-lambda-headers (body &key doc-string-allowed)
557   (loop with documentation = nil
558         with declarations = nil
559         for form on body do
560         (cond
561           ((and doc-string-allowed (stringp (car form)))
562            (if (cdr form)               ; CLHS 3.4.11
563                (if documentation
564                    (ip-error "~@<Duplicate doc string ~S.~:@>" (car form))
565                    (setf documentation (car form)))
566                (return (values form documentation declarations))))
567           ((and (consp (car form)) (eql (caar form) 'declare))
568            (setf declarations (append declarations (cdar form))))
569           (t (return (values form documentation declarations))))
570         finally (return (values nil documentation declarations))))
571
572 ;;; Create an interpreted function from the lambda-form EXP evaluated
573 ;;; in the environment ENV.
574 (defun eval-lambda (exp env)
575   (case (car exp)
576     ((lambda)
577      (multiple-value-bind (body documentation declarations)
578          (parse-lambda-headers (cddr exp) :doc-string-allowed t)
579        (make-interpreted-function :lambda-list (second exp)
580                                   :env env :body body
581                                   :documentation documentation
582                                   :source-location (sb!c::make-definition-source-location)
583                                   :declarations declarations)))
584     ((sb!int:named-lambda)
585      (multiple-value-bind (body documentation declarations)
586          (parse-lambda-headers (cdddr exp) :doc-string-allowed t)
587        (make-interpreted-function :name (second exp)
588                                   :lambda-list (third exp)
589                                   :env env :body body
590                                   :documentation documentation
591                                   :source-location (sb!c::make-definition-source-location)
592                                   :declarations declarations)))))
593
594 (defun eval-progn (body env)
595   (let ((previous-exp nil))
596     (dolist (exp body)
597       (if previous-exp
598           (%eval previous-exp env))
599       (setf previous-exp exp))
600     ;; Preserve tail call
601     (%eval previous-exp env)))
602
603 (defun eval-if (body env)
604   (program-destructuring-bind (test if-true &optional if-false) body
605     (if (%eval test env)
606         (%eval if-true env)
607         (%eval if-false env))))
608
609 (defun eval-let (body env)
610   (program-destructuring-bind (bindings &body body) body
611     ;; First evaluate the bindings in parallel
612     (let ((bindings (mapcar
613                      #'(lambda (binding)
614                          (cons (binding-name binding)
615                                (%eval (binding-value binding) env)))
616                      bindings)))
617       (multiple-value-bind (body documentation declarations)
618           (parse-lambda-headers body :doc-string-allowed nil)
619         (declare (ignore documentation))
620         ;; Then establish them into the environment, and evaluate the
621         ;; body.
622         (call-with-new-env env bindings declarations t
623                            #'(lambda (env)
624                                (eval-progn body env)))))))
625
626 (defun eval-let* (body old-env)
627   (program-destructuring-bind (bindings &body body) body
628     (multiple-value-bind (body documentation declarations)
629         (parse-lambda-headers body :doc-string-allowed nil)
630       (declare (ignore documentation))
631       ;; First we separate the special declarations into bound and
632       ;; free declarations.
633       (let ((specials (declared-specials declarations))
634             var-specials free-specials)
635         (dolist (special specials)
636           (if (member special bindings :key #'binding-name)
637               (push special var-specials)
638               (push special free-specials)))
639         (let ((env (make-env :parent old-env
640                              :declarations declarations)))
641           ;; Then we establish the bindings into the environment
642           ;; sequentially.
643           (eval-next-let*-binding
644            (mapcar #'(lambda (binding)
645                        (cons (binding-name binding)
646                              (binding-value binding)))
647                    bindings)
648            var-specials env
649            #'(lambda ()
650                ;; Now that we're done evaluating the bindings, add the
651                ;; free special declarations. See also
652                ;; CALL-WITH-NEW-ENV-FULL-PARSING.
653                (dolist (special free-specials)
654                  (push-var special *special* env))
655                (eval-progn body env))))))))
656
657 ;; Return a named local function in the environment ENV, made from the
658 ;; definition form FUNCTION-DEF.
659 (defun eval-local-function-def (function-def env)
660   (program-destructuring-bind (name lambda-list &body local-body) function-def
661     (multiple-value-bind (local-body documentation declarations)
662         (parse-lambda-headers local-body :doc-string-allowed t)
663       (%eval `#'(sb!int:named-lambda ,name ,lambda-list
664                   ,@(if documentation
665                         (list documentation)
666                         nil)
667                   (declare ,@declarations)
668                   (block ,(cond ((consp name) (second name))
669                                 (t name))
670                     ,@local-body))
671              env))))
672
673 (defun eval-flet (body env)
674   (program-destructuring-bind ((&rest local-functions) &body body) body
675     (multiple-value-bind (body documentation declarations)
676         (parse-lambda-headers body :doc-string-allowed nil)
677       (declare (ignore documentation))
678       (let* ((specials (declared-specials declarations))
679              (new-env (make-env :parent env
680                                 :vars (special-bindings specials env)
681                                 :declarations declarations)))
682         (dolist (function-def local-functions)
683           (push-fun (car function-def)
684                     ;; Evaluate the function definitions in ENV.
685                     (eval-local-function-def function-def env)
686                     ;; Do package-lock checks in ENV.
687                     env
688                     ;; But add the bindings to the child environment.
689                     new-env))
690         (eval-progn body new-env)))))
691
692 (defun eval-labels (body old-env)
693   (program-destructuring-bind ((&rest local-functions) &body body) body
694     (multiple-value-bind (body documentation declarations)
695         (parse-lambda-headers body :doc-string-allowed nil)
696       (declare (ignore documentation))
697       ;; Create a child environment, evaluate the function definitions
698       ;; in it, and add them into the same environment.
699       (let ((env (make-env :parent old-env
700                            :declarations declarations)))
701         (dolist (function-def local-functions)
702           (push-fun (car function-def)
703                     (eval-local-function-def function-def env)
704                     old-env
705                     env))
706         ;; And then add an environment for the body of the LABELS.  A
707         ;; separate environment from the one where we added the
708         ;; functions to is needed, since any special variable
709         ;; declarations need to be in effect in the body, but not in
710         ;; the bodies of the local functions.
711         (let* ((specials (declared-specials declarations))
712                (new-env (make-env :parent env
713                                   :vars (special-bindings specials env))))
714           (eval-progn body new-env))))))
715
716 ;; Return a local macro-expander in the environment ENV, made from the
717 ;; definition form FUNCTION-DEF.
718 (defun eval-local-macro-def (function-def env)
719   (program-destructuring-bind (name lambda-list &body local-body) function-def
720     (multiple-value-bind (local-body documentation declarations)
721         (parse-lambda-headers local-body :doc-string-allowed t)
722       ;; HAS-ENVIRONMENT and HAS-WHOLE will be either NIL or the name
723       ;; of the variable. (Better names?)
724       (let (has-environment has-whole)
725         ;; Filter out &WHOLE and &ENVIRONMENT from the lambda-list, and
726         ;; do some syntax checking.
727         (when (eq (car lambda-list) '&whole)
728           (setf has-whole (second lambda-list))
729           (setf lambda-list (cddr lambda-list)))
730         (setf lambda-list
731               (loop with skip = 0
732                     for element in lambda-list
733                     if (cond
734                          ((/= skip 0)
735                           (decf skip)
736                           (setf has-environment element)
737                           nil)
738                          ((eq element '&environment)
739                           (if has-environment
740                               (ip-error "Repeated &ENVIRONMENT.")
741                               (setf skip 1))
742                           nil)
743                          ((eq element '&whole)
744                           (ip-error "&WHOLE may only appear first ~
745                                      in MACROLET lambda-list."))
746                          (t t))
747                     collect element))
748         (let ((outer-whole (gensym "WHOLE"))
749               (environment (or has-environment (gensym "ENVIRONMENT")))
750               (macro-name (gensym "NAME")))
751           (%eval `#'(lambda (,outer-whole ,environment)
752                       ,@(if documentation
753                             (list documentation)
754                             nil)
755                       (declare ,@(unless has-environment
756                                          `((ignore ,environment))))
757                       (program-destructuring-bind
758                           (,@(if has-whole
759                                  (list '&whole has-whole)
760                                  nil)
761                              ,macro-name ,@lambda-list)
762                           ,outer-whole
763                         (declare (ignore ,macro-name)
764                                  ,@declarations)
765                         (block ,name ,@local-body)))
766                  env))))))
767
768 (defun eval-macrolet (body env)
769   (program-destructuring-bind ((&rest local-functions) &body body) body
770     (flet ((generate-fbinding (macro-def)
771              (cons (car macro-def) *macro*))
772            (generate-mbinding (macro-def)
773              (let ((name (car macro-def))
774                    (sb!c:*lexenv* (env-native-lexenv env)))
775                (when (fboundp name)
776                  (program-assert-symbol-home-package-unlocked
777                   :eval name "binding ~A as a local macro"))
778                (cons name (eval-local-macro-def macro-def env)))))
779       (multiple-value-bind (body documentation declarations)
780           (parse-lambda-headers body :doc-string-allowed nil)
781         (declare (ignore documentation))
782         (let* ((specials (declared-specials declarations))
783                (new-env (make-env :parent env
784                                   :vars (special-bindings specials env)
785                                   :funs (mapcar #'generate-fbinding
786                                                 local-functions)
787                                   :expanders (mapcar #'generate-mbinding
788                                                      local-functions)
789                                   :declarations declarations)))
790           (eval-progn body new-env))))))
791
792 (defun eval-symbol-macrolet (body env)
793   (program-destructuring-bind ((&rest bindings) &body body) body
794     (flet ((generate-binding (binding)
795              (cons (car binding) *symbol-macro*))
796            (generate-sm-binding (binding)
797              (let ((name (car binding))
798                    (sb!c:*lexenv* (env-native-lexenv env)))
799                (when (or (boundp name)
800                          (eq (sb!int:info :variable :kind name) :macro))
801                  (program-assert-symbol-home-package-unlocked
802                   :eval name "binding ~A as a local symbol-macro"))
803                (cons name (second binding)))))
804       (multiple-value-bind (body documentation declarations)
805           (parse-lambda-headers body :doc-string-allowed nil)
806         (declare (ignore documentation))
807         (let ((specials (declared-specials declarations)))
808           (dolist (binding bindings)
809             (when (specialp (binding-name binding) specials)
810               (ip-error "~@<Can't bind SYMBOL-MACROLET of special ~
811                          variable ~S.~:@>"
812                         (binding-name binding)))))
813         (let* ((specials (declared-specials declarations))
814                (new-env (make-env :parent env
815                                   :vars (nconc-2 (mapcar #'generate-binding
816                                                          bindings)
817                                                  (special-bindings specials env))
818                                   :symbol-expansions (mapcar
819                                                       #'generate-sm-binding
820                                                       bindings)
821                                   :declarations declarations)))
822           (eval-progn body new-env))))))
823
824 (defun eval-progv (body env)
825   (program-destructuring-bind (vars vals &body body) body
826     (progv (%eval vars env) (%eval vals env)
827       (eval-progn body env))))
828
829 (defun eval-function (body env)
830   (program-destructuring-bind (name) body
831     (cond
832       ;; LAMBDAP assumes that the argument is a cons, so we need the
833       ;; initial symbol case, instead of relying on the fall-through
834       ;; case that has the same function body.
835       ((symbolp name) (nth-value 0 (get-function name env)))
836       ((lambdap name) (eval-lambda name env))
837       (t (nth-value 0 (get-function name env))))))
838
839 (defun eval-eval-when (body env)
840   (program-destructuring-bind ((&rest situation) &body body) body
841     ;; FIXME: check that SITUATION only contains valid situations
842     (if (or (member :execute situation)
843             (member 'eval situation))
844         (eval-progn body env))))
845
846 (defun eval-quote (body env)
847   (declare (ignore env))
848   (program-destructuring-bind (object) body
849     object))
850
851 (defun eval-setq (pairs env)
852   (when (oddp (length pairs))
853     (ip-error "~@<Odd number of args to SETQ: ~S~:@>" (cons 'setq pairs)))
854   (let ((last nil))
855     (loop for (var new-val) on pairs by #'cddr do
856           (handler-case
857               (multiple-value-bind (expansion type) (get-variable var env)
858                 (ecase type
859                   (:expansion
860                    (setf last
861                          (%eval (list 'setf expansion new-val) env)))
862                   (:variable
863                    (setf last (set-variable var (%eval new-val env)
864                                             env)))))
865             (unbound-variable (c)
866               (declare (ignore c))
867               (setf last (setf (symbol-value var)
868                                (%eval new-val env))))))
869     last))
870
871 (defun eval-multiple-value-call (body env)
872   (program-destructuring-bind (function-form &body forms) body
873     (%apply (%eval function-form env)
874             (loop for form in forms
875                   nconc (multiple-value-list (%eval form env))))))
876
877 (defun eval-multiple-value-prog1 (body env)
878   (program-destructuring-bind (first-form &body forms) body
879     (multiple-value-prog1 (%eval first-form env)
880       (eval-progn forms env))))
881
882 (defun eval-catch (body env)
883   (program-destructuring-bind (tag &body forms) body
884     (catch (%eval tag env)
885       (eval-progn forms env))))
886
887 (defun eval-tagbody (body old-env)
888   (let ((env (make-env :parent old-env))
889         (tags nil)
890         (start body)
891         (target-tag nil))
892     (tagbody
893        (flet ((go-to-tag (tag)
894                 (setf target-tag tag)
895                 (go go-to-tag)))
896          ;; For each tag, store a trampoline function into the environment
897          ;; and the location in the body into the TAGS alist.
898          (do ((form body (cdr form)))
899              ((null form) nil)
900            (when (atom (car form))
901              (when (assoc (car form) tags)
902                (ip-error "The tag :A appears more than once in a tagbody."))
903              (push (cons (car form) (cdr form)) tags)
904              (push (cons (car form) #'go-to-tag) (env-tags env)))))
905        ;; And then evaluate the forms in the body, starting from the
906        ;; first one.
907        (go execute)
908      go-to-tag
909        ;; The trampoline has set the TARGET-TAG. Restart evaluation of
910        ;; the body from the location in body that matches the tag.
911        (setf start (cdr (assoc target-tag tags)))
912      execute
913        (dolist (form start)
914          (when (not (atom form))
915            (%eval form env))))))
916
917 (defun eval-go (body env)
918   (program-destructuring-bind (tag) body
919     (let ((target (get-tag-binding tag env)))
920       (if target
921           ;; Call the GO-TO-TAG trampoline
922           (funcall (cdr target) tag)
923           (ip-error "~@<Attempt to GO to nonexistent tag: ~S~:@>" tag)))))
924
925 (defun eval-block (body old-env)
926   (flet ((return-from-eval-block (&rest values)
927            (return-from eval-block (values-list values))))
928     (program-destructuring-bind (name &body body) body
929       (unless (symbolp name)
930         (ip-error "~@<The block name ~S is not a symbol.~:@>" name))
931       (let ((env (make-env
932                   :blocks (list (cons name #'return-from-eval-block))
933                   :parent old-env)))
934         (eval-progn body env)))))
935
936 (defun eval-return-from (body env)
937   (program-destructuring-bind (name &optional result) body
938     (let ((target (get-block-binding name env)))
939       (if target
940           (multiple-value-call (cdr target) (%eval result env))
941           (ip-error "~@<Return for unknown block: ~S~:@>" name)))))
942
943 (defun eval-the (body env)
944   (program-destructuring-bind (value-type form) body
945     (declare (ignore value-type))
946     ;; FIXME: We should probably check the types here, even though
947     ;; the consequences of the values not being of the asserted types
948     ;; are formally undefined.
949     (%eval form env)))
950
951 (defun eval-unwind-protect (body env)
952   (program-destructuring-bind (protected-form &body cleanup-forms) body
953     (unwind-protect (%eval protected-form env)
954       (eval-progn cleanup-forms env))))
955
956 (defun eval-throw (body env)
957   (program-destructuring-bind (tag result-form) body
958     (throw (%eval tag env)
959       (%eval result-form env))))
960
961 (defun eval-load-time-value (body env)
962   (program-destructuring-bind (form &optional read-only-p) body
963     (declare (ignore read-only-p))
964     (%eval form env)))
965
966 (defun eval-locally (body env)
967   (multiple-value-bind (body documentation declarations)
968       (parse-lambda-headers body :doc-string-allowed nil)
969     (declare (ignore documentation))
970     (let* ((specials (declared-specials declarations))
971            (new-env (if (or specials declarations)
972                         (make-env :parent env
973                                   :vars (special-bindings specials env)
974                                   :declarations declarations)
975                         env)))
976       (eval-progn body new-env))))
977
978 (defun eval-args (args env)
979   (mapcar #'(lambda (arg) (%eval arg env)) args))
980
981 ;;; The expansion of SB-SYS:WITH-PINNED-OBJECTS on GENCGC uses some
982 ;;; VOPs which can't be reasonably implemented in the interpreter. So
983 ;;; we special-case the macro.
984 (defun eval-with-pinned-objects (args env)
985   (program-destructuring-bind (values &body body) args
986     (if (null values)
987         (eval-progn body env)
988         (sb!sys:with-pinned-objects ((car values))
989           (eval-with-pinned-objects (cons (cdr values) body) env)))))
990
991 (define-condition macroexpand-hook-type-error (type-error)
992   ()
993   (:report (lambda (condition stream)
994              (format stream "The value of *MACROEXPAND-HOOK* is not a designator for a compiled function: ~A"
995                      (type-error-datum condition)))))
996
997 (defvar *eval-dispatch-functions* nil)
998
999 ;;; Dispatch to the appropriate EVAL-FOO function based on the contents of EXP.
1000 (declaim (inline %%eval))
1001 (defun %%eval (exp env)
1002   (cond
1003     ((symbolp exp)
1004      ;; CLHS 3.1.2.1.1 Symbols as Forms
1005      (multiple-value-bind (value kind) (get-variable exp env)
1006        (ecase kind
1007          (:variable value)
1008          (:expansion (%eval value env)))))
1009     ;; CLHS 3.1.2.1.3 Self-Evaluating Objects
1010     ((atom exp) exp)
1011     ;; CLHS 3.1.2.1.2 Conses as Forms
1012     ((consp exp)
1013      (case (car exp)
1014        ;; CLHS 3.1.2.1.2.1 Special Forms
1015        ((block)                (eval-block (cdr exp) env))
1016        ((catch)                (eval-catch (cdr exp) env))
1017        ((eval-when)            (eval-eval-when (cdr exp) env))
1018        ((flet)                 (eval-flet (cdr exp) env))
1019        ((function)             (eval-function (cdr exp) env))
1020        ((go)                   (eval-go (cdr exp) env))
1021        ((if)                   (eval-if (cdr exp) env))
1022        ((labels)               (eval-labels (cdr exp) env))
1023        ((let)                  (eval-let (cdr exp) env))
1024        ((let*)                 (eval-let* (cdr exp) env))
1025        ((load-time-value)      (eval-load-time-value (cdr exp) env))
1026        ((locally)              (eval-locally (cdr exp) env))
1027        ((macrolet)             (eval-macrolet (cdr exp) env))
1028        ((multiple-value-call)  (eval-multiple-value-call (cdr exp) env))
1029        ((multiple-value-prog1) (eval-multiple-value-prog1 (cdr exp) env))
1030        ((progn)                (eval-progn (cdr exp) env))
1031        ((progv)                (eval-progv (cdr exp) env))
1032        ((quote)                (eval-quote (cdr exp) env))
1033        ((return-from)          (eval-return-from (cdr exp) env))
1034        ((setq)                 (eval-setq (cdr exp) env))
1035        ((symbol-macrolet)      (eval-symbol-macrolet (cdr exp) env))
1036        ((tagbody)              (eval-tagbody (cdr exp) env))
1037        ((the)                  (eval-the (cdr exp) env))
1038        ((throw)                (eval-throw (cdr exp) env))
1039        ((unwind-protect)       (eval-unwind-protect (cdr exp) env))
1040        ;; SBCL-specific:
1041        ((sb!ext:truly-the)     (eval-the (cdr exp) env))
1042        ;; Not a special form, but a macro whose expansion wouldn't be
1043        ;; handled correctly by the evaluator.
1044        ((sb!sys:with-pinned-objects) (eval-with-pinned-objects (cdr exp) env))
1045        (t
1046         (let ((dispatcher (getf *eval-dispatch-functions* (car exp))))
1047           (cond
1048             (dispatcher
1049              (funcall dispatcher exp env))
1050             ;; CLHS 3.1.2.1.2.4 Lambda Forms
1051             ((and (consp (car exp)) (eq (caar exp) 'lambda))
1052              (interpreted-apply (eval-function (list (car exp)) env)
1053                                 (eval-args (cdr exp) env)))
1054             (t
1055              (multiple-value-bind (function kind) (get-function (car exp) env)
1056                (ecase kind
1057                  ;; CLHS 3.1.2.1.2.3 Function Forms
1058                  (:function (%apply function (eval-args (cdr exp) env)))
1059                  ;; CLHS 3.1.2.1.2.2 Macro Forms
1060                  (:macro
1061                   (let ((hook *macroexpand-hook*))
1062                     ;; Having an interpreted function as the
1063                     ;; macroexpander hook could cause an infinite
1064                     ;; loop.
1065                     (unless (compiled-function-p
1066                              (etypecase hook
1067                                (function hook)
1068                                (symbol (symbol-function hook))))
1069                       (error 'macroexpand-hook-type-error
1070                              :datum hook
1071                              :expected-type 'compiled-function))
1072                     (%eval (funcall hook
1073                                     function
1074                                     exp
1075                                     (env-native-lexenv env))
1076                            env)))))))))))))
1077
1078 (defun %eval (exp env)
1079   (incf *eval-calls*)
1080   (if *eval-verbose*
1081       ;; Dynamically binding *EVAL-LEVEL* will prevent tail call
1082       ;; optimization. So only do it when its value will be used for
1083       ;; printing debug output.
1084       (let ((*eval-level* (1+ *eval-level*)))
1085         (let ((*print-circle* t))
1086           (format t "~&~vA~S~%" *eval-level* "" `(%eval ,exp)))
1087         (%%eval exp env))
1088       (%%eval exp env)))
1089
1090 (defun %apply (fun args)
1091   (etypecase fun
1092     (interpreted-function (interpreted-apply fun args))
1093     (function (apply fun args))
1094     (symbol (apply fun args))))
1095
1096 (defun interpreted-apply (fun args)
1097   (let ((lambda-list (interpreted-function-lambda-list fun))
1098         (env (interpreted-function-env fun))
1099         (body (interpreted-function-body fun))
1100         (declarations (interpreted-function-declarations fun)))
1101     (call-with-new-env-full-parsing
1102      env lambda-list args declarations
1103      #'(lambda (env)
1104          (eval-progn body env)))))
1105
1106 ;;; We need separate conditions for the different *-TOO-COMPLEX-ERRORs to
1107 ;;; avoid spuriously triggering the handler in EVAL-IN-NATIVE-ENVIRONMENT
1108 ;;; on code like:
1109 ;;;
1110 ;;;   (let ((sb-ext:*evaluator-mode* :interpret))
1111 ;;;     (let ((fun (eval '(let ((a 1)) (lambda () a)))))
1112 ;;;         (eval `(compile nil ,fun))))
1113 ;;;
1114 ;;; FIXME: should these be exported?
1115 (define-condition interpreter-environment-too-complex-error (simple-error)
1116   ())
1117 (define-condition compiler-environment-too-complex-error (simple-error)
1118   ())
1119
1120 ;;; Try to compile an interpreted function. If the environment
1121 ;;; contains local functions or lexical variables we'll punt on
1122 ;;; compiling it.
1123 (defun prepare-for-compile (function)
1124   (let ((env (interpreted-function-env function)))
1125     (when (or (env-tags env)
1126               (env-blocks env)
1127               (find-if-not #'(lambda (x) (eq x *macro*))
1128                            (env-funs env) :key #'cdr)
1129               (find-if-not #'(lambda (x) (eq x *symbol-macro*))
1130                            (env-vars env)
1131                            :key #'cdr))
1132       (error 'interpreter-environment-too-complex-error
1133              :format-control
1134              "~@<Lexical environment of ~S is too complex to compile.~:@>"
1135              :format-arguments
1136              (list function)))
1137     (values
1138      `(sb!int:named-lambda ,(interpreted-function-name function)
1139           ,(interpreted-function-lambda-list function)
1140         (declare ,@(interpreted-function-declarations function))
1141         ,@(interpreted-function-body function))
1142      (env-native-lexenv env))))
1143
1144 ;;; Convert a compiler LEXENV to an interpreter ENV. This is needed
1145 ;;; for EVAL-IN-LEXENV.
1146 (defun make-env-from-native-environment (lexenv)
1147   (let ((native-funs (sb!c::lexenv-funs lexenv))
1148         (native-vars (sb!c::lexenv-vars lexenv)))
1149     (flet ((is-macro (thing)
1150              (and (consp thing) (eq (car thing) 'sb!sys:macro))))
1151       (when (or (sb!c::lexenv-blocks lexenv)
1152                 (sb!c::lexenv-cleanup lexenv)
1153                 (sb!c::lexenv-lambda lexenv)
1154                 (sb!c::lexenv-tags lexenv)
1155                 (sb!c::lexenv-type-restrictions lexenv)
1156                 (find-if-not #'is-macro native-funs :key #'cdr)
1157                 (find-if-not #'is-macro native-vars :key #'cdr))
1158         (error 'compiler-environment-too-complex-error
1159                :format-control
1160                "~@<Lexical environment is too complex to evaluate in: ~S~:@>"
1161                :format-arguments
1162                (list lexenv))))
1163     (flet ((make-binding (native)
1164              (cons (car native) *symbol-macro*))
1165            (make-sm-binding (native)
1166              (cons (car native) (cddr native)))
1167            (make-fbinding (native)
1168              (cons (car native) *macro*))
1169            (make-mbinding (native)
1170              (cons (car native) (cddr native))))
1171       (%make-env nil
1172                  (mapcar #'make-binding native-vars)
1173                  (mapcar #'make-fbinding native-funs)
1174                  (mapcar #'make-mbinding native-funs)
1175                  (mapcar #'make-sm-binding native-vars)
1176                  nil
1177                  nil
1178                  nil
1179                  lexenv))))
1180
1181 (defun eval-in-environment (form env)
1182   (%eval form env))
1183
1184 (defun eval-in-native-environment (form lexenv)
1185   (handler-bind
1186       ((sb!impl::eval-error
1187          (lambda (condition)
1188            (error 'interpreted-program-error
1189                   :condition (sb!int:encapsulated-condition condition)
1190                   :form form))))
1191     (sb!c:with-compiler-error-resignalling
1192       (handler-case
1193           (let ((env (make-env-from-native-environment lexenv)))
1194             (%eval form env))
1195         (compiler-environment-too-complex-error (condition)
1196           (declare (ignore condition))
1197           (sb!int:style-warn 'sb!kernel:lexical-environment-too-complex
1198                              :form form :lexenv lexenv)
1199           (sb!int:simple-eval-in-lexenv form lexenv))))))