macroexp.el 9.2 KB

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