0.8.16.25:
[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 (#-sb-xc :compile-toplevel :load-toplevel :execute)
20   (defun %expander-for-defmacro (name lambda-list body)
21     (unless (symbolp name)
22       (error "The macro name ~S is not a symbol." name))
23     ;; When we are building the cross-compiler, we could be in a host
24     ;; lisp which implements CL macros (e.g. CL:AND) as special
25     ;; operators (while still providing a macroexpansion for
26     ;; compliance): therefore can't use the host's SPECIAL-OPERATOR-P
27     ;; as a discriminator, but that's OK because the set of forms the
28     ;; cross-compiler compiles is tightly controlled.  -- CSR,
29     ;; 2003-04-20
30     #-sb-xc-host
31     (when (special-operator-p name)
32       (error "The special operator ~S can't be redefined as a macro."
33              name))
34     (with-unique-names (whole environment)
35       (multiple-value-bind (new-body local-decs doc)
36           (parse-defmacro lambda-list whole body name 'defmacro
37                           :environment environment)
38         (let ((def `(lambda (,whole ,environment)
39                       ,@local-decs
40                       ,new-body))
41               ;; If we want to move over to list-style names
42               ;; [e.g. (DEFMACRO FOO), maybe to support some XREF-like
43               ;; functionality] here might be a good place to start.
44               (debug-name (sb!c::debug-namify "DEFMACRO " name)))
45           `(eval-when (:compile-toplevel :load-toplevel :execute)
46              (sb!c::%defmacro ',name #',def ',lambda-list ,doc ,debug-name)))))))
47
48 (macrolet
49     ((def (times set-p)
50        `(eval-when (,@times)
51           (defun sb!c::%defmacro (name definition lambda-list doc debug-name)
52             ;; old note (ca. 1985, maybe:-): "Eventually %%DEFMACRO
53             ;; should deal with clearing old compiler information for
54             ;; the functional value."
55             ,@(unless set-p
56                 '((declare (ignore lambda-list debug-name))))
57             (ecase (info :function :kind name)
58               ((nil))
59               (:function
60                ;; (remhash name *free-funs*)
61                (undefine-fun-name name)
62                (style-warn
63                 "~S is being redefined as a macro when it was ~
64                  previously ~(~A~) to be a function."
65                 name
66                 (info :function :where-from name)))
67               (:macro)
68               (:special-form
69                (error "The special form ~S can't be redefined as a macro."
70                       name)))
71             (clear-info :function :where-from name)
72             ;; FIXME: It would be nice to warn about DEFMACRO of an
73             ;; already-defined macro, but that's slightly hard to do
74             ;; because in common usage DEFMACRO is defined at compile
75             ;; time and then redefined at load time. We'd need to make a
76             ;; distinction between the defined-at-compile-time state and
77             ;; the defined-at-load-time state to make this work. (Trying
78             ;; to warn about duplicate DEFTYPEs runs into the same
79             ;; problem.)
80             #+nil (when (sb!xc:macro-function name)
81                     ;; Someday we could check for macro arguments
82                     ;; being incompatibly redefined. Doing this right
83                     ;; will involve finding the old macro lambda-list
84                     ;; and comparing it with the new one.
85                     (style-warn "redefining ~S in DEFMACRO" name))
86             (setf (sb!xc:macro-function name) definition
87                   (fdocumentation name 'function) doc)
88             ,(when set-p
89                    `(case (widetag-of definition)
90                       (#.sb!vm:closure-header-widetag
91                        (setf (%simple-fun-arglist (%closure-fun definition))
92                              lambda-list
93                              (%simple-fun-name (%closure-fun definition))
94                              debug-name))
95                       (#.sb!vm:simple-fun-header-widetag
96                        (setf (%simple-fun-arglist definition) lambda-list
97                              (%simple-fun-name definition) debug-name))))
98             name))))
99   (progn
100     (def (:load-toplevel :execute) #-sb-xc-host t #+sb-xc-host nil)
101     (def (#-sb-xc :compile-toplevel) nil)))
102
103 ;;; Parse the definition and make an expander function. The actual
104 ;;; definition is done by %DEFMACRO which we expand into. After the
105 ;;; compiler has gotten the information it wants out of macro
106 ;;; definition, it compiles a call to %DEFMACRO which happens at load
107 ;;; time.
108 (defmacro sb!xc:defmacro (name lambda-list &rest body)
109   (%expander-for-defmacro name lambda-list body))
110
111 ;;; In the cross-compiler, we not only need to support the definition
112 ;;; of target macros at cross-compiler-build-time (with SB!XC:DEFMACRO
113 ;;; running in the cross-compilation host), we also need to support
114 ;;; the definition of target macros at target compilation time (with
115 ;;; CL:DEFMACRO processed by the cross-compiler)..
116 #+sb-xc-host
117 (sb!xc:defmacro defmacro (name lambda-list &rest body)
118   (%expander-for-defmacro name lambda-list body))
119
120 ;;; DEFMACRO-MUNDANELY is like SB!XC:DEFMACRO, except that it doesn't
121 ;;; have any EVAL-WHEN or IR1 magic associated with it, so it only
122 ;;; takes effect in :LOAD-TOPLEVEL or :EXECUTE situations.
123 (def!macro defmacro-mundanely (name lambda-list &body body)
124
125   ;; old way:
126   ;;(let ((whole (gensym "WHOLE-"))
127   ;;      (environment (gensym "ENVIRONMENT-")))
128   ;;  (multiple-value-bind (new-body local-decs doc)
129   ;;      (parse-defmacro lambda-list whole body name 'defmacro
130   ;;                      :environment environment)
131   ;;    `(progn
132   ;;       (setf (sb!xc:macro-function ',name)
133   ;;             (lambda (,whole ,environment)
134   ;;                 ,@local-decs
135   ;;                 (block ,name
136   ;;                 ,new-body)))
137   ;;       (setf (fdocumentation ',name 'macro)
138   ;;             ,doc)
139   ;;       ',name)))
140
141   `(let ()
142      (sb!xc:defmacro ,name ,lambda-list ,@body)))