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