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