byte-run.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ;;; byte-run.el --- byte-compiler support for inlining
  2. ;; Copyright (C) 1992, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Jamie Zawinski <jwz@lucid.com>
  4. ;; Hallvard Furuseth <hbf@ulrik.uio.no>
  5. ;; Maintainer: FSF
  6. ;; Keywords: internal
  7. ;; Package: emacs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; interface to selectively inlining functions.
  21. ;; This only happens when source-code optimization is turned on.
  22. ;;; Code:
  23. ;; We define macro-declaration-function here because it is needed to
  24. ;; handle declarations in macro definitions and this is the first file
  25. ;; loaded by loadup.el that uses declarations in macros.
  26. (defun macro-declaration-function (macro decl)
  27. "Process a declaration found in a macro definition.
  28. This is set as the value of the variable `macro-declaration-function'.
  29. MACRO is the name of the macro being defined.
  30. DECL is a list `(declare ...)' containing the declarations.
  31. The return value of this function is not used."
  32. ;; We can't use `dolist' or `cadr' yet for bootstrapping reasons.
  33. (let (d)
  34. ;; Ignore the first element of `decl' (it's always `declare').
  35. (while (setq decl (cdr decl))
  36. (setq d (car decl))
  37. (if (and (consp d)
  38. (listp (cdr d))
  39. (null (cdr (cdr d))))
  40. (cond ((eq (car d) 'indent)
  41. (put macro 'lisp-indent-function (car (cdr d))))
  42. ((eq (car d) 'debug)
  43. (put macro 'edebug-form-spec (car (cdr d))))
  44. ((eq (car d) 'doc-string)
  45. (put macro 'doc-string-elt (car (cdr d))))
  46. (t
  47. (message "Unknown declaration %s" d)))
  48. (message "Invalid declaration %s" d)))))
  49. (setq macro-declaration-function 'macro-declaration-function)
  50. ;; Redefined in byte-optimize.el.
  51. ;; This is not documented--it's not clear that we should promote it.
  52. (fset 'inline 'progn)
  53. ;;; Interface to inline functions.
  54. ;; (defmacro proclaim-inline (&rest fns)
  55. ;; "Cause the named functions to be open-coded when called from compiled code.
  56. ;; They will only be compiled open-coded when byte-compile-optimize is true."
  57. ;; (cons 'eval-and-compile
  58. ;; (mapcar (lambda (x)
  59. ;; (or (memq (get x 'byte-optimizer)
  60. ;; '(nil byte-compile-inline-expand))
  61. ;; (error
  62. ;; "%s already has a byte-optimizer, can't make it inline"
  63. ;; x))
  64. ;; (list 'put (list 'quote x)
  65. ;; ''byte-optimizer ''byte-compile-inline-expand))
  66. ;; fns)))
  67. ;; (defmacro proclaim-notinline (&rest fns)
  68. ;; "Cause the named functions to no longer be open-coded."
  69. ;; (cons 'eval-and-compile
  70. ;; (mapcar (lambda (x)
  71. ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
  72. ;; (put x 'byte-optimizer nil))
  73. ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
  74. ;; ''byte-compile-inline-expand)
  75. ;; (list 'put x ''byte-optimizer nil)))
  76. ;; fns)))
  77. ;; This has a special byte-hunk-handler in bytecomp.el.
  78. (defmacro defsubst (name arglist &rest body)
  79. "Define an inline function. The syntax is just like that of `defun'."
  80. (declare (debug defun))
  81. (or (memq (get name 'byte-optimizer)
  82. '(nil byte-compile-inline-expand))
  83. (error "`%s' is a primitive" name))
  84. `(prog1
  85. (defun ,name ,arglist ,@body)
  86. (eval-and-compile
  87. (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
  88. (defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
  89. (defun set-advertised-calling-convention (function signature when)
  90. "Set the advertised SIGNATURE of FUNCTION.
  91. This will allow the byte-compiler to warn the programmer when she uses
  92. an obsolete calling convention. WHEN specifies since when the calling
  93. convention was modified."
  94. (puthash (indirect-function function) signature
  95. advertised-signature-table))
  96. (defun make-obsolete (obsolete-name current-name &optional when)
  97. "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
  98. The warning will say that CURRENT-NAME should be used instead.
  99. If CURRENT-NAME is a string, that is the `use instead' message
  100. \(it should end with a period, and not start with a capital).
  101. WHEN should be a string indicating when the function
  102. was first made obsolete, for example a date or a release number."
  103. (interactive "aMake function obsolete: \nxObsoletion replacement: ")
  104. (put obsolete-name 'byte-obsolete-info
  105. ;; The second entry used to hold the `byte-compile' handler, but
  106. ;; is not used any more nowadays.
  107. (purecopy (list current-name nil when)))
  108. obsolete-name)
  109. (set-advertised-calling-convention
  110. ;; New code should always provide the `when' argument.
  111. 'make-obsolete '(obsolete-name current-name when) "23.1")
  112. (defmacro define-obsolete-function-alias (obsolete-name current-name
  113. &optional when docstring)
  114. "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
  115. \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
  116. is equivalent to the following two lines of code:
  117. \(defalias 'old-fun 'new-fun \"old-fun's doc.\")
  118. \(make-obsolete 'old-fun 'new-fun \"22.1\")
  119. See the docstrings of `defalias' and `make-obsolete' for more details."
  120. (declare (doc-string 4))
  121. `(progn
  122. (defalias ,obsolete-name ,current-name ,docstring)
  123. (make-obsolete ,obsolete-name ,current-name ,when)))
  124. (set-advertised-calling-convention
  125. ;; New code should always provide the `when' argument.
  126. 'define-obsolete-function-alias
  127. '(obsolete-name current-name when &optional docstring) "23.1")
  128. (defun make-obsolete-variable (obsolete-name current-name &optional when access-type)
  129. "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
  130. The warning will say that CURRENT-NAME should be used instead.
  131. If CURRENT-NAME is a string, that is the `use instead' message.
  132. WHEN should be a string indicating when the variable
  133. was first made obsolete, for example a date or a release number.
  134. ACCESS-TYPE if non-nil should specify the kind of access that will trigger
  135. obsolescence warnings; it can be either `get' or `set'."
  136. (put obsolete-name 'byte-obsolete-variable
  137. (purecopy (list current-name access-type when)))
  138. obsolete-name)
  139. (set-advertised-calling-convention
  140. ;; New code should always provide the `when' argument.
  141. 'make-obsolete-variable
  142. '(obsolete-name current-name when &optional access-type) "23.1")
  143. (defmacro define-obsolete-variable-alias (obsolete-name current-name
  144. &optional when docstring)
  145. "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
  146. This uses `defvaralias' and `make-obsolete-variable' (which see).
  147. See the Info node `(elisp)Variable Aliases' for more details.
  148. If CURRENT-NAME is a defcustom (more generally, any variable
  149. where OBSOLETE-NAME may be set, e.g. in a .emacs file, before the
  150. alias is defined), then the define-obsolete-variable-alias
  151. statement should be evaluated before the defcustom, if user
  152. customizations are to be respected. The simplest way to achieve
  153. this is to place the alias statement before the defcustom (this
  154. is not necessary for aliases that are autoloaded, or in files
  155. dumped with Emacs). This is so that any user customizations are
  156. applied before the defcustom tries to initialize the
  157. variable (this is due to the way `defvaralias' works).
  158. For the benefit of `custom-set-variables', if OBSOLETE-NAME has
  159. any of the following properties, they are copied to
  160. CURRENT-NAME, if it does not already have them:
  161. 'saved-value, 'saved-variable-comment."
  162. (declare (doc-string 4))
  163. `(progn
  164. (defvaralias ,obsolete-name ,current-name ,docstring)
  165. ;; See Bug#4706.
  166. (dolist (prop '(saved-value saved-variable-comment))
  167. (and (get ,obsolete-name prop)
  168. (null (get ,current-name prop))
  169. (put ,current-name prop (get ,obsolete-name prop))))
  170. (make-obsolete-variable ,obsolete-name ,current-name ,when)))
  171. (set-advertised-calling-convention
  172. ;; New code should always provide the `when' argument.
  173. 'define-obsolete-variable-alias
  174. '(obsolete-name current-name when &optional docstring) "23.1")
  175. ;; FIXME This is only defined in this file because the variable- and
  176. ;; function- versions are too. Unlike those two, this one is not used
  177. ;; by the byte-compiler (would be nice if it could warn about obsolete
  178. ;; faces, but it doesn't really do anything special with faces).
  179. ;; It only really affects M-x describe-face output.
  180. (defmacro define-obsolete-face-alias (obsolete-face current-face when)
  181. "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
  182. The string WHEN gives the Emacs version where OBSOLETE-FACE became
  183. obsolete."
  184. `(progn
  185. (put ,obsolete-face 'face-alias ,current-face)
  186. ;; Used by M-x describe-face.
  187. (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
  188. (defmacro dont-compile (&rest body)
  189. "Like `progn', but the body always runs interpreted (not compiled).
  190. If you think you need this, you're probably making a mistake somewhere."
  191. (declare (debug t) (indent 0))
  192. (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
  193. ;; interface to evaluating things at compile time and/or load time
  194. ;; these macro must come after any uses of them in this file, as their
  195. ;; definition in the file overrides the magic definitions on the
  196. ;; byte-compile-macro-environment.
  197. (defmacro eval-when-compile (&rest body)
  198. "Like `progn', but evaluates the body at compile time if you're compiling.
  199. Thus, the result of the body appears to the compiler as a quoted constant.
  200. In interpreted code, this is entirely equivalent to `progn'."
  201. (declare (debug t) (indent 0))
  202. ;; Not necessary because we have it in b-c-initial-macro-environment
  203. ;; (list 'quote (eval (cons 'progn body)))
  204. (cons 'progn body))
  205. (defmacro eval-and-compile (&rest body)
  206. "Like `progn', but evaluates the body at compile time and at load time."
  207. (declare (debug t) (indent 0))
  208. ;; Remember, it's magic.
  209. (cons 'progn body))
  210. (put 'with-no-warnings 'lisp-indent-function 0)
  211. (defun with-no-warnings (&rest body)
  212. "Like `progn', but prevents compiler warnings in the body."
  213. ;; The implementation for the interpreter is basically trivial.
  214. (car (last body)))
  215. ;; I nuked this because it's not a good idea for users to think of using it.
  216. ;; These options are a matter of installation preference, and have nothing to
  217. ;; with particular source files; it's a mistake to suggest to users
  218. ;; they should associate these with particular source files.
  219. ;; There is hardly any reason to change these parameters, anyway.
  220. ;; --rms.
  221. ;; (put 'byte-compiler-options 'lisp-indent-function 0)
  222. ;; (defmacro byte-compiler-options (&rest args)
  223. ;; "Set some compilation-parameters for this file. This will affect only the
  224. ;; file in which it appears; this does nothing when evaluated, and when loaded
  225. ;; from a .el file.
  226. ;;
  227. ;; Each argument to this macro must be a list of a key and a value.
  228. ;;
  229. ;; Keys: Values: Corresponding variable:
  230. ;;
  231. ;; verbose t, nil byte-compile-verbose
  232. ;; optimize t, nil, source, byte byte-compile-optimize
  233. ;; warnings list of warnings byte-compile-warnings
  234. ;; Valid elements: (callargs redefine free-vars unresolved)
  235. ;; file-format emacs18, emacs19 byte-compile-compatibility
  236. ;;
  237. ;; For example, this might appear at the top of a source file:
  238. ;;
  239. ;; (byte-compiler-options
  240. ;; (optimize t)
  241. ;; (warnings (- free-vars)) ; Don't warn about free variables
  242. ;; (file-format emacs19))"
  243. ;; nil)
  244. ;;; byte-run.el ends here