aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Stallman2020-01-05 17:21:41 -0500
committerEli Zaretskii2020-01-11 11:48:17 +0200
commitfd8128f0c18d536cc31578b29f3fd426bbc61630 (patch)
treec0605a95be7e8b95c6bc9a713c2cfd84f6b2b132
parent524441d6b3f1da298a048862d330258eebca1118 (diff)
downloademacs-fd8128f0c18d536cc31578b29f3fd426bbc61630.tar.gz
emacs-fd8128f0c18d536cc31578b29f3fd426bbc61630.zip
; Move the description of define-inline to a different node in functions.texi
-rw-r--r--doc/lispref/functions.texi196
1 files changed, 100 insertions, 96 deletions
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index 8e3e6aefb00..5cf67ba6473 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -576,8 +576,9 @@ naming conventions, which are being phased out.
576@cindex defining a function 576@cindex defining a function
577 577
578 We usually give a name to a function when it is first created. This 578 We usually give a name to a function when it is first created. This
579is called @dfn{defining a function}, and it is done with the 579is called @dfn{defining a function}, and we usually do it with the
580@code{defun} macro. 580@code{defun} macro. This section also describes other ways to define
581a function.
581 582
582@defmac defun name args [doc] [declare] [interactive] body@dots{} 583@defmac defun name args [doc] [declare] [interactive] body@dots{}
583@code{defun} is the usual way to define new Lisp functions. It 584@code{defun} is the usual way to define new Lisp functions. It
@@ -682,95 +683,8 @@ definition will have no effect on them.
682and tells the Lisp compiler to perform inline expansion on it. 683and tells the Lisp compiler to perform inline expansion on it.
683@xref{Inline Functions}. 684@xref{Inline Functions}.
684 685
685 Alternatively, you can define a function by providing the code which 686 To undefine a function name, use @code{fmakunbound}.
686will inline it as a compiler macro. The following macros make this 687@xref{Function Cells}.
687possible.
688
689@c FIXME: Can define-inline use the interactive spec?
690@defmac define-inline name args [doc] [declare] body@dots{}
691Define a function @var{name} by providing code that does its inlining,
692as a compiler macro. The function will accept the argument list
693@var{args} and will have the specified @var{body}.
694
695If present, @var{doc} should be the function's documentation string
696(@pxref{Function Documentation}); @var{declare}, if present, should be
697a @code{declare} form (@pxref{Declare Form}) specifying the function's
698metadata.
699@end defmac
700
701Functions defined via @code{define-inline} have several advantages
702with respect to macros defined by @code{defsubst} or @code{defmacro}:
703
704@itemize @minus
705@item
706They can be passed to @code{mapcar} (@pxref{Mapping Functions}).
707
708@item
709They are more efficient.
710
711@item
712They can be used as @dfn{place forms} to store values
713(@pxref{Generalized Variables}).
714
715@item
716They behave in a more predictable way than @code{cl-defsubst}
717(@pxref{Argument Lists,,, cl, Common Lisp Extensions for GNU Emacs
718Lisp}).
719@end itemize
720
721Like @code{defmacro}, a function inlined with @code{define-inline}
722inherits the scoping rules, either dynamic or lexical, from the call
723site. @xref{Variable Scoping}.
724
725The following macros should be used in the body of a function defined
726by @code{define-inline}.
727
728@defmac inline-quote expression
729Quote @var{expression} for @code{define-inline}. This is similar to
730the backquote (@pxref{Backquote}), but quotes code and accepts only
731@code{,}, not @code{,@@}.
732@end defmac
733
734@defmac inline-letevals (bindings@dots{}) body@dots{}
735This is similar to @code{let} (@pxref{Local Variables}): it sets up
736local variables as specified by @var{bindings}, and then evaluates
737@var{body} with those bindings in effect. Each element of
738@var{bindings} should be either a symbol or a list of the form
739@w{@code{(@var{var} @var{expr})}}; the result is to evaluate
740@var{expr} and bind @var{var} to the result. The tail of
741@var{bindings} can be either @code{nil} or a symbol which should hold
742a list of arguments, in which case each argument is evaluated, and the
743symbol is bound to the resulting list.
744@end defmac
745
746@defmac inline-const-p expression
747Return non-@code{nil} if the value of @var{expression} is already
748known.
749@end defmac
750
751@defmac inline-const-val expression
752Return the value of @var{expression}.
753@end defmac
754
755@defmac inline-error format &rest args
756Signal an error, formatting @var{args} according to @var{format}.
757@end defmac
758
759Here's an example of using @code{define-inline}:
760
761@lisp
762(define-inline myaccessor (obj)
763 (inline-letevals (obj)
764 (inline-quote (if (foo-p ,obj) (aref (cdr ,obj) 3) (aref ,obj 2)))))
765@end lisp
766
767@noindent
768This is equivalent to
769
770@lisp
771(defsubst myaccessor (obj)
772 (if (foo-p obj) (aref (cdr obj) 3) (aref obj 2)))
773@end lisp
774 688
775@node Calling Functions 689@node Calling Functions
776@section Calling Functions 690@section Calling Functions
@@ -2155,8 +2069,12 @@ this:
2155 An @dfn{inline function} is a function that works just like an 2069 An @dfn{inline function} is a function that works just like an
2156ordinary function, except for one thing: when you byte-compile a call 2070ordinary function, except for one thing: when you byte-compile a call
2157to the function (@pxref{Byte Compilation}), the function's definition 2071to the function (@pxref{Byte Compilation}), the function's definition
2158is expanded into the caller. To define an inline function, use 2072is expanded into the caller.
2159@code{defsubst} instead of @code{defun}. 2073
2074 The simple way to define an inline function, is to write
2075@code{defsubst} instead of @code{defun}. The rest of the definition
2076looks just the same, but using @code{defsubst} says to make it inline
2077for byte compilation.
2160 2078
2161@defmac defsubst name args [doc] [declare] [interactive] body@dots{} 2079@defmac defsubst name args [doc] [declare] [interactive] body@dots{}
2162This macro defines an inline function. Its syntax is exactly the same 2080This macro defines an inline function. Its syntax is exactly the same
@@ -2194,9 +2112,95 @@ argument of an inline function is evaluated exactly once, you needn't
2194worry about how many times the body uses the arguments, as you do for 2112worry about how many times the body uses the arguments, as you do for
2195macros. 2113macros.
2196 2114
2197 As an alternative to @code{defsubst}, you can use 2115 Alternatively, you can define a function by providing the code which
2198@code{define-inline} to define functions via their exhaustive compiler 2116will inline it as a compiler macro. The following macros make this
2199macro. @xref{Defining Functions, define-inline}. 2117possible.
2118
2119@c FIXME: Can define-inline use the interactive spec?
2120@defmac define-inline name args [doc] [declare] body@dots{}
2121Define a function @var{name} by providing code that does its inlining,
2122as a compiler macro. The function will accept the argument list
2123@var{args} and will have the specified @var{body}.
2124
2125If present, @var{doc} should be the function's documentation string
2126(@pxref{Function Documentation}); @var{declare}, if present, should be
2127a @code{declare} form (@pxref{Declare Form}) specifying the function's
2128metadata.
2129@end defmac
2130
2131Functions defined via @code{define-inline} have several advantages
2132with respect to macros defined by @code{defsubst} or @code{defmacro}:
2133
2134@itemize @minus
2135@item
2136They can be passed to @code{mapcar} (@pxref{Mapping Functions}).
2137
2138@item
2139They are more efficient.
2140
2141@item
2142They can be used as @dfn{place forms} to store values
2143(@pxref{Generalized Variables}).
2144
2145@item
2146They behave in a more predictable way than @code{cl-defsubst}
2147(@pxref{Argument Lists,,, cl, Common Lisp Extensions for GNU Emacs
2148Lisp}).
2149@end itemize
2150
2151Like @code{defmacro}, a function inlined with @code{define-inline}
2152inherits the scoping rules, either dynamic or lexical, from the call
2153site. @xref{Variable Scoping}.
2154
2155The following macros should be used in the body of a function defined
2156by @code{define-inline}.
2157
2158@defmac inline-quote expression
2159Quote @var{expression} for @code{define-inline}. This is similar to
2160the backquote (@pxref{Backquote}), but quotes code and accepts only
2161@code{,}, not @code{,@@}.
2162@end defmac
2163
2164@defmac inline-letevals (bindings@dots{}) body@dots{}
2165This is similar to @code{let} (@pxref{Local Variables}): it sets up
2166local variables as specified by @var{bindings}, and then evaluates
2167@var{body} with those bindings in effect. Each element of
2168@var{bindings} should be either a symbol or a list of the form
2169@w{@code{(@var{var} @var{expr})}}; the result is to evaluate
2170@var{expr} and bind @var{var} to the result. The tail of
2171@var{bindings} can be either @code{nil} or a symbol which should hold
2172a list of arguments, in which case each argument is evaluated, and the
2173symbol is bound to the resulting list.
2174@end defmac
2175
2176@defmac inline-const-p expression
2177Return non-@code{nil} if the value of @var{expression} is already
2178known.
2179@end defmac
2180
2181@defmac inline-const-val expression
2182Return the value of @var{expression}.
2183@end defmac
2184
2185@defmac inline-error format &rest args
2186Signal an error, formatting @var{args} according to @var{format}.
2187@end defmac
2188
2189Here's an example of using @code{define-inline}:
2190
2191@lisp
2192(define-inline myaccessor (obj)
2193 (inline-letevals (obj)
2194 (inline-quote (if (foo-p ,obj) (aref (cdr ,obj) 3) (aref ,obj 2)))))
2195@end lisp
2196
2197@noindent
2198This is equivalent to
2199
2200@lisp
2201(defsubst myaccessor (obj)
2202 (if (foo-p obj) (aref (cdr obj) 3) (aref obj 2)))
2203@end lisp
2200 2204
2201@node Declare Form 2205@node Declare Form
2202@section The @code{declare} Form 2206@section The @code{declare} Form