3 ;;; Method combination implementation
5 ;;; (c) 2009 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensible 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 (export 'basic-message)
32 (defclass basic-message (sod-message)
33 ((argument-tail :type list :reader sod-message-argument-tail))
35 "Base class for built-in message classes.
37 Provides the basic functionality for the built-in method combinations.
38 This is a separate class so that `special effect' messages can avoid
39 inheriting its default behaviour.
41 The function type protocol is implemented on `basic-message' using slot
42 reader methods. The actual values are computed on demand."))
44 (define-on-demand-slot basic-message argument-tail (message)
47 (if (or (eq arg :ellipsis) (argument-name arg)) arg
48 (make-argument (make-instance 'temporary-argument
51 (argument-type arg))))
52 (c-function-arguments (sod-message-type message)))))
54 (defmethod sod-message-method-class
55 ((message basic-message) (class sod-class) pset)
56 (let ((role (get-property pset :role :keyword nil)))
58 ((:before :after) 'daemon-direct-method)
59 (:around 'delegating-direct-method)
60 ((nil) (error "How odd: a primary method slipped through the net"))
61 (t (error "Unknown method role ~A" role)))))
63 (export 'simple-message)
64 (defclass simple-message (basic-message)
67 "Base class for messages with `simple' method combinations.
69 A simple method combination is one which has only one method role other
70 than the `before', `after' and `around' methods provided by
71 `basic-message'. We call these `primary' methods, and the programmer
72 designates them by not specifying an explicit role.
74 If the programmer doesn't define any primary methods then the effective
75 method is null -- i.e., the method entry pointer shows up as a null
78 (defmethod sod-message-method-class
79 ((message simple-message) (class sod-class) pset)
80 (if (get-property pset :role :keyword nil)
82 (primary-method-class message)))
84 (defmethod primary-method-class ((message simple-message))
87 ;;;--------------------------------------------------------------------------
88 ;;; Direct method classes.
90 (export '(basic-direct-method sod-method-role))
91 (defclass basic-direct-method (sod-method)
92 ((role :initarg :role :type symbol :reader sod-method-role)
93 (function-type :type c-function-type :reader sod-method-function-type))
95 "Base class for built-in direct method classes.
97 Provides the basic functionality for the built-in direct-method classes.
98 This is a separate class so that `special effect' methods can avoid
99 inheriting its default behaviour and slots.
101 A basic method can be assigned a `role', which may be set either as an
102 initarg or using the `:role' property. Roles are used for method
105 The function type protocol is implemented on `basic-direct-method' using
106 slot reader methods."))
108 (defmethod shared-initialize :after
109 ((method basic-direct-method) slot-names &key pset)
110 (declare (ignore slot-names))
111 (default-slot (method 'role) (get-property pset :role :keyword nil)))
113 (defun direct-method-suppliedp-struct-tag (direct-method)
114 (with-slots ((class %class) role message) direct-method
115 (format nil "~A__~@[~(~A~)_~]suppliedp_~A__~A"
117 (sod-class-nickname (sod-message-class message))
118 (sod-message-name message))))
120 (defun effective-method-keyword-struct-tag (effective-method)
121 (with-slots ((class %class) message) effective-method
122 (format nil "~A__keywords_~A__~A"
124 (sod-class-nickname (sod-message-class message))
125 (sod-message-name message))))
127 (defun fix-up-keyword-method-args (method args)
128 "Adjust the ARGS to include METHOD's `suppliedp' and keyword arguments.
130 Return the adjusted list. The `suppliedp' argument, if any, is prepended
131 to the list; the keyword arguments are added to the end.
133 (The input ARGS list is not actually modified.)"
134 (let* ((type (sod-method-type method))
135 (keys (c-function-keywords type))
136 (tag (direct-method-suppliedp-struct-tag method)))
138 (list (make-argument "suppliedp" (c-type (struct tag)))))
140 (mapcar (lambda (key)
141 (make-argument (argument-name key)
142 (argument-type key)))
145 (define-on-demand-slot basic-direct-method function-type (method)
146 (let* ((message (sod-method-message method))
147 (type (sod-method-type method))
148 (method-args (c-function-arguments type)))
149 (when (keyword-message-p message)
150 (setf method-args (fix-up-keyword-method-args method method-args)))
151 (c-type (fun (lisp (c-type-subtype type))
152 ("me" (* (class (sod-method-class method))))
155 (defmethod sod-method-description ((method basic-direct-method))
156 (with-slots (role) method
157 (if role (string-downcase role)
160 (defmethod sod-method-function-name ((method basic-direct-method))
161 (with-slots ((class %class) role message) method
162 (format nil "~A__~@[~(~A~)_~]method_~A__~A" class role
163 (sod-class-nickname (sod-message-class message))
164 (sod-message-name message))))
166 (export 'daemon-direct-method)
167 (defclass daemon-direct-method (basic-direct-method)
170 "A daemon direct method is invoked for side effects and cannot override.
172 This is the direct method class for `before' and `after' methods, which
173 cannot choose to override the remaining methods and are not involved in
174 the computation of the final result.
176 In C terms, a daemon method must return `void', and is not passed a
177 `next_method' pointer."))
179 (defmethod check-method-type ((method daemon-direct-method)
180 (message sod-message)
181 (type c-function-type))
182 (with-slots ((msgtype %type)) message
183 (check-method-return-type type c-type-void)
184 (check-method-argument-lists type msgtype)))
186 (export 'delegating-direct-method)
187 (defclass delegating-direct-method (basic-direct-method)
188 ((next-method-type :type c-function-type
189 :reader sod-method-next-method-type))
191 "A delegating direct method can choose to override other methods.
193 This is the direct method class for `around' and standard-method-
194 combination primary methods, which are given the choice of computing the
195 entire method's result or delegating to (usually) less-specific methods.
197 In C terms, a delegating method is passed a `next_method' pointer so that
198 it can delegate part of its behaviour. (A delegating direct method for a
199 varargs message is also given an additional `va_list' argument,
200 conventionally named `sod__ap_master', which it is expected to pass on to
201 its `next_method' function if necessary.)
203 The function type protocol is implemented on `delegating-direct-method'
204 using slot reader methods.."))
206 (define-on-demand-slot delegating-direct-method next-method-type (method)
207 (let* ((message (sod-method-message method))
208 (return-type (c-type-subtype (sod-message-type message)))
209 (msgargs (sod-message-argument-tail message))
210 (arguments (cond ((varargs-message-p message)
211 (cons (make-argument *sod-master-ap*
214 ((keyword-message-p message)
215 (cons (make-argument *sod-keywords*
216 (c-type (* (void :const))))
220 (c-type (fun (lisp return-type)
221 ("me" (* (class (sod-method-class method))))
224 (define-on-demand-slot delegating-direct-method function-type (method)
225 (let* ((message (sod-method-message method))
226 (type (sod-method-type method))
227 (method-args (c-function-arguments type))
228 (next-method-arg (make-argument
231 (commentify-function-type
232 (sod-method-next-method-type method))))))
233 (cond ((varargs-message-p message)
234 (push (make-argument *sod-master-ap* c-type-va-list)
236 (push next-method-arg method-args))
237 ((keyword-message-p message)
238 (push (make-argument *sod-keywords* (c-type (* (void :const))))
240 (push next-method-arg method-args)
242 (fix-up-keyword-method-args method method-args)))
244 (push next-method-arg method-args)))
245 (c-type (fun (lisp (c-type-subtype type))
246 ("me" (* (class (sod-method-class method))))
249 ;;;--------------------------------------------------------------------------
250 ;;; Effective method classes.
252 (defmethod method-keyword-argument-lists
253 ((method effective-method) direct-methods)
254 (with-slots (message) method
255 (and (keyword-message-p message)
256 (cons (cons (format nil "message ~A (at ~A)"
257 message (file-location message))
258 (c-function-keywords (sod-message-type message)))
260 (cons (format nil "method for ~A on ~A (at ~A)"
264 (c-function-keywords (sod-method-type m))))
267 (defmethod shared-initialize :after
268 ((method effective-method) slot-names &key direct-methods)
269 (declare (ignore slot-names))
271 ;; Set the keyword argument list.
272 (with-slots (message keywords) method
274 (merge-keyword-lists (method-keyword-argument-lists
275 method direct-methods)))))
277 (export '(basic-effective-method
278 effective-method-around-methods effective-method-before-methods
279 effective-method-after-methods))
280 (defclass basic-effective-method (effective-method)
281 ((around-methods :initarg :around-methods :initform nil
282 :type list :reader effective-method-around-methods)
283 (before-methods :initarg :before-methods :initform nil
284 :type list :reader effective-method-before-methods)
285 (after-methods :initarg :after-methods :initform nil
286 :type list :reader effective-method-after-methods)
287 (basic-argument-names :type list
288 :reader effective-method-basic-argument-names)
289 (functions :type list :reader effective-method-functions))
291 "Base class for built-in effective method classes.
293 This class maintains lists of the applicable `before', `after' and
294 `around' methods and provides behaviour for invoking these methods
297 The argument names protocol is implemented on `basic-effective-method'
298 using a slot reader method."))
300 (define-on-demand-slot basic-effective-method basic-argument-names (method)
301 (let* ((message (effective-method-message method))
302 (raw-tail (sod-message-argument-tail message)))
303 (mapcar #'argument-name (reify-variable-argument-tail raw-tail))))
305 (defmethod effective-method-function-name ((method effective-method))
306 (let* ((class (effective-method-class method))
307 (message (effective-method-message method))
308 (message-class (sod-message-class message)))
309 (format nil "~A__emethod_~A__~A"
311 (sod-class-nickname message-class)
312 (sod-message-name message))))
314 (define-on-demand-slot basic-effective-method functions (method)
315 (compute-method-entry-functions method))
317 (export 'simple-effective-method)
318 (defclass simple-effective-method (basic-effective-method)
319 ((primary-methods :initarg :primary-methods :initform nil
320 :type list :reader effective-method-primary-methods))
322 "Effective method counterpart to `simple-message'."))
324 (defmethod shared-initialize :after
325 ((method simple-effective-method) slot-names &key direct-methods)
326 (declare (ignore slot-names))
327 (categorize (method direct-methods :bind ((role (sod-method-role method))))
328 ((primary (null role))
329 (before (eq role :before))
330 (after (eq role :after))
331 (around (eq role :around)))
332 (with-slots (primary-methods before-methods after-methods around-methods)
334 (setf primary-methods primary
335 before-methods before
336 after-methods (reverse after)
337 around-methods around))))
339 ;;;--------------------------------------------------------------------------
342 (defmethod shared-initialize :after
343 ((codegen method-codegen) slot-names &key)
344 (declare (ignore slot-names))
345 (with-slots (message target) codegen
347 (if (eq (c-type-subtype (sod-message-type message)) c-type-void)
351 ;;;--------------------------------------------------------------------------
352 ;;; Invoking direct methods.
354 (export 'basic-effective-method-body)
355 (defun basic-effective-method-body (codegen target method body)
356 "Build the common method-invocation structure.
358 Writes to CODEGEN some basic method-invocation instructions. It invokes
359 the `around' methods, from most- to least-specific. If they all delegate,
360 then the `before' methods are run, most-specific first; next, the
361 instructions generated by BODY (invoked with a target argument); then, the
362 `after' methods are run, least-specific first; and, finally, the value
363 delivered by the BODY is returned to the `around' methods. The result
364 returned by the outermost `around' method -- or, if there are none,
365 delivered by the BODY -- is finally delivered to the TARGET."
367 (with-slots (message (class %class)
368 before-methods after-methods around-methods)
370 (let* ((message-type (sod-message-type message))
371 (return-type (c-type-subtype message-type))
372 (basic-tail (effective-method-basic-argument-names method)))
373 (flet ((method-kernel (target)
374 (dolist (before before-methods)
375 (invoke-method codegen :void basic-tail before))
376 (if (null after-methods)
377 (funcall body target)
378 (convert-stmts codegen target return-type
380 (funcall body target)
381 (dolist (after (reverse after-methods))
382 (invoke-method codegen :void
383 basic-tail after)))))))
384 (invoke-delegation-chain codegen target basic-tail
385 around-methods #'method-kernel)))))
387 ;;;--------------------------------------------------------------------------
388 ;;; Method entry points.
390 (defparameter *method-entry-inline-threshold* 200
391 "Threshold below which effective method bodies are inlined into entries.
393 After the effective method body has been computed, we calculate its
394 metric, multiply by the number of entries we need to generate, and compare
395 it with this threshold. If the metric is below the threshold then we
396 fold the method body into the entry functions; otherwise we split the
397 effective method out into its own function.")
399 (defmethod method-entry-function-name
400 ((method effective-method) (chain-head sod-class) role)
401 (let* ((class (effective-method-class method))
402 (message (effective-method-message method))
403 (message-class (sod-message-class message)))
404 (if (or (not (slot-boundp method 'functions))
405 (slot-value method 'functions))
406 (format nil "~A__mentry~@[__~(~A~)~]_~A__~A__chain_~A"
408 (sod-class-nickname message-class)
409 (sod-message-name message)
410 (sod-class-nickname chain-head))
413 (defmethod method-entry-slot-name ((entry method-entry))
414 (let* ((method (method-entry-effective-method entry))
415 (message (effective-method-message method))
416 (name (sod-message-name message))
417 (role (method-entry-role entry)))
418 (method-entry-slot-name-by-role entry role name)))
420 (defmethod method-entry-function-type ((entry method-entry))
421 (let* ((method (method-entry-effective-method entry))
422 (message (effective-method-message method))
423 (type (sod-message-type message))
424 (keywordsp (keyword-message-p message))
425 (raw-tail (append (sod-message-argument-tail message)
426 (and keywordsp (list :ellipsis))))
427 (tail (ecase (method-entry-role entry)
429 (:valist (reify-variable-argument-tail raw-tail)))))
430 (c-type (fun (lisp (c-type-subtype type))
431 ("me" (* (class (method-entry-chain-tail entry))))
434 (defgeneric effective-method-keyword-parser-function-name (method)
436 "Return the name of the keyword-parsing function for an effective METHOD.
438 See `make-keyword-parser-function' for details of what this function
441 (defmethod effective-method-keyword-parser-function-name
442 ((method basic-effective-method))
443 (with-slots ((class %class) message) method
444 (format nil "~A__kwparse_~A__~A"
446 (sod-class-nickname (sod-message-class message))
447 (sod-message-name message))))
449 (defun make-keyword-parser-function (codegen method tag set keywords)
450 "Construct and return a keyword-argument parsing function.
452 The function is contributed to the CODEGEN, with the name constructed from
453 the effective METHOD. It will populate an argument structure with the
454 given TAG. In case of error, it will mention the name SET in its report.
455 The KEYWORDS are a list of `argument' objects naming the keywords to be
458 The generated function has the signature
460 void NAME(struct TAG *kw, va_list *ap, struct kwval *v, size_t n)
462 It assumes that AP includes the first keyword name. (This makes it
463 different from the keyword-parsing functions generated by the
464 `KWSET_PARSEFN' macro, but this interface is slightly more convenient and
465 we don't need to cope with functions which accept no required
468 ;; Let's start, then.
469 (codegen-push codegen)
471 ;; Set up the local variables we'll need.
472 (macrolet ((var (name type)
473 `(ensure-var codegen ,name (c-type ,type))))
474 (var "k" const-string)
475 (var "aap" (* va-list))
476 (var "t" (* (struct "kwtab" :const)))
477 (var "vv" (* (struct "kwval" :const)))
480 (flet ((call (target func &rest args)
481 ;; Call FUNC with ARGS; return result in TARGET.
483 (apply #'deliver-call codegen target func args))
485 (convert (target type)
486 ;; Fetch the object of TYPE pointed to by `v->val', and store it
489 (deliver-expr codegen target
490 (format nil "*(~A)v->val"
491 (make-pointer-type (qualify-c-type
492 type (list :const))))))
494 (namecheck (var name conseq alt)
495 ;; Return an instruction: if VAR matches the string NAME then do
496 ;; CONSEQ; otherwise do ALT.
498 (make-if-inst (make-call-inst "!strcmp"
499 var (prin1-to-string name))
502 ;; Prepare the main parsing loops. We're going to construct them both at
503 ;; the same time. They're not quite similar enough for it to be
504 ;; worthwhile abstracting this further, but carving up the keywords is
505 ;; too tedious to write out more than once.
506 (let ((va-act (make-expr-inst (make-call-inst "kw_unknown" set "k")))
507 (tab-act (make-expr-inst (make-call-inst "kw_unknown"
509 (name (effective-method-keyword-parser-function-name method)))
511 ;; Work through the keywords. We're going to be building up the
512 ;; conditional dispatch from the end, so reverse the (nicely sorted)
513 ;; list before processing it.
514 (dolist (key (reverse keywords))
515 (let* ((key-name (argument-name key))
516 (key-type (argument-type key)))
518 ;; Handle the varargs case.
519 (codegen-push codegen)
520 (deliver-expr codegen (format nil "kw->~A__suppliedp" key-name) 1)
521 (call (format nil "kw->~A" key-name) "va_arg" "*ap" key-type)
522 (setf va-act (namecheck "k" key-name
523 (codegen-pop-block codegen) va-act))
525 ;; Handle the table case.
526 (codegen-push codegen)
527 (deliver-expr codegen (format nil "kw->~A__suppliedp" key-name) 1)
528 (convert (format nil "kw->~A" key-name) key-type)
529 (setf tab-act (namecheck "v->kw" key-name
530 (codegen-pop-block codegen) tab-act))))
532 ;; Deal with the special `kw.' keywords read via varargs.
533 (codegen-push codegen)
534 (call "vv" "va_arg" "*ap" (c-type (* (struct "kwval" :const))))
535 (call "nn" "va_arg" "*ap" c-type-size-t)
536 (call :void name "kw" *null-pointer* "vv" "nn")
537 (setf va-act (namecheck "k" "kw.tab"
538 (codegen-pop-block codegen) va-act))
540 (codegen-push codegen)
541 (call "aap" "va_arg" "*ap" (c-type (* va-list)))
542 (call :void name "kw" "aap" *null-pointer* 0)
543 (setf va-act (namecheck "k" "kw.va_list"
544 (codegen-pop-block codegen) va-act))
546 ;; Finish up the varargs loop.
547 (emit-banner codegen "Parse keywords from the variable-length tail.")
548 (codegen-push codegen)
549 (call "k" "va_arg" "*ap" c-type-const-string)
550 (emit-inst codegen (make-if-inst "!k" (make-break-inst)))
551 (emit-inst codegen va-act)
552 (let ((loop (make-for-inst nil nil nil (codegen-pop-block codegen))))
554 (make-if-inst "ap" (make-block-inst nil (list loop)))))
556 ;; Deal with the special `kw.' keywords read from a table.
557 (codegen-push codegen)
558 (deliver-expr codegen "t"
559 (format nil "(~A)v->val"
560 (c-type (* (struct "kwtab" :const)))))
561 (call :void name "kw" *null-pointer* "t->v" "t->n")
562 (setf tab-act (namecheck "v->kw" "kw.tab"
563 (codegen-pop-block codegen) tab-act))
565 (emit-banner codegen "Parse keywords from the argument table.")
566 (codegen-push codegen)
567 (convert "aap" (c-type (* va-list)))
568 (call :void name "kw" "aap" *null-pointer* 0)
569 (setf tab-act (namecheck "v->kw" "kw.va_list"
570 (codegen-pop-block codegen) tab-act))
572 ;; Finish off the table loop.
573 (codegen-push codegen)
574 (emit-inst codegen tab-act)
575 (emit-inst codegen (make-expr-inst "v++"))
576 (emit-inst codegen (make-expr-inst "n--"))
577 (emit-inst codegen (make-while-inst "n" (codegen-pop-block codegen)))
579 ;; Wrap the whole lot up with a nice bow.
580 (let ((message (effective-method-message method)))
581 (codegen-pop-function codegen name
583 ("kw" (* (struct tag)))
585 ("v" (* (struct "kwval" :const)))
587 "Keyword parsing for `~A.~A' on class `~A'."
589 (sod-message-class message))
590 (sod-message-name message)
591 (effective-method-class method))))))
593 (defmethod make-method-entries ((method basic-effective-method)
594 (chain-head sod-class)
595 (chain-tail sod-class))
597 (message (effective-method-message method)))
599 (push (make-instance 'method-entry
600 :method method :role role
601 :chain-head chain-head
602 :chain-tail chain-tail)
604 (when (or (varargs-message-p message)
605 (keyword-message-p message))
610 (defmethod compute-method-entry-functions ((method basic-effective-method))
612 ;; OK, there's quite a lot of this, so hold tight.
614 ;; The first thing we need to do is find all of the related objects. This
615 ;; is a bit verbose but fairly straightforward.
617 ;; Next, we generate the effective method body -- using `compute-effective-
618 ;; method-body' of all things. This gives us the declarations and body for
619 ;; an effective method function, but we don't have an actual function yet.
621 ;; Now we look at the chains which are actually going to need a method
622 ;; entry: only those chains whose tail (most specific) class is a
623 ;; superclass of the class which defined the message need an entry. We
624 ;; build a list of these tail classes.
626 ;; Having done this, we decide whether it's better to generate a standalone
627 ;; effective-method function and call it from each of the method entries,
628 ;; or to inline the effective method body into each of the entries.
630 ;; Most of the complexity here comes from (a) dealing with the two
631 ;; different strategies for constructing method entry functions and (b)
632 ;; (unsurprisingly) the mess involved with dealing with varargs messages.
634 (let* ((message (effective-method-message method))
635 (class (effective-method-class method))
636 (message-class (sod-message-class message))
637 (return-type (c-type-subtype (sod-message-type message)))
638 (codegen (make-instance 'method-codegen
643 ;; Method entry details.
644 (chain-tails (remove-if-not (lambda (super)
645 (sod-subclass-p super message-class))
647 (sod-class-chains class))))
648 (n-entries (length chain-tails))
649 (raw-entry-args (append (sod-message-argument-tail message)
650 (and (keyword-message-p message)
652 (entry-args (reify-variable-argument-tail raw-entry-args))
653 (parm-n (let ((tail (last (cons (make-argument "me" c-type-void)
655 (and tail (eq (cadr tail) :ellipsis)
656 (argument-name (car tail)))))
657 (entry-target (codegen-target codegen))
659 ;; Effective method function details.
660 (emf-name (effective-method-function-name method))
661 (ilayout-type (c-type (* (struct (ilayout-struct-tag class)))))
662 (emf-type (c-type (fun (lisp return-type)
663 ("sod__obj" (lisp ilayout-type))
666 (flet ((setup-entry (tail)
667 (let ((head (sod-class-chain-head tail)))
668 (codegen-push codegen)
669 (ensure-var codegen "sod__obj" ilayout-type
670 (make-convert-to-ilayout-inst class
673 (let* ((head (sod-class-chain-head tail))
674 (role (if parm-n :valist nil))
675 (name (method-entry-function-name method head role))
676 (type (c-type (fun (lisp return-type)
677 ("me" (* (class tail)))
679 (codegen-pop-function codegen name type
680 "~@(~@[~A ~]entry~) function ~:_~
681 for method `~A.~A' ~:_~
682 via chain headed by `~A' ~:_~
684 (if parm-n "Indirect argument-tail" nil)
685 (sod-class-nickname message-class)
686 (sod-message-name message)
689 ;; If this is a varargs or keyword method then we've made the
690 ;; `:valist' role. Also make the `nil' role.
692 (let ((call (apply #'make-call-inst name "me"
693 (mapcar #'argument-name entry-args)))
694 (main (method-entry-function-name method head nil))
695 (main-type (c-type (fun (lisp return-type)
696 ("me" (* (class tail)))
698 (codegen-push codegen)
699 (ensure-var codegen *sod-ap* c-type-va-list)
700 (convert-stmts codegen entry-target return-type
702 (deliver-call codegen :void "va_start"
704 (deliver-expr codegen target call)
705 (deliver-call codegen :void "va_end"
707 (codegen-pop-function codegen main main-type
708 "Variable-length argument list ~:_~
710 for method `~A.~A' ~:_~
711 via chain headed by `~A' ~:_~
713 (sod-class-nickname message-class)
714 (sod-message-name message)
717 ;; Generate the method body. We'll work out what to do with it later.
718 (codegen-push codegen)
719 (let* ((result (if (eq return-type c-type-void) nil
720 (temporary-var codegen return-type)))
721 (emf-target (or result :void)))
722 (compute-effective-method-body method codegen emf-target)
723 (multiple-value-bind (vars insts) (codegen-pop codegen)
724 (cond ((or (= n-entries 1)
725 (<= (* n-entries (reduce #'+ insts :key #'inst-metric))
726 *method-entry-inline-threshold*))
728 ;; The effective method body is simple -- or there's only
729 ;; one of them. We'll inline the method body into the entry
731 (dolist (tail chain-tails)
734 (if (typep var 'var-inst)
735 (ensure-var codegen (inst-name var)
736 (inst-type var) (inst-init var))
737 (emit-decl codegen var)))
738 (emit-insts codegen insts)
739 (deliver-expr codegen entry-target result)
740 (finish-entry tail)))
744 ;; The effective method body is complicated and we'd need
745 ;; more than one copy. We'll generate an effective method
746 ;; function and call it a lot.
747 (codegen-build-function codegen emf-name emf-type vars
748 (nconc insts (and result
749 (list (make-return-inst result))))
750 "Effective method function ~:_for `~A.~A' ~:_~
752 (sod-class-nickname message-class)
753 (sod-message-name message)
754 (effective-method-class method))
756 (let ((call (apply #'make-call-inst emf-name "sod__obj"
757 (mapcar #'argument-name entry-args))))
758 (dolist (tail chain-tails)
760 (deliver-expr codegen entry-target call)
761 (finish-entry tail)))))))
763 (codegen-functions codegen))))
765 (defmethod compute-effective-method-body :around
766 ((method basic-effective-method) codegen target)
767 (let* ((message (effective-method-message method))
768 (keywordsp (keyword-message-p message))
769 (keywords (effective-method-keywords method))
770 (ap-addr (format nil "&~A" *sod-tmp-ap*))
771 (set (format nil "\"~A:~A.~A\""
772 (sod-class-name (effective-method-class method))
773 (sod-class-nickname (sod-message-class message))
774 (sod-message-name message))))
775 (labels ((call (target func &rest args)
776 (apply #'deliver-call codegen target func args))
777 (parse-keywords (body)
778 (ensure-var codegen *sod-tmp-ap* c-type-va-list)
779 (call :void "va_copy" *sod-tmp-ap* *sod-ap*)
781 (call :void "va_end" *sod-tmp-ap*)))
782 (cond ((not keywordsp)
785 (let ((*keyword-struct-disposition* :null))
786 (parse-keywords (lambda ()
788 (codegen kw c-type-const-string)
790 *sod-tmp-ap* c-type-const-string)
791 (call :void "kw_parseempty" set
792 kw ap-addr *null-pointer* 0))))
796 (effective-method-keyword-parser-function-name method))
797 (tag (effective-method-keyword-struct-tag method))
798 (kw-addr (format nil "&~A" *sod-keywords*))
799 (*keyword-struct-disposition* :local))
800 (ensure-var codegen *sod-keywords* (c-type (struct tag)))
801 (make-keyword-parser-function codegen method tag set keywords)
802 (parse-keywords (lambda ()
803 (call :void name kw-addr ap-addr
805 (call-next-method)))))))
807 (defmethod effective-method-live-p ((method simple-effective-method))
808 (effective-method-primary-methods method))
810 (defmethod compute-method-entry-functions :around ((method effective-method))
811 (if (effective-method-live-p method)
815 (defmethod compute-effective-method-body
816 ((method simple-effective-method) codegen target)
817 (basic-effective-method-body codegen target method
819 (simple-method-body method
823 ;;;--------------------------------------------------------------------------
824 ;;; Standard method combination.
826 (export 'standard-message)
827 (defclass standard-message (simple-message)
830 "Message class for standard method combinations.
832 Standard method combination is a simple method combination where the
833 primary methods are invoked as a delegation chain, from most- to
836 (export 'standard-effective-method)
837 (defclass standard-effective-method (simple-effective-method) ()
838 (:documentation "Effective method counterpart to `standard-message'."))
840 (defmethod primary-method-class ((message standard-message))
841 'delegating-direct-method)
843 (defmethod sod-message-effective-method-class ((message standard-message))
844 'standard-effective-method)
846 (defmethod simple-method-body
847 ((method standard-effective-method) codegen target)
848 (invoke-delegation-chain codegen
850 (effective-method-basic-argument-names method)
851 (effective-method-primary-methods method)
854 ;;;----- That's all, folks --------------------------------------------------