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