Change all but 1 last use of PARSE-DEFMACRO to MAKE-MACRO-LAMBDA.
[sbcl.git] / src / code / defmacro.lisp
blob49557bec0b092c468a784c056ecbf44a66ba756e
1 ;;;; DEFMACRO machinery
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.
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
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 ;; 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)))
38 `(progn
39 #-sb-xc-host
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 ',lambda-list
48 (sb!c:source-location)))))))
50 (macrolet
51 ((def (times set-p)
52 `(eval-when (,@times)
53 (defun sb!c::%defmacro (name definition lambda-list source-location)
54 (declare (ignorable source-location)) ; xc-host doesn't use
55 ;; old note (ca. 1985, maybe:-): "Eventually %%DEFMACRO
56 ;; should deal with clearing old compiler information for
57 ;; the functional value."
58 ,@(unless set-p
59 '((declare (ignore lambda-list))))
60 (let ((kind (info :function :kind name)))
61 ;; Check for special form before package locks.
62 (when (eq :special-form kind)
63 (error "The special operator ~S can't be redefined as a macro."
64 name))
65 (with-single-package-locked-error (:symbol name "defining ~S as a macro")
66 (when (eq :function kind)
67 (style-warn
68 "~S is being redefined as a macro when it was ~
69 previously ~(~A~) to be a function."
70 name (info :function :where-from name))
71 (undefine-fun-name name))
72 (clear-info :function :where-from name)
73 #-sb-xc-host
74 (when (fboundp name)
75 ;; Someday we could check for macro arguments
76 ;; being incompatibly redefined. Doing this right
77 ;; will involve finding the old macro lambda-list
78 ;; and comparing it with the new one.
79 (warn 'redefinition-with-defmacro
80 :name name
81 :new-function definition
82 :new-location source-location))
83 (setf (sb!xc:macro-function name) definition)
84 ,(when set-p
85 `(setf (%fun-lambda-list definition) lambda-list))))
86 name))))
87 (progn
88 (def (:load-toplevel :execute) #-sb-xc-host t #+sb-xc-host nil)
89 (def (#-sb-xc :compile-toplevel) nil)))
91 ;;; Parse the definition and make an expander function. The actual
92 ;;; definition is done by %DEFMACRO which we expand into. After the
93 ;;; compiler has gotten the information it wants out of macro
94 ;;; definition, it compiles a call to %DEFMACRO which happens at load
95 ;;; time.
96 (defmacro sb!xc:defmacro (name lambda-list &rest body)
97 (%expander-for-defmacro name lambda-list body))
99 ;;; In the cross-compiler, we not only need to support the definition
100 ;;; of target macros at cross-compiler-build-time (with SB!XC:DEFMACRO
101 ;;; running in the cross-compilation host), we also need to support
102 ;;; the definition of target macros at target compilation time (with
103 ;;; CL:DEFMACRO processed by the cross-compiler)..
104 #+sb-xc-host
105 (sb!xc:defmacro defmacro (name lambda-list &rest body)
106 (%expander-for-defmacro name lambda-list body))
108 ;;; DEFMACRO-MUNDANELY is like SB!XC:DEFMACRO, except that it doesn't
109 ;;; have any EVAL-WHEN or IR1 magic associated with it, so it only
110 ;;; takes effect in :LOAD-TOPLEVEL or :EXECUTE situations.
111 (def!macro defmacro-mundanely (name lambda-list &body body)
113 ;; old way:
114 ;;(let ((whole (gensym "WHOLE-"))
115 ;; (environment (gensym "ENVIRONMENT-")))
116 ;; (multiple-value-bind (new-body local-decs doc)
117 ;; (parse-defmacro lambda-list whole body name 'defmacro
118 ;; :environment environment)
119 ;; `(progn
120 ;; (setf (sb!xc:macro-function ',name)
121 ;; (lambda (,whole ,environment)
122 ;; ,@local-decs
123 ;; (block ,name
124 ;; ,new-body)))
125 ;; (setf (fdocumentation ',name 'macro)
126 ;; ,doc)
127 ;; ',name)))
129 `(let ()
130 (sb!xc:defmacro ,name ,lambda-list ,@body)))