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