1.0.9.48: texi2pdf rework (Aymeric Vincent sbcl-devel 2007-09-05)
[sbcl/lichteblau.git] / src / code / macroexpand.lisp
blobca38c6c5ae1ebc64b57b0045f43c9e8be9a2cb2c
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 (let* ((venv (when env (sb!c::lexenv-vars env)))
57 (local-def (cdr (assoc form venv))))
58 (cond ((and (consp local-def)
59 (eq (car local-def) 'macro))
60 (values (cdr local-def) t))
61 (local-def
62 (values form nil))
63 ((eq (info :variable :kind form) :macro)
64 (values (info :variable :macro-expansion form) t))
66 (values form nil)))))
68 (values form nil))))
70 (defun sb!xc:macroexpand (form &optional env)
71 #!+sb-doc
72 "Repetitively call MACROEXPAND-1 until the form can no longer be expanded.
73 Returns the final resultant form, and T if it was expanded. ENV is the
74 lexical environment to expand in, or NIL (the default) for the null
75 environment."
76 (labels ((frob (form expanded)
77 (multiple-value-bind (new-form newly-expanded-p)
78 (sb!xc:macroexpand-1 form env)
79 (if newly-expanded-p
80 (frob new-form t)
81 (values new-form expanded)))))
82 (frob form nil)))