1.0.22.13: fixed bug 426: nested inline expansion failure
[sbcl/tcr.git] / src / code / macroexpand.lisp
blob4b7a1a2d686e813a93f181b57f728fb5398c4432
1 ;;;; MACROEXPAND and friends
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 ;;;; syntactic environment access
16 (defun sb!xc:special-operator-p (symbol)
17 #!+sb-doc
18 "If the symbol globally names a special form, return T, otherwise NIL."
19 (declare (symbol symbol))
20 (eq (info :function :kind symbol) :special-form))
22 (defvar sb!xc:*macroexpand-hook* 'funcall
23 #!+sb-doc
24 "The value of this variable must be a designator for a function that can
25 take three arguments, a macro expander function, the macro form to be
26 expanded, and the lexical environment to expand in. The function should
27 return the expanded form. This function is called by MACROEXPAND-1
28 whenever a runtime expansion is needed. Initially this is set to
29 FUNCALL.")
31 (defun sb!xc:macroexpand-1 (form &optional env)
32 #!+sb-doc
33 "If form is a macro (or symbol macro), expand it once. Return two values,
34 the expanded form and a T-or-NIL flag indicating whether the form was, in
35 fact, a macro. ENV is the lexical environment to expand in, which defaults
36 to the null environment."
37 (cond ((and (consp form) (symbolp (car form)))
38 (let ((def (sb!xc:macro-function (car form) env)))
39 (if def
40 (values (funcall sb!xc:*macroexpand-hook*
41 def
42 form
43 ;; As far as I can tell, it's not clear from
44 ;; the ANSI spec whether a MACRO-FUNCTION
45 ;; function needs to be prepared to handle
46 ;; NIL as a lexical environment. CMU CL
47 ;; passed NIL through to the MACRO-FUNCTION
48 ;; function, but I prefer SBCL "be conservative
49 ;; in what it sends and liberal in what it
50 ;; accepts" by doing the defaulting itself.
51 ;; -- WHN 19991128
52 (coerce-to-lexenv env))
54 (values form nil))))
55 ((symbolp form)
56 (flet ((perform-symbol-expansion (symbol expansion)
57 ;; CLHS 3.1.2.1.1 specifies that symbol-macros are expanded
58 ;; via the macroexpand hook, too.
59 (funcall sb!xc:*macroexpand-hook*
60 (constantly expansion)
61 symbol
62 env)))
63 (let* ((venv (when env (sb!c::lexenv-vars env)))
64 (local-def (cdr (assoc form venv))))
65 (cond ((and (consp local-def)
66 (eq (car local-def) 'macro))
67 (values (perform-symbol-expansion form (cdr local-def)) t))
68 (local-def
69 (values form nil))
70 ((eq (info :variable :kind form) :macro)
71 (let ((expansion (info :variable :macro-expansion form)))
72 (values (perform-symbol-expansion form expansion) t)))
74 (values form nil))))))
76 (values form nil))))
78 (defun sb!xc:macroexpand (form &optional env)
79 #!+sb-doc
80 "Repetitively call MACROEXPAND-1 until the form can no longer be expanded.
81 Returns the final resultant form, and T if it was expanded. ENV is the
82 lexical environment to expand in, or NIL (the default) for the null
83 environment."
84 (labels ((frob (form expanded)
85 (multiple-value-bind (new-form newly-expanded-p)
86 (sb!xc:macroexpand-1 form env)
87 (if newly-expanded-p
88 (frob new-form t)
89 (values new-form expanded)))))
90 (frob form nil)))