0.6.10.15:
[sbcl.git] / src / code / defboot.lisp
1 ;;;; bootstrapping fundamental machinery (e.g. DEFUN, DEFCONSTANT,
2 ;;;; DEFVAR) from special forms and primitive functions
3 ;;;;
4 ;;;; KLUDGE: The bootstrapping aspect of this is now obsolete. It was
5 ;;;; originally intended that this file file would be loaded into a
6 ;;;; Lisp image which had Common Lisp primitives defined, and DEFMACRO
7 ;;;; defined, and little else. Since then that approach has been
8 ;;;; dropped and this file has been modified somewhat to make it work
9 ;;;; more cleanly when used to predefine macros at
10 ;;;; build-the-cross-compiler time.
11
12 ;;;; This software is part of the SBCL system. See the README file for
13 ;;;; more information.
14 ;;;;
15 ;;;; This software is derived from the CMU CL system, which was
16 ;;;; written at Carnegie Mellon University and released into the
17 ;;;; public domain. The software is in the public domain and is
18 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
19 ;;;; files for more information.
20
21 (in-package "SB!IMPL")
22 \f
23 ;;;; IN-PACKAGE
24
25 (defmacro-mundanely in-package (package-designator)
26   `(eval-when (:compile-toplevel :load-toplevel :execute)
27      (setq *package* (find-undeleted-package-or-lose ',package-designator))))
28 \f
29 ;;; MULTIPLE-VALUE-FOO
30
31 (defun list-of-symbols-p (x)
32   (and (listp x)
33        (every #'symbolp x)))
34
35 (defmacro-mundanely multiple-value-bind (vars value-form &body body)
36   (if (list-of-symbols-p vars)
37     ;; It's unclear why it would be important to special-case the LENGTH=1 case
38     ;; at this level, but the CMU CL code did it, so.. -- WHN 19990411
39     (if (= (length vars) 1)
40       `(let ((,(car vars) ,value-form))
41          ,@body)
42       (let ((ignore (gensym)))
43         `(multiple-value-call #'(lambda (&optional ,@vars &rest ,ignore)
44                                   (declare (ignore ,ignore))
45                                   ,@body)
46                               ,value-form)))
47     (error "Vars is not a list of symbols: ~S" vars)))
48
49 (defmacro-mundanely multiple-value-setq (vars value-form)
50   (cond ((null vars)
51          ;; The ANSI spec says that the primary value of VALUE-FORM must be
52          ;; returned. The general-case-handling code below doesn't do this
53          ;; correctly in the special case when there are no vars bound, so we
54          ;; handle this special case separately here.
55          (let ((g (gensym)))
56            `(multiple-value-bind (,g) ,value-form
57               ,g)))
58         ((list-of-symbols-p vars)
59          (let ((temps (make-gensym-list (length vars))))
60            `(multiple-value-bind ,temps ,value-form
61               ,@(mapcar #'(lambda (var temp)
62                             `(setq ,var ,temp))
63                         vars temps)
64               ,(car temps))))
65         (t (error "Vars is not a list of symbols: ~S" vars))))
66
67 (defmacro-mundanely multiple-value-list (value-form)
68   `(multiple-value-call #'list ,value-form))
69 \f
70 ;;;; various conditional constructs
71
72 ;;; COND defined in terms of IF
73 (defmacro-mundanely cond (&rest clauses)
74   (if (endp clauses)
75     nil
76     (let ((clause (first clauses)))
77       (if (atom clause)
78         (error "Cond clause is not a list: ~S" clause)
79         (let ((test (first clause))
80               (forms (rest clause)))
81           (if (endp forms)
82             (let ((n-result (gensym)))
83               `(let ((,n-result ,test))
84                  (if ,n-result
85                    ,n-result
86                    (cond ,@(rest clauses)))))
87             `(if ,test
88                (progn ,@forms)
89                (cond ,@(rest clauses)))))))))
90
91 ;;; other things defined in terms of COND
92 (defmacro-mundanely when (test &body forms)
93   #!+sb-doc
94   "If the first argument is true, the rest of the forms are
95   evaluated as a PROGN."
96   `(cond (,test nil ,@forms)))
97 (defmacro-mundanely unless (test &body forms)
98   #!+sb-doc
99   "If the first argument is not true, the rest of the forms are
100   evaluated as a PROGN."
101   `(cond ((not ,test) nil ,@forms)))
102 (defmacro-mundanely and (&rest forms)
103   (cond ((endp forms) t)
104         ((endp (rest forms)) (first forms))
105         (t
106          `(if ,(first forms)
107               (and ,@(rest forms))
108               nil))))
109 (defmacro-mundanely or (&rest forms)
110   (cond ((endp forms) nil)
111         ((endp (rest forms)) (first forms))
112         (t
113          (let ((n-result (gensym)))
114            `(let ((,n-result ,(first forms)))
115               (if ,n-result
116                   ,n-result
117                   (or ,@(rest forms))))))))
118 \f
119 ;;;; various sequencing constructs
120
121 (defmacro-mundanely prog (varlist &body body-decls)
122   (multiple-value-bind (body decls) (parse-body body-decls nil)
123     `(block nil
124        (let ,varlist
125          ,@decls
126          (tagbody ,@body)))))
127
128 (defmacro-mundanely prog* (varlist &body body-decls)
129   (multiple-value-bind (body decls) (parse-body body-decls nil)
130     `(block nil
131        (let* ,varlist
132          ,@decls
133          (tagbody ,@body)))))
134
135 (defmacro-mundanely prog1 (result &body body)
136   (let ((n-result (gensym)))
137     `(let ((,n-result ,result))
138        ,@body
139        ,n-result)))
140
141 (defmacro-mundanely prog2 (form1 result &body body)
142   `(prog1 (progn ,form1 ,result) ,@body))
143 \f
144 ;;; Now that we have the definition of MULTIPLE-VALUE-BIND, we can make a
145 ;;; reasonably readable definition of DEFUN.
146 ;;;
147 ;;; DEFUN expands into %DEFUN which is a function that is treated
148 ;;; magically by the compiler (through an IR1 transform) in order to
149 ;;; handle stuff like inlining. After the compiler has gotten the
150 ;;; information it wants out of macro definition, it compiles a call
151 ;;; to %%DEFUN which happens at load time.
152 (defmacro-mundanely defun (&whole whole name args &body body)
153   (multiple-value-bind (forms decls doc) (parse-body body)
154     (let ((def `(lambda ,args
155                   ,@decls
156                   (block ,(function-name-block-name name)
157                     ,@forms))))
158       `(sb!c::%defun ',name #',def ,doc ',whole))))
159 #+sb-xc-host (/show "before PROCLAIM" (sb!c::info :function :kind 'sb!c::%%defun))
160 #+sb-xc-host (sb!xc:proclaim '(ftype function sb!c::%%defun)) ; to avoid
161                                         ; undefined function warnings
162 #+sb-xc-host (/show "after PROCLAIM" (sb!c::info :function :kind 'sb!c::%%defun))
163 (defun sb!c::%%defun (name def doc &optional inline-expansion)
164   (when (fboundp name)
165     (style-warn "redefining ~S in DEFUN" name))
166   (setf (sb!xc:fdefinition name) def)
167   (when doc
168     ;; FIXME: This should use shared SETF-name parsing logic.
169     (if (and (consp name) (eq (first name) 'setf))
170         (setf (fdocumentation (second name) 'setf) doc)
171         (setf (fdocumentation name 'function) doc)))
172   (sb!c::proclaim-as-function-name name)
173   (if (eq (info :function :where-from name) :assumed)
174       (progn
175         (setf (info :function :where-from name) :defined)
176         (if (info :function :assumed-type name)
177             (setf (info :function :assumed-type name) nil))))
178   (when (or inline-expansion
179             (info :function :inline-expansion name))
180     (setf (info :function :inline-expansion name)
181           inline-expansion))
182   name)
183 ;;; Ordinarily this definition of SB!C:%DEFUN as an ordinary function is not
184 ;;; used: the parallel (but different) definition as an IR1 transform takes
185 ;;; precedence. However, it's still good to define this in order to keep the
186 ;;; interpreter happy. We define it here (instead of alongside the parallel
187 ;;; IR1 transform) because while the IR1 transform is needed and appropriate
188 ;;; in the cross-compiler running in the host Common Lisp, this parallel
189 ;;; ordinary function definition is only appropriate in the target Lisp.
190 (defun sb!c::%defun (name def doc source)
191   (declare (ignore source))
192   (setf (sb!eval:interpreted-function-name def) name)
193   (ecase (info :function :where-from name)
194     (:assumed
195       (setf (info :function :where-from name) :defined)
196       (setf (info :function :type name)
197               (extract-function-type def))
198       (when (info :function :assumed-type name)
199         (setf (info :function :assumed-type name) nil)))
200     (:declared)
201     (:defined
202         (setf (info :function :type name) (extract-function-type def))))
203   (sb!c::%%defun name def doc))
204 \f
205 ;;;; DEFVAR and DEFPARAMETER
206
207 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
208   #!+sb-doc
209   "For defining global variables at top level. Declares the variable
210   SPECIAL and, optionally, initializes it. If the variable already has a
211   value, the old value is not clobbered. The third argument is an optional
212   documentation string for the variable."
213   `(progn
214      (declaim (special ,var))
215      ,@(when valp
216          `((unless (boundp ',var)
217              (setq ,var ,val))))
218      ,@(when docp
219          `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
220      ',var))
221
222 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
223   #!+sb-doc
224   "Defines a parameter that is not normally changed by the program,
225   but that may be changed without causing an error. Declares the
226   variable special and sets its value to VAL. The third argument is
227   an optional documentation string for the parameter."
228   `(progn
229      (declaim (special ,var))
230      (setq ,var ,val)
231      ,@(when docp
232          ;; FIXME: The various FUNCALL #'(SETF FDOCUMENTATION) and
233          ;; other FUNCALL #'(SETF FOO) forms in the code should
234          ;; unbogobootstrapized back to ordinary SETF forms.
235          `((funcall #'(setf fdocumentation) ',doc ',var 'variable)))
236      ',var))
237 \f
238 ;;;; iteration constructs
239
240 ;;; (These macros are defined in terms of a function DO-DO-BODY which is also
241 ;;; used by SB!INT:DO-ANONYMOUS. Since these macros should not be loaded
242 ;;; on the cross-compilation host, but SB!INT:DO-ANONYMOUS and DO-DO-BODY
243 ;;; should be, these macros can't conveniently be in the same file as
244 ;;; DO-DO-BODY.)
245 (defmacro-mundanely do (varlist endlist &body body)
246   #!+sb-doc
247   "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
248   Iteration construct. Each Var is initialized in parallel to the value of the
249   specified Init form. On subsequent iterations, the Vars are assigned the
250   value of the Step form (if any) in parallel. The Test is evaluated before
251   each evaluation of the body Forms. When the Test is true, the Exit-Forms
252   are evaluated as a PROGN, with the result being the value of the DO. A block
253   named NIL is established around the entire expansion, allowing RETURN to be
254   used as an alternate exit mechanism."
255   (do-do-body varlist endlist body 'let 'psetq 'do nil))
256 (defmacro-mundanely do* (varlist endlist &body body)
257   #!+sb-doc
258   "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
259   Iteration construct. Each Var is initialized sequentially (like LET*) to the
260   value of the specified Init form. On subsequent iterations, the Vars are
261   sequentially assigned the value of the Step form (if any). The Test is
262   evaluated before each evaluation of the body Forms. When the Test is true,
263   the Exit-Forms are evaluated as a PROGN, with the result being the value
264   of the DO. A block named NIL is established around the entire expansion,
265   allowing RETURN to be used as an laternate exit mechanism."
266   (do-do-body varlist endlist body 'let* 'setq 'do* nil))
267
268 ;;; DOTIMES and DOLIST could be defined more concisely using destructuring
269 ;;; macro lambda lists or DESTRUCTURING-BIND, but then it'd be tricky to use
270 ;;; them before those things were defined. They're used enough times before
271 ;;; destructuring mechanisms are defined that it looks as though it's worth
272 ;;; just implementing them ASAP, at the cost of being unable to use the
273 ;;; standard destructuring mechanisms.
274 (defmacro-mundanely dotimes (var-count-result &body body)
275   (multiple-value-bind ; to roll our own destructuring
276       (var count result)
277       (apply (lambda (var count &optional (result nil))
278                (values var count result))
279              var-count-result)
280     (cond ((numberp count)
281            `(do ((,var 0 (1+ ,var)))
282                 ((>= ,var ,count) ,result)
283               (declare (type unsigned-byte ,var))
284               ,@body))
285           (t (let ((v1 (gensym)))
286                `(do ((,var 0 (1+ ,var)) (,v1 ,count))
287                     ((>= ,var ,v1) ,result)
288                   (declare (type unsigned-byte ,var))
289                   ,@body))))))
290 (defmacro-mundanely dolist (var-list-result &body body)
291   (multiple-value-bind ; to roll our own destructuring
292       (var list result)
293       (apply (lambda (var list &optional (result nil))
294                (values var list result))
295              var-list-result)
296     ;; We repeatedly bind the var instead of setting it so that we never have
297     ;; to give the var an arbitrary value such as NIL (which might conflict
298     ;; with a declaration). If there is a result form, we introduce a
299     ;; gratuitous binding of the variable to NIL w/o the declarations, then
300     ;; evaluate the result form in that environment. We spuriously reference
301     ;; the gratuitous variable, since we don't want to use IGNORABLE on what
302     ;; might be a special var.
303     (let ((n-list (gensym)))
304       `(do ((,n-list ,list (cdr ,n-list)))
305            ((endp ,n-list)
306             ,@(if result
307                 `((let ((,var nil))
308                     ,var
309                     ,result))
310                 '(nil)))
311          (let ((,var (car ,n-list)))
312            ,@body)))))
313 \f
314 ;;;; miscellaneous
315
316 (defmacro-mundanely return (&optional (value nil))
317   `(return-from nil ,value))
318
319 (defmacro-mundanely psetq (&rest pairs)
320   #!+sb-doc
321   "SETQ {var value}*
322    Set the variables to the values, like SETQ, except that assignments
323    happen in parallel, i.e. no assignments take place until all the
324    forms have been evaluated."
325   ;; (This macro is used in the definition of DO, so we can't use DO in the
326   ;; definition of this macro without getting into confusing bootstrap issues.)
327   (prog ((lets nil)
328          (setqs nil)
329          (pairs pairs))
330     :again
331     (when (atom (cdr pairs))
332       (return `(let ,(nreverse lets)
333                  (setq ,@(nreverse setqs))
334                  nil)))
335     (let ((gen (gensym)))
336       (setq lets (cons `(,gen ,(cadr pairs)) lets)
337             setqs (list* gen (car pairs) setqs)
338             pairs (cddr pairs)))
339     (go :again)))
340
341 (defmacro-mundanely lambda (&whole whole args &body body)
342   (declare (ignore args body))
343   `#',whole)