Commit | Line | Data |
---|---|---|
dea4d055 MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; Class construction protocol implementation | |
4 | ;;; | |
5 | ;;; (c) 2009 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
10 | ;;; This file is part of the Sensble Object Design, an object system for C. | |
11 | ;;; | |
12 | ;;; SOD 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 2 of the License, or | |
15 | ;;; (at your option) any later version. | |
16 | ;;; | |
17 | ;;; SOD 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 SOD; if not, write to the Free Software Foundation, | |
24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | (cl:in-package #:sod) | |
27 | ||
28 | ;;;-------------------------------------------------------------------------- | |
29 | ;;; Classes. | |
30 | ||
31 | (defmethod guess-metaclass ((class sod-class)) | |
32 | "Default metaclass-guessing function for classes. | |
33 | ||
34 | Return the most specific metaclass of any of the CLASS's direct | |
35 | superclasses." | |
36 | (do ((supers (sod-class-direct-superclasses class) (cdr supers)) | |
37 | (meta nil (let ((candidate (sod-class-metaclass (car supers)))) | |
38 | (cond ((null meta) candidate) | |
39 | ((sod-subclass-p meta candidate) meta) | |
40 | ((sod-subclass-p candidate meta) candidate) | |
41 | (t (error "Unable to choose metaclass for `~A'" | |
42 | class)))))) | |
43 | ((endp supers) meta))) | |
44 | ||
45 | (defmethod shared-initialize :after ((class sod-class) slot-names &key pset) | |
46 | "Specific behaviour for SOD class initialization. | |
47 | ||
48 | Properties inspected are as follows: | |
49 | ||
50 | * `:metaclass' names the metaclass to use. If unspecified, nil is | |
51 | stored, and (unless you intervene later) `guess-metaclass' will be | |
52 | called by `finalize-sod-class' to find a suitable default. | |
53 | ||
54 | * `:nick' provides a nickname for the class. If unspecified, a default | |
55 | (the class's name, forced to lowercase) will be chosen in | |
56 | `finalize-sod-class'. | |
57 | ||
58 | * `:link' names the chained superclass. If unspecified, this class will | |
59 | be left at the head of its chain." | |
60 | ||
61 | ;; If no nickname, copy the class name. It won't be pretty, though. | |
62 | (default-slot-from-property (class 'nickname slot-names) | |
63 | (pset :nick :id) | |
64 | (string-downcase (slot-value class 'name))) | |
65 | ||
66 | ;; If no metaclass, guess one in a (Lisp) class-specific way. | |
67 | (default-slot-from-property (class 'metaclass slot-names) | |
68 | (pset :metaclass :id meta (find-sod-class meta)) | |
69 | (guess-metaclass class)) | |
70 | ||
71 | ;; If no chain-link, then start a new chain here. | |
72 | (default-slot-from-property (class 'chain-link slot-names) | |
73 | (pset :link :id link (find-sod-class link)) | |
74 | nil)) | |
75 | ||
76 | ;;;-------------------------------------------------------------------------- | |
77 | ;;; Slots. | |
78 | ||
79 | (defmethod make-sod-slot | |
80 | ((class sod-class) name type pset &optional location) | |
81 | (with-default-error-location (location) | |
52a79ab8 | 82 | (let ((slot (make-instance (get-property pset :slot-class :symbol |
dea4d055 MW |
83 | 'sod-slot) |
84 | :class class | |
85 | :name name | |
86 | :type type | |
87 | :location (file-location location) | |
88 | :pset pset))) | |
89 | (with-slots (slots) class | |
2e1a785d MW |
90 | (setf slots (append slots (list slot)))) |
91 | slot))) | |
dea4d055 MW |
92 | |
93 | (defmethod shared-initialize :after ((slot sod-slot) slot-names &key pset) | |
94 | "This method does nothing. | |
95 | ||
96 | It only exists so that it isn't an error to provide a `:pset' initarg | |
97 | to (make-instance 'sod-slot ...)." | |
98 | ||
99 | (declare (ignore slot-names pset))) | |
100 | ||
101 | ;;;-------------------------------------------------------------------------- | |
102 | ;;; Slot initializers. | |
103 | ||
104 | (defmethod make-sod-instance-initializer | |
105 | ((class sod-class) nick name value-kind value-form pset | |
106 | &optional location) | |
107 | (with-default-error-location (location) | |
108 | (let* ((slot (find-instance-slot-by-name class nick name)) | |
109 | (initializer (make-sod-initializer-using-slot | |
110 | class slot 'sod-instance-initializer | |
111 | value-kind value-form pset | |
112 | (file-location location)))) | |
113 | (with-slots (instance-initializers) class | |
114 | (setf instance-initializers | |
2e1a785d MW |
115 | (append instance-initializers (list initializer)))) |
116 | initializer))) | |
dea4d055 MW |
117 | |
118 | (defmethod make-sod-class-initializer | |
119 | ((class sod-class) nick name value-kind value-form pset | |
120 | &optional location) | |
121 | (with-default-error-location (location) | |
122 | (let* ((slot (find-class-slot-by-name class nick name)) | |
123 | (initializer (make-sod-initializer-using-slot | |
124 | class slot 'sod-class-initializer | |
125 | value-kind value-form pset | |
126 | (file-location location)))) | |
127 | (with-slots (class-initializers) class | |
128 | (setf class-initializers | |
2e1a785d MW |
129 | (append class-initializers (list initializer)))) |
130 | initializer))) | |
dea4d055 MW |
131 | |
132 | (defmethod make-sod-initializer-using-slot | |
133 | ((class sod-class) (slot sod-slot) | |
134 | init-class value-kind value-form pset location) | |
52a79ab8 | 135 | (make-instance (get-property pset :initializer-class :symbol init-class) |
dea4d055 MW |
136 | :class class |
137 | :slot slot | |
138 | :value-kind value-kind | |
139 | :value-form value-form | |
140 | :location location | |
141 | :pset pset)) | |
142 | ||
143 | (defmethod shared-initialize :after | |
144 | ((init sod-initializer) slot-names &key pset) | |
145 | "This method does nothing. | |
146 | ||
147 | It only exists so that it isn't an error to provide a `:pset' initarg | |
148 | to (make-instance 'sod-initializer ...)." | |
149 | (declare (ignore slot-names pset)) | |
150 | nil) | |
151 | ||
152 | ;;;-------------------------------------------------------------------------- | |
153 | ;;; Messages. | |
154 | ||
155 | (defmethod make-sod-message | |
156 | ((class sod-class) name type pset &optional location) | |
157 | (with-default-error-location (location) | |
d145f6df MW |
158 | (let* ((msg-class (or (get-property pset :message-class :symbol) |
159 | (and (get-property pset :combination :keyword) | |
160 | 'aggregating-message) | |
161 | 'standard-message)) | |
162 | (message (make-instance msg-class | |
163 | :class class | |
164 | :name name | |
165 | :type type | |
166 | :location (file-location location) | |
167 | :pset pset))) | |
dea4d055 | 168 | (with-slots (messages) class |
2e1a785d MW |
169 | (setf messages (append messages (list message)))) |
170 | message))) | |
dea4d055 MW |
171 | |
172 | (defmethod shared-initialize :after | |
173 | ((message sod-message) slot-names &key pset) | |
174 | (declare (ignore slot-names pset)) | |
175 | (with-slots (type) message | |
176 | (check-message-type message type))) | |
177 | ||
178 | (defmethod check-message-type ((message sod-message) (type c-function-type)) | |
179 | nil) | |
180 | ||
181 | (defmethod check-message-type ((message sod-message) (type c-type)) | |
182 | (error "Messages must have function type, not ~A" type)) | |
183 | ||
184 | ;;;-------------------------------------------------------------------------- | |
185 | ;;; Methods. | |
186 | ||
187 | (defmethod make-sod-method | |
188 | ((class sod-class) nick name type body pset &optional location) | |
189 | (with-default-error-location (location) | |
190 | (let* ((message (find-message-by-name class nick name)) | |
191 | (method (make-sod-method-using-message message class | |
192 | type body pset | |
193 | (file-location location)))) | |
194 | (with-slots (methods) class | |
2e1a785d MW |
195 | (setf methods (append methods (list method)))) |
196 | method))) | |
dea4d055 MW |
197 | |
198 | (defmethod make-sod-method-using-message | |
199 | ((message sod-message) (class sod-class) type body pset location) | |
52a79ab8 | 200 | (make-instance (or (get-property pset :method-class :symbol) |
dea4d055 MW |
201 | (sod-message-method-class message class pset)) |
202 | :message message | |
203 | :class class | |
204 | :type type | |
205 | :body body | |
206 | :location location | |
207 | :pset pset)) | |
208 | ||
209 | (defmethod sod-message-method-class | |
210 | ((message sod-message) (class sod-class) pset) | |
211 | (declare (ignore pset)) | |
212 | 'sod-method) | |
213 | ||
214 | (defmethod shared-initialize :after | |
215 | ((method sod-method) slot-names &key pset) | |
216 | (declare (ignore slot-names pset)) | |
217 | ||
218 | ;; Check that the arguments are named if we have a method body. | |
219 | (with-slots (body type) method | |
220 | (unless (or (not body) | |
9ec578d9 | 221 | (every (lambda (arg) |
c07860af MW |
222 | (or (eq arg :ellipsis) |
223 | (argument-name arg) | |
9ec578d9 MW |
224 | (eq (argument-type arg) (c-type void)))) |
225 | (c-function-arguments type))) | |
dea4d055 MW |
226 | (error "Abstract declarators not permitted in method definitions"))) |
227 | ||
228 | ;; Check the method type. | |
229 | (with-slots (message type) method | |
230 | (check-method-type method message type))) | |
231 | ||
232 | (defmethod check-method-type | |
233 | ((method sod-method) (message sod-message) (type c-type)) | |
234 | (error "Methods must have function type, not ~A" type)) | |
235 | ||
236 | (defmethod check-method-type | |
237 | ((method sod-method) (message sod-message) (type c-function-type)) | |
238 | (with-slots ((msgtype type)) message | |
239 | (unless (c-type-equal-p (c-type-subtype msgtype) | |
240 | (c-type-subtype type)) | |
241 | (error "Method return type ~A doesn't match message ~A" | |
242 | (c-type-subtype msgtype) (c-type-subtype type))) | |
243 | (unless (argument-lists-compatible-p (c-function-arguments msgtype) | |
244 | (c-function-arguments type)) | |
245 | (error "Method arguments ~A don't match message ~A" type msgtype)))) | |
246 | ||
247 | ;;;----- That's all, folks -------------------------------------------------- |