Declare COERCE and two helpers as EXPLICIT-CHECK.
[sbcl.git] / src / code / defmacro.lisp
blob2d9368da4677ac0a1725d5feeff5eb086bc3948e
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 (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."
59 name))
60 (with-single-package-locked-error (:symbol name "defining ~S as a macro")
61 (when (eq :function kind)
62 (style-warn
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)
68 #-sb-xc-host
69 (when (fboundp 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
75 :name name
76 :new-function definition
77 :new-location source-location))
78 (setf (sb!xc:macro-function name) definition)))
79 name))
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
85 ;;; time.
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)..
94 #+sb-xc-host
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)
103 ;; old way:
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)
109 ;; `(progn
110 ;; (setf (sb!xc:macro-function ',name)
111 ;; (lambda (,whole ,environment)
112 ;; ,@local-decs
113 ;; (block ,name
114 ;; ,new-body)))
115 ;; (setf (fdocumentation ',name 'macro)
116 ;; ,doc)
117 ;; ',name)))
119 `(let ()
120 (sb!xc:defmacro ,name ,lambda-list ,@body)))