Don't define HAVE_STRFTIME on MS-Windows.
[emacs.git] / lisp / emacs-lisp / macroexp.el
blobaf8047256e2fe9aadd8c28cd71bc5cd0174be1c8
1 ;;; macroexp.el --- Additional macro-expansion support
2 ;;
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Miles Bader <miles@gnu.org>
6 ;; Keywords: lisp, compiler, macros
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
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.
30 ;;; Code:
32 ;; Bound by the top-level `macroexpand-all', and modified to include any
33 ;; macros defined by `defmacro'.
34 (defvar macroexpand-all-environment nil)
36 (defun maybe-cons (car cdr original-cons)
37 "Return (CAR . CDR), using ORIGINAL-CONS if possible."
38 (if (and (eq car (car original-cons)) (eq cdr (cdr original-cons)))
39 original-cons
40 (cons car cdr)))
42 ;; We use this special macro to iteratively process forms and share list
43 ;; structure of the result with the input. Doing so recursively using
44 ;; `maybe-cons' results in excessively deep recursion for very long
45 ;; input forms.
46 (defmacro macroexp-accumulate (var+list &rest body)
47 "Return a list of the results of evaluating BODY for each element of LIST.
48 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
49 Return a list of the values of the final form in BODY.
50 The list structure of the result will share as much with LIST as
51 possible (for instance, when BODY just returns VAR unchanged, the
52 result will be eq to LIST).
54 \(fn (VAR LIST) BODY...)"
55 (declare (indent 1))
56 (let ((var (car var+list))
57 (list (cadr var+list))
58 (shared (make-symbol "shared"))
59 (unshared (make-symbol "unshared"))
60 (tail (make-symbol "tail"))
61 (new-el (make-symbol "new-el")))
62 `(let* ((,shared ,list)
63 (,unshared nil)
64 (,tail ,shared)
65 ,var ,new-el)
66 (while ,tail
67 (setq ,var (car ,tail)
68 ,new-el (progn ,@body))
69 (unless (eq ,var ,new-el)
70 (while (not (eq ,shared ,tail))
71 (push (pop ,shared) ,unshared))
72 (setq ,shared (cdr ,shared))
73 (push ,new-el ,unshared))
74 (setq ,tail (cdr ,tail)))
75 (nconc (nreverse ,unshared) ,shared))))
77 (defun macroexpand-all-forms (forms &optional skip)
78 "Return FORMS with macros expanded. FORMS is a list of forms.
79 If SKIP is non-nil, then don't expand that many elements at the start of
80 FORMS."
81 (macroexp-accumulate (form forms)
82 (if (or (null skip) (zerop skip))
83 (macroexpand-all-1 form)
84 (setq skip (1- skip))
85 form)))
87 (defun macroexpand-all-clauses (clauses &optional skip)
88 "Return CLAUSES with macros expanded.
89 CLAUSES is a list of lists of forms; any clause that's not a list is ignored.
90 If SKIP is non-nil, then don't expand that many elements at the start of
91 each clause."
92 (macroexp-accumulate (clause clauses)
93 (if (listp clause)
94 (macroexpand-all-forms clause skip)
95 clause)))
97 (defun macroexpand-all-1 (form)
98 "Expand all macros in FORM.
99 This is an internal version of `macroexpand-all'.
100 Assumes the caller has bound `macroexpand-all-environment'."
101 (if (and (listp form) (eq (car form) 'backquote-list*))
102 ;; Special-case `backquote-list*', as it is normally a macro that
103 ;; generates exceedingly deep expansions from relatively shallow input
104 ;; forms. We just process it `in reverse' -- first we expand all the
105 ;; arguments, _then_ we expand the top-level definition.
106 (macroexpand (macroexpand-all-forms form 1)
107 macroexpand-all-environment)
108 ;; Normal form; get its expansion, and then expand arguments.
109 (setq form (macroexpand form macroexpand-all-environment))
110 (pcase form
111 (`(cond . ,clauses)
112 (maybe-cons 'cond (macroexpand-all-clauses clauses) form))
113 (`(condition-case . ,(or `(,err ,body . ,handlers) dontcare))
114 (maybe-cons
115 'condition-case
116 (maybe-cons err
117 (maybe-cons (macroexpand-all-1 body)
118 (macroexpand-all-clauses handlers 1)
119 (cddr form))
120 (cdr form))
121 form))
122 (`(defmacro ,name . ,args-and-body)
123 (push (cons name (cons 'lambda args-and-body))
124 macroexpand-all-environment)
125 (macroexpand-all-forms form 3))
126 (`(defun . ,_) (macroexpand-all-forms form 3))
127 (`(,(or `defvar `defconst) . ,_) (macroexpand-all-forms form 2))
128 (`(function ,(and f `(lambda . ,_)))
129 (maybe-cons 'function
130 (maybe-cons (macroexpand-all-forms f 2)
132 (cdr form))
133 form))
134 (`(,(or `function `quote) . ,_) form)
135 (`(,(and fun (or `let `let*)) . ,(or `(,bindings . ,body) dontcare))
136 (maybe-cons fun
137 (maybe-cons (macroexpand-all-clauses bindings 1)
138 (macroexpand-all-forms body)
139 (cdr form))
140 form))
141 (`(,(and fun `(lambda . ,_)) . ,args)
142 ;; Embedded lambda in function position.
143 (maybe-cons (macroexpand-all-forms fun 2)
144 (macroexpand-all-forms args)
145 form))
146 ;; The following few cases are for normal function calls that
147 ;; are known to funcall one of their arguments. The byte
148 ;; compiler has traditionally handled these functions specially
149 ;; by treating a lambda expression quoted by `quote' as if it
150 ;; were quoted by `function'. We make the same transformation
151 ;; here, so that any code that cares about the difference will
152 ;; see the same transformation.
153 ;; First arg is a function:
154 (`(,(and fun (or `apply `mapcar `mapatoms `mapconcat `mapc)) ',f . ,args)
155 ;; We don't use `maybe-cons' since there's clearly a change.
156 (cons fun
157 (cons (macroexpand-all-1 (list 'function f))
158 (macroexpand-all-forms args))))
159 ;; Second arg is a function:
160 (`(,(and fun (or `sort)) ,arg1 ',f . ,args)
161 ;; We don't use `maybe-cons' since there's clearly a change.
162 (cons fun
163 (cons (macroexpand-all-1 arg1)
164 (cons (macroexpand-all-1
165 (list 'function f))
166 (macroexpand-all-forms args)))))
167 (`(,_ . ,_)
168 ;; For every other list, we just expand each argument (for
169 ;; setq/setq-default this works alright because the variable names
170 ;; are symbols).
171 (macroexpand-all-forms form 1))
172 (t form))))
174 ;;;###autoload
175 (defun macroexpand-all (form &optional environment)
176 "Return result of expanding macros at all levels in FORM.
177 If no macros are expanded, FORM is returned unchanged.
178 The second optional arg ENVIRONMENT specifies an environment of macro
179 definitions to shadow the loaded ones for use in file byte-compilation."
180 (let ((macroexpand-all-environment environment))
181 (macroexpand-all-1 form)))
183 (provide 'macroexp)
185 ;;; macroexp.el ends here