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