Merge profiler branch
[bpt/emacs.git] / lisp / emacs-lisp / byte-run.el
1 ;;; byte-run.el --- byte-compiler support for inlining -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1992, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Maintainer: FSF
8 ;; Keywords: internal
9 ;; Package: emacs
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 ;; `macro-declaration-function' are both obsolete (as marked at the end of this
34 ;; file) but used in many .elc files.
35
36 (defvar macro-declaration-function #'macro-declaration-function
37 "Function to process declarations in a macro definition.
38 The function will be called with two args MACRO and DECL.
39 MACRO is the name of the macro being defined.
40 DECL is a list `(declare ...)' containing the declarations.
41 The value the function returns is not used.")
42
43 (defalias 'macro-declaration-function
44 #'(lambda (macro decl)
45 "Process a declaration found in a macro definition.
46 This is set as the value of the variable `macro-declaration-function'.
47 MACRO is the name of the macro being defined.
48 DECL is a list `(declare ...)' containing the declarations.
49 The return value of this function is not used."
50 ;; We can't use `dolist' or `cadr' yet for bootstrapping reasons.
51 (let (d)
52 ;; Ignore the first element of `decl' (it's always `declare').
53 (while (setq decl (cdr decl))
54 (setq d (car decl))
55 (if (and (consp d)
56 (listp (cdr d))
57 (null (cdr (cdr d))))
58 (cond ((eq (car d) 'indent)
59 (put macro 'lisp-indent-function (car (cdr d))))
60 ((eq (car d) 'debug)
61 (put macro 'edebug-form-spec (car (cdr d))))
62 ((eq (car d) 'doc-string)
63 (put macro 'doc-string-elt (car (cdr d))))
64 (t
65 (message "Unknown declaration %s" d)))
66 (message "Invalid declaration %s" d))))))
67
68 ;; We define macro-declaration-alist here because it is needed to
69 ;; handle declarations in macro definitions and this is the first file
70 ;; loaded by loadup.el that uses declarations in macros.
71
72 (defvar defun-declarations-alist
73 (list
74 ;; We can only use backquotes inside the lambdas and not for those
75 ;; properties that are used by functions loaded before backquote.el.
76 (list 'advertised-calling-convention
77 #'(lambda (f _args arglist when)
78 (list 'set-advertised-calling-convention
79 (list 'quote f) (list 'quote arglist) (list 'quote when))))
80 (list 'obsolete
81 #'(lambda (f _args new-name when)
82 `(make-obsolete ',f ',new-name ,when)))
83 (list 'compiler-macro
84 #'(lambda (f _args compiler-function)
85 `(put ',f 'compiler-macro #',compiler-function)))
86 (list 'doc-string
87 #'(lambda (f _args pos)
88 (list 'put (list 'quote f) ''doc-string-elt (list 'quote pos))))
89 (list 'indent
90 #'(lambda (f _args val)
91 (list 'put (list 'quote f)
92 ''lisp-indent-function (list 'quote val)))))
93 "List associating function properties to their macro expansion.
94 Each element of the list takes the form (PROP FUN) where FUN is
95 a function. For each (PROP . VALUES) in a function's declaration,
96 the FUN corresponding to PROP is called with the function name,
97 the function's arglist, and the VALUES and should return the code to use
98 to set this property.")
99
100 (defvar macro-declarations-alist
101 (cons
102 (list 'debug
103 #'(lambda (name _args spec)
104 (list 'progn :autoload-end
105 (list 'put (list 'quote name)
106 ''edebug-form-spec (list 'quote spec)))))
107 defun-declarations-alist)
108 "List associating properties of macros to their macro expansion.
109 Each element of the list takes the form (PROP FUN) where FUN is
110 a function. For each (PROP . VALUES) in a macro's declaration,
111 the FUN corresponding to PROP is called with the function name
112 and the VALUES and should return the code to use to set this property.")
113
114 (put 'defmacro 'doc-string-elt 3)
115 (defalias 'defmacro
116 (cons
117 'macro
118 #'(lambda (name arglist &optional docstring decl &rest body)
119 "Define NAME as a macro.
120 When the macro is called, as in (NAME ARGS...),
121 the function (lambda ARGLIST BODY...) is applied to
122 the list ARGS... as it appears in the expression,
123 and the result should be a form to be evaluated instead of the original.
124 DECL is a declaration, optional, of the form (declare DECLS...) where
125 DECLS is a list of elements of the form (PROP . VALUES). These are
126 interpreted according to `macro-declarations-alist'.
127 The return value is undefined."
128 (if (stringp docstring) nil
129 (if decl (setq body (cons decl body)))
130 (setq decl docstring)
131 (setq docstring nil))
132 (if (or (null decl) (eq 'declare (car-safe decl))) nil
133 (setq body (cons decl body))
134 (setq decl nil))
135 (if (null body) (setq body '(nil)))
136 (if docstring (setq body (cons docstring body)))
137 ;; Can't use backquote because it's not defined yet!
138 (let* ((fun (list 'function (cons 'lambda (cons arglist body))))
139 (def (list 'defalias
140 (list 'quote name)
141 (list 'cons ''macro fun)))
142 (declarations
143 (mapcar
144 #'(lambda (x)
145 (let ((f (cdr (assq (car x) macro-declarations-alist))))
146 (if f (apply (car f) name arglist (cdr x))
147 (message "Warning: Unknown macro property %S in %S"
148 (car x) name))))
149 (cdr decl))))
150 (if declarations
151 (cons 'prog1 (cons def declarations))
152 def)))))
153
154 ;; Now that we defined defmacro we can use it!
155 (defmacro defun (name arglist &optional docstring &rest body)
156 "Define NAME as a function.
157 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
158 See also the function `interactive'.
159 DECL is a declaration, optional, of the form (declare DECLS...) where
160 DECLS is a list of elements of the form (PROP . VALUES). These are
161 interpreted according to `defun-declarations-alist'.
162 The return value is undefined.
163
164 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
165 ;; We can't just have `decl' as an &optional argument, because we need
166 ;; to distinguish
167 ;; (defun foo (arg) (toto) nil)
168 ;; from
169 ;; (defun foo (arg) (toto)).
170 (declare (doc-string 3))
171 (let ((decls (cond
172 ((eq (car-safe docstring) 'declare)
173 (prog1 (cdr docstring) (setq docstring nil)))
174 ((eq (car-safe (car body)) 'declare)
175 (prog1 (cdr (car body)) (setq body (cdr body)))))))
176 (if docstring (setq body (cons docstring body))
177 (if (null body) (setq body '(nil))))
178 (let ((declarations
179 (mapcar
180 #'(lambda (x)
181 (let ((f (cdr (assq (car x) defun-declarations-alist))))
182 (cond
183 (f (apply (car f) name arglist (cdr x)))
184 ;; Yuck!!
185 ((and (featurep 'cl)
186 (memq (car x) ;C.f. cl-do-proclaim.
187 '(special inline notinline optimize warn)))
188 (push (list 'declare x)
189 (if (stringp docstring) (cdr body) body))
190 nil)
191 (t (message "Warning: Unknown defun property `%S' in %S"
192 (car x) name)))))
193 decls))
194 (def (list 'defalias
195 (list 'quote name)
196 (list 'function
197 (cons 'lambda
198 (cons arglist body))))))
199 (if declarations
200 (cons 'prog1 (cons def declarations))
201 def))))
202 \f
203 ;; Redefined in byte-optimize.el.
204 ;; This is not documented--it's not clear that we should promote it.
205 (fset 'inline 'progn)
206
207 ;;; Interface to inline functions.
208
209 ;; (defmacro proclaim-inline (&rest fns)
210 ;; "Cause the named functions to be open-coded when called from compiled code.
211 ;; They will only be compiled open-coded when byte-compile-optimize is true."
212 ;; (cons 'eval-and-compile
213 ;; (mapcar (lambda (x)
214 ;; (or (memq (get x 'byte-optimizer)
215 ;; '(nil byte-compile-inline-expand))
216 ;; (error
217 ;; "%s already has a byte-optimizer, can't make it inline"
218 ;; x))
219 ;; (list 'put (list 'quote x)
220 ;; ''byte-optimizer ''byte-compile-inline-expand))
221 ;; fns)))
222
223 ;; (defmacro proclaim-notinline (&rest fns)
224 ;; "Cause the named functions to no longer be open-coded."
225 ;; (cons 'eval-and-compile
226 ;; (mapcar (lambda (x)
227 ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
228 ;; (put x 'byte-optimizer nil))
229 ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
230 ;; ''byte-compile-inline-expand)
231 ;; (list 'put x ''byte-optimizer nil)))
232 ;; fns)))
233
234 (defmacro defsubst (name arglist &rest body)
235 "Define an inline function. The syntax is just like that of `defun'."
236 (declare (debug defun) (doc-string 3))
237 (or (memq (get name 'byte-optimizer)
238 '(nil byte-compile-inline-expand))
239 (error "`%s' is a primitive" name))
240 `(prog1
241 (defun ,name ,arglist ,@body)
242 (eval-and-compile
243 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
244
245 (defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
246
247 (defun set-advertised-calling-convention (function signature _when)
248 "Set the advertised SIGNATURE of FUNCTION.
249 This will allow the byte-compiler to warn the programmer when she uses
250 an obsolete calling convention. WHEN specifies since when the calling
251 convention was modified."
252 (puthash (indirect-function function) signature
253 advertised-signature-table))
254
255 (defun make-obsolete (obsolete-name current-name &optional when)
256 "Make the byte-compiler warn that function OBSOLETE-NAME is obsolete.
257 OBSOLETE-NAME should be a function name or macro name (a symbol).
258
259 The warning will say that CURRENT-NAME should be used instead.
260 If CURRENT-NAME is a string, that is the `use instead' message
261 \(it should end with a period, and not start with a capital).
262 WHEN should be a string indicating when the function
263 was first made obsolete, for example a date or a release number."
264 (declare (advertised-calling-convention
265 ;; New code should always provide the `when' argument.
266 (obsolete-name current-name when) "23.1"))
267 (interactive "aMake function obsolete: \nxObsoletion replacement: ")
268 (put obsolete-name 'byte-obsolete-info
269 ;; The second entry used to hold the `byte-compile' handler, but
270 ;; is not used any more nowadays.
271 (purecopy (list current-name nil when)))
272 obsolete-name)
273
274 (defmacro define-obsolete-function-alias (obsolete-name current-name
275 &optional when docstring)
276 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
277
278 \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
279
280 is equivalent to the following two lines of code:
281
282 \(defalias 'old-fun 'new-fun \"old-fun's doc.\")
283 \(make-obsolete 'old-fun 'new-fun \"22.1\")
284
285 See the docstrings of `defalias' and `make-obsolete' for more details."
286 (declare (doc-string 4)
287 (advertised-calling-convention
288 ;; New code should always provide the `when' argument.
289 (obsolete-name current-name when &optional docstring) "23.1"))
290 `(progn
291 (defalias ,obsolete-name ,current-name ,docstring)
292 (make-obsolete ,obsolete-name ,current-name ,when)))
293
294 (defun make-obsolete-variable (obsolete-name current-name &optional when access-type)
295 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
296 The warning will say that CURRENT-NAME should be used instead.
297 If CURRENT-NAME is a string, that is the `use instead' message.
298 WHEN should be a string indicating when the variable
299 was first made obsolete, for example a date or a release number.
300 ACCESS-TYPE if non-nil should specify the kind of access that will trigger
301 obsolescence warnings; it can be either `get' or `set'."
302 (declare (advertised-calling-convention
303 ;; New code should always provide the `when' argument.
304 (obsolete-name current-name when &optional access-type) "23.1"))
305 (put obsolete-name 'byte-obsolete-variable
306 (purecopy (list current-name access-type when)))
307 obsolete-name)
308
309
310 (defmacro define-obsolete-variable-alias (obsolete-name current-name
311 &optional when docstring)
312 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
313 This uses `defvaralias' and `make-obsolete-variable' (which see).
314 See the Info node `(elisp)Variable Aliases' for more details.
315
316 If CURRENT-NAME is a defcustom (more generally, any variable
317 where OBSOLETE-NAME may be set, e.g. in an init file, before the
318 alias is defined), then the define-obsolete-variable-alias
319 statement should be evaluated before the defcustom, if user
320 customizations are to be respected. The simplest way to achieve
321 this is to place the alias statement before the defcustom (this
322 is not necessary for aliases that are autoloaded, or in files
323 dumped with Emacs). This is so that any user customizations are
324 applied before the defcustom tries to initialize the
325 variable (this is due to the way `defvaralias' works).
326
327 For the benefit of `custom-set-variables', if OBSOLETE-NAME has
328 any of the following properties, they are copied to
329 CURRENT-NAME, if it does not already have them:
330 'saved-value, 'saved-variable-comment."
331 (declare (doc-string 4)
332 (advertised-calling-convention
333 ;; New code should always provide the `when' argument.
334 (obsolete-name current-name when &optional docstring) "23.1"))
335 `(progn
336 (defvaralias ,obsolete-name ,current-name ,docstring)
337 ;; See Bug#4706.
338 (dolist (prop '(saved-value saved-variable-comment))
339 (and (get ,obsolete-name prop)
340 (null (get ,current-name prop))
341 (put ,current-name prop (get ,obsolete-name prop))))
342 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
343
344 ;; FIXME This is only defined in this file because the variable- and
345 ;; function- versions are too. Unlike those two, this one is not used
346 ;; by the byte-compiler (would be nice if it could warn about obsolete
347 ;; faces, but it doesn't really do anything special with faces).
348 ;; It only really affects M-x describe-face output.
349 (defmacro define-obsolete-face-alias (obsolete-face current-face when)
350 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
351 The string WHEN gives the Emacs version where OBSOLETE-FACE became
352 obsolete."
353 `(progn
354 (put ,obsolete-face 'face-alias ,current-face)
355 ;; Used by M-x describe-face.
356 (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
357
358 (defmacro dont-compile (&rest body)
359 "Like `progn', but the body always runs interpreted (not compiled).
360 If you think you need this, you're probably making a mistake somewhere."
361 (declare (debug t) (indent 0))
362 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
363
364 \f
365 ;; interface to evaluating things at compile time and/or load time
366 ;; these macro must come after any uses of them in this file, as their
367 ;; definition in the file overrides the magic definitions on the
368 ;; byte-compile-macro-environment.
369
370 (defmacro eval-when-compile (&rest body)
371 "Like `progn', but evaluates the body at compile time if you're compiling.
372 Thus, the result of the body appears to the compiler as a quoted constant.
373 In interpreted code, this is entirely equivalent to `progn'."
374 (declare (debug t) (indent 0))
375 ;; Not necessary because we have it in b-c-initial-macro-environment
376 ;; (list 'quote (eval (cons 'progn body)))
377 (cons 'progn body))
378
379 (defmacro eval-and-compile (&rest body)
380 "Like `progn', but evaluates the body at compile time and at load time."
381 (declare (debug t) (indent 0))
382 ;; Remember, it's magic.
383 (cons 'progn body))
384
385 (put 'with-no-warnings 'lisp-indent-function 0)
386 (defun with-no-warnings (&rest body)
387 "Like `progn', but prevents compiler warnings in the body."
388 ;; The implementation for the interpreter is basically trivial.
389 (car (last body)))
390
391 \f
392 ;; I nuked this because it's not a good idea for users to think of using it.
393 ;; These options are a matter of installation preference, and have nothing to
394 ;; with particular source files; it's a mistake to suggest to users
395 ;; they should associate these with particular source files.
396 ;; There is hardly any reason to change these parameters, anyway.
397 ;; --rms.
398
399 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
400 ;; (defmacro byte-compiler-options (&rest args)
401 ;; "Set some compilation-parameters for this file. This will affect only the
402 ;; file in which it appears; this does nothing when evaluated, and when loaded
403 ;; from a .el file.
404 ;;
405 ;; Each argument to this macro must be a list of a key and a value.
406 ;;
407 ;; Keys: Values: Corresponding variable:
408 ;;
409 ;; verbose t, nil byte-compile-verbose
410 ;; optimize t, nil, source, byte byte-compile-optimize
411 ;; warnings list of warnings byte-compile-warnings
412 ;; Valid elements: (callargs redefine free-vars unresolved)
413 ;; file-format emacs18, emacs19 byte-compile-compatibility
414 ;;
415 ;; For example, this might appear at the top of a source file:
416 ;;
417 ;; (byte-compiler-options
418 ;; (optimize t)
419 ;; (warnings (- free-vars)) ; Don't warn about free variables
420 ;; (file-format emacs19))"
421 ;; nil)
422
423 (make-obsolete-variable 'macro-declaration-function
424 'macro-declarations-alist "24.3")
425 (make-obsolete 'macro-declaration-function
426 'macro-declarations-alist "24.3")
427
428 ;;; byte-run.el ends here