* lisp/emacs-lisp/cconv.el (cconv-closure-convert-rec): Let the byte
[bpt/emacs.git] / lisp / emacs-lisp / eieio-comp.el
CommitLineData
6dd12ef2
CY
1;;; eieio-comp.el -- eieio routines to help with byte compilation
2
95df8112
GM
3;; Copyright (C) 1995-1996, 1998-2002, 2005, 2008-2011
4;; Free Software Foundation, Inc.
6dd12ef2 5
9ffe3f52 6;; Author: Eric M. Ludlam <zappo@gnu.org>
6dd12ef2 7;; Version: 0.2
bd78fa1d
CY
8;; Keywords: lisp, tools
9;; Package: eieio
6dd12ef2
CY
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;; Byte compiler functions for defmethod. This will affect the new GNU
29;; byte compiler for Emacs 19 and better. This function will be called by
30;; the byte compiler whenever a `defmethod' is encountered in a file.
31;; It will output a function call to `eieio-defmethod' with the byte
32;; compiled function as a parameter.
33
34;;; Code:
35
36(declare-function eieio-defgeneric-form "eieio" (method doc-string))
37
38;; Some compatibility stuff
39(eval-and-compile
40 (if (not (fboundp 'byte-compile-compiled-obj-to-list))
41 (defun byte-compile-compiled-obj-to-list (moose) nil))
42
43 (if (not (boundp 'byte-compile-outbuffer))
44 (defvar byte-compile-outbuffer nil))
45 )
46
47;; This teaches the byte compiler how to do this sort of thing.
3e21b6a7 48(put 'defmethod 'byte-hunk-handler 'eieio-byte-compile-file-form-defmethod)
6dd12ef2 49
3e21b6a7 50(defun eieio-byte-compile-file-form-defmethod (form)
6dd12ef2 51 "Mumble about the method we are compiling.
a8f316ca
JB
52This function is mostly ripped from `byte-compile-file-form-defun',
53but it's been modified to handle the special syntax of the `defmethod'
54command. There should probably be one for `defgeneric' as well, but
6dd12ef2
CY
55that is called but rarely. Argument FORM is the body of the method."
56 (setq form (cdr form))
57 (let* ((meth (car form))
58 (key (progn (setq form (cdr form))
59 (cond ((or (eq ':BEFORE (car form))
60 (eq ':before (car form)))
61 (setq form (cdr form))
62 ":before ")
63 ((or (eq ':AFTER (car form))
64 (eq ':after (car form)))
65 (setq form (cdr form))
66 ":after ")
67 ((or (eq ':PRIMARY (car form))
68 (eq ':primary (car form)))
69 (setq form (cdr form))
70 ":primary ")
71 ((or (eq ':STATIC (car form))
72 (eq ':static (car form)))
73 (setq form (cdr form))
74 ":static ")
75 (t ""))))
76 (params (car form))
3e21b6a7 77 (lamparams (eieio-byte-compile-defmethod-param-convert params))
6dd12ef2
CY
78 (arg1 (car params))
79 (class (if (listp arg1) (nth 1 arg1) nil))
80 (my-outbuffer (if (eval-when-compile (featurep 'xemacs))
81 byte-compile-outbuffer
4e44448f
GM
82 (cond ((boundp 'bytecomp-outbuffer)
83 bytecomp-outbuffer) ; Emacs >= 23.2
84 ((boundp 'outbuffer) outbuffer)
85 (t (error "Unable to set outbuffer"))))))
6dd12ef2
CY
86 (let ((name (format "%s::%s" (or class "#<generic>") meth)))
87 (if byte-compile-verbose
88 ;; #### filename used free
4e44448f
GM
89 (message "Compiling %s... (%s)"
90 (cond ((boundp 'bytecomp-filename) bytecomp-filename)
91 ((boundp 'filename) filename)
92 (t ""))
93 name))
6dd12ef2
CY
94 (setq byte-compile-current-form name) ; for warnings
95 )
96 ;; Flush any pending output
97 (byte-compile-flush-pending)
98 ;; Byte compile the body. For the byte compiled forms, add the
99 ;; rest arguments, which will get ignored by the engine which will
100 ;; add them later (I hope)
3e21b6a7
SM
101 ;; FIXME: This relies on compiler's internal. Make sure it still
102 ;; works with lexical-binding code. Maybe calling `byte-compile'
103 ;; would be preferable.
6dd12ef2
CY
104 (let* ((new-one (byte-compile-lambda
105 (append (list 'lambda lamparams)
106 (cdr form))))
107 (code (byte-compile-byte-code-maker new-one)))
108 (princ "\n(eieio-defmethod '" my-outbuffer)
109 (princ meth my-outbuffer)
110 (princ " '(" my-outbuffer)
111 (princ key my-outbuffer)
112 (prin1 params my-outbuffer)
113 (princ " " my-outbuffer)
114 (prin1 code my-outbuffer)
115 (princ "))" my-outbuffer)
116 )
117 ;; Now add this function to the list of known functions.
118 ;; Don't bother with a doc string. Not relevant here.
119 (add-to-list 'byte-compile-function-environment
120 (cons meth
121 (eieio-defgeneric-form meth "")))
122
123 ;; Remove it from the undefined list if it is there.
124 (let ((elt (assq meth byte-compile-unresolved-functions)))
125 (if elt (setq byte-compile-unresolved-functions
126 (delq elt byte-compile-unresolved-functions))))
127
128 ;; nil prevents cruft from appearing in the output buffer.
129 nil))
130
3e21b6a7 131(defun eieio-byte-compile-defmethod-param-convert (paramlist)
a8f316ca 132 "Convert method params into the params used by the `defmethod' thingy.
9ffe3f52 133Argument PARAMLIST is the parameter list to convert."
6dd12ef2
CY
134 (let ((argfix nil))
135 (while paramlist
136 (setq argfix (cons (if (listp (car paramlist))
137 (car (car paramlist))
138 (car paramlist))
139 argfix))
140 (setq paramlist (cdr paramlist)))
141 (nreverse argfix)))
142
143(provide 'eieio-comp)
144
145;;; eieio-comp.el ends here