c04295dcd58fd0a07cef8856d04d36fb078ee8ea
[bpt/emacs.git] / lisp / emacs-lisp / eieio-comp.el
1 ;;; eieio-comp.el -- eieio routines to help with byte compilation
2
3 ;; Copyright (C) 1995-1996, 1998-2002, 2005, 2008-2011 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Version: 0.2
7 ;; Keywords: lisp, tools
8 ;; Package: eieio
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Byte compiler functions for defmethod. This will affect the new GNU
28 ;; byte compiler for Emacs 19 and better. This function will be called by
29 ;; the byte compiler whenever a `defmethod' is encountered in a file.
30 ;; It will output a function call to `eieio-defmethod' with the byte
31 ;; compiled function as a parameter.
32
33 ;;; Code:
34
35 (declare-function eieio-defgeneric-form "eieio" (method doc-string))
36
37 ;; Some compatibility stuff
38 (eval-and-compile
39 (if (not (fboundp 'byte-compile-compiled-obj-to-list))
40 (defun byte-compile-compiled-obj-to-list (moose) nil))
41
42 (if (not (boundp 'byte-compile-outbuffer))
43 (defvar byte-compile-outbuffer nil))
44 )
45
46 ;; This teaches the byte compiler how to do this sort of thing.
47 (put 'defmethod 'byte-hunk-handler 'byte-compile-file-form-defmethod)
48
49 (defun byte-compile-file-form-defmethod (form)
50 "Mumble about the method we are compiling.
51 This function is mostly ripped from `byte-compile-file-form-defun',
52 but it's been modified to handle the special syntax of the `defmethod'
53 command. There should probably be one for `defgeneric' as well, but
54 that is called but rarely. Argument FORM is the body of the method."
55 (setq form (cdr form))
56 (let* ((meth (car form))
57 (key (progn (setq form (cdr form))
58 (cond ((or (eq ':BEFORE (car form))
59 (eq ':before (car form)))
60 (setq form (cdr form))
61 ":before ")
62 ((or (eq ':AFTER (car form))
63 (eq ':after (car form)))
64 (setq form (cdr form))
65 ":after ")
66 ((or (eq ':PRIMARY (car form))
67 (eq ':primary (car form)))
68 (setq form (cdr form))
69 ":primary ")
70 ((or (eq ':STATIC (car form))
71 (eq ':static (car form)))
72 (setq form (cdr form))
73 ":static ")
74 (t ""))))
75 (params (car form))
76 (lamparams (byte-compile-defmethod-param-convert params))
77 (arg1 (car params))
78 (class (if (listp arg1) (nth 1 arg1) nil))
79 (my-outbuffer (if (eval-when-compile (featurep 'xemacs))
80 byte-compile-outbuffer
81 (cond ((boundp 'bytecomp-outbuffer)
82 bytecomp-outbuffer) ; Emacs >= 23.2
83 ((boundp 'outbuffer) outbuffer)
84 (t (error "Unable to set outbuffer"))))))
85 (let ((name (format "%s::%s" (or class "#<generic>") meth)))
86 (if byte-compile-verbose
87 ;; #### filename used free
88 (message "Compiling %s... (%s)"
89 (cond ((boundp 'bytecomp-filename) bytecomp-filename)
90 ((boundp 'filename) filename)
91 (t ""))
92 name))
93 (setq byte-compile-current-form name) ; for warnings
94 )
95 ;; Flush any pending output
96 (byte-compile-flush-pending)
97 ;; Byte compile the body. For the byte compiled forms, add the
98 ;; rest arguments, which will get ignored by the engine which will
99 ;; add them later (I hope)
100 (let* ((new-one (byte-compile-lambda
101 (append (list 'lambda lamparams)
102 (cdr form))))
103 (code (byte-compile-byte-code-maker new-one)))
104 (princ "\n(eieio-defmethod '" my-outbuffer)
105 (princ meth my-outbuffer)
106 (princ " '(" my-outbuffer)
107 (princ key my-outbuffer)
108 (prin1 params my-outbuffer)
109 (princ " " my-outbuffer)
110 (prin1 code my-outbuffer)
111 (princ "))" my-outbuffer)
112 )
113 ;; Now add this function to the list of known functions.
114 ;; Don't bother with a doc string. Not relevant here.
115 (add-to-list 'byte-compile-function-environment
116 (cons meth
117 (eieio-defgeneric-form meth "")))
118
119 ;; Remove it from the undefined list if it is there.
120 (let ((elt (assq meth byte-compile-unresolved-functions)))
121 (if elt (setq byte-compile-unresolved-functions
122 (delq elt byte-compile-unresolved-functions))))
123
124 ;; nil prevents cruft from appearing in the output buffer.
125 nil))
126
127 (defun byte-compile-defmethod-param-convert (paramlist)
128 "Convert method params into the params used by the `defmethod' thingy.
129 Argument PARAMLIST is the parameter list to convert."
130 (let ((argfix nil))
131 (while paramlist
132 (setq argfix (cons (if (listp (car paramlist))
133 (car (car paramlist))
134 (car paramlist))
135 argfix))
136 (setq paramlist (cdr paramlist)))
137 (nreverse argfix)))
138
139 (provide 'eieio-comp)
140
141 ;;; eieio-comp.el ends here