(declare-function): Doc fix.
[emacs.git] / lisp / emacs-lisp / byte-run.el
blob2ba708d9c921754f25c35ee68912330a04fb546b
1 ;;; byte-run.el --- byte-compiler support for inlining
3 ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Jamie Zawinski <jwz@lucid.com>
7 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
8 ;; Maintainer: FSF
9 ;; Keywords: internal
11 ;; This file is part of GNU Emacs.
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, or (at your option)
16 ;; any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; interface to selectively inlining functions.
31 ;; This only happens when source-code optimization is turned on.
33 ;;; Code:
35 ;; We define macro-declaration-function here because it is needed to
36 ;; handle declarations in macro definitions and this is the first file
37 ;; loaded by loadup.el that uses declarations in macros.
39 (defun macro-declaration-function (macro decl)
40 "Process a declaration found in a macro definition.
41 This is set as the value of the variable `macro-declaration-function'.
42 MACRO is the name of the macro being defined.
43 DECL is a list `(declare ...)' containing the declarations.
44 The return value of this function is not used."
45 ;; We can't use `dolist' or `cadr' yet for bootstrapping reasons.
46 (let (d)
47 ;; Ignore the first element of `decl' (it's always `declare').
48 (while (setq decl (cdr decl))
49 (setq d (car decl))
50 (cond ((and (consp d) (eq (car d) 'indent))
51 (put macro 'lisp-indent-function (car (cdr d))))
52 ((and (consp d) (eq (car d) 'debug))
53 (put macro 'edebug-form-spec (car (cdr d))))
54 ((and (consp d) (eq (car d) 'doc-string))
55 (put macro 'doc-string-elt (car (cdr d))))
57 (message "Unknown declaration %s" d))))))
59 (setq macro-declaration-function 'macro-declaration-function)
62 ;; Redefined in byte-optimize.el.
63 ;; This is not documented--it's not clear that we should promote it.
64 (fset 'inline 'progn)
65 (put 'inline 'lisp-indent-function 0)
67 ;;; Interface to inline functions.
69 ;; (defmacro proclaim-inline (&rest fns)
70 ;; "Cause the named functions to be open-coded when called from compiled code.
71 ;; They will only be compiled open-coded when byte-compile-optimize is true."
72 ;; (cons 'eval-and-compile
73 ;; (mapcar '(lambda (x)
74 ;; (or (memq (get x 'byte-optimizer)
75 ;; '(nil byte-compile-inline-expand))
76 ;; (error
77 ;; "%s already has a byte-optimizer, can't make it inline"
78 ;; x))
79 ;; (list 'put (list 'quote x)
80 ;; ''byte-optimizer ''byte-compile-inline-expand))
81 ;; fns)))
83 ;; (defmacro proclaim-notinline (&rest fns)
84 ;; "Cause the named functions to no longer be open-coded."
85 ;; (cons 'eval-and-compile
86 ;; (mapcar '(lambda (x)
87 ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
88 ;; (put x 'byte-optimizer nil))
89 ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
90 ;; ''byte-compile-inline-expand)
91 ;; (list 'put x ''byte-optimizer nil)))
92 ;; fns)))
94 ;; This has a special byte-hunk-handler in bytecomp.el.
95 (defmacro defsubst (name arglist &rest body)
96 "Define an inline function. The syntax is just like that of `defun'."
97 (declare (debug defun))
98 (or (memq (get name 'byte-optimizer)
99 '(nil byte-compile-inline-expand))
100 (error "`%s' is a primitive" name))
101 `(prog1
102 (defun ,name ,arglist ,@body)
103 (eval-and-compile
104 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
106 (defmacro declare-function (fn file &optional arglist)
107 "Tell the byte-compiler that function FN is defined, in FILE.
108 Optional ARGLIST is the argument list used by the function. The
109 FILE argument is not used by the byte-compiler, but by the
110 `check-declare' package, which checks that FILE contains a
111 definition for FN. ARGLIST is used by both the byte-compiler and
112 `check-declare' to check for consistency.
114 FILE can be either a Lisp file (in which case the \".el\"
115 extension is optional), or a C file. FILE should be either
116 absolute, or relative to the location of the file containing the
117 declaration (for a Lisp file), or to the Emacs \"src/\" directory
118 \(for a C file).
120 Note that for the purposes of `check-declare', this statement
121 must be the first non-whitespace on a line, and everything up to
122 the end of FILE must be all on the same line. For example:
124 \(declare-function c-end-of-defun \"progmodes/cc-cmds.el\"
125 \(&optional arg))"
126 ;; Does nothing - byte-compile-declare-function does the work.
127 nil)
129 (defun make-obsolete (obsolete-name current-name &optional when)
130 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
131 The warning will say that CURRENT-NAME should be used instead.
132 If CURRENT-NAME is a string, that is the `use instead' message.
133 If provided, WHEN should be a string indicating when the function
134 was first made obsolete, for example a date or a release number."
135 (interactive "aMake function obsolete: \nxObsoletion replacement: ")
136 (let ((handler (get obsolete-name 'byte-compile)))
137 (if (eq 'byte-compile-obsolete handler)
138 (setq handler (nth 1 (get obsolete-name 'byte-obsolete-info)))
139 (put obsolete-name 'byte-compile 'byte-compile-obsolete))
140 (put obsolete-name 'byte-obsolete-info (list current-name handler when)))
141 obsolete-name)
143 (defmacro define-obsolete-function-alias (obsolete-name current-name
144 &optional when docstring)
145 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
147 \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
149 is equivalent to the following two lines of code:
151 \(defalias 'old-fun 'new-fun \"old-fun's doc.\")
152 \(make-obsolete 'old-fun 'new-fun \"22.1\")
154 See the docstrings of `defalias' and `make-obsolete' for more details."
155 (declare (doc-string 4))
156 `(progn
157 (defalias ,obsolete-name ,current-name ,docstring)
158 (make-obsolete ,obsolete-name ,current-name ,when)))
160 (defun make-obsolete-variable (obsolete-name current-name &optional when)
161 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
162 The warning will say that CURRENT-NAME should be used instead.
163 If CURRENT-NAME is a string, that is the `use instead' message.
164 If provided, WHEN should be a string indicating when the variable
165 was first made obsolete, for example a date or a release number."
166 (interactive
167 (list
168 (let ((str (completing-read "Make variable obsolete: " obarray 'boundp t)))
169 (if (equal str "") (error ""))
170 (intern str))
171 (car (read-from-string (read-string "Obsoletion replacement: ")))))
172 (put obsolete-name 'byte-obsolete-variable (cons current-name when))
173 obsolete-name)
175 (defmacro define-obsolete-variable-alias (obsolete-name current-name
176 &optional when docstring)
177 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
179 \(define-obsolete-variable-alias 'old-var 'new-var \"22.1\" \"old-var's doc.\")
181 is equivalent to the following two lines of code:
183 \(defvaralias 'old-var 'new-var \"old-var's doc.\")
184 \(make-obsolete-variable 'old-var 'new-var \"22.1\")
186 See the docstrings of `defvaralias' and `make-obsolete-variable' or
187 Info node `(elisp)Variable Aliases' for more details."
188 (declare (doc-string 4))
189 `(progn
190 (defvaralias ,obsolete-name ,current-name ,docstring)
191 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
193 (defmacro dont-compile (&rest body)
194 "Like `progn', but the body always runs interpreted (not compiled).
195 If you think you need this, you're probably making a mistake somewhere."
196 (declare (debug t) (indent 0))
197 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
200 ;; interface to evaluating things at compile time and/or load time
201 ;; these macro must come after any uses of them in this file, as their
202 ;; definition in the file overrides the magic definitions on the
203 ;; byte-compile-macro-environment.
205 (defmacro eval-when-compile (&rest body)
206 "Like `progn', but evaluates the body at compile time if you're compiling.
207 Thus, the result of the body appears to the compiler as a quoted constant.
208 In interpreted code, this is entirely equivalent to `progn'."
209 (declare (debug t) (indent 0))
210 ;; Not necessary because we have it in b-c-initial-macro-environment
211 ;; (list 'quote (eval (cons 'progn body)))
212 (cons 'progn body))
214 (defmacro eval-and-compile (&rest body)
215 "Like `progn', but evaluates the body at compile time and at load time."
216 (declare (debug t) (indent 0))
217 ;; Remember, it's magic.
218 (cons 'progn body))
220 (put 'with-no-warnings 'lisp-indent-function 0)
221 (defun with-no-warnings (&rest body)
222 "Like `progn', but prevents compiler warnings in the body."
223 ;; The implementation for the interpreter is basically trivial.
224 (car (last body)))
227 ;; I nuked this because it's not a good idea for users to think of using it.
228 ;; These options are a matter of installation preference, and have nothing to
229 ;; with particular source files; it's a mistake to suggest to users
230 ;; they should associate these with particular source files.
231 ;; There is hardly any reason to change these parameters, anyway.
232 ;; --rms.
234 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
235 ;; (defmacro byte-compiler-options (&rest args)
236 ;; "Set some compilation-parameters for this file. This will affect only the
237 ;; file in which it appears; this does nothing when evaluated, and when loaded
238 ;; from a .el file.
240 ;; Each argument to this macro must be a list of a key and a value.
242 ;; Keys: Values: Corresponding variable:
244 ;; verbose t, nil byte-compile-verbose
245 ;; optimize t, nil, source, byte byte-compile-optimize
246 ;; warnings list of warnings byte-compile-warnings
247 ;; Legal elements: (callargs redefine free-vars unresolved)
248 ;; file-format emacs18, emacs19 byte-compile-compatibility
250 ;; For example, this might appear at the top of a source file:
252 ;; (byte-compiler-options
253 ;; (optimize t)
254 ;; (warnings (- free-vars)) ; Don't warn about free variables
255 ;; (file-format emacs19))"
256 ;; nil)
258 ;; arch-tag: 76f8328a-1f66-4df2-9b6d-5c3666dc05e9
259 ;;; byte-run.el ends here