88155
|
1 ;;; macroexp.el --- Additional macro-expansion support
|
|
2 ;;
|
|
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
|
|
4 ;;
|
|
5 ;; Author: Miles Bader <miles@gnu.org>
|
|
6 ;; Keywords: lisp, compiler, macros
|
|
7
|
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
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 2, or (at your option)
|
|
13 ;; any later version.
|
|
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
|
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
23 ;; Boston, MA 02110-1301, USA.
|
|
24
|
|
25 ;;; Commentary:
|
|
26 ;;
|
|
27 ;; This file contains macro-expansions functions that are not defined in
|
|
28 ;; the Lisp core, namely `macroexpand-all', which expands all macros in
|
|
29 ;; a form, not just a top-level one.
|
|
30 ;;
|
|
31
|
|
32 ;;; Code:
|
|
33
|
|
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.
|
|
48 (defmacro macroexp-accumulate (var+list &rest body)
|
|
49 "Return a list of the results of evaluating BODY for each element of LIST.
|
|
50 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
|
|
51 Return a list of the values of the final form in BODY.
|
|
52 The list structure of the result will share as much with LIST as
|
|
53 possible (for instance, when BODY just returns VAR unchanged, the
|
|
54 result will be eq to LIST).
|
|
55
|
|
56 \(fn (VAR LIST) BODY...)"
|
|
57 (let ((var (car var+list))
|
|
58 (list (cadr var+list))
|
|
59 (shared (make-symbol "shared"))
|
|
60 (unshared (make-symbol "unshared"))
|
|
61 (tail (make-symbol "tail"))
|
|
62 (new-el (make-symbol "new-el")))
|
|
63 `(let* ((,shared ,list)
|
|
64 (,unshared nil)
|
|
65 (,tail ,shared)
|
|
66 ,var ,new-el)
|
|
67 (while ,tail
|
|
68 (setq ,var (car ,tail)
|
|
69 ,new-el (progn ,@body))
|
|
70 (unless (eq ,var ,new-el)
|
|
71 (while (not (eq ,shared ,tail))
|
|
72 (push (pop ,shared) ,unshared))
|
|
73 (setq ,shared (cdr ,shared))
|
|
74 (push ,new-el ,unshared))
|
|
75 (setq ,tail (cdr ,tail)))
|
|
76 (nconc (nreverse ,unshared) ,shared))))
|
|
77 (put 'macroexp-accumulate 'lisp-indent-function 1)
|
|
78
|
|
79 (defun macroexpand-all-forms (forms &optional skip)
|
|
80 "Return FORMS with macros expanded. FORMS is a list of forms.
|
|
81 If SKIP is non-nil, then don't expand that many elements at the start of
|
|
82 FORMS."
|
|
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.
|
|
91 CLAUSES is a list of lists of forms; any clause that's not a list is ignored.
|
|
92 If SKIP is non-nil, then don't expand that many elements at the start of
|
|
93 each clause."
|
|
94 (macroexp-accumulate (clause clauses)
|
|
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.
|
|
101 This is an internal version of `macroexpand-all'.
|
|
102 Assumes 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.
|
|
111 (setq form (macroexpand form macroexpand-all-environment))
|
|
112 (if (consp form)
|
|
113 (let ((fun (car form)))
|
|
114 (cond
|
|
115 ((eq fun 'cond)
|
|
116 (maybe-cons fun (macroexpand-all-clauses (cdr form)) form))
|
|
117 ((eq fun 'condition-case)
|
|
118 (maybe-cons
|
|
119 fun
|
|
120 (maybe-cons (cadr form)
|
|
121 (maybe-cons (macroexpand-all-1 (nth 2 form))
|
|
122 (macroexpand-all-clauses (nthcdr 3 form) 1)
|
|
123 (cddr form))
|
|
124 (cdr form))
|
|
125 form))
|
|
126 ((eq fun 'defmacro)
|
|
127 (push (cons (cadr form) (cons 'lambda (cddr form)))
|
|
128 macroexpand-all-environment)
|
|
129 (macroexpand-all-forms form 3))
|
|
130 ((eq fun 'defun)
|
|
131 (macroexpand-all-forms form 3))
|
|
132 ((memq fun '(defvar defconst))
|
|
133 (macroexpand-all-forms form 2))
|
|
134 ((eq fun 'function)
|
|
135 (if (and (consp (cadr form)) (eq (car (cadr form)) 'lambda))
|
|
136 (maybe-cons fun
|
|
137 (maybe-cons (macroexpand-all-forms (cadr form) 2)
|
|
138 nil
|
|
139 (cadr form))
|
|
140 form)
|
|
141 form))
|
|
142 ((memq fun '(let let*))
|
|
143 (maybe-cons fun
|
|
144 (maybe-cons (macroexpand-all-clauses (cadr form) 1)
|
|
145 (macroexpand-all-forms (cddr form))
|
|
146 (cdr form))
|
|
147 form))
|
|
148 ((eq fun 'quote)
|
|
149 form)
|
|
150 ((and (consp fun) (eq (car fun) 'lambda))
|
|
151 ;; embedded lambda
|
|
152 (maybe-cons (macroexpand-all-forms fun 2)
|
|
153 (macroexpand-all-forms (cdr form))
|
|
154 form))
|
|
155 ;; The following few cases are for normal function calls that
|
|
156 ;; are known to funcall one of their arguments. The byte
|
|
157 ;; compiler has traditionally handled these functions specially
|
|
158 ;; by treating a lambda expression quoted by `quote' as if it
|
|
159 ;; were quoted by `function'. We make the same transformation
|
|
160 ;; here, so that any code that cares about the difference will
|
|
161 ;; see the same transformation.
|
|
162 ;; First arg is a function:
|
|
163 ((and (memq fun '(apply mapcar mapatoms mapconcat mapc))
|
|
164 (consp (cadr form))
|
|
165 (eq (car (cadr form)) 'quote))
|
|
166 ;; We don't use `maybe-cons' since there's clearly a change.
|
|
167 (cons fun
|
|
168 (cons (macroexpand-all-1 (cons 'function (cdr (cadr form))))
|
|
169 (macroexpand-all-forms (cddr form)))))
|
|
170 ;; Second arg is a function:
|
|
171 ((and (eq fun 'sort)
|
|
172 (consp (nth 2 form))
|
|
173 (eq (car (nth 2 form)) 'quote))
|
|
174 ;; We don't use `maybe-cons' since there's clearly a change.
|
|
175 (cons fun
|
|
176 (cons (macroexpand-all-1 (cadr form))
|
|
177 (cons (macroexpand-all-1
|
|
178 (cons 'function (cdr (nth 2 form))))
|
|
179 (macroexpand-all-forms (nthcdr 3 form))))))
|
|
180 (t
|
|
181 ;; For everything else, we just expand each argument (for
|
|
182 ;; setq/setq-default this works alright because the variable names
|
|
183 ;; are symbols).
|
|
184 (macroexpand-all-forms form 1))))
|
|
185 form)))
|
|
186
|
|
187 ;;;###autoload
|
|
188 (defun macroexpand-all (form &optional environment)
|
|
189 "Return result of expanding macros at all levels in FORM.
|
|
190 If no macros are expanded, FORM is returned unchanged.
|
|
191 The second optional arg ENVIRONMENT specifies an environment of macro
|
|
192 definitions to shadow the loaded ones for use in file byte-compilation."
|
|
193 (let ((macroexpand-all-environment environment))
|
|
194 (macroexpand-all-1 form)))
|
|
195
|
|
196 (provide 'macroexp)
|
|
197
|
|
198 ;;; arch-tag: af9b8c24-c196-43bc-91e1-a3570790fa5a
|
|
199 ;;; macroexp.el ends here
|