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 (: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."
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)
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)))))))
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."
49 '((declare (ignore lambda-list debug-name))))
50 (ecase (info :function :kind name)
53 ;; (remhash name *free-funs*)
54 (undefine-fun-name name)
56 "~S is being redefined as a macro when it was ~
57 previously ~(~A~) to be a function."
59 (info :function :where-from name)))
62 (error "The special form ~S can't be redefined as a macro."
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
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)
82 `(case (widetag-of definition)
83 (#.sb!vm:closure-header-widetag
84 (setf (%simple-fun-arglist (%closure-fun definition))
86 (%simple-fun-name (%closure-fun definition))
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))))
94 (def (:load-toplevel :execute) #-sb-xc-host t #+sb-xc-host nil)
95 (def (:compile-toplevel) nil)))
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
102 (defmacro sb!xc:defmacro (name lambda-list &rest body)
103 (%expander-for-defmacro name lambda-list body))
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)..
111 (sb!xc:defmacro defmacro (name lambda-list &rest body)
112 (%expander-for-defmacro name lambda-list body))
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)
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)
126 ;; (setf (sb!xc:macro-function ',name)
127 ;; (lambda (,whole ,environment)
131 ;; (setf (fdocumentation ',name 'macro)
136 (sb!xc:defmacro ,name ,lambda-list ,@body)))