summaryrefslogtreecommitdiff
path: root/protocols.lisp
blob: 06348476ff0eadb3c19711c94228432612c5044c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
(in-package #:mulk.protocols)


(defvar *protocols* (make-hash-table))


(defun find-protocol (name &optional (errorp t))
  (let ((protocol (gethash name *protocols* nil)))
    (when (and errorp (null protocol))
      (error "There is no protocol named ~A." name))
    protocol))


(defun (setf find-protocol) (protocol name &optional errorp)
  (declare (ignore errorp))
  (setf (gethash name *protocols*) protocol))


(defun conforms-to-p (class protocol)
  (let ((real-class (typecase class
                      (symbol (find-class class))
                      (t class)))
        (real-protocol (typecase protocol
                         (symbol (find-protocol protocol))
                         (t protocol))))
    (or (%conforms-to-p real-class real-protocol)
        (some #'(lambda (superclass)
                  (conforms-to-p superclass real-protocol))
              (class-direct-superclasses real-class)))))


(defun really-conforms-to-p (class protocol)
  (let ((real-class (typecase class
                      (symbol (find-class class))
                      (t class)))
        (real-protocol (typecase protocol
                         (symbol (find-protocol protocol))
                         (t protocol))))
    (or (%really-conforms-to-p real-class real-protocol)
        (some #'(lambda (superclass)
                  (really-conforms-to-p superclass real-protocol))
              (class-direct-superclasses real-class)))))


(defgeneric %conforms-to-p (class protocol))

(defmethod %conforms-to-p ((class t) (protocol t))
  (declare (ignore class protocol))
  nil)


(defgeneric %really-conforms-to-p (class protocol))

(defmethod %really-conforms-to-p ((class t) (protocol t))
  (declare (ignore class protocol))
  nil)


(defclass protocol ()
  ((name :initarg :name
         :reader protocol-name)
   (superprotocols :initarg :superprotocols
                   :reader protocol-superprotocols)
   (methods :initarg :methods
            :reader protocol-methods)
   (options :initarg :options
            :reader protocol-options)))


(defmacro define-protocol (name superprotocols methods &rest options)
  "Define a new protocol.

superprotocols ::= (*name*\\*)

methods ::= ((method-name [\\* | class-name]\\*)\\*)"
  `(progn
     (when (find-protocol ',name nil)
       (warn (make-condition 'simple-style-warning
                             :format-control "Redefining protocol ~A."
                             :format-arguments (list ',name))))
     (setf (find-protocol ',name)
           (make-instance 'protocol
              :name ',name
              :superprotocols (mapcar #'find-protocol ',superprotocols)
              :methods ',methods
              :options ',options))))


(defmethod print-object ((protocol protocol) stream)
  (print-unreadable-object (protocol stream)
    (format stream "PROTOCOL ~A" (protocol-name protocol))))


(defun ensure-conformance (class-name protocol-designator)
  (let* ((protocol (typecase protocol-designator
                     (symbol (find-protocol protocol-designator))
                     (t protocol-designator)))
         (protocol-name (protocol-name protocol))
         (conforming-p nil))
    (with-slots (name superprotocols methods options)
        protocol
      (loop for method in methods
            for (name . raw-argument-class-list) = method
            for argument-class-list = (substitute class-name
                                                  '*
                                                  raw-argument-class-list)
            for real-argument-class-list = (mapcar #'find-class
                                                   argument-class-list)
            when (null (funcall #'compute-applicable-methods-using-classes
                                (fdefinition name)
                                real-argument-class-list))
              collect method into missing-methods
              and do (warn (make-condition 'simple-style-warning
                                :format-control "Class ~A does not implement ~
                                                 method ~A with argument types ~
                                                 ~A as required by ~
                                                 protocol ~A."
                                :format-arguments (list class-name
                                                        (first method)
                                                        real-argument-class-list
                                                        (protocol-name protocol))))
            finally (setq conforming-p (null missing-methods)))
      (let ((really-conforming-p
             (and conforming-p
                  (every #'identity
                         ;; We have to use MAPCAR because we
                         ;; don't want short-circuiting.
                         (mapcar #'(lambda (superprotocol)
                                     (ensure-conformance class-name
                                                         superprotocol))
                                 superprotocols)))))
        (ensure-method #'%conforms-to-p
                       '(lambda (x y) t)
                       :specializers (list (intern-eql-specializer (find-class class-name))
                                           (intern-eql-specializer (find-protocol protocol-name))))
        (ensure-method #'%really-conforms-to-p
                       `(lambda (x y) ,really-conforming-p)
                       :specializers (list (intern-eql-specializer (find-class class-name))
                                           (intern-eql-specializer (find-protocol protocol-name))))))))


(defmacro implement-protocols (class protocols &body definitions)
  `(progn
     ,@definitions
     ,@(mapcar #'(lambda (protocol)
                   `(ensure-conformance ',class ',protocol))
               protocols)))