* lisp/emacs-lisp/byte-run.el (defmacro, defun): Move from C.
[bpt/emacs.git] / lisp / emacs-lisp / byte-run.el
1 ;;; byte-run.el --- byte-compiler support for inlining
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 ;; 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 (defvar macro-declaration-function #'macro-declaration-function
38 "Function to process declarations in a macro definition.
39 The function will be called with two args MACRO and DECL.
40 MACRO is the name of the macro being defined.
41 DECL is a list `(declare ...)' containing the declarations.
42 The 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.
47 This is set as the value of the variable `macro-declaration-function'.
48 MACRO is the name of the macro being defined.
49 DECL is a list `(declare ...)' containing the declarations.
50 The return value of this function is not used."
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.
75 When the macro is called, as in (NAME ARGS...),
76 the function (lambda ARGLIST BODY...) is applied to
77 the list ARGS... as it appears in the expression,
78 and the result should be a form to be evaluated instead of the original.
79
80 DECL is a declaration, optional, which can specify how to indent
81 calls to this macro, how Edebug should handle it, and which argument
82 should be treated as documentation. It looks like this:
83 (declare SPECS...)
84 The 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.
119 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
120 See 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)))))
129 \f
130 ;; Redefined in byte-optimize.el.
131 ;; This is not documented--it's not clear that we should promote it.
132 (fset 'inline 'progn)
133
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
140 ;; (mapcar (lambda (x)
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
153 ;; (mapcar (lambda (x)
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'."
164 (declare (debug defun) (doc-string 3))
165 (or (memq (get name 'byte-optimizer)
166 '(nil byte-compile-inline-expand))
167 (error "`%s' is a primitive" name))
168 `(prog1
169 (defun ,name ,arglist ,@body)
170 (eval-and-compile
171 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
172
173 (defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
174
175 (defun set-advertised-calling-convention (function signature when)
176 "Set the advertised SIGNATURE of FUNCTION.
177 This will allow the byte-compiler to warn the programmer when she uses
178 an obsolete calling convention. WHEN specifies since when the calling
179 convention was modified."
180 (puthash (indirect-function function) signature
181 advertised-signature-table))
182
183 (defun make-obsolete (obsolete-name current-name &optional when)
184 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
185 The warning will say that CURRENT-NAME should be used instead.
186 If CURRENT-NAME is a string, that is the `use instead' message
187 \(it should end with a period, and not start with a capital).
188 WHEN should be a string indicating when the function
189 was first made obsolete, for example a date or a release number."
190 (interactive "aMake function obsolete: \nxObsoletion replacement: ")
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.
194 (purecopy (list current-name nil when)))
195 obsolete-name)
196 (set-advertised-calling-convention
197 ;; New code should always provide the `when' argument.
198 'make-obsolete '(obsolete-name current-name when) "23.1")
199
200 (defmacro define-obsolete-function-alias (obsolete-name current-name
201 &optional when docstring)
202 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
203
204 \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
205
206 is 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
211 See the docstrings of `defalias' and `make-obsolete' for more details."
212 (declare (doc-string 4))
213 `(progn
214 (defalias ,obsolete-name ,current-name ,docstring)
215 (make-obsolete ,obsolete-name ,current-name ,when)))
216 (set-advertised-calling-convention
217 ;; New code should always provide the `when' argument.
218 'define-obsolete-function-alias
219 '(obsolete-name current-name when &optional docstring) "23.1")
220
221 (defun make-obsolete-variable (obsolete-name current-name &optional when access-type)
222 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
223 The warning will say that CURRENT-NAME should be used instead.
224 If CURRENT-NAME is a string, that is the `use instead' message.
225 WHEN should be a string indicating when the variable
226 was first made obsolete, for example a date or a release number.
227 ACCESS-TYPE if non-nil should specify the kind of access that will trigger
228 obsolescence warnings; it can be either `get' or `set'."
229 (put obsolete-name 'byte-obsolete-variable
230 (purecopy (list current-name access-type when)))
231 obsolete-name)
232 (set-advertised-calling-convention
233 ;; New code should always provide the `when' argument.
234 'make-obsolete-variable
235 '(obsolete-name current-name when &optional access-type) "23.1")
236
237 (defmacro define-obsolete-variable-alias (obsolete-name current-name
238 &optional when docstring)
239 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
240 This uses `defvaralias' and `make-obsolete-variable' (which see).
241 See the Info node `(elisp)Variable Aliases' for more details.
242
243 If CURRENT-NAME is a defcustom (more generally, any variable
244 where OBSOLETE-NAME may be set, e.g. in a .emacs file, before the
245 alias is defined), then the define-obsolete-variable-alias
246 statement should be evaluated before the defcustom, if user
247 customizations are to be respected. The simplest way to achieve
248 this is to place the alias statement before the defcustom (this
249 is not necessary for aliases that are autoloaded, or in files
250 dumped with Emacs). This is so that any user customizations are
251 applied before the defcustom tries to initialize the
252 variable (this is due to the way `defvaralias' works).
253
254 For the benefit of `custom-set-variables', if OBSOLETE-NAME has
255 any of the following properties, they are copied to
256 CURRENT-NAME, if it does not already have them:
257 'saved-value, 'saved-variable-comment."
258 (declare (doc-string 4))
259 `(progn
260 (defvaralias ,obsolete-name ,current-name ,docstring)
261 ;; See Bug#4706.
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))))
266 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
267 (set-advertised-calling-convention
268 ;; New code should always provide the `when' argument.
269 'define-obsolete-variable-alias
270 '(obsolete-name current-name when &optional docstring) "23.1")
271
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.
277 (defmacro define-obsolete-face-alias (obsolete-face current-face when)
278 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
279 The string WHEN gives the Emacs version where OBSOLETE-FACE became
280 obsolete."
281 `(progn
282 (put ,obsolete-face 'face-alias ,current-face)
283 ;; Used by M-x describe-face.
284 (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
285
286 (defmacro dont-compile (&rest body)
287 "Like `progn', but the body always runs interpreted (not compiled).
288 If you think you need this, you're probably making a mistake somewhere."
289 (declare (debug t) (indent 0))
290 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
291
292 \f
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.
297
298 (defmacro eval-when-compile (&rest body)
299 "Like `progn', but evaluates the body at compile time if you're compiling.
300 Thus, the result of the body appears to the compiler as a quoted constant.
301 In interpreted code, this is entirely equivalent to `progn'."
302 (declare (debug t) (indent 0))
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
307 (defmacro eval-and-compile (&rest body)
308 "Like `progn', but evaluates the body at compile time and at load time."
309 (declare (debug t) (indent 0))
310 ;; Remember, it's magic.
311 (cons 'progn body))
312
313 (put 'with-no-warnings 'lisp-indent-function 0)
314 (defun with-no-warnings (&rest body)
315 "Like `progn', but prevents compiler warnings in the body."
316 ;; The implementation for the interpreter is basically trivial.
317 (car (last body)))
318
319 \f
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.
326
327 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
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
340 ;; Valid elements: (callargs redefine free-vars unresolved)
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