* Makefile.am (EXTRA_DIST): Add liece.xbm and liece.xpm.
[elisp/liece.git] / lisp / liece-handler.el
1 ;;; liece-handler.el --- function overloading facilities
2 ;; Copyright (C) 1998-2000 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1999-06-05
6
7 ;; This file is part of Liece.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24
25 ;;; Commentary:
26 ;; 
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (eval-when-compile (require 'liece-inlines))
33
34 (eval-when-compile (require 'liece-clfns))
35
36 (defmacro liece-handler-make-obarray (backend)
37   `(defvar ,(intern (format "liece-handler-%s-obarray" backend))
38      (make-vector 107 0)))
39
40 (defmacro liece-handler-obarray (backend)
41   `(symbol-value (intern-soft (format "liece-handler-%s-obarray" ,backend))))
42
43 (defun liece-handler-override-function-definition (name backend args function)
44   (let ((ref (symbol-name (liece-gensym))))
45     (if (symbolp name)
46         (setq name (symbol-name name)))
47     (put (intern name (liece-handler-obarray backend)) 'unifiers
48          (nconc (get (intern name (liece-handler-obarray backend)) 'unifiers)
49                 (list `(,(intern ref (liece-handler-obarray backend))
50                         ,@args))))
51     (fset (intern ref (liece-handler-obarray backend)) function)))
52
53 (defun liece-handler-unify-argument-list-function (args unifiers)
54   (let ((index 0)
55         (unfs (copy-alist unifiers))
56         (len (length args))
57         type)
58     (setq unfs
59           (remove-if (lambda (unf) (/= (length (cdr unf)) len)) unfs))
60     (dolist (arg args)
61       (if (listp arg)
62           (setq unfs (remove-if-not
63                       (lambda (unf)
64                         (let ((spec (nth index (cdr unf))))
65                           (or (not (listp spec))
66                               (eq (car spec) (car arg)))))
67                       unfs)))
68       (incf index))
69     (if (caar unfs)
70         (symbol-function (caar unfs)))))
71
72 (defmacro liece-handler-define-backend (type &optional parents)
73   `(liece-handler-make-obarray ,type))
74
75 (defun liece-handler-find-function (name args backend)
76   (let* ((fsym (intern-soft name (liece-handler-obarray backend)))
77          (unifiers (if fsym (get fsym 'unifiers))))
78     (liece-handler-unify-argument-list-function args unifiers)))
79
80 (defun liece-handler-define-function (name specs function)
81   (let ((args (butlast specs))
82         (backend (car (last specs))))
83     (liece-handler-override-function-definition name backend args function)))
84
85 (provide 'liece-handler)
86
87 ;;; liece-handler.el ends here