74e2ea9666e4d154ba71f17ad8c9bdf174bd1ec9
[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     (let ((whole (gensym "WHOLE-"))
22           (environment (gensym "ENV-")))
23       (multiple-value-bind (new-body local-decs doc)
24           (parse-defmacro lambda-list whole body name 'defmacro
25                           :environment environment)
26         (let ((def `(lambda (,whole ,environment)
27                       ,@local-decs
28                       (block ,name
29                         ,new-body))))
30           `(sb!c::%defmacro ',name #',def ',lambda-list ,doc))))))
31
32 ;;; Ordinarily this definition of SB!C:%DEFMACRO as an ordinary
33 ;;; function is not used: the parallel (but different) definition as
34 ;;; an IR1 transform takes precedence. However, this definition is
35 ;;; still useful in the target interpreter, and in the
36 ;;; cross-compilation host.
37 (defun sb!c::%defmacro (name definition lambda-list doc)
38   (sb!c::%%defmacro name definition doc))
39
40 ;;; (called by SB!C::%DEFMACRO)
41 (eval-when (:compile-toplevel :load-toplevel :execute)
42   (defun sb!c::%%defmacro (name definition doc)
43     ;; old note (ca. 1985, maybe:-): "Eventually %%DEFMACRO should
44     ;; deal with clearing old compiler information for the functional
45     ;; value."
46     (clear-info :function :where-from name)
47     ;; FIXME: It would be nice to warn about DEFMACRO of an
48     ;; already-defined macro, but that's slightly hard to do because
49     ;; in common usage DEFMACRO is defined at compile time and then
50     ;; redefined at load time. We'd need to make a distinction between
51     ;; the defined-at-compile-time state and the defined-at-load-time
52     ;; state to make this work. (Trying to warn about duplicate
53     ;; DEFTYPEs runs into the same problem.)
54     #+nil (when (sb!xc:macro-function name)
55             (style-warn "redefining ~S in DEFMACRO" name))
56     (setf (sb!xc:macro-function name) definition
57           (fdocumentation name 'function) doc)
58     name))
59
60 ;;; Parse the definition and make an expander function. The actual
61 ;;; definition is done by %DEFMACRO which we expand into, and which is
62 ;;; handled magically by an IR1 transform. After the compiler has
63 ;;; gotten the information it wants out of macro definition, it
64 ;;; compiles a call to %%DEFMACRO which happens at load time.
65 (defmacro sb!xc:defmacro (name lambda-list &rest body)
66   (%expander-for-defmacro name lambda-list body))
67
68 ;;; In the cross-compiler, we not only need to support the definition
69 ;;; of target macros at cross-compiler-build-time (with SB!XC:DEFMACRO
70 ;;; running in the cross-compilation host), we also need to support
71 ;;; the definition of target macros at target compilation time (with
72 ;;; CL:DEFMACRO processed by the cross-compiler)..
73 #+sb-xc-host
74 (sb!xc:defmacro defmacro (name lambda-list &rest body)
75   (%expander-for-defmacro name lambda-list body))
76
77 ;;; DEFMACRO-MUNDANELY is like SB!XC:DEFMACRO, except that it doesn't
78 ;;; have any EVAL-WHEN or IR1 magic associated with it, so it only
79 ;;; takes effect in :LOAD-TOPLEVEL or :EXECUTE situations.
80 (def!macro defmacro-mundanely (name lambda-list &body body)
81   (let ((whole (gensym "WHOLE-"))
82                   (environment (gensym "ENVIRONMENT-")))
83               (multiple-value-bind (new-body local-decs doc)
84                   (parse-defmacro lambda-list whole body name 'defmacro
85                                   :environment environment)
86       `(progn
87          (setf (sb!xc:macro-function ',name)
88                (lambda (,whole ,environment)
89                    ,@local-decs
90                    (block ,name
91                    ,new-body)))
92          (setf (fdocumentation ',name 'macro)
93                ,doc)
94          ',name))))