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