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