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-function-name ((method basic-direct-method))
156 (with-slots ((class %class) role message) method
157 (format nil "~A__~@[~(~A~)_~]method_~A__~A" class role
158 (sod-class-nickname (sod-message-class message))
159 (sod-message-name message))))
161 (export 'daemon-direct-method)
162 (defclass daemon-direct-method (basic-direct-method)
165 "A daemon direct method is invoked for side effects and cannot override.
167 This is the direct method class for `before' and `after' methods, which
168 cannot choose to override the remaining methods and are not involved in
169 the computation of the final result.
171 In C terms, a daemon method must return `void', and is not passed a
172 `next_method' pointer."))
174 (defmethod check-method-type ((method daemon-direct-method)
175 (message sod-message)
176 (type c-function-type))
177 (with-slots ((msgtype %type)) message
178 (check-method-return-type type c-type-void)
179 (check-method-argument-lists type msgtype)))
181 (export 'delegating-direct-method)
182 (defclass delegating-direct-method (basic-direct-method)
183 ((next-method-type :type c-function-type
184 :reader sod-method-next-method-type))
186 "A delegating direct method can choose to override other methods.
188 This is the direct method class for `around' and standard-method-
189 combination primary methods, which are given the choice of computing the
190 entire method's result or delegating to (usually) less-specific methods.
192 In C terms, a delegating method is passed a `next_method' pointer so that
193 it can delegate part of its behaviour. (A delegating direct method for a
194 varargs message is also given an additional `va_list' argument,
195 conventionally named `sod__ap_master', which it is expected to pass on to
196 its `next_method' function if necessary.)
198 The function type protocol is implemented on `delegating-direct-method'
199 using slot reader methods.."))
201 (define-on-demand-slot delegating-direct-method next-method-type (method)
202 (let* ((message (sod-method-message method))
203 (return-type (c-type-subtype (sod-message-type message)))
204 (msgargs (sod-message-argument-tail message))
205 (arguments (cond ((varargs-message-p message)
206 (cons (make-argument *sod-master-ap*
209 ((keyword-message-p message)
210 (cons (make-argument *sod-keywords*
211 (c-type (* (void :const))))
215 (c-type (fun (lisp return-type)
216 ("me" (* (class (sod-method-class method))))
219 (define-on-demand-slot delegating-direct-method function-type (method)
220 (let* ((message (sod-method-message method))
221 (type (sod-method-type method))
222 (method-args (c-function-arguments type))
223 (next-method-arg (make-argument
226 (commentify-function-type
227 (sod-method-next-method-type method))))))
228 (cond ((varargs-message-p message)
229 (push (make-argument *sod-master-ap* c-type-va-list)
231 (push next-method-arg method-args))
232 ((keyword-message-p message)
233 (push (make-argument *sod-keywords* (c-type (* (void :const))))
235 (push next-method-arg method-args)
237 (fix-up-keyword-method-args method method-args)))
239 (push next-method-arg method-args)))
240 (c-type (fun (lisp (c-type-subtype type))
241 ("me" (* (class (sod-method-class method))))
244 ;;;--------------------------------------------------------------------------
245 ;;; Effective method classes.
247 (defmethod method-keyword-argument-lists
248 ((method effective-method) direct-methods)
249 (with-slots (message) method
250 (and (keyword-message-p message)
252 (let ((type (sod-method-type m)))
253 (cons (c-function-keywords type)
254 (format nil "method for ~A on ~A (at ~A)"
257 (file-location m)))))
260 (defmethod shared-initialize :after
261 ((method effective-method) slot-names &key direct-methods)
262 (declare (ignore slot-names))
264 ;; Set the keyword argument list.
265 (with-slots (message keywords) method
267 (merge-keyword-lists (method-keyword-argument-lists
268 method direct-methods)))))
270 (export '(basic-effective-method
271 effective-method-around-methods effective-method-before-methods
272 effective-method-after-methods))
273 (defclass basic-effective-method (effective-method)
274 ((around-methods :initarg :around-methods :initform nil
275 :type list :reader effective-method-around-methods)
276 (before-methods :initarg :before-methods :initform nil
277 :type list :reader effective-method-before-methods)
278 (after-methods :initarg :after-methods :initform nil
279 :type list :reader effective-method-after-methods)
280 (basic-argument-names :type list
281 :reader effective-method-basic-argument-names)
282 (functions :type list :reader effective-method-functions))
284 "Base class for built-in effective method classes.
286 This class maintains lists of the applicable `before', `after' and
287 `around' methods and provides behaviour for invoking these methods
290 The argument names protocol is implemented on `basic-effective-method'
291 using a slot reader method."))
293 (define-on-demand-slot basic-effective-method basic-argument-names (method)
294 (let* ((message (effective-method-message method))
295 (raw-tail (sod-message-argument-tail message)))
296 (mapcar #'argument-name (reify-variable-argument-tail raw-tail))))
298 (defmethod effective-method-function-name ((method effective-method))
299 (let* ((class (effective-method-class method))
300 (message (effective-method-message method))
301 (message-class (sod-message-class message)))
302 (format nil "~A__emethod_~A__~A"
304 (sod-class-nickname message-class)
305 (sod-message-name message))))
307 (define-on-demand-slot basic-effective-method functions (method)
308 (compute-method-entry-functions method))
310 (export 'simple-effective-method)
311 (defclass simple-effective-method (basic-effective-method)
312 ((primary-methods :initarg :primary-methods :initform nil
313 :type list :reader effective-method-primary-methods))
315 "Effective method counterpart to `simple-message'."))
317 (defmethod shared-initialize :after
318 ((method simple-effective-method) slot-names &key direct-methods)
319 (declare (ignore slot-names))
320 (categorize (method direct-methods :bind ((role (sod-method-role method))))
321 ((primary (null role))
322 (before (eq role :before))
323 (after (eq role :after))
324 (around (eq role :around)))
325 (with-slots (primary-methods before-methods after-methods around-methods)
327 (setf primary-methods primary
328 before-methods before
329 after-methods (reverse after)
330 around-methods around))))
332 ;;;--------------------------------------------------------------------------
335 (defmethod shared-initialize :after
336 ((codegen method-codegen) slot-names &key)
337 (declare (ignore slot-names))
338 (with-slots (message target) codegen
340 (if (eq (c-type-subtype (sod-message-type message)) c-type-void)
344 ;;;--------------------------------------------------------------------------
345 ;;; Invoking direct methods.
347 (export 'basic-effective-method-body)
348 (defun basic-effective-method-body (codegen target method body)
349 "Build the common method-invocation structure.
351 Writes to CODEGEN some basic method-invocation instructions. It invokes
352 the `around' methods, from most- to least-specific. If they all delegate,
353 then the `before' methods are run, most-specific first; next, the
354 instructions generated by BODY (invoked with a target argument); then, the
355 `after' methods are run, least-specific first; and, finally, the value
356 delivered by the BODY is returned to the `around' methods. The result
357 returned by the outermost `around' method -- or, if there are none,
358 delivered by the BODY -- is finally delivered to the TARGET."
360 (with-slots (message (class %class)
361 before-methods after-methods around-methods)
363 (let* ((message-type (sod-message-type message))
364 (return-type (c-type-subtype message-type))
365 (basic-tail (effective-method-basic-argument-names method)))
366 (flet ((method-kernel (target)
367 (dolist (before before-methods)
368 (invoke-method codegen :void basic-tail before))
369 (if (null after-methods)
370 (funcall body target)
371 (convert-stmts codegen target return-type
373 (funcall body target)
374 (dolist (after (reverse after-methods))
375 (invoke-method codegen :void
376 basic-tail after)))))))
377 (invoke-delegation-chain codegen target basic-tail
378 around-methods #'method-kernel)))))
380 ;;;--------------------------------------------------------------------------
381 ;;; Method entry points.
383 (defparameter *method-entry-inline-threshold* 200
384 "Threshold below which effective method bodies are inlined into entries.
386 After the effective method body has been computed, we calculate its
387 metric, multiply by the number of entries we need to generate, and compare
388 it with this threshold. If the metric is below the threshold then we
389 fold the method body into the entry functions; otherwise we split the
390 effective method out into its own function.")
392 (defmethod method-entry-function-name
393 ((method effective-method) (chain-head sod-class) role)
394 (let* ((class (effective-method-class method))
395 (message (effective-method-message method))
396 (message-class (sod-message-class message)))
397 (if (or (not (slot-boundp method 'functions))
398 (slot-value method 'functions))
399 (format nil "~A__mentry~@[__~(~A~)~]_~A__~A__chain_~A"
401 (sod-class-nickname message-class)
402 (sod-message-name message)
403 (sod-class-nickname chain-head))
406 (defmethod method-entry-slot-name ((entry method-entry))
407 (let* ((method (method-entry-effective-method entry))
408 (message (effective-method-message method))
409 (name (sod-message-name message))
410 (role (method-entry-role entry)))
411 (method-entry-slot-name-by-role entry role name)))
413 (defmethod method-entry-function-type ((entry method-entry))
414 (let* ((method (method-entry-effective-method entry))
415 (message (effective-method-message method))
416 (type (sod-message-type message))
417 (keywordsp (keyword-message-p message))
418 (raw-tail (append (sod-message-argument-tail message)
419 (and keywordsp (list :ellipsis))))
420 (tail (ecase (method-entry-role entry)
422 (:valist (reify-variable-argument-tail raw-tail)))))
423 (c-type (fun (lisp (c-type-subtype type))
424 ("me" (* (class (method-entry-chain-tail entry))))
427 (defgeneric effective-method-keyword-parser-function-name (method)
429 "Return the name of the keyword-parsing function for an effective METHOD.
431 See `make-keyword-parser-function' for details of what this function
434 (defmethod effective-method-keyword-parser-function-name
435 ((method basic-effective-method))
436 (with-slots ((class %class) message) method
437 (format nil "~A__kwparse_~A__~A"
439 (sod-class-nickname (sod-message-class message))
440 (sod-message-name message))))
442 (defun make-keyword-parser-function (codegen method tag set keywords)
443 "Construct and return a keyword-argument parsing function.
445 The function is contributed to the CODEGEN, with the name constructed from
446 the effective METHOD. It will populate an argument structure with the
447 given TAG. In case of error, it will mention the name SET in its report.
448 The KEYWORDS are a list of `argument' objects naming the keywords to be
451 The generated function has the signature
453 void NAME(struct TAG *kw, va_list *ap, struct kwval *v, size_t n)
455 It assumes that AP includes the first keyword name. (This makes it
456 different from the keyword-parsing functions generated by the
457 `KWSET_PARSEFN' macro, but this interface is slightly more convenient and
458 we don't need to cope with functions which accept no required
461 ;; Let's start, then.
462 (codegen-push codegen)
464 ;; Set up the local variables we'll need.
465 (macrolet ((var (name type)
466 `(ensure-var codegen ,name (c-type ,type))))
467 (var "k" const-string)
468 (var "aap" (* va-list))
469 (var "t" (* (struct "kwtab" :const)))
470 (var "vv" (* (struct "kwval" :const)))
473 (flet ((call (target func &rest args)
474 ;; Call FUNC with ARGS; return result in TARGET.
476 (apply #'deliver-call codegen target func args))
478 (convert (target type)
479 ;; Fetch the object of TYPE pointed to by `v->val', and store it
482 (deliver-expr codegen target
483 (format nil "*(~A)v->val"
484 (make-pointer-type (qualify-c-type
485 type (list :const))))))
487 (namecheck (var name conseq alt)
488 ;; Return an instruction: if VAR matches the string NAME then do
489 ;; CONSEQ; otherwise do ALT.
491 (make-if-inst (make-call-inst "!strcmp"
492 var (prin1-to-string name))
495 ;; Prepare the main parsing loops. We're going to construct them both at
496 ;; the same time. They're not quite similar enough for it to be
497 ;; worthwhile abstracting this further, but carving up the keywords is
498 ;; too tedious to write out more than once.
499 (let ((va-act (make-expr-inst (make-call-inst "kw_unknown" set "k")))
500 (tab-act (make-expr-inst (make-call-inst "kw_unknown"
502 (name (effective-method-keyword-parser-function-name method)))
504 ;; Work through the keywords. We're going to be building up the
505 ;; conditional dispatch from the end, so reverse the (nicely sorted)
506 ;; list before processing it.
507 (dolist (key (reverse keywords))
508 (let* ((key-name (argument-name key))
509 (key-type (argument-type key)))
511 ;; Handle the varargs case.
512 (codegen-push codegen)
513 (deliver-expr codegen (format nil "kw->~A__suppliedp" key-name) 1)
514 (call (format nil "kw->~A" key-name) "va_arg" "*ap" key-type)
515 (setf va-act (namecheck "k" key-name
516 (codegen-pop-block codegen) va-act))
518 ;; Handle the table case.
519 (codegen-push codegen)
520 (deliver-expr codegen (format nil "kw->~A__suppliedp" key-name) 1)
521 (convert (format nil "kw->~A" key-name) key-type)
522 (setf tab-act (namecheck "v->kw" key-name
523 (codegen-pop-block codegen) tab-act))))
525 ;; Deal with the special `kw.' keywords read via varargs.
526 (codegen-push codegen)
527 (call "vv" "va_arg" "*ap" (c-type (* (struct "kwval" :const))))
528 (call "nn" "va_arg" "*ap" c-type-size-t)
529 (call :void name "kw" *null-pointer* "vv" "nn")
530 (setf va-act (namecheck "k" "kw.tab"
531 (codegen-pop-block codegen) va-act))
533 (codegen-push codegen)
534 (call "aap" "va_arg" "*ap" (c-type (* va-list)))
535 (call :void name "kw" "aap" *null-pointer* 0)
536 (setf va-act (namecheck "k" "kw.va_list"
537 (codegen-pop-block codegen) va-act))
539 ;; Finish up the varargs loop.
540 (emit-banner codegen "Parse keywords from the variable-length tail.")
541 (codegen-push codegen)
542 (call "k" "va_arg" "*ap" c-type-const-string)
543 (emit-inst codegen (make-if-inst "!k" (make-break-inst)))
544 (emit-inst codegen va-act)
545 (let ((loop (make-for-inst nil nil nil (codegen-pop-block codegen))))
547 (make-if-inst "ap" (make-block-inst nil (list loop)))))
549 ;; Deal with the special `kw.' keywords read from a table.
550 (codegen-push codegen)
551 (deliver-expr codegen "t"
552 (format nil "(~A)v->val"
553 (c-type (* (struct "kwtab" :const)))))
554 (call :void name "kw" *null-pointer* "t->v" "t->n")
555 (setf tab-act (namecheck "v->kw" "kw.tab"
556 (codegen-pop-block codegen) tab-act))
558 (emit-banner codegen "Parse keywords from the argument table.")
559 (codegen-push codegen)
560 (convert "aap" (c-type (* va-list)))
561 (call :void name "kw" "aap" *null-pointer* 0)
562 (setf tab-act (namecheck "v->kw" "kw.va_list"
563 (codegen-pop-block codegen) tab-act))
565 ;; Finish off the table loop.
566 (codegen-push codegen)
567 (emit-inst codegen tab-act)
568 (emit-inst codegen (make-expr-inst "v++"))
569 (emit-inst codegen (make-expr-inst "n--"))
570 (emit-inst codegen (make-while-inst "n" (codegen-pop-block codegen)))
572 ;; Wrap the whole lot up with a nice bow.
573 (let ((message (effective-method-message method)))
574 (codegen-pop-function codegen name
576 ("kw" (* (struct tag)))
578 ("v" (* (struct "kwval" :const)))
580 "Keyword parsing for `~A.~A' on class `~A'."
582 (sod-message-class message))
583 (sod-message-name message)
584 (effective-method-class method))))))
586 (defmethod make-method-entries ((method basic-effective-method)
587 (chain-head sod-class)
588 (chain-tail sod-class))
590 (message (effective-method-message method)))
592 (push (make-instance 'method-entry
593 :method method :role role
594 :chain-head chain-head
595 :chain-tail chain-tail)
597 (when (or (varargs-message-p message)
598 (keyword-message-p message))
603 (defmethod compute-method-entry-functions ((method basic-effective-method))
605 ;; OK, there's quite a lot of this, so hold tight.
607 ;; The first thing we need to do is find all of the related objects. This
608 ;; is a bit verbose but fairly straightforward.
610 ;; Next, we generate the effective method body -- using `compute-effective-
611 ;; method-body' of all things. This gives us the declarations and body for
612 ;; an effective method function, but we don't have an actual function yet.
614 ;; Now we look at the chains which are actually going to need a method
615 ;; entry: only those chains whose tail (most specific) class is a
616 ;; superclass of the class which defined the message need an entry. We
617 ;; build a list of these tail classes.
619 ;; Having done this, we decide whether it's better to generate a standalone
620 ;; effective-method function and call it from each of the method entries,
621 ;; or to inline the effective method body into each of the entries.
623 ;; Most of the complexity here comes from (a) dealing with the two
624 ;; different strategies for constructing method entry functions and (b)
625 ;; (unsurprisingly) the mess involved with dealing with varargs messages.
627 (let* ((message (effective-method-message method))
628 (class (effective-method-class method))
629 (message-class (sod-message-class message))
630 (return-type (c-type-subtype (sod-message-type message)))
631 (codegen (make-instance 'method-codegen
636 ;; Method entry details.
637 (chain-tails (remove-if-not (lambda (super)
638 (sod-subclass-p super message-class))
640 (sod-class-chains class))))
641 (n-entries (length chain-tails))
642 (raw-entry-args (append (sod-message-argument-tail message)
643 (and (keyword-message-p message)
645 (entry-args (reify-variable-argument-tail raw-entry-args))
646 (parm-n (let ((tail (last (cons (make-argument "me" c-type-void)
648 (and tail (eq (cadr tail) :ellipsis)
649 (argument-name (car tail)))))
650 (entry-target (codegen-target codegen))
652 ;; Effective method function details.
653 (emf-name (effective-method-function-name method))
654 (ilayout-type (c-type (* (struct (ilayout-struct-tag class)))))
655 (emf-type (c-type (fun (lisp return-type)
656 ("sod__obj" (lisp ilayout-type))
659 (flet ((setup-entry (tail)
660 (let ((head (sod-class-chain-head tail)))
661 (codegen-push codegen)
662 (ensure-var codegen "sod__obj" ilayout-type
663 (make-convert-to-ilayout-inst class
666 (let* ((head (sod-class-chain-head tail))
667 (role (if parm-n :valist nil))
668 (name (method-entry-function-name method head role))
669 (type (c-type (fun (lisp return-type)
670 ("me" (* (class tail)))
672 (codegen-pop-function codegen name type
673 "~@(~@[~A ~]entry~) function ~:_~
674 for method `~A.~A' ~:_~
675 via chain headed by `~A' ~:_~
677 (if parm-n "Indirect argument-tail" nil)
678 (sod-class-nickname message-class)
679 (sod-message-name message)
682 ;; If this is a varargs or keyword method then we've made the
683 ;; `:valist' role. Also make the `nil' role.
685 (let ((call (apply #'make-call-inst name "me"
686 (mapcar #'argument-name entry-args)))
687 (main (method-entry-function-name method head nil))
688 (main-type (c-type (fun (lisp return-type)
689 ("me" (* (class tail)))
691 (codegen-push codegen)
692 (ensure-var codegen *sod-ap* c-type-va-list)
693 (convert-stmts codegen entry-target return-type
695 (deliver-call codegen :void "va_start"
697 (deliver-expr codegen target call)
698 (deliver-call codegen :void "va_end"
700 (codegen-pop-function codegen main main-type
701 "Variable-length argument list ~:_~
703 for method `~A.~A' ~:_~
704 via chain headed by `~A' ~:_~
706 (sod-class-nickname message-class)
707 (sod-message-name message)
710 ;; Generate the method body. We'll work out what to do with it later.
711 (codegen-push codegen)
712 (let* ((result (if (eq return-type c-type-void) nil
713 (temporary-var codegen return-type)))
714 (emf-target (or result :void)))
715 (compute-effective-method-body method codegen emf-target)
716 (multiple-value-bind (vars insts) (codegen-pop codegen)
717 (cond ((or (= n-entries 1)
718 (<= (* n-entries (reduce #'+ insts :key #'inst-metric))
719 *method-entry-inline-threshold*))
721 ;; The effective method body is simple -- or there's only
722 ;; one of them. We'll inline the method body into the entry
724 (dolist (tail chain-tails)
727 (if (typep var 'var-inst)
728 (ensure-var codegen (inst-name var)
729 (inst-type var) (inst-init var))
730 (emit-decl codegen var)))
731 (emit-insts codegen insts)
732 (deliver-expr codegen entry-target result)
733 (finish-entry tail)))
737 ;; The effective method body is complicated and we'd need
738 ;; more than one copy. We'll generate an effective method
739 ;; function and call it a lot.
740 (codegen-build-function codegen emf-name emf-type vars
741 (nconc insts (and result
742 (list (make-return-inst result))))
743 "Effective method function ~:_for `~A.~A' ~:_~
745 (sod-class-nickname message-class)
746 (sod-message-name message)
747 (effective-method-class method))
749 (let ((call (apply #'make-call-inst emf-name "sod__obj"
750 (mapcar #'argument-name entry-args))))
751 (dolist (tail chain-tails)
753 (deliver-expr codegen entry-target call)
754 (finish-entry tail)))))))
756 (codegen-functions codegen))))
758 (defmethod compute-effective-method-body :around
759 ((method basic-effective-method) codegen target)
760 (let* ((message (effective-method-message method))
761 (keywordsp (keyword-message-p message))
762 (keywords (effective-method-keywords method))
763 (ap-addr (format nil "&~A" *sod-tmp-ap*))
764 (set (format nil "\"~A:~A.~A\""
765 (sod-class-name (effective-method-class method))
766 (sod-class-nickname (sod-message-class message))
767 (sod-message-name message))))
768 (labels ((call (target func &rest args)
769 (apply #'deliver-call codegen target func args))
770 (parse-keywords (body)
771 (ensure-var codegen *sod-tmp-ap* c-type-va-list)
772 (call :void "va_copy" *sod-tmp-ap* *sod-ap*)
774 (call :void "va_end" *sod-tmp-ap*)))
775 (cond ((not keywordsp)
778 (let ((*keyword-struct-disposition* :null))
779 (parse-keywords (lambda ()
781 (codegen kw c-type-const-string)
783 *sod-tmp-ap* c-type-const-string)
784 (call :void "kw_parseempty" set
785 kw ap-addr *null-pointer* 0))))
789 (effective-method-keyword-parser-function-name method))
790 (tag (effective-method-keyword-struct-tag method))
791 (kw-addr (format nil "&~A" *sod-keywords*))
792 (*keyword-struct-disposition* :local))
793 (ensure-var codegen *sod-keywords* (c-type (struct tag)))
794 (make-keyword-parser-function codegen method tag set keywords)
795 (parse-keywords (lambda ()
796 (call :void name kw-addr ap-addr
798 (call-next-method)))))))
800 (defmethod effective-method-live-p ((method simple-effective-method))
801 (effective-method-primary-methods method))
803 (defmethod compute-method-entry-functions :around ((method effective-method))
804 (if (effective-method-live-p method)
808 (defmethod compute-effective-method-body
809 ((method simple-effective-method) codegen target)
810 (basic-effective-method-body codegen target method
812 (simple-method-body method
816 ;;;--------------------------------------------------------------------------
817 ;;; Standard method combination.
819 (export 'standard-message)
820 (defclass standard-message (simple-message)
823 "Message class for standard method combinations.
825 Standard method combination is a simple method combination where the
826 primary methods are invoked as a delegation chain, from most- to
829 (export 'standard-effective-method)
830 (defclass standard-effective-method (simple-effective-method) ()
831 (:documentation "Effective method counterpart to `standard-message'."))
833 (defmethod primary-method-class ((message standard-message))
834 'delegating-direct-method)
836 (defmethod sod-message-effective-method-class ((message standard-message))
837 'standard-effective-method)
839 (defmethod simple-method-body
840 ((method standard-effective-method) codegen target)
841 (invoke-delegation-chain codegen
843 (effective-method-basic-argument-names method)
844 (effective-method-primary-methods method)
847 ;;;----- That's all, folks --------------------------------------------------