src/method-impl.lisp: Argument name list should only contain names.
[sod] / src / method-impl.lisp
CommitLineData
1f1d88f5
MW
1;;; -*-lisp-*-
2;;;
dea4d055 3;;; Method combination implementation
1f1d88f5
MW
4;;;
5;;; (c) 2009 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
dea4d055 10;;; This file is part of the Sensble Object Design, an object system for C.
1f1d88f5
MW
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;;;--------------------------------------------------------------------------
1f1d88f5
MW
29;;; Message classes.
30
dea4d055 31(export 'basic-message)
1f1d88f5
MW
32(defclass basic-message (sod-message)
33 ((argument-tail :type list :reader sod-message-argument-tail)
34 (no-varargs-tail :type list :reader sod-message-no-varargs-tail))
35 (:documentation
36 "Base class for built-in message classes.
37
38 Provides the basic functionality for the built-in method combinations.
39 This is a separate class so that `special effect' messages can avoid
40 inheriting its default behaviour.
41
dea4d055 42 The function type protocol is implemented on `basic-message' using slot
1f1d88f5 43 reader methods. The actual values are computed on demand in methods
dea4d055 44 defined on `slot-unbound'."))
1f1d88f5
MW
45
46(defmethod slot-unbound (class
47 (message basic-message)
48 (slot-name (eql 'argument-tail)))
1d8cc67a 49 (declare (ignore class))
1f1d88f5 50 (let ((seq 0))
dea4d055
MW
51 (setf (slot-value message 'argument-tail)
52 (mapcar (lambda (arg)
53 (if (or (eq arg :ellipsis) (argument-name arg)) arg
54 (make-argument (make-instance 'temporary-argument
55 :tag (prog1 seq
56 (incf seq)))
57 (argument-type arg))))
58 (c-function-arguments (sod-message-type message))))))
1f1d88f5
MW
59
60(defmethod slot-unbound (class
61 (message basic-message)
62 (slot-name (eql 'no-varargs-tail)))
1d8cc67a 63 (declare (ignore class))
dea4d055
MW
64 (setf (slot-value message 'no-varargs-tail)
65 (mapcar (lambda (arg)
66 (if (eq arg :ellipsis)
67 (make-argument *sod-ap* (c-type va-list))
68 arg))
69 (sod-message-argument-tail message))))
1f1d88f5
MW
70
71(defmethod sod-message-method-class
72 ((message basic-message) (class sod-class) pset)
73 (let ((role (get-property pset :role :keyword nil)))
74 (case role
75 ((:before :after) 'daemon-direct-method)
76 (:around 'delegating-direct-method)
77 ((nil) (error "How odd: a primary method slipped through the net"))
78 (t (error "Unknown method role ~A" role)))))
79
dea4d055
MW
80(export 'simple-message)
81(defclass simple-message (basic-message)
82 ()
83 (:documentation
84 "Base class for messages with `simple' method combinations.
1f1d88f5 85
dea4d055 86 A simple method combination is one which has only one method role other
3109662a
MW
87 than the `before', `after' and `around' methods provided by
88 `basic-message'. We call these `primary' methods, and the programmer
89 designates them by not specifying an explicit role.
1f1d88f5 90
dea4d055
MW
91 If the programmer doesn't define any primary methods then the effective
92 method is null -- i.e., the method entry pointer shows up as a null
93 pointer."))
94
95(defmethod sod-message-method-class
96 ((message simple-message) (class sod-class) pset)
97 (if (get-property pset :role :keyword nil)
98 (call-next-method)
99 (primary-method-class message)))
1f1d88f5 100
d7e47285
MW
101(defmethod primary-method-class ((message simple-message))
102 'basic-direct-method)
103
1f1d88f5
MW
104;;;--------------------------------------------------------------------------
105;;; Direct method classes.
106
dea4d055 107(export 'basic-direct-method)
1f1d88f5 108(defclass basic-direct-method (sod-method)
77027cca
MW
109 ((role :initarg :role :type symbol :reader sod-method-role)
110 (function-type :type c-function-type :reader sod-method-function-type))
1f1d88f5
MW
111 (:documentation
112 "Base class for built-in direct method classes.
113
114 Provides the basic functionality for the built-in direct-method classes.
115 This is a separate class so that `special effect' methods can avoid
116 inheriting its default behaviour and slots.
117
118 A basic method can be assigned a `role', which may be set either as an
dea4d055 119 initarg or using the `:role' property. Roles are used for method
1f1d88f5
MW
120 categorization.
121
dea4d055 122 The function type protocol is implemented on `basic-direct-method' using
1f1d88f5 123 slot reader methods. The actual values are computed on demand in methods
dea4d055 124 defined on `slot-unbound'."))
1f1d88f5
MW
125
126(defmethod shared-initialize :after
127 ((method basic-direct-method) slot-names &key pset)
128 (declare (ignore slot-names))
129 (default-slot (method 'role) (get-property pset :role :keyword nil)))
130
131(defmethod slot-unbound
132 (class (method basic-direct-method) (slot-name (eql 'function-type)))
1d8cc67a 133 (declare (ignore class))
1f1d88f5
MW
134 (let ((type (sod-method-type method)))
135 (setf (slot-value method 'function-type)
136 (c-type (fun (lisp (c-type-subtype type))
137 ("me" (* (class (sod-method-class method))))
138 . (c-function-arguments type))))))
139
ddee4bb1 140(defmethod sod-method-function-name ((method basic-direct-method))
1f1d88f5
MW
141 (with-slots (class role message) method
142 (format nil "~A__~@[~(~A~)_~]method_~A__~A" class role
143 (sod-class-nickname (sod-message-class message))
144 (sod-message-name message))))
145
dea4d055 146(export 'daemon-direct-method)
1f1d88f5
MW
147(defclass daemon-direct-method (basic-direct-method)
148 ()
149 (:documentation
150 "A daemon direct method is invoked for side effects and cannot override.
151
152 This is the direct method class for `before' and `after' methods, which
153 cannot choose to override the remaining methods and are not involved in
154 the computation of the final result.
155
156 In C terms, a daemon method must return `void', and is not passed a
157 `next_method' pointer."))
158
dea4d055
MW
159(defmethod check-method-type ((method daemon-direct-method)
160 (message sod-message)
161 (type c-function-type))
1f1d88f5
MW
162 (with-slots ((msgtype type)) message
163 (unless (c-type-equal-p (c-type-subtype type) (c-type void))
164 (error "Method return type ~A must be `void'" (c-type-subtype type)))
165 (unless (argument-lists-compatible-p (c-function-arguments msgtype)
166 (c-function-arguments type))
167 (error "Method arguments ~A don't match message ~A" type msgtype))))
168
dea4d055 169(export 'delegating-direct-method)
1f1d88f5
MW
170(defclass delegating-direct-method (basic-direct-method)
171 ((next-method-type :type c-function-type
172 :reader sod-method-next-method-type))
173 (:documentation
174 "A delegating direct method can choose to override other methods.
175
176 This is the direct method class for `around' and standard-method-
177 combination primary methods, which are given the choice of computing the
178 entire method's result or delegating to (usually) less-specific methods.
179
180 In C terms, a delegating method is passed a `next_method' pointer so that
181 it can delegate part of its behaviour. (A delegating direct method for a
182 varargs message is also given an additional `va_list' argument,
183 conventionally named `sod__ap_master', which it is expected to pass on to
184 its `next_method' function if necessary.)
185
bf090e02 186 The function type protocol is implemented on `delegating-direct-method'
1f1d88f5 187 using slot reader methods. The actual values are computed on demand in
bf090e02 188 methods defined on `slot-unbound'."))
1f1d88f5
MW
189
190(defmethod slot-unbound (class
191 (method delegating-direct-method)
192 (slot-name (eql 'next-method-type)))
1d8cc67a 193 (declare (ignore class))
1f1d88f5 194 (let* ((message (sod-method-message method))
e5fae432
MW
195 (return-type (c-type-subtype (sod-message-type message)))
196 (msgargs (sod-message-argument-tail message))
197 (arguments (if (varargs-message-p message)
198 (cons (make-argument *sod-master-ap*
199 (c-type va-list))
200 (butlast msgargs))
201 msgargs)))
1f1d88f5 202 (setf (slot-value method 'next-method-type)
e5fae432 203 (c-type (fun (lisp return-type)
1f1d88f5 204 ("me" (* (class (sod-method-class method))))
e5fae432 205 . arguments)))))
1f1d88f5
MW
206
207(defmethod slot-unbound (class
208 (method delegating-direct-method)
209 (slot-name (eql 'function-type)))
1d8cc67a 210 (declare (ignore class))
1f1d88f5
MW
211 (let* ((message (sod-method-message method))
212 (type (sod-method-type method))
213 (method-args (c-function-arguments type)))
214 (setf (slot-value method 'function-type)
215 (c-type (fun (lisp (c-type-subtype type))
216 ("me" (* (class (sod-method-class method))))
217 ("next_method" (* (lisp (commentify-function-type
218 (sod-method-next-method-type
219 method)))))
bf090e02
MW
220 .
221 (if (varargs-message-p message)
222 (cons (make-argument *sod-master-ap*
223 (c-type va-list))
224 method-args)
225 method-args))))))
1f1d88f5
MW
226
227;;;--------------------------------------------------------------------------
228;;; Effective method classes.
229
dea4d055 230(export 'basic-effective-method)
1f1d88f5 231(defclass basic-effective-method (effective-method)
77027cca
MW
232 ((around-methods :initarg :around-methods :initform nil
233 :type list :reader effective-method-around-methods)
234 (before-methods :initarg :before-methods :initform nil
235 :type list :reader effective-method-before-methods)
236 (after-methods :initarg :after-methods :initform nil
237 :type list :reader effective-method-after-methods)
1f1d88f5
MW
238 (basic-argument-names :type list
239 :reader effective-method-basic-argument-names)
240 (functions :type list :reader effective-method-functions))
241 (:documentation
242 "Base class for built-in effective method classes.
243
244 This class maintains lists of the applicable `before', `after' and
245 `around' methods and provides behaviour for invoking these methods
246 correctly.
247
dea4d055
MW
248 The argument names protocol is implemented on `basic-effective-method'
249 using a slot reader method. The actual values are computed on demand in
250 methods defined on `slot-unbound'."))
1f1d88f5
MW
251
252(defmethod slot-unbound (class
253 (method basic-effective-method)
254 (slot-name (eql 'basic-argument-names)))
1d8cc67a 255 (declare (ignore class))
1f1d88f5
MW
256 (let ((message (effective-method-message method)))
257 (setf (slot-value method 'basic-argument-names)
258 (subst *sod-master-ap* *sod-ap*
259 (mapcar #'argument-name
260 (sod-message-no-varargs-tail message))))))
261
dea4d055
MW
262(defmethod effective-method-function-name ((method effective-method))
263 (let* ((class (effective-method-class method))
264 (message (effective-method-message method))
265 (message-class (sod-message-class message)))
266 (format nil "~A__emethod_~A__~A"
267 class
268 (sod-class-nickname message-class)
269 (sod-message-name message))))
1f1d88f5 270
dea4d055
MW
271(defmethod slot-unbound
272 (class (method basic-effective-method) (slot-name (eql 'functions)))
1d8cc67a 273 (declare (ignore class))
dea4d055
MW
274 (setf (slot-value method 'functions)
275 (compute-method-entry-functions method)))
1f1d88f5 276
dea4d055
MW
277(export 'simple-effective-method)
278(defclass simple-effective-method (basic-effective-method)
279 ((primary-methods :initarg :primary-methods :initform nil
280 :type list :reader effective-method-primary-methods))
1f1d88f5 281 (:documentation
dea4d055 282 "Effective method counterpart to `simple-message'."))
1f1d88f5 283
dea4d055
MW
284(defmethod shared-initialize :after
285 ((method simple-effective-method) slot-names &key direct-methods)
286 (declare (ignore slot-names))
287 (categorize (method direct-methods :bind ((role (sod-method-role method))))
288 ((primary (null role))
289 (before (eq role :before))
290 (after (eq role :after))
291 (around (eq role :around)))
292 (with-slots (primary-methods before-methods after-methods around-methods)
293 method
294 (setf primary-methods primary
295 before-methods before
296 after-methods (reverse after)
297 around-methods around))))
298
299;;;--------------------------------------------------------------------------
300;;; Code generation.
1f1d88f5
MW
301
302(defmethod shared-initialize :after
303 ((codegen method-codegen) slot-names &key)
1d8cc67a 304 (declare (ignore slot-names))
1f1d88f5
MW
305 (with-slots (message target) codegen
306 (setf target
307 (if (eq (c-type-subtype (sod-message-type message)) (c-type void))
308 :void
309 :return))))
310
dea4d055
MW
311;;;--------------------------------------------------------------------------
312;;; Invoking direct methods.
1f1d88f5 313
dea4d055 314(export 'basic-effective-method-body)
1f1d88f5
MW
315(defun basic-effective-method-body (codegen target method body)
316 "Build the common method-invocation structure.
317
318 Writes to CODEGEN some basic method-invocation instructions. It invokes
319 the `around' methods, from most- to least-specific. If they all delegate,
320 then the `before' methods are run, most-specific first; next, the
321 instructions generated by BODY (invoked with a target argument); then, the
322 `after' methods are run, least-specific first; and, finally, the value
323 delivered by the BODY is returned to the `around' methods. The result
324 returned by the outermost `around' method -- or, if there are none,
325 delivered by the BODY -- is finally delivered to the TARGET."
326
327 (with-slots (message class before-methods after-methods around-methods)
328 method
329 (let* ((message-type (sod-message-type message))
330 (return-type (c-type-subtype message-type))
1f1d88f5
MW
331 (basic-tail (effective-method-basic-argument-names method)))
332 (flet ((method-kernel (target)
333 (dolist (before before-methods)
334 (invoke-method codegen :void basic-tail before))
cb35f25e 335 (if (null after-methods)
1f1d88f5
MW
336 (funcall body target)
337 (convert-stmts codegen target return-type
338 (lambda (target)
339 (funcall body target)
340 (dolist (after (reverse after-methods))
341 (invoke-method codegen :void
a898c9e2 342 basic-tail after)))))))
1f1d88f5
MW
343 (invoke-delegation-chain codegen target basic-tail
344 around-methods #'method-kernel)))))
345
346;;;--------------------------------------------------------------------------
dea4d055 347;;; Method entry points.
1f1d88f5 348
a07d8d00 349(defparameter *method-entry-inline-threshold* 200
1f1d88f5
MW
350 "Threshold below which effective method bodies are inlined into entries.
351
352 After the effective method body has been computed, we calculate its
353 metric, multiply by the number of entries we need to generate, and compare
354 it with this threshold. If the metric is below the threshold then we
355 fold the method body into the entry functions; otherwise we split the
356 effective method out into its own function.")
357
1f1d88f5
MW
358(defmethod method-entry-function-name
359 ((method effective-method) (chain-head sod-class))
360 (let* ((class (effective-method-class method))
361 (message (effective-method-message method))
362 (message-class (sod-message-class message)))
dea4d055
MW
363 (if (or (not (slot-boundp method 'functions))
364 (slot-value method 'functions))
365 (format nil "~A__mentry_~A__~A__chain_~A"
366 class
367 (sod-class-nickname message-class)
368 (sod-message-name message)
369 (sod-class-nickname chain-head))
370 0)))
371
372(defmethod method-entry-function-type ((entry method-entry))
373 (let* ((method (method-entry-effective-method entry))
374 (message (effective-method-message method))
375 (type (sod-message-type message)))
376 (c-type (fun (lisp (c-type-subtype type))
377 ("me" (* (class (method-entry-chain-tail entry))))
378 . (sod-message-argument-tail message)))))
379
380(defmethod make-method-entry ((method basic-effective-method)
381 (chain-head sod-class) (chain-tail sod-class))
382 (make-instance 'method-entry
383 :method method
384 :chain-head chain-head
385 :chain-tail chain-tail))
1f1d88f5
MW
386
387(defmethod compute-method-entry-functions ((method basic-effective-method))
388
389 ;; OK, there's quite a lot of this, so hold tight.
390 ;;
391 ;; The first thing we need to do is find all of the related objects. This
392 ;; is a bit verbose but fairly straightforward.
393 ;;
bf090e02
MW
394 ;; Next, we generate the effective method body -- using `compute-effective-
395 ;; method-body' of all things. This gives us the declarations and body for
1f1d88f5
MW
396 ;; an effective method function, but we don't have an actual function yet.
397 ;;
398 ;; Now we look at the chains which are actually going to need a method
399 ;; entry: only those chains whose tail (most specific) class is a
400 ;; superclass of the class which defined the message need an entry. We
401 ;; build a list of these tail classes.
402 ;;
403 ;; Having done this, we decide whether it's better to generate a standalone
404 ;; effective-method function and call it from each of the method entries,
405 ;; or to inline the effective method body into each of the entries.
406 ;;
407 ;; Most of the complexity here comes from (a) dealing with the two
408 ;; different strategies for constructing method entry functions and (b)
409 ;; (unsurprisingly) the mess involved with dealing with varargs messages.
410
411 (let* ((message (effective-method-message method))
412 (class (effective-method-class method))
413 (message-class (sod-message-class message))
414 (return-type (c-type-subtype (sod-message-type message)))
415 (codegen (make-instance 'method-codegen
416 :message message
417 :class class
418 :method method))
419
420 ;; Effective method function details.
421 (emf-name (effective-method-function-name method))
422 (ilayout-type (c-type (* (struct (ilayout-struct-tag class)))))
423 (emf-arg-tail (mapcar (lambda (arg)
424 (if (eq (argument-name arg) *sod-ap*)
425 (make-argument *sod-master-ap*
426 (c-type va-list))
427 arg))
428 (sod-message-no-varargs-tail message)))
429 (emf-type (c-type (fun (lisp return-type)
430 ("sod__obj" (lisp ilayout-type))
431 . (sod-message-no-varargs-tail message))))
1f1d88f5
MW
432
433 ;; Method entry details.
434 (chain-tails (remove-if-not (lambda (super)
435 (sod-subclass-p super message-class))
436 (mapcar #'car
437 (sod-class-chains class))))
438 (n-entries (length chain-tails))
439 (entry-args (sod-message-argument-tail message))
440 (parm-n (do ((prev "me" (car args))
441 (args entry-args (cdr args)))
442 ((endp args) nil)
443 (when (eq (car args) :ellipsis)
444 (return prev))))
445 (entry-target (codegen-target codegen)))
446
dea4d055
MW
447 (flet ((setup-entry (tail)
448 (let ((head (sod-class-chain-head tail)))
449 (codegen-push codegen)
450 (ensure-var codegen "sod__obj" ilayout-type
451 (make-convert-to-ilayout-inst class
452 head "me"))))
453 (varargs-prologue ()
454 (ensure-var codegen *sod-master-ap* (c-type va-list))
455 (emit-inst codegen
0c455928
MW
456 (make-va-start-inst *sod-master-ap*
457 (argument-name parm-n))))
dea4d055
MW
458 (varargs-epilogue ()
459 (emit-inst codegen (make-va-end-inst *sod-master-ap*)))
460 (finish-entry (tail)
461 (let* ((head (sod-class-chain-head tail))
462 (name (method-entry-function-name method head))
463 (type (c-type (fun (lisp return-type)
464 ("me" (* (class tail)))
465 . entry-args))))
466 (codegen-pop-function codegen name type))))
1f1d88f5
MW
467
468 ;; Generate the method body. We'll work out what to do with it later.
469 (codegen-push codegen)
4e561d89 470 (let* ((result (if (eq return-type c-type-void) nil
9ec578d9
MW
471 (temporary-var codegen return-type)))
472 (emf-target (or result :void)))
473 (compute-effective-method-body method codegen emf-target)
474 (multiple-value-bind (vars insts) (codegen-pop codegen)
475 (cond ((or (= n-entries 1)
476 (<= (* n-entries (reduce #'+ insts :key #'inst-metric))
477 *method-entry-inline-threshold*))
478
479 ;; The effective method body is simple -- or there's only
480 ;; one of them. We'll inline the method body into the entry
481 ;; functions.
1f1d88f5
MW
482 (dolist (tail chain-tails)
483 (setup-entry tail)
9ec578d9 484 (dolist (var vars)
66836e14
MW
485 (if (typep var 'var-inst)
486 (ensure-var codegen (inst-name var)
487 (inst-type var) (inst-init var))
488 (emit-decl codegen var)))
9ec578d9
MW
489 (when parm-n (varargs-prologue))
490 (emit-insts codegen insts)
491 (when parm-n (varargs-epilogue))
492 (deliver-expr codegen entry-target result)
493 (finish-entry tail)))
494
495 (t
496
497 ;; The effective method body is complicated and we'd need
498 ;; more than one copy. We'll generate an effective method
499 ;; function and call it a lot.
500 (codegen-build-function codegen emf-name emf-type vars
501 (nconc insts (and result
502 (list (make-return-inst result)))))
503
504 (let ((call (make-call-inst emf-name
505 (cons "sod__obj" (mapcar #'argument-name
506 emf-arg-tail)))))
507 (dolist (tail chain-tails)
508 (setup-entry tail)
509 (cond (parm-n
510 (varargs-prologue)
511 (convert-stmts codegen entry-target return-type
512 (lambda (target)
513 (deliver-expr codegen
514 target call)
515 (varargs-epilogue))))
516 (t
517 (deliver-expr codegen entry-target call)))
518 (finish-entry tail)))))))
1f1d88f5
MW
519
520 (codegen-functions codegen))))
521
dea4d055
MW
522(defmethod compute-method-entry-functions
523 ((method simple-effective-method))
524 (if (effective-method-primary-methods method)
525 (call-next-method)
526 nil))
527
528(defmethod compute-effective-method-body
529 ((method simple-effective-method) codegen target)
599fe2c7
MW
530 (basic-effective-method-body codegen target method
531 (lambda (target)
532 (simple-method-body method
533 codegen
534 target))))
1f1d88f5 535
dea4d055
MW
536;;;--------------------------------------------------------------------------
537;;; Standard method combination.
ddee4bb1 538
dea4d055
MW
539(export 'standard-message)
540(defclass standard-message (simple-message)
541 ()
542 (:documentation
1a93d254 543 "Message class for standard method combinations.
1f1d88f5 544
dea4d055
MW
545 Standard method combination is a simple method combination where the
546 primary methods are invoked as a delegation chain, from most- to
547 least-specific."))
548
549(export 'standard-effective-method)
550(defclass standard-effective-method (simple-effective-method) ()
551 (:documentation "Effective method counterpart to `standard-message'."))
552
553(defmethod primary-method-class ((message standard-message))
554 'delegating-direct-method)
555
556(defmethod message-effective-method-class ((message standard-message))
557 'standard-effective-method)
558
559(defmethod simple-method-body
560 ((method standard-effective-method) codegen target)
561 (invoke-delegation-chain codegen
562 target
563 (effective-method-basic-argument-names method)
564 (effective-method-primary-methods method)
565 nil))
a07d8d00 566
1f1d88f5 567;;;----- That's all, folks --------------------------------------------------