1.0.22.22: (SETF FIND-CLASSOID) to drop DEFTYPE lambda-lists and source-locations
[sbcl/tcr.git] / src / code / eval.lisp
blobf639dd752cd2940012c86496fde3af978421a7e4
1 ;;;; EVAL and friends
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!IMPL")
14 (defparameter *eval-calls* 0)
16 (defun !eval-cold-init ()
17 (setf *eval-calls* 0
18 *evaluator-mode* :compile)
19 #!+sb-eval
20 (setf sb!eval::*eval-level* -1
21 sb!eval::*eval-verbose* nil))
23 ;;; general case of EVAL (except in that it can't handle toplevel
24 ;;; EVAL-WHEN magic properly): Delegate to #'COMPILE.
25 (defun %simple-eval (expr lexenv)
26 ;; FIXME: It might be nice to quieten the toplevel by muffling
27 ;; warnings generated by this compilation (since we're about to
28 ;; execute the results irrespective of the warnings). We might want
29 ;; to be careful about not muffling warnings arising from inner
30 ;; evaluations/compilations, though [e.g. the ignored variable in
31 ;; (DEFUN FOO (X) 1)]. -- CSR, 2003-05-13
33 ;; As of 1.0.21.6 we muffle compiler notes lexically here, which seems
34 ;; always safe. --NS
35 (let* (;; why PROGN? So that attempts to eval free declarations
36 ;; signal errors rather than return NIL. -- CSR, 2007-05-01
37 (lambda `(lambda ()
38 (declare (muffle-conditions compiler-note))
39 (progn ,expr)))
40 (fun (sb!c:compile-in-lexenv nil lambda lexenv)))
41 (funcall fun)))
43 ;;; Handle PROGN and implicit PROGN.
44 (defun simple-eval-progn-body (progn-body lexenv)
45 (unless (list-with-length-p progn-body)
46 (let ((*print-circle* t))
47 (error 'simple-program-error
48 :format-control
49 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
50 :format-arguments (list progn-body))))
51 ;; Note:
52 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
53 ;; need to take care to return all the values of the final EVAL.
54 ;; * It's left as an exercise to the reader to verify that this
55 ;; gives the right result when PROGN-BODY is NIL, because
56 ;; (FIRST NIL) = (REST NIL) = NIL.
57 (do* ((i progn-body rest-i)
58 (rest-i (rest i) (rest i)))
59 (nil)
60 (if rest-i ; if not last element of list
61 (simple-eval-in-lexenv (first i) lexenv)
62 (return (simple-eval-in-lexenv (first i) lexenv)))))
64 (defun simple-eval-locally (exp lexenv &key vars)
65 (multiple-value-bind (body decls)
66 (parse-body (rest exp) :doc-string-allowed nil)
67 (let ((lexenv
68 ;; KLUDGE: Uh, yeah. I'm not anticipating
69 ;; winning any prizes for this code, which was
70 ;; written on a "let's get it to work" basis.
71 ;; These seem to be the variables that need
72 ;; bindings for PROCESS-DECLS to work
73 ;; (*FREE-FUNS* and *FREE-VARS* so that
74 ;; references to free functions and variables
75 ;; in the declarations can be noted;
76 ;; *UNDEFINED-WARNINGS* so that warnings about
77 ;; undefined things can be accumulated [and
78 ;; then thrown away, as it happens]). -- CSR,
79 ;; 2002-10-24
80 (let* ((sb!c:*lexenv* lexenv)
81 (sb!c::*free-funs* (make-hash-table :test 'equal))
82 (sb!c::*free-vars* (make-hash-table :test 'eq))
83 (sb!c::*undefined-warnings* nil))
84 ;; FIXME: VALUES declaration
85 (sb!c::process-decls decls
86 vars
87 nil
88 :lexenv lexenv
89 :context :eval))))
90 (simple-eval-progn-body body lexenv))))
92 ;;;; EVAL-ERROR
93 ;;;;
94 ;;;; Analogous to COMPILER-ERROR, but simpler.
96 (define-condition eval-error (encapsulated-condition)
98 (:report (lambda (condition stream)
99 (print-object (encapsulated-condition condition) stream))))
101 (defun eval-error (condition)
102 (signal 'eval-error :condition condition)
103 (bug "Unhandled EVAL-ERROR"))
105 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
106 ;;; magical cases, and call %SIMPLE-EVAL for the rest.
107 (defun simple-eval-in-lexenv (original-exp lexenv)
108 (declare (optimize (safety 1)))
109 ;; (aver (lexenv-simple-p lexenv))
110 (incf *eval-calls*)
111 (handler-bind
112 ((sb!c:compiler-error
113 (lambda (c)
114 (if (boundp 'sb!c::*compiler-error-bailout*)
115 ;; if we're in the compiler, delegate either to a higher
116 ;; authority or, if that's us, back down to the
117 ;; outermost compiler handler...
118 (progn
119 (signal c)
120 nil)
121 ;; ... if we're not in the compiler, better signal the
122 ;; error straight away.
123 (invoke-restart 'sb!c::signal-error)))))
124 (let ((exp (macroexpand original-exp lexenv)))
125 (handler-bind ((eval-error
126 (lambda (condition)
127 (error 'interpreted-program-error
128 :condition (encapsulated-condition condition)
129 :form exp))))
130 (typecase exp
131 (symbol
132 (ecase (info :variable :kind exp)
133 ((:special :global :constant)
134 (symbol-value exp))
135 ;; FIXME: This special case here is a symptom of non-ANSI
136 ;; weirdness in SBCL's ALIEN implementation, which could
137 ;; cause problems for e.g. code walkers. It'd probably be
138 ;; good to ANSIfy it by making alien variable accessors
139 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
140 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
141 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
142 ;; be retained for compatibility, it can be implemented
143 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
144 ;; happy.
145 (:alien
146 (%simple-eval original-exp lexenv))))
147 (list
148 (let ((name (first exp))
149 (n-args (1- (length exp))))
150 (case name
151 ((function)
152 (unless (= n-args 1)
153 (error "wrong number of args to FUNCTION:~% ~S" exp))
154 (let ((name (second exp)))
155 (if (and (legal-fun-name-p name)
156 (not (consp (let ((sb!c:*lexenv* lexenv))
157 (sb!c:lexenv-find name funs)))))
158 (%coerce-name-to-fun name)
159 (%simple-eval original-exp lexenv))))
160 ((quote)
161 (unless (= n-args 1)
162 (error "wrong number of args to QUOTE:~% ~S" exp))
163 (second exp))
164 (setq
165 (unless (evenp n-args)
166 (error "odd number of args to SETQ:~% ~S" exp))
167 (unless (zerop n-args)
168 (do ((name (cdr exp) (cddr name)))
169 ((null name)
170 (do ((args (cdr exp) (cddr args)))
171 ((null (cddr args))
172 ;; We duplicate the call to SET so that the
173 ;; correct value gets returned.
174 (set (first args)
175 (simple-eval-in-lexenv (second args) lexenv)))
176 (set (first args)
177 (simple-eval-in-lexenv (second args) lexenv))))
178 (let ((symbol (first name)))
179 (case (info :variable :kind symbol)
180 (:special)
181 (t (return (%simple-eval original-exp lexenv))))
182 (unless (type= (info :variable :type symbol)
183 *universal-type*)
184 ;; let the compiler deal with type checking
185 (return (%simple-eval original-exp lexenv)))))))
186 ((progn)
187 (simple-eval-progn-body (rest exp) lexenv))
188 ((eval-when)
189 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
190 ;; instead of PROGRAM-ERROR when there's something wrong
191 ;; with the syntax here (e.g. missing SITUATIONS). This
192 ;; could be fixed by hand-crafting clauses to catch and
193 ;; report each possibility, but it would probably be
194 ;; cleaner to write a new macro
195 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
196 ;; DESTRUCTURING-BIND and promotes any mismatch to
197 ;; PROGRAM-ERROR, then to use it here and in (probably
198 ;; dozens of) other places where the same problem
199 ;; arises.
200 (destructuring-bind (eval-when situations &rest body) exp
201 (declare (ignore eval-when))
202 (multiple-value-bind (ct lt e)
203 (sb!c:parse-eval-when-situations situations)
204 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
205 ;; the situation :EXECUTE (or EVAL) controls whether
206 ;; evaluation occurs for other EVAL-WHEN forms; that
207 ;; is, those that are not top level forms, or those
208 ;; in code processed by EVAL or COMPILE. If the
209 ;; :EXECUTE situation is specified in such a form,
210 ;; then the body forms are processed as an implicit
211 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
212 (declare (ignore ct lt))
213 (when e
214 (simple-eval-progn-body body lexenv)))))
215 ((locally)
216 (simple-eval-locally exp lexenv))
217 ((macrolet)
218 (destructuring-bind (definitions &rest body)
219 (rest exp)
220 (let ((lexenv
221 (let ((sb!c:*lexenv* lexenv))
222 (sb!c::funcall-in-macrolet-lexenv
223 definitions
224 (lambda (&key funs)
225 (declare (ignore funs))
226 sb!c:*lexenv*)
227 :eval))))
228 (simple-eval-locally `(locally ,@body) lexenv))))
229 ((symbol-macrolet)
230 (destructuring-bind (definitions &rest body) (rest exp)
231 (multiple-value-bind (lexenv vars)
232 (let ((sb!c:*lexenv* lexenv))
233 (sb!c::funcall-in-symbol-macrolet-lexenv
234 definitions
235 (lambda (&key vars)
236 (values sb!c:*lexenv* vars))
237 :eval))
238 (simple-eval-locally `(locally ,@body) lexenv :vars vars))))
239 ((if)
240 (destructuring-bind (test then &optional else) (rest exp)
241 (eval-in-lexenv (if (eval-in-lexenv test lexenv)
242 then
243 else)
244 lexenv)))
245 ((let let*)
246 (destructuring-bind (definitions &rest body) (rest exp)
247 (if (null definitions)
248 (simple-eval-locally `(locally ,@body) lexenv)
249 (%simple-eval exp lexenv))))
251 (if (and (symbolp name)
252 (eq (info :function :kind name) :function))
253 (collect ((args))
254 (dolist (arg (rest exp))
255 (args (eval-in-lexenv arg lexenv)))
256 (apply (symbol-function name) (args)))
257 (%simple-eval exp lexenv))))))
259 exp))))))
261 (defun eval-in-lexenv (exp lexenv)
262 #!+sb-eval
263 (if (eq *evaluator-mode* :compile)
264 (simple-eval-in-lexenv exp lexenv)
265 (sb!eval:eval-in-native-environment exp lexenv))
266 #!-sb-eval
267 (simple-eval-in-lexenv exp lexenv))
269 (defun eval (original-exp)
270 #!+sb-doc
271 "Evaluate the argument in a null lexical environment, returning the
272 result or results."
273 (eval-in-lexenv original-exp (make-null-lexenv)))
276 ;;; miscellaneous full function definitions of things which are
277 ;;; ordinarily handled magically by the compiler
279 (defun apply (function arg &rest arguments)
280 #!+sb-doc
281 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
282 the manner of LIST*. That is, a list is made of the values of all but the
283 last argument, appended to the value of the last argument, which must be a
284 list."
285 (cond ((atom arguments)
286 (apply function arg))
287 ((atom (cdr arguments))
288 (apply function (cons arg (car arguments))))
289 (t (do* ((a1 arguments a2)
290 (a2 (cdr arguments) (cdr a2)))
291 ((atom (cdr a2))
292 (rplacd a1 (car a2))
293 (apply function (cons arg arguments)))))))
295 (defun funcall (function &rest arguments)
296 #!+sb-doc
297 "Call FUNCTION with the given ARGUMENTS."
298 (apply function arguments))
300 (defun values (&rest values)
301 #!+sb-doc
302 "Return all arguments, in order, as values."
303 (declare (truly-dynamic-extent values))
304 (values-list values))
306 (defun values-list (list)
307 #!+sb-doc
308 "Return all of the elements of LIST, in order, as values."
309 (values-list list))