1 ;;;; DEFMACRO machinery
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 ;;; the guts of the DEFMACRO macro, pulled out into a separate
15 ;;; function in order to make it easier to express the common
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,
31 (when (special-operator-p name
)
32 (error "The special operator ~S can't be redefined as a macro."
34 ;; The name of the lambda is (MACRO-FUNCTION name)
35 ;; which does not conflict with any legal function name.
36 (let ((def (make-macro-lambda (sb!c
::debug-name
'macro-function name
)
37 lambda-list body
'defmacro name
)))
40 ;; Getting this to cross-compile with the check enabled
41 ;; would require %COMPILER-DEFMACRO to be defined earlier,
42 ;; but symmetry suggests it be near %COMPILER-DEFUN,
43 ;; which isn't soon enough. So leave it out.
44 (eval-when (:compile-toplevel
)
45 (sb!c
::%compiler-defmacro
:macro-function
',name t
))
46 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
47 (sb!c
::%defmacro
',name
,def
(sb!c
:source-location
)))))))
49 (eval-when (#-sb-xc
:compile-toplevel
:load-toplevel
:execute
)
50 (defun sb!c
::%defmacro
(name definition source-location
)
51 (declare (ignorable source-location
)) ; xc-host doesn't use
52 ;; old note (ca. 1985, maybe:-): "Eventually %%DEFMACRO
53 ;; should deal with clearing old compiler information for
54 ;; the functional value."
55 (let ((kind (info :function
:kind name
)))
56 ;; Check for special form before package locks.
57 (when (eq :special-form kind
)
58 (error "The special operator ~S can't be redefined as a macro."
60 (with-single-package-locked-error (:symbol name
"defining ~S as a macro")
61 (when (eq :function kind
)
63 "~S is being redefined as a macro when it was ~
64 previously ~(~A~) to be a function."
65 name
(info :function
:where-from name
))
66 (undefine-fun-name name
))
67 (clear-info :function
:where-from name
)
70 ;; Someday we could check for macro arguments
71 ;; being incompatibly redefined. Doing this right
72 ;; will involve finding the old macro lambda-list
73 ;; and comparing it with the new one.
74 (warn 'redefinition-with-defmacro
76 :new-function definition
77 :new-location source-location
))
78 (setf (sb!xc
:macro-function name
) definition
)))
81 ;;; Parse the definition and make an expander function. The actual
82 ;;; definition is done by %DEFMACRO which we expand into. After the
83 ;;; compiler has gotten the information it wants out of macro
84 ;;; definition, it compiles a call to %DEFMACRO which happens at load
86 (defmacro sb
!xc
:defmacro
(name lambda-list
&rest body
)
87 (%expander-for-defmacro name lambda-list body
))
89 ;;; In the cross-compiler, we not only need to support the definition
90 ;;; of target macros at cross-compiler-build-time (with SB!XC:DEFMACRO
91 ;;; running in the cross-compilation host), we also need to support
92 ;;; the definition of target macros at target compilation time (with
93 ;;; CL:DEFMACRO processed by the cross-compiler)..
95 (sb!xc
:defmacro defmacro
(name lambda-list
&rest body
)
96 (%expander-for-defmacro name lambda-list body
))
98 ;;; DEFMACRO-MUNDANELY is like SB!XC:DEFMACRO, except that it doesn't
99 ;;; have any EVAL-WHEN or IR1 magic associated with it, so it only
100 ;;; takes effect in :LOAD-TOPLEVEL or :EXECUTE situations.
101 (def!macro defmacro-mundanely
(name lambda-list
&body body
)
104 ;;(let ((whole (gensym "WHOLE-"))
105 ;; (environment (gensym "ENVIRONMENT-")))
106 ;; (multiple-value-bind (new-body local-decs doc)
107 ;; (parse-defmacro lambda-list whole body name 'defmacro
108 ;; :environment environment)
110 ;; (setf (sb!xc:macro-function ',name)
111 ;; (lambda (,whole ,environment)
115 ;; (setf (fdocumentation ',name 'macro)
120 (sb!xc
:defmacro
,name
,lambda-list
,@body
)))