Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / emacs-lisp / macroexp.el
CommitLineData
a9de04fa 1;;; macroexp.el --- Additional macro-expansion support -*- lexical-binding: t -*-
4d449b11 2;;
acaf905b 3;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
4d449b11
MB
4;;
5;; Author: Miles Bader <miles@gnu.org>
6;; Keywords: lisp, compiler, macros
7
8;; This file is part of GNU Emacs.
9
d6cba7ae 10;; GNU Emacs is free software: you can redistribute it and/or modify
4d449b11 11;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
4d449b11
MB
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
d6cba7ae 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
4d449b11
MB
22
23;;; Commentary:
24;;
25;; This file contains macro-expansions functions that are not defined in
26;; the Lisp core, namely `macroexpand-all', which expands all macros in
27;; a form, not just a top-level one.
28;;
29
30;;; Code:
31
43e67019
SM
32(eval-when-compile (require 'cl))
33
4d449b11
MB
34;; Bound by the top-level `macroexpand-all', and modified to include any
35;; macros defined by `defmacro'.
36(defvar macroexpand-all-environment nil)
37
38(defun maybe-cons (car cdr original-cons)
39 "Return (CAR . CDR), using ORIGINAL-CONS if possible."
40 (if (and (eq car (car original-cons)) (eq cdr (cdr original-cons)))
41 original-cons
42 (cons car cdr)))
43
44;; We use this special macro to iteratively process forms and share list
45;; structure of the result with the input. Doing so recursively using
46;; `maybe-cons' results in excessively deep recursion for very long
47;; input forms.
5de35ba4 48(defmacro macroexp-accumulate (var+list &rest body)
4d449b11
MB
49 "Return a list of the results of evaluating BODY for each element of LIST.
50Evaluate BODY with VAR bound to each `car' from LIST, in turn.
51Return a list of the values of the final form in BODY.
52The list structure of the result will share as much with LIST as
53possible (for instance, when BODY just returns VAR unchanged, the
5de35ba4
RS
54result will be eq to LIST).
55
56\(fn (VAR LIST) BODY...)"
6fe79b7c 57 (declare (indent 1))
5de35ba4
RS
58 (let ((var (car var+list))
59 (list (cadr var+list))
4d449b11
MB
60 (shared (make-symbol "shared"))
61 (unshared (make-symbol "unshared"))
62 (tail (make-symbol "tail"))
63 (new-el (make-symbol "new-el")))
64 `(let* ((,shared ,list)
65 (,unshared nil)
66 (,tail ,shared)
67 ,var ,new-el)
68 (while ,tail
69 (setq ,var (car ,tail)
70 ,new-el (progn ,@body))
71 (unless (eq ,var ,new-el)
72 (while (not (eq ,shared ,tail))
73 (push (pop ,shared) ,unshared))
74 (setq ,shared (cdr ,shared))
75 (push ,new-el ,unshared))
76 (setq ,tail (cdr ,tail)))
77 (nconc (nreverse ,unshared) ,shared))))
4d449b11
MB
78
79(defun macroexpand-all-forms (forms &optional skip)
80 "Return FORMS with macros expanded. FORMS is a list of forms.
81If SKIP is non-nil, then don't expand that many elements at the start of
82FORMS."
83 (macroexp-accumulate (form forms)
84 (if (or (null skip) (zerop skip))
85 (macroexpand-all-1 form)
86 (setq skip (1- skip))
87 form)))
88
89(defun macroexpand-all-clauses (clauses &optional skip)
90 "Return CLAUSES with macros expanded.
91CLAUSES is a list of lists of forms; any clause that's not a list is ignored.
92If SKIP is non-nil, then don't expand that many elements at the start of
93each clause."
3731a850 94 (macroexp-accumulate (clause clauses)
4d449b11
MB
95 (if (listp clause)
96 (macroexpand-all-forms clause skip)
97 clause)))
98
99(defun macroexpand-all-1 (form)
100 "Expand all macros in FORM.
101This is an internal version of `macroexpand-all'.
102Assumes the caller has bound `macroexpand-all-environment'."
103 (if (and (listp form) (eq (car form) 'backquote-list*))
104 ;; Special-case `backquote-list*', as it is normally a macro that
105 ;; generates exceedingly deep expansions from relatively shallow input
106 ;; forms. We just process it `in reverse' -- first we expand all the
107 ;; arguments, _then_ we expand the top-level definition.
108 (macroexpand (macroexpand-all-forms form 1)
109 macroexpand-all-environment)
110 ;; Normal form; get its expansion, and then expand arguments.
a9de04fa
SM
111 (let ((new-form (macroexpand form macroexpand-all-environment)))
112 (when (and (not (eq form new-form)) ;It was a macro call.
113 (car-safe form)
114 (symbolp (car form))
115 (get (car form) 'byte-obsolete-info)
116 (fboundp 'byte-compile-warn-obsolete))
117 (byte-compile-warn-obsolete (car form)))
118 (setq form new-form))
6fe79b7c
SM
119 (pcase form
120 (`(cond . ,clauses)
121 (maybe-cons 'cond (macroexpand-all-clauses clauses) form))
122 (`(condition-case . ,(or `(,err ,body . ,handlers) dontcare))
123 (maybe-cons
124 'condition-case
125 (maybe-cons err
126 (maybe-cons (macroexpand-all-1 body)
127 (macroexpand-all-clauses handlers 1)
128 (cddr form))
129 (cdr form))
130 form))
131 (`(defmacro ,name . ,args-and-body)
132 (push (cons name (cons 'lambda args-and-body))
133 macroexpand-all-environment)
ba83908c
SM
134 (let ((n 3))
135 ;; Don't macroexpand `declare' since it should really be "expanded"
136 ;; away when `defmacro' is expanded, but currently defmacro is not
137 ;; itself a macro. So both `defmacro' and `declare' need to be
138 ;; handled directly in bytecomp.el.
139 ;; FIXME: Maybe a simpler solution is to (defalias 'declare 'quote).
140 (while (or (stringp (nth n form))
141 (eq (car-safe (nth n form)) 'declare))
142 (setq n (1+ n)))
143 (macroexpand-all-forms form n)))
6fe79b7c
SM
144 (`(defun . ,_) (macroexpand-all-forms form 3))
145 (`(,(or `defvar `defconst) . ,_) (macroexpand-all-forms form 2))
146 (`(function ,(and f `(lambda . ,_)))
147 (maybe-cons 'function
148 (maybe-cons (macroexpand-all-forms f 2)
149 nil
150 (cdr form))
151 form))
152 (`(,(or `function `quote) . ,_) form)
153 (`(,(and fun (or `let `let*)) . ,(or `(,bindings . ,body) dontcare))
154 (maybe-cons fun
155 (maybe-cons (macroexpand-all-clauses bindings 1)
156 (macroexpand-all-forms body)
157 (cdr form))
158 form))
159 (`(,(and fun `(lambda . ,_)) . ,args)
160 ;; Embedded lambda in function position.
161 (maybe-cons (macroexpand-all-forms fun 2)
162 (macroexpand-all-forms args)
163 form))
164 ;; The following few cases are for normal function calls that
165 ;; are known to funcall one of their arguments. The byte
166 ;; compiler has traditionally handled these functions specially
167 ;; by treating a lambda expression quoted by `quote' as if it
168 ;; were quoted by `function'. We make the same transformation
169 ;; here, so that any code that cares about the difference will
170 ;; see the same transformation.
171 ;; First arg is a function:
9c848d8a 172 (`(,(and fun (or `funcall `apply `mapcar `mapatoms `mapconcat `mapc))
876c194c 173 ',(and f `(lambda . ,_)) . ,args)
9c848d8a
SM
174 (byte-compile-log-warning
175 (format "%s quoted with ' rather than with #'"
176 (list 'lambda (nth 1 f) '...))
177 t)
6fe79b7c
SM
178 ;; We don't use `maybe-cons' since there's clearly a change.
179 (cons fun
180 (cons (macroexpand-all-1 (list 'function f))
181 (macroexpand-all-forms args))))
182 ;; Second arg is a function:
876c194c 183 (`(,(and fun (or `sort)) ,arg1 ',(and f `(lambda . ,_)) . ,args)
9c848d8a
SM
184 (byte-compile-log-warning
185 (format "%s quoted with ' rather than with #'"
186 (list 'lambda (nth 1 f) '...))
187 t)
6fe79b7c
SM
188 ;; We don't use `maybe-cons' since there's clearly a change.
189 (cons fun
190 (cons (macroexpand-all-1 arg1)
191 (cons (macroexpand-all-1
192 (list 'function f))
193 (macroexpand-all-forms args)))))
2ec42da9
SM
194 ;; Macro expand compiler macros. This cannot be delayed to
195 ;; byte-optimize-form because the output of the compiler-macro can
196 ;; use macros.
43e67019 197 ;; FIXME: Don't depend on CL.
e2abe5a1
SM
198 (`(,(pred (lambda (fun)
199 (and (symbolp fun)
200 (eq (get fun 'byte-compile)
201 'cl-byte-compile-compiler-macro)
202 (functionp 'compiler-macroexpand))))
43e67019 203 . ,_)
2ec42da9 204 (let ((newform (with-no-warnings (compiler-macroexpand form))))
43e67019
SM
205 (if (eq form newform)
206 (macroexpand-all-forms form 1)
207 (macroexpand-all-1 newform))))
6fe79b7c
SM
208 (`(,_ . ,_)
209 ;; For every other list, we just expand each argument (for
210 ;; setq/setq-default this works alright because the variable names
211 ;; are symbols).
212 (macroexpand-all-forms form 1))
213 (t form))))
4d449b11
MB
214
215;;;###autoload
216(defun macroexpand-all (form &optional environment)
217 "Return result of expanding macros at all levels in FORM.
218If no macros are expanded, FORM is returned unchanged.
219The second optional arg ENVIRONMENT specifies an environment of macro
220definitions to shadow the loaded ones for use in file byte-compilation."
221 (let ((macroexpand-all-environment environment))
222 (macroexpand-all-1 form)))
223
224(provide 'macroexp)
225
4d449b11 226;;; macroexp.el ends here