home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / bbdb / defsubst.el < prev    next >
Encoding:
Text File  |  1992-09-10  |  2.8 KB  |  73 lines

  1. ;;; -*- Mode:Emacs-Lisp -*-
  2.  
  3. ;;; This file is obsolete; the Emacs 19 byte compiler does this and
  4. ;;; much more.  The v19 byte-compiler works in v18, and is available 
  5. ;;; from archive.cis.ohio-state.edu in the file
  6. ;;; pub/gnu/emacs/elisp-archive/packages/bytecomp.tar.Z.  It is
  7. ;;; also included with Lucid GNU Emacs, a divergent fork of FSF's v19,
  8. ;;; which is available from labrea.stanford.edu in /pub/gnu/lucid/.
  9.  
  10. ;;; A function call is about the most expensive thing that one can do in
  11. ;;; emacs-lisp.  This file defines `defsubst', which, in effect, defines
  12. ;;; an inline function (a function that is open-coded at compile time).
  13. ;;; It does this by defining a macro which evaluates its arguments like
  14. ;;; a function.
  15. ;;;
  16. ;;; this file also modifies the byte-compiler to expand and compile 
  17. ;;; top-level calls to macros, because otherwise defsubst-ed routines
  18. ;;; would always run interpreted (lose lose).
  19. ;;;
  20. ;;; Created by Jamie Zawinski <jwz@lucid.com>, 14-feb-91.
  21.  
  22. (defmacro defsubst (name arglist &rest body)
  23.   "Same syntax as DEFUN, except that a function call will not be generated
  24. when invoking this function: it is fast and undebuggable.  (But, if the
  25. function this defines is called from code that is not byte-compiled, it
  26. will actually run much slower...)
  27.  
  28. Actually, this defines a macro, not a function, so you can't funcall it.
  29. But that's the price you have to pay for speed..."
  30.   (let ((restp nil))
  31.     (list 'defmacro name arglist
  32.       (cons 'cons
  33.         (list ''let
  34.           (list 'cons
  35.         (cons 'list
  36.           (delq nil (mapcar (function (lambda (x)
  37.                 (cond ((eq x '&optional) nil)
  38.                       ((eq x '&rest)
  39.                        (setq restp t)
  40.                        nil)
  41.                       (t (list 'list (list 'quote x)
  42.                        (if restp
  43.                            (list 'cons ''list x)
  44.                            x))))))
  45.                 arglist)))
  46.         (list 'quote body)))))))
  47.  
  48.  
  49. ;;; Since defsubst is a macro which expands into a defmacro (which should then
  50. ;;; be evaluated, compiled, and added to the compile-time macro-environment)
  51. ;;; it is necessary for the byte compiler to macroexpand top-level forms at
  52. ;;; compile time (and then compile them if possible.)  It should have done
  53. ;;; this all along anyway...
  54. ;;;
  55. ;;; It also makes (progn (foo ...)) at top level equivalent to (foo ...) at
  56. ;;; top level.  This means that defmacros may be enclosed inside progns and
  57. ;;; still make it in to the compilation environment.
  58.  
  59. (require 'byte-compile "bytecomp")
  60.  
  61. (if (not (fboundp 'defsubst-original-byte-compile-file-form))
  62.     (fset 'defsubst-original-byte-compile-file-form
  63.       (symbol-function 'byte-compile-file-form)))
  64.  
  65. (defun byte-compile-file-form (form)
  66.   (setq form (macroexpand form byte-compile-macro-environment))
  67.   (if (eq (car form) 'progn)
  68.       (cons 'progn (mapcar 'byte-compile-file-form (cdr form)))
  69.       (defsubst-original-byte-compile-file-form form)))
  70.  
  71.  
  72. (provide 'defsubst)
  73.