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