0.pre7.38:
[sbcl.git] / src / code / defmacro.lisp
1 ;;;; DEFMACRO machinery
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 ;;; the guts of the DEFMACRO macro, pulled out into a separate
15 ;;; function in order to make it easier to express the common 
16 ;;; bootstrap idiom
17 ;;;   CL:DEFMACRO SB!XC:DEFMACRO
18 ;;;   SB!XC:DEFMACRO CL:DEFMACRO
19 (eval-when (:compile-toplevel :load-toplevel :execute)
20   (defun %expander-for-defmacro (name lambda-list body)
21     (let ((whole (gensym "WHOLE-"))
22           (environment (gensym "ENV-")))
23       (multiple-value-bind (new-body local-decs doc)
24           (parse-defmacro lambda-list whole body name 'defmacro
25                           :environment environment)
26         (let ((def `(lambda (,whole ,environment)
27                       ,@local-decs
28                       (block ,name
29                         ,new-body))))
30           `(sb!c::%defmacro ',name #',def ',lambda-list ,doc))))))
31
32 ;;; Ordinarily this definition of SB!C:%DEFMACRO as an ordinary
33 ;;; function is not used: the parallel (but different) definition as
34 ;;; an IR1 transform takes precedence. However, this definition is
35 ;;; still useful in the target interpreter, and in the
36 ;;; cross-compilation host.
37 (defun sb!c::%defmacro (name definition lambda-list doc)
38   (declare (ignore lambda-list))
39   (sb!c::%%defmacro name definition doc))
40
41 ;;; (called by SB!C::%DEFMACRO)
42 (eval-when (:compile-toplevel :load-toplevel :execute)
43   (defun sb!c::%%defmacro (name definition doc)
44     ;; old note (ca. 1985, maybe:-): "Eventually %%DEFMACRO should
45     ;; deal with clearing old compiler information for the functional
46     ;; value."
47     (clear-info :function :where-from name)
48     ;; FIXME: It would be nice to warn about DEFMACRO of an
49     ;; already-defined macro, but that's slightly hard to do because
50     ;; in common usage DEFMACRO is defined at compile time and then
51     ;; redefined at load time. We'd need to make a distinction between
52     ;; the defined-at-compile-time state and the defined-at-load-time
53     ;; state to make this work. (Trying to warn about duplicate
54     ;; DEFTYPEs runs into the same problem.)
55     #+nil (when (sb!xc:macro-function name)
56             (style-warn "redefining ~S in DEFMACRO" name))
57     (setf (sb!xc:macro-function name) definition
58           (fdocumentation name 'function) doc)
59     name))
60
61 ;;; Parse the definition and make an expander function. The actual
62 ;;; definition is done by %DEFMACRO which we expand into, and which is
63 ;;; handled magically by an IR1 transform. After the compiler has
64 ;;; gotten the information it wants out of macro definition, it
65 ;;; compiles a call to %%DEFMACRO which happens at load time.
66 (defmacro sb!xc:defmacro (name lambda-list &rest body)
67   (%expander-for-defmacro name lambda-list body))
68
69 ;;; In the cross-compiler, we not only need to support the definition
70 ;;; of target macros at cross-compiler-build-time (with SB!XC:DEFMACRO
71 ;;; running in the cross-compilation host), we also need to support
72 ;;; the definition of target macros at target compilation time (with
73 ;;; CL:DEFMACRO processed by the cross-compiler)..
74 #+sb-xc-host
75 (sb!xc:defmacro defmacro (name lambda-list &rest body)
76   (%expander-for-defmacro name lambda-list body))
77
78 ;;; DEFMACRO-MUNDANELY is like SB!XC:DEFMACRO, except that it doesn't
79 ;;; have any EVAL-WHEN or IR1 magic associated with it, so it only
80 ;;; takes effect in :LOAD-TOPLEVEL or :EXECUTE situations.
81 (def!macro defmacro-mundanely (name lambda-list &body body)
82   (let ((whole (gensym "WHOLE-"))
83         (environment (gensym "ENVIRONMENT-")))
84     (multiple-value-bind (new-body local-decs doc)
85         (parse-defmacro lambda-list whole body name 'defmacro
86                         :environment environment)
87       `(progn
88          (setf (sb!xc:macro-function ',name)
89                (lambda (,whole ,environment)
90                    ,@local-decs
91                    (block ,name
92                    ,new-body)))
93          (setf (fdocumentation ',name 'macro)
94                ,doc)
95          ',name))))