3 ;;;; This software is part of the SBCL system. See the README file for
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 ;;; general case of EVAL (except in that it can't handle toplevel
15 ;;; EVAL-WHEN magic properly): Delegate to the byte compiler.
17 (funcall (compile (gensym "EVAL-TMPFUN-")
20 ;; SPEED=0,DEBUG=1 => byte-compile
21 (declare (optimize (speed 0) (debug 1)))
23 ;; Other than that, basically we care about
24 ;; compilation speed, compilation speed, and
25 ;; compilation speed. (There are cases where
26 ;; the user wants something else, but we don't
27 ;; know enough to guess that; and if he is
28 ;; unhappy about our guessed emphasis, he
29 ;; should explicitly compile his code, with
30 ;; explicit declarations to tell us what to
32 (declare (optimize (space 1) (safety 1)))
33 (declare (optimize (compilation-speed 3)))
37 ;;; Handle PROGN and implicit PROGN.
38 (defun eval-progn-body (progn-body)
39 (unless (list-with-length-p progn-body)
40 (let ((*print-circle* t))
41 (error 'simple-program-error
43 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
44 :format-arguments (list progn-body))))
46 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
47 ;; need to take care to return all the values of the final EVAL.
48 ;; * It's left as an exercise to the reader to verify that this
49 ;; gives the right result when PROGN-BODY is NIL, because
50 ;; (FIRST NIL) = (REST NIL) = NIL.
51 (do* ((i progn-body rest-i)
52 (rest-i (rest i) (rest i)))
54 (if rest-i ; if not last element of list
56 (return (eval (first i))))))
58 ;;; Pick off a few easy cases, and the various top-level EVAL-WHEN
59 ;;; magical cases, and call %EVAL for the rest.
60 (defun eval (original-exp)
62 "Evaluate the argument in a null lexical environment, returning the
64 (declare (optimize (safety 1)))
65 (let ((exp (macroexpand original-exp)))
68 (ecase (info :variable :kind exp)
70 (values (info :variable :constant-value exp)))
73 ;; FIXME: This special case here is a symptom of non-ANSI
74 ;; weirdness in SBCL's ALIEN implementation, which could
75 ;; cause problems for e.g. code walkers. It'd probably be
76 ;; good to ANSIfy it by making alien variable accessors into
77 ;; ordinary forms, e.g. (SB-UNIX:ENV) and (SETF SB-UNIX:ENV),
78 ;; instead of magical symbols, e.g. plain SB-UNIX:ENV. Then
79 ;; if the old magical-symbol syntax is to be retained for
80 ;; compatibility, it can be implemented with
81 ;; DEFINE-SYMBOL-MACRO, keeping the code walkers happy.
83 (%eval original-exp))))
85 (let ((name (first exp))
86 (n-args (1- (length exp))))
90 (error "wrong number of args to FUNCTION:~% ~S" exp))
91 (let ((name (second exp)))
94 (eq (car name) 'setf)))
96 (%eval original-exp))))
99 (error "wrong number of args to QUOTE:~% ~S" exp))
102 (unless (evenp n-args)
103 (error "odd number of args to SETQ:~% ~S" exp))
104 (unless (zerop n-args)
105 (do ((name (cdr exp) (cddr name)))
107 (do ((args (cdr exp) (cddr args)))
109 ;; We duplicate the call to SET so that the
110 ;; correct value gets returned.
111 (set (first args) (eval (second args))))
112 (set (first args) (eval (second args)))))
113 (let ((symbol (first name)))
114 (case (info :variable :kind symbol)
115 ;; FIXME: I took out the *TOP-LEVEL-AUTO-DECLARE*
116 ;; test here, and removed the *TOP-LEVEL-AUTO-DECLARE*
117 ;; variable; the code should now act as though that
118 ;; variable is NIL. This should be tested..
120 (t (return (%eval original-exp))))))))
122 (eval-progn-body (rest exp)))
124 ;; FIXME: DESTRUCTURING-BIND returns
125 ;; DEFMACRO-LL-ARG-COUNT-ERROR instead of PROGRAM-ERROR
126 ;; when there's something wrong with the syntax here (e.g.
127 ;; missing SITUATIONS). This could be fixed by
128 ;; hand-crafting clauses to catch and report each
129 ;; possibility, but it would probably be cleaner to write
130 ;; a new macro DESTRUCTURING-BIND-PROGRAM-SYNTAX which
131 ;; does DESTRUCTURING-BIND and promotes any mismatch to
132 ;; PROGRAM-ERROR, then to use it here and in (probably
133 ;; dozens of) other places where the same problem arises.
134 (destructuring-bind (eval-when situations &rest body) exp
135 (declare (ignore eval-when))
136 (multiple-value-bind (ct lt e)
137 (sb!c:parse-eval-when-situations situations)
138 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
139 ;; the situation :EXECUTE (or EVAL) controls whether
140 ;; evaluation occurs for other EVAL-WHEN forms; that
141 ;; is, those that are not top level forms, or those in
142 ;; code processed by EVAL or COMPILE. If the :EXECUTE
143 ;; situation is specified in such a form, then the
144 ;; body forms are processed as an implicit PROGN;
145 ;; otherwise, the EVAL-WHEN form returns NIL.
146 (declare (ignore ct lt))
148 (eval-progn-body body)))))
150 (if (and (symbolp name)
151 (eq (info :function :kind name) :function))
153 (dolist (arg (rest exp))
155 (apply (symbol-function name) (args)))
156 (%eval original-exp))))))
160 ;;; Given a function, return three values:
161 ;;; 1] A lambda expression that could be used to define the function,
162 ;;; or NIL if the definition isn't available.
163 ;;; 2] NIL if the function was definitely defined in a null lexical
164 ;;; environment, and T otherwise.
165 ;;; 3] Some object that \"names\" the function. Although this is
166 ;;; allowed to be any object, CMU CL always returns a valid
167 ;;; function name or a string.
169 ;;; If interpreted, use the interpreter interface. Otherwise, see
170 ;;; whether it was compiled with COMPILE. If that fails, check for an
171 ;;; inline expansion.
172 (defun function-lambda-expression (fun)
173 (declare (type function fun))
174 (let* ((fun (%function-self fun))
175 (name (%function-name fun))
176 (code (sb!di::function-code-header fun))
177 (info (sb!kernel:%code-debug-info code)))
179 (let ((source (first (sb!c::compiled-debug-info-source info))))
180 (cond ((and (eq (sb!c::debug-source-from source) :lisp)
181 (eq (sb!c::debug-source-info source) fun))
182 (values (second (svref (sb!c::debug-source-name source) 0))
187 (let ((exp (info :function :inline-expansion name)))
189 (values exp nil name)
190 (values nil t name))))))
191 (values nil t name))))
193 ;;; miscellaneous full function definitions of things which are
194 ;;; ordinarily handled magically by the compiler
196 (defun apply (function arg &rest arguments)
198 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
199 the manner of LIST*. That is, a list is made of the values of all but the
200 last argument, appended to the value of the last argument, which must be a
202 (cond ((atom arguments)
203 (apply function arg))
204 ((atom (cdr arguments))
205 (apply function (cons arg (car arguments))))
206 (t (do* ((a1 arguments a2)
207 (a2 (cdr arguments) (cdr a2)))
210 (apply function (cons arg arguments)))))))
212 (defun funcall (function &rest arguments)
214 "Call FUNCTION with the given ARGUMENTS."
215 (apply function arguments))
217 (defun values (&rest values)
219 "Return all arguments, in order, as values."
220 (values-list values))
222 (defun values-list (list)
224 "Return all of the elements of LIST, in order, as values."