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