3 ;;; Class construction protocol implementation
5 ;;; (c) 2009 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensble Object Design, an object system for C.
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.
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.
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.
28 ;;;--------------------------------------------------------------------------
31 (defmethod guess-metaclass ((class sod-class))
32 "Default metaclass-guessing function for classes.
34 Return the most specific metaclass of any of the CLASS's direct
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'"
43 ((endp supers) meta)))
45 (defmethod shared-initialize :after ((class sod-class) slot-names &key pset)
46 "Specific behaviour for SOD class initialization.
48 Properties inspected are as follows:
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.
54 * `:nick' provides a nickname for the class. If unspecified, a default
55 (the class's name, forced to lowercase) will be chosen in
58 * `:link' names the chained superclass. If unspecified, this class will
59 be left at the head of its chain."
61 ;; If no nickname, copy the class name. It won't be pretty, though.
62 (default-slot-from-property (class 'nickname slot-names)
64 (string-downcase (slot-value class 'name)))
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))
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))
76 ;;;--------------------------------------------------------------------------
79 (defmethod make-sod-slot
80 ((class sod-class) name type pset &optional location)
81 (with-default-error-location (location)
82 (let ((slot (make-instance (get-property pset :slot-class :symbol
87 :location (file-location location)
89 (with-slots (slots) class
90 (setf slots (append slots (list slot)))))))
92 (defmethod shared-initialize :after ((slot sod-slot) slot-names &key pset)
93 "This method does nothing.
95 It only exists so that it isn't an error to provide a `:pset' initarg
96 to (make-instance 'sod-slot ...)."
98 (declare (ignore slot-names pset)))
100 ;;;--------------------------------------------------------------------------
101 ;;; Slot initializers.
103 (defmethod make-sod-instance-initializer
104 ((class sod-class) nick name value-kind value-form pset
106 (with-default-error-location (location)
107 (let* ((slot (find-instance-slot-by-name class nick name))
108 (initializer (make-sod-initializer-using-slot
109 class slot 'sod-instance-initializer
110 value-kind value-form pset
111 (file-location location))))
112 (with-slots (instance-initializers) class
113 (setf instance-initializers
114 (append instance-initializers (list initializer)))))))
116 (defmethod make-sod-class-initializer
117 ((class sod-class) nick name value-kind value-form pset
119 (with-default-error-location (location)
120 (let* ((slot (find-class-slot-by-name class nick name))
121 (initializer (make-sod-initializer-using-slot
122 class slot 'sod-class-initializer
123 value-kind value-form pset
124 (file-location location))))
125 (with-slots (class-initializers) class
126 (setf class-initializers
127 (append class-initializers (list initializer)))))))
129 (defmethod make-sod-initializer-using-slot
130 ((class sod-class) (slot sod-slot)
131 init-class value-kind value-form pset location)
132 (make-instance (get-property pset :initializer-class :symbol init-class)
135 :value-kind value-kind
136 :value-form value-form
140 (defmethod shared-initialize :after
141 ((init sod-initializer) slot-names &key pset)
142 "This method does nothing.
144 It only exists so that it isn't an error to provide a `:pset' initarg
145 to (make-instance 'sod-initializer ...)."
146 (declare (ignore slot-names pset))
149 ;;;--------------------------------------------------------------------------
152 (defmethod make-sod-message
153 ((class sod-class) name type pset &optional location)
154 (with-default-error-location (location)
155 (let ((message (make-instance (get-property pset :message-class :symbol
160 :location (file-location location)
162 (with-slots (messages) class
163 (setf messages (append messages (list message)))))))
165 (defmethod shared-initialize :after
166 ((message sod-message) slot-names &key pset)
167 (declare (ignore slot-names pset))
168 (with-slots (type) message
169 (check-message-type message type)))
171 (defmethod check-message-type ((message sod-message) (type c-function-type))
174 (defmethod check-message-type ((message sod-message) (type c-type))
175 (error "Messages must have function type, not ~A" type))
177 ;;;--------------------------------------------------------------------------
180 (defmethod make-sod-method
181 ((class sod-class) nick name type body pset &optional location)
182 (with-default-error-location (location)
183 (let* ((message (find-message-by-name class nick name))
184 (method (make-sod-method-using-message message class
186 (file-location location))))
187 (with-slots (methods) class
188 (setf methods (append methods (list method)))))))
190 (defmethod make-sod-method-using-message
191 ((message sod-message) (class sod-class) type body pset location)
192 (make-instance (or (get-property pset :method-class :symbol)
193 (sod-message-method-class message class pset))
201 (defmethod sod-message-method-class
202 ((message sod-message) (class sod-class) pset)
203 (declare (ignore pset))
206 (defmethod shared-initialize :after
207 ((method sod-method) slot-names &key pset)
208 (declare (ignore slot-names pset))
210 ;; Check that the arguments are named if we have a method body.
211 (with-slots (body type) method
212 (unless (or (not body)
214 (or (argument-name arg)
215 (eq (argument-type arg) (c-type void))))
216 (c-function-arguments type)))
217 (error "Abstract declarators not permitted in method definitions")))
219 ;; Check the method type.
220 (with-slots (message type) method
221 (check-method-type method message type)))
223 (defmethod check-method-type
224 ((method sod-method) (message sod-message) (type c-type))
225 (error "Methods must have function type, not ~A" type))
227 (defmethod check-method-type
228 ((method sod-method) (message sod-message) (type c-function-type))
229 (with-slots ((msgtype type)) message
230 (unless (c-type-equal-p (c-type-subtype msgtype)
231 (c-type-subtype type))
232 (error "Method return type ~A doesn't match message ~A"
233 (c-type-subtype msgtype) (c-type-subtype type)))
234 (unless (argument-lists-compatible-p (c-function-arguments msgtype)
235 (c-function-arguments type))
236 (error "Method arguments ~A don't match message ~A" type msgtype))))
238 ;;;----- That's all, folks --------------------------------------------------