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