3 ;;; Class layout 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 print-object ((slot effective-slot) stream)
32 (maybe-print-unreadable-object (slot stream :type t)
33 (format stream "~A~@[ = ~@_~A~]"
34 (effective-slot-direct-slot slot)
35 (effective-slot-initializer slot))))
37 (defmethod find-slot-initializer ((class sod-class) (slot sod-slot))
40 (sod-class-instance-initializers super)
41 :key #'sod-initializer-slot))
42 (sod-class-precedence-list class)))
44 (defmethod compute-effective-slot ((class sod-class) (slot sod-slot))
45 (make-instance 'effective-slot
48 :initializer (find-slot-initializer class slot)))
50 ;;;--------------------------------------------------------------------------
51 ;;; Special-purpose slot objects.
53 (export 'sod-class-slot)
54 (defclass sod-class-slot (sod-slot)
55 ((initializer-function :initarg :initializer-function
56 :type (or symbol function)
57 :reader sod-slot-initializer-function)
58 (prepare-function :initarg :prepare-function :type (or symbol function)
59 :reader sod-slot-prepare-function))
61 "Special class for slots defined on `SodClass'.
63 These slots need class-specific initialization. It's easier to keep all
64 of the information (name, type, and how to initialize them) about these
65 slots in one place, so that's what we do here."))
67 (defmethod shared-initialize :after
68 ((slot sod-class-slot) slot-names &key pset)
69 (declare (ignore slot-names))
70 (default-slot (slot 'initializer-function)
71 (get-property pset :initializer-function :func nil))
72 (default-slot (slot 'prepare-function)
73 (get-property pset :prepare-function :func nil)))
75 (export 'sod-class-effective-slot)
76 (defclass sod-class-effective-slot (effective-slot)
77 ((initializer-function :initarg :initializer-function
78 :type (or symbol function)
79 :reader effective-slot-initializer-function)
80 (prepare-function :initarg :prepare-function :type (or symbol function)
81 :reader effective-slot-prepare-function))
83 "Special class for slots defined on `SodClass'.
85 This class ignores any explicit initializers and computes initializer
86 values using the slot's INIT-FUNC slot and a magical protocol during
87 metaclass instance construction."))
89 (defmethod compute-effective-slot ((class sod-class) (slot sod-class-slot))
90 (make-instance 'sod-class-effective-slot
91 :class class :slot slot
92 :initializer-function (sod-slot-initializer-function slot)
93 :prepare-function (sod-slot-prepare-function slot)
94 :initializer (find-slot-initializer class slot)))
96 ;;;--------------------------------------------------------------------------
97 ;;; Effective methods.
99 (defmethod print-object ((method effective-method) stream)
100 (maybe-print-unreadable-object (method stream :type t)
101 (format stream "~A ~A"
102 (effective-method-message method)
103 (effective-method-class method))))
105 (defmethod print-object ((entry method-entry) stream)
106 (maybe-print-unreadable-object (entry stream :type t)
107 (format stream "~A:~A"
108 (method-entry-effective-method entry)
109 (sod-class-nickname (method-entry-chain-head entry)))))
111 (defmethod compute-sod-effective-method
112 ((message sod-message) (class sod-class))
113 (let ((direct-methods (mappend (lambda (super)
115 (sod-class-methods super)
116 :key #'sod-method-message
118 (sod-class-precedence-list class))))
119 (make-instance (message-effective-method-class message)
122 :direct-methods direct-methods)))
124 (defmethod compute-effective-methods ((class sod-class))
125 (mapcan (lambda (super)
126 (mapcar (lambda (message)
127 (compute-sod-effective-method message class))
128 (sod-class-messages super)))
129 (sod-class-precedence-list class)))
131 (defmethod slot-unbound
132 (clos-class (class sod-class) (slot-name (eql 'effective-methods)))
133 (declare (ignore clos-class))
134 (setf (slot-value class 'effective-methods)
135 (compute-effective-methods class)))
137 ;;;--------------------------------------------------------------------------
142 (defmethod print-object ((islots islots) stream)
143 (print-unreadable-object (islots stream :type t)
144 (format stream "~A <= ~A ~_~:<~@{~S~^ ~_~}~:>"
145 (islots-subclass islots)
146 (islots-class islots)
147 (islots-slots islots))))
149 (defmethod compute-islots ((class sod-class) (subclass sod-class))
150 (make-instance 'islots
153 :slots (mapcar (lambda (slot)
154 (compute-effective-slot subclass slot))
155 (sod-class-slots class))))
158 ;;; Do we need a construction protocol here?
160 (defmethod print-object ((vtp vtable-pointer) stream)
161 (print-unreadable-object (vtp stream :type t)
162 (format stream "~A:~A"
163 (vtable-pointer-class vtp)
164 (sod-class-nickname (vtable-pointer-chain-head vtp)))))
168 (defmethod print-object ((ichain ichain) stream)
169 (print-unreadable-object (ichain stream :type t)
170 (format stream "~A:~A ~_~:<~@{~S~^ ~_~}~:>"
171 (ichain-class ichain)
172 (sod-class-nickname (ichain-head ichain))
173 (ichain-body ichain))))
175 (defmethod compute-ichain ((class sod-class) chain)
176 (let* ((chain-head (car chain))
177 (chain-tail (find chain-head (mapcar #'car (sod-class-chains class))
178 :key #'sod-class-chain-head))
179 (vtable-pointer (make-instance 'vtable-pointer
181 :chain-head chain-head
182 :chain-tail chain-tail))
183 (islots (remove-if-not #'islots-slots
184 (mapcar (lambda (super)
185 (compute-islots super class))
187 (make-instance 'ichain
189 :chain-head chain-head
190 :chain-tail chain-tail
191 :body (cons vtable-pointer islots))))
195 (defmethod print-object ((ilayout ilayout) stream)
196 (print-unreadable-object (ilayout stream :type t)
197 (format stream "~A ~_~:<~@{~S~^ ~_~}~:>"
198 (ilayout-class ilayout)
199 (ilayout-ichains ilayout))))
201 (defmethod compute-ilayout ((class sod-class))
202 (make-instance 'ilayout
204 :ichains (mapcar (lambda (chain)
205 (compute-ichain class
207 (sod-class-chains class))))
209 (defmethod slot-unbound
210 (clos-class (class sod-class) (slot-name (eql 'ilayout)))
211 (declare (ignore clos-class))
212 (setf (slot-value class 'ilayout)
213 (compute-ilayout class)))
215 ;;;--------------------------------------------------------------------------
220 (defmethod print-object ((vtmsgs vtmsgs) stream)
221 (print-unreadable-object (vtmsgs stream :type t)
222 (format stream "~A <= ~A ~_~:<~@{~S~^ ~_~}~:>"
223 (vtmsgs-subclass vtmsgs)
224 (vtmsgs-class vtmsgs)
225 (vtmsgs-entries vtmsgs))))
227 (defmethod compute-vtmsgs
230 (chain-head sod-class)
231 (chain-tail sod-class))
232 (flet ((make-entry (message)
233 (let ((method (find message
234 (sod-class-effective-methods subclass)
235 :key #'effective-method-message)))
236 (make-method-entry method chain-head chain-tail))))
237 (make-instance 'vtmsgs
240 :chain-head chain-head
241 :chain-tail chain-tail
242 :entries (mapcar #'make-entry
243 (sod-class-messages class)))))
247 (defmethod print-object ((cptr class-pointer) stream)
248 (print-unreadable-object (cptr stream :type t)
249 (format stream "~A:~A"
250 (class-pointer-metaclass cptr)
251 (sod-class-nickname (class-pointer-meta-chain-head cptr)))))
253 (defmethod make-class-pointer
254 ((class sod-class) (chain-head sod-class)
255 (metaclass sod-class) (meta-chain-head sod-class))
257 ;; Slightly tricky. We don't necessarily want a pointer to the metaclass,
258 ;; but to its most specific subclass on the given chain. Fortunately, CL
259 ;; is good at this game.
260 (let* ((meta-chains (sod-class-chains metaclass))
261 (meta-chain-tails (mapcar #'car meta-chains))
262 (meta-chain-tail (find meta-chain-head meta-chain-tails
263 :key #'sod-class-chain-head)))
264 (make-instance 'class-pointer
266 :chain-head chain-head
267 :metaclass meta-chain-tail
268 :meta-chain-head meta-chain-head)))
272 (defmethod print-object ((boff base-offset) stream)
273 (print-unreadable-object (boff stream :type t)
274 (format stream "~A:~A"
275 (base-offset-class boff)
276 (sod-class-nickname (base-offset-chain-head boff)))))
278 (defmethod make-base-offset ((class sod-class) (chain-head sod-class))
279 (make-instance 'base-offset
281 :chain-head chain-head))
285 (defmethod print-object ((choff chain-offset) stream)
286 (print-unreadable-object (choff stream :type t)
287 (format stream "~A:~A->~A"
288 (chain-offset-class choff)
289 (sod-class-nickname (chain-offset-chain-head choff))
290 (sod-class-nickname (chain-offset-target-head choff)))))
292 (defmethod make-chain-offset
293 ((class sod-class) (chain-head sod-class) (target-head sod-class))
294 (make-instance 'chain-offset
296 :chain-head chain-head
297 :target-head target-head))
301 (defmethod print-object ((vtable vtable) stream)
302 (print-unreadable-object (vtable stream :type t)
303 (format stream "~A:~A ~_~:<~@{~S~^ ~_~}~:>"
304 (vtable-class vtable)
305 (sod-class-nickname (vtable-chain-head vtable))
306 (vtable-body vtable))))
308 ;; Special variables used by `compute-vtable'.
309 (defvar *done-metaclass-chains*)
310 (defvar *done-instance-chains*)
312 (defmethod compute-vtable-items
313 ((class sod-class) (super sod-class) (chain-head sod-class)
314 (chain-tail sod-class) (emit function))
316 ;; If this class introduces new metaclass chains, then emit pointers to
318 (let* ((metasuper (sod-class-metaclass super))
319 (metasuper-chains (sod-class-chains metasuper))
320 (metasuper-chain-heads (mapcar (lambda (chain)
321 (sod-class-chain-head (car chain)))
323 (dolist (metasuper-chain-head metasuper-chain-heads)
324 (unless (member metasuper-chain-head *done-metaclass-chains*)
325 (funcall emit (make-class-pointer class
328 metasuper-chain-head))
329 (push metasuper-chain-head *done-metaclass-chains*))))
331 ;; If there are new instance chains, then emit offsets to them.
332 (let* ((chains (sod-class-chains super))
333 (chain-heads (mapcar (lambda (chain)
334 (sod-class-chain-head (car chain)))
336 (dolist (head chain-heads)
337 (unless (member head *done-instance-chains*)
338 (funcall emit (make-chain-offset class chain-head head))
339 (push head *done-instance-chains*))))
341 ;; Finally, if there are interesting methods, emit those too.
342 (when (sod-class-messages super)
343 (funcall emit (compute-vtmsgs super class chain-head chain-tail))))
345 (defmethod compute-vtable ((class sod-class) (chain list))
346 (let* ((chain-head (car chain))
347 (chain-tail (find chain-head (mapcar #'car (sod-class-chains class))
348 :key #'sod-class-chain-head))
349 (*done-metaclass-chains* nil)
350 (*done-instance-chains* (list chain-head))
351 (done-superclasses nil)
356 ;; Find the root chain in the metaclass and write a pointer.
357 (let* ((metaclass (sod-class-metaclass class))
358 (metaclass-root (find-root-metaclass class))
359 (metaclass-root-head (sod-class-chain-head metaclass-root)))
360 (emit (make-class-pointer class chain-head metaclass
361 metaclass-root-head))
362 (push metaclass-root-head *done-metaclass-chains*))
364 ;; Write an offset to the instance base.
365 (emit (make-base-offset class chain-head))
367 ;; Now walk the chain. As we ascend the chain, scan the class
368 ;; precedence list of each class in reverse to ensure that we have
369 ;; everything interesting.
370 (dolist (super chain)
371 (dolist (sub (reverse (sod-class-precedence-list super)))
372 (unless (member sub done-superclasses)
373 (compute-vtable-items class
378 (push sub done-superclasses))))
381 (make-instance 'vtable
383 :chain-head chain-head
384 :chain-tail chain-tail
385 :body (nreverse items)))))
387 (defmethod compute-vtables ((class sod-class))
388 (mapcar (lambda (chain)
389 (compute-vtable class (reverse chain)))
390 (sod-class-chains class)))
392 (defmethod slot-unbound
393 (clos-class (class sod-class) (slot-name (eql 'vtables)))
394 (declare (ignore clos-class))
395 (setf (slot-value class 'vtables)
396 (compute-vtables class)))
398 ;;;----- That's all, folks --------------------------------------------------