Commit | Line | Data |
---|---|---|
5e046f6d JB |
1 | ;;; byte-run.el --- byte-compiler support for inlining |
2 | ||
d59c3137 | 3 | ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005, |
8b72699e | 4 | ;; 2006, 2007, 2008 Free Software Foundation, Inc. |
5e046f6d JB |
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 | ||
d6cba7ae | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
5e046f6d | 14 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
5e046f6d JB |
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 | |
d6cba7ae | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
5e046f6d JB |
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 | ||
623374a5 LK |
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 | (cond ((and (consp d) (eq (car d) 'indent)) | |
49 | (put macro 'lisp-indent-function (car (cdr d)))) | |
50 | ((and (consp d) (eq (car d) 'debug)) | |
51 | (put macro 'edebug-form-spec (car (cdr d)))) | |
38729cfb SM |
52 | ((and (consp d) (eq (car d) 'doc-string)) |
53 | (put macro 'doc-string-elt (car (cdr d)))) | |
623374a5 LK |
54 | (t |
55 | (message "Unknown declaration %s" d)))))) | |
56 | ||
57 | (setq macro-declaration-function 'macro-declaration-function) | |
58 | ||
59 | \f | |
5e046f6d JB |
60 | ;; Redefined in byte-optimize.el. |
61 | ;; This is not documented--it's not clear that we should promote it. | |
62 | (fset 'inline 'progn) | |
3fdfb09c | 63 | (put 'inline 'lisp-indent-function 0) |
5e046f6d | 64 | |
5e046f6d JB |
65 | ;;; Interface to inline functions. |
66 | ||
67 | ;; (defmacro proclaim-inline (&rest fns) | |
68 | ;; "Cause the named functions to be open-coded when called from compiled code. | |
69 | ;; They will only be compiled open-coded when byte-compile-optimize is true." | |
70 | ;; (cons 'eval-and-compile | |
71 | ;; (mapcar '(lambda (x) | |
72 | ;; (or (memq (get x 'byte-optimizer) | |
73 | ;; '(nil byte-compile-inline-expand)) | |
74 | ;; (error | |
75 | ;; "%s already has a byte-optimizer, can't make it inline" | |
76 | ;; x)) | |
77 | ;; (list 'put (list 'quote x) | |
78 | ;; ''byte-optimizer ''byte-compile-inline-expand)) | |
79 | ;; fns))) | |
80 | ||
81 | ;; (defmacro proclaim-notinline (&rest fns) | |
82 | ;; "Cause the named functions to no longer be open-coded." | |
83 | ;; (cons 'eval-and-compile | |
84 | ;; (mapcar '(lambda (x) | |
85 | ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand) | |
86 | ;; (put x 'byte-optimizer nil)) | |
87 | ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer) | |
88 | ;; ''byte-compile-inline-expand) | |
89 | ;; (list 'put x ''byte-optimizer nil))) | |
90 | ;; fns))) | |
91 | ||
92 | ;; This has a special byte-hunk-handler in bytecomp.el. | |
93 | (defmacro defsubst (name arglist &rest body) | |
94 | "Define an inline function. The syntax is just like that of `defun'." | |
66599b54 | 95 | (declare (debug defun)) |
5e046f6d JB |
96 | (or (memq (get name 'byte-optimizer) |
97 | '(nil byte-compile-inline-expand)) | |
98 | (error "`%s' is a primitive" name)) | |
66599b54 SM |
99 | `(prog1 |
100 | (defun ,name ,arglist ,@body) | |
101 | (eval-and-compile | |
102 | (put ',name 'byte-optimizer 'byte-compile-inline-expand)))) | |
5e046f6d | 103 | |
fbb70c53 JB |
104 | (defun make-obsolete (obsolete-name current-name &optional when) |
105 | "Make the byte-compiler warn that OBSOLETE-NAME is obsolete. | |
106 | The warning will say that CURRENT-NAME should be used instead. | |
584dcd8f GM |
107 | If CURRENT-NAME is a string, that is the `use instead' message |
108 | \(it should end with a period, and not start with a capital). | |
5e046f6d JB |
109 | If provided, WHEN should be a string indicating when the function |
110 | was first made obsolete, for example a date or a release number." | |
111 | (interactive "aMake function obsolete: \nxObsoletion replacement: ") | |
fbb70c53 | 112 | (let ((handler (get obsolete-name 'byte-compile))) |
5e046f6d | 113 | (if (eq 'byte-compile-obsolete handler) |
fbb70c53 JB |
114 | (setq handler (nth 1 (get obsolete-name 'byte-obsolete-info))) |
115 | (put obsolete-name 'byte-compile 'byte-compile-obsolete)) | |
116 | (put obsolete-name 'byte-obsolete-info (list current-name handler when))) | |
117 | obsolete-name) | |
5e046f6d | 118 | |
fbb70c53 | 119 | (defmacro define-obsolete-function-alias (obsolete-name current-name |
0448b476 | 120 | &optional when docstring) |
fbb70c53 | 121 | "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete. |
342ef03d LT |
122 | |
123 | \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\") | |
124 | ||
125 | is equivalent to the following two lines of code: | |
126 | ||
127 | \(defalias 'old-fun 'new-fun \"old-fun's doc.\") | |
128 | \(make-obsolete 'old-fun 'new-fun \"22.1\") | |
129 | ||
130 | See the docstrings of `defalias' and `make-obsolete' for more details." | |
79e74246 | 131 | (declare (doc-string 4)) |
0448b476 | 132 | `(progn |
fbb70c53 JB |
133 | (defalias ,obsolete-name ,current-name ,docstring) |
134 | (make-obsolete ,obsolete-name ,current-name ,when))) | |
0448b476 | 135 | |
fbb70c53 JB |
136 | (defun make-obsolete-variable (obsolete-name current-name &optional when) |
137 | "Make the byte-compiler warn that OBSOLETE-NAME is obsolete. | |
138 | The warning will say that CURRENT-NAME should be used instead. | |
139 | If CURRENT-NAME is a string, that is the `use instead' message. | |
5e046f6d JB |
140 | If provided, WHEN should be a string indicating when the variable |
141 | was first made obsolete, for example a date or a release number." | |
142 | (interactive | |
143 | (list | |
144 | (let ((str (completing-read "Make variable obsolete: " obarray 'boundp t))) | |
145 | (if (equal str "") (error "")) | |
146 | (intern str)) | |
147 | (car (read-from-string (read-string "Obsoletion replacement: "))))) | |
fbb70c53 JB |
148 | (put obsolete-name 'byte-obsolete-variable (cons current-name when)) |
149 | obsolete-name) | |
5e046f6d | 150 | |
fbb70c53 | 151 | (defmacro define-obsolete-variable-alias (obsolete-name current-name |
f7f8f37a | 152 | &optional when docstring) |
fbb70c53 | 153 | "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete. |
342ef03d LT |
154 | |
155 | \(define-obsolete-variable-alias 'old-var 'new-var \"22.1\" \"old-var's doc.\") | |
156 | ||
157 | is equivalent to the following two lines of code: | |
158 | ||
159 | \(defvaralias 'old-var 'new-var \"old-var's doc.\") | |
160 | \(make-obsolete-variable 'old-var 'new-var \"22.1\") | |
161 | ||
f8754ca2 GM |
162 | If CURRENT-NAME is a defcustom (more generally, any variable |
163 | where OBSOLETE-NAME may be set, e.g. in a .emacs file, before the | |
164 | alias is defined), then the define-obsolete-variable-alias | |
165 | statement should be placed before the defcustom. This is so that | |
166 | any user customizations are applied before the defcustom tries to | |
167 | initialize the variable (this is due to the way `defvaralias' works). | |
168 | Exceptions to this rule occur for define-obsolete-variable-alias | |
169 | statements that are autoloaded, or in files dumped with Emacs. | |
170 | ||
342ef03d LT |
171 | See the docstrings of `defvaralias' and `make-obsolete-variable' or |
172 | Info node `(elisp)Variable Aliases' for more details." | |
79e74246 | 173 | (declare (doc-string 4)) |
f7f8f37a | 174 | `(progn |
fbb70c53 | 175 | (defvaralias ,obsolete-name ,current-name ,docstring) |
79e74246 | 176 | (make-obsolete-variable ,obsolete-name ,current-name ,when))) |
f7f8f37a | 177 | |
5e046f6d JB |
178 | (defmacro dont-compile (&rest body) |
179 | "Like `progn', but the body always runs interpreted (not compiled). | |
180 | If you think you need this, you're probably making a mistake somewhere." | |
623374a5 | 181 | (declare (debug t) (indent 0)) |
5e046f6d JB |
182 | (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body))))) |
183 | ||
184 | \f | |
79e74246 SM |
185 | ;; interface to evaluating things at compile time and/or load time |
186 | ;; these macro must come after any uses of them in this file, as their | |
187 | ;; definition in the file overrides the magic definitions on the | |
188 | ;; byte-compile-macro-environment. | |
5e046f6d | 189 | |
5e046f6d | 190 | (defmacro eval-when-compile (&rest body) |
8cd567b8 RS |
191 | "Like `progn', but evaluates the body at compile time if you're compiling. |
192 | Thus, the result of the body appears to the compiler as a quoted constant. | |
193 | In interpreted code, this is entirely equivalent to `progn'." | |
623374a5 | 194 | (declare (debug t) (indent 0)) |
5e046f6d JB |
195 | ;; Not necessary because we have it in b-c-initial-macro-environment |
196 | ;; (list 'quote (eval (cons 'progn body))) | |
197 | (cons 'progn body)) | |
198 | ||
5e046f6d JB |
199 | (defmacro eval-and-compile (&rest body) |
200 | "Like `progn', but evaluates the body at compile time and at load time." | |
623374a5 | 201 | (declare (debug t) (indent 0)) |
5e046f6d JB |
202 | ;; Remember, it's magic. |
203 | (cons 'progn body)) | |
204 | ||
3fdfb09c | 205 | (put 'with-no-warnings 'lisp-indent-function 0) |
ae122ad2 | 206 | (defun with-no-warnings (&rest body) |
5e046f6d JB |
207 | "Like `progn', but prevents compiler warnings in the body." |
208 | ;; The implementation for the interpreter is basically trivial. | |
ae122ad2 | 209 | (car (last body))) |
5e046f6d JB |
210 | |
211 | \f | |
79e74246 SM |
212 | ;; I nuked this because it's not a good idea for users to think of using it. |
213 | ;; These options are a matter of installation preference, and have nothing to | |
214 | ;; with particular source files; it's a mistake to suggest to users | |
215 | ;; they should associate these with particular source files. | |
216 | ;; There is hardly any reason to change these parameters, anyway. | |
217 | ;; --rms. | |
5e046f6d | 218 | |
3fdfb09c | 219 | ;; (put 'byte-compiler-options 'lisp-indent-function 0) |
5e046f6d JB |
220 | ;; (defmacro byte-compiler-options (&rest args) |
221 | ;; "Set some compilation-parameters for this file. This will affect only the | |
222 | ;; file in which it appears; this does nothing when evaluated, and when loaded | |
223 | ;; from a .el file. | |
224 | ;; | |
225 | ;; Each argument to this macro must be a list of a key and a value. | |
226 | ;; | |
227 | ;; Keys: Values: Corresponding variable: | |
228 | ;; | |
229 | ;; verbose t, nil byte-compile-verbose | |
230 | ;; optimize t, nil, source, byte byte-compile-optimize | |
231 | ;; warnings list of warnings byte-compile-warnings | |
9b9a4122 | 232 | ;; Valid elements: (callargs redefine free-vars unresolved) |
5e046f6d JB |
233 | ;; file-format emacs18, emacs19 byte-compile-compatibility |
234 | ;; | |
235 | ;; For example, this might appear at the top of a source file: | |
236 | ;; | |
237 | ;; (byte-compiler-options | |
238 | ;; (optimize t) | |
239 | ;; (warnings (- free-vars)) ; Don't warn about free variables | |
240 | ;; (file-format emacs19))" | |
241 | ;; nil) | |
242 | ||
79e74246 | 243 | ;; arch-tag: 76f8328a-1f66-4df2-9b6d-5c3666dc05e9 |
5e046f6d | 244 | ;;; byte-run.el ends here |