89909
|
1 ;;; macroexp.el --- Additional macro-expansion support
|
|
2 ;;
|
|
3 ;; Copyright (C) 2001, 2002, 2003 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., 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, 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 (#1=#:\(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 (let ((var (car #1#))
|
|
56 (list (cadr #1#))
|
|
57 (shared (make-symbol "shared"))
|
|
58 (unshared (make-symbol "unshared"))
|
|
59 (tail (make-symbol "tail"))
|
|
60 (new-el (make-symbol "new-el")))
|
|
61 `(let* ((,shared ,list)
|
|
62 (,unshared nil)
|
|
63 (,tail ,shared)
|
|
64 ,var ,new-el)
|
|
65 (while ,tail
|
|
66 (setq ,var (car ,tail)
|
|
67 ,new-el (progn ,@body))
|
|
68 (unless (eq ,var ,new-el)
|
|
69 (while (not (eq ,shared ,tail))
|
|
70 (push (pop ,shared) ,unshared))
|
|
71 (setq ,shared (cdr ,shared))
|
|
72 (push ,new-el ,unshared))
|
|
73 (setq ,tail (cdr ,tail)))
|
|
74 (nconc (nreverse ,unshared) ,shared))))
|
|
75 (put 'macroexp-accumulate 'lisp-indent-function 1)
|
|
76
|
|
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)))
|
|
86
|
|
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)))
|
|
96
|
|
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 (if (consp form)
|
|
111 (let ((fun (car form)))
|
|
112 (cond
|
|
113 ((eq fun 'cond)
|
|
114 (maybe-cons fun (macroexpand-all-clauses (cdr form)) form))
|
|
115 ((eq fun 'condition-case)
|
|
116 (maybe-cons
|
|
117 fun
|
|
118 (maybe-cons (cadr form)
|
|
119 (maybe-cons (macroexpand-all-1 (nth 2 form))
|
|
120 (macroexpand-all-clauses (nthcdr 3 form) 1)
|
|
121 (cddr form))
|
|
122 (cdr form))
|
|
123 form))
|
|
124 ((eq fun 'defmacro)
|
|
125 (push (cons (cadr form) (cons 'lambda (cddr form)))
|
|
126 macroexpand-all-environment)
|
|
127 (macroexpand-all-forms form 3))
|
|
128 ((eq fun 'defun)
|
|
129 (macroexpand-all-forms form 3))
|
|
130 ((memq fun '(defvar defconst))
|
|
131 (macroexpand-all-forms form 2))
|
|
132 ((eq fun 'function)
|
|
133 (if (and (consp (cadr form)) (eq (car (cadr form)) 'lambda))
|
|
134 (maybe-cons fun
|
|
135 (maybe-cons (macroexpand-all-forms (cadr form) 2)
|
|
136 nil
|
|
137 (cadr form))
|
|
138 form)
|
|
139 form))
|
|
140 ((memq fun '(let let*))
|
|
141 (maybe-cons fun
|
|
142 (maybe-cons (macroexpand-all-clauses (cadr form) 1)
|
|
143 (macroexpand-all-forms (cddr form))
|
|
144 (cdr form))
|
|
145 form))
|
|
146 ((eq fun 'quote)
|
|
147 form)
|
|
148 ((and (consp fun) (eq (car fun) 'lambda))
|
|
149 ;; embedded lambda
|
|
150 (maybe-cons (macroexpand-all-forms fun 2)
|
|
151 (macroexpand-all-forms (cdr form))
|
|
152 form))
|
|
153 ;; The following few cases are for normal function calls that
|
|
154 ;; are known to funcall one of their arguments. The byte
|
|
155 ;; compiler has traditionally handled these functions specially
|
|
156 ;; by treating a lambda expression quoted by `quote' as if it
|
|
157 ;; were quoted by `function'. We make the same transformation
|
|
158 ;; here, so that any code that cares about the difference will
|
|
159 ;; see the same transformation.
|
|
160 ;; First arg is a function:
|
|
161 ((and (memq fun '(apply mapcar mapatoms mapconcat mapc))
|
|
162 (consp (cadr form))
|
|
163 (eq (car (cadr form)) 'quote))
|
|
164 ;; We don't use `maybe-cons' since there's clearly a change.
|
|
165 (cons fun
|
|
166 (cons (macroexpand-all-1 (cons 'function (cdr (cadr form))))
|
|
167 (macroexpand-all-forms (cddr form)))))
|
|
168 ;; Second arg is a function:
|
|
169 ((and (eq fun 'sort)
|
|
170 (consp (nth 2 form))
|
|
171 (eq (car (nth 2 form)) 'quote))
|
|
172 ;; We don't use `maybe-cons' since there's clearly a change.
|
|
173 (cons fun
|
|
174 (cons (macroexpand-all-1 (cadr form))
|
|
175 (cons (macroexpand-all-1
|
|
176 (cons 'function (cdr (nth 2 form))))
|
|
177 (macroexpand-all-forms (nthcdr 3 form))))))
|
|
178 (t
|
|
179 ;; For everything else, we just expand each argument (for
|
|
180 ;; setq/setq-default this works alright because the variable names
|
|
181 ;; are symbols).
|
|
182 (macroexpand-all-forms form 1))))
|
|
183 form)))
|
|
184
|
|
185 ;;;###autoload
|
|
186 (defun macroexpand-all (form &optional environment)
|
|
187 "Return result of expanding macros at all levels in FORM.
|
|
188 If no macros are expanded, FORM is returned unchanged.
|
|
189 The second optional arg ENVIRONMENT specifies an environment of macro
|
|
190 definitions to shadow the loaded ones for use in file byte-compilation."
|
|
191 (let ((macroexpand-all-environment environment))
|
|
192 (macroexpand-all-1 form)))
|
|
193
|
|
194 (provide 'macroexp)
|
|
195
|
|
196 ;;; arch-tag: af9b8c24-c196-43bc-91e1-a3570790fa5a
|
|
197 ;;; macroexp.el ends here
|