chiark / gitweb /
2300ac68441a9318cac83ac2f4ef9356d3c25fdf
[sod] / src / method-impl.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Method combination implementation
4 ;;;
5 ;;; (c) 2009 Straylight/Edgeware
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This file is part of the Sensible Object Design, an object system for C.
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 ;;;--------------------------------------------------------------------------
29 ;;; Message classes.
30
31 (export 'basic-message)
32 (defclass basic-message (sod-message)
33   ((argument-tail :type list :reader sod-message-argument-tail))
34   (:documentation
35    "Base class for built-in message classes.
36
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.
40
41    The function type protocol is implemented on `basic-message' using slot
42    reader methods.  The actual values are computed on demand."))
43
44 (define-on-demand-slot basic-message argument-tail (message)
45   (let ((seq 0))
46     (mapcar (lambda (arg)
47               (if (or (eq arg :ellipsis) (argument-name arg)) arg
48                   (make-argument (make-instance 'temporary-argument
49                                                 :tag (prog1 seq
50                                                        (incf seq)))
51                                  (argument-type arg))))
52             (c-function-arguments (sod-message-type message)))))
53
54 (defmethod sod-message-method-class
55     ((message basic-message) (class sod-class) pset)
56   (let ((role (get-property pset :role :keyword nil)))
57     (case role
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)))))
62
63 (export 'simple-message)
64 (defclass simple-message (basic-message)
65   ()
66   (:documentation
67    "Base class for messages with `simple' method combinations.
68
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.
73
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
76    pointer."))
77
78 (defmethod sod-message-method-class
79     ((message simple-message) (class sod-class) pset)
80   (if (get-property pset :role :keyword nil)
81       (call-next-method)
82       (primary-method-class message)))
83
84 (defmethod primary-method-class ((message simple-message))
85   'basic-direct-method)
86
87 ;;;--------------------------------------------------------------------------
88 ;;; Direct method classes.
89
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))
94   (:documentation
95    "Base class for built-in direct method classes.
96
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.
100
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
103    categorization.
104
105    The function type protocol is implemented on `basic-direct-method' using
106    slot reader methods."))
107
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)))
112
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"
116             class role
117             (sod-class-nickname (sod-message-class message))
118             (sod-message-name message))))
119
120 (defun effective-method-keyword-struct-tag (effective-method)
121   (with-slots ((class %class) message) effective-method
122     (format nil "~A__keywords_~A__~A"
123             class
124             (sod-class-nickname (sod-message-class message))
125             (sod-message-name message))))
126
127 (defun fix-up-keyword-method-args (method args)
128   "Adjust the ARGS to include METHOD's `suppliedp' and keyword arguments.
129
130    Return the adjusted list.  The `suppliedp' argument, if any, is prepended
131    to the list; the keyword arguments are added to the end.
132
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)))
137     (append (and keys
138                  (list (make-argument "suppliedp" (c-type (struct tag)))))
139             args
140             (mapcar (lambda (key)
141                       (make-argument (argument-name key)
142                                      (argument-type key)))
143                     keys))))
144
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))))
153                  . method-args))))
154
155 (defmethod sod-method-description ((method basic-direct-method))
156   (with-slots (role) method
157     (if role (string-downcase role)
158         "primary")))
159
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))))
165
166 (export 'daemon-direct-method)
167 (defclass daemon-direct-method (basic-direct-method)
168   ()
169   (:documentation
170    "A daemon direct method is invoked for side effects and cannot override.
171
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.
175
176    In C terms, a daemon method must return `void', and is not passed a
177    `next_method' pointer."))
178
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)))
185
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))
190   (:documentation
191    "A delegating direct method can choose to override other methods.
192
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.
196
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.)
202
203    The function type protocol is implemented on `delegating-direct-method'
204    using slot reader methods.."))
205
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*
212                                                 c-type-va-list)
213                                  (butlast msgargs)))
214                           ((keyword-message-p message)
215                            (cons (make-argument *sod-keywords*
216                                                 (c-type (* (void :const))))
217                                  msgargs))
218                           (t
219                            msgargs))))
220     (c-type (fun (lisp return-type)
221                  ("me" (* (class (sod-method-class method))))
222                  . arguments))))
223
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
229                            "next_method"
230                            (make-pointer-type
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)
235                  method-args)
236            (push next-method-arg method-args))
237           ((keyword-message-p message)
238            (push (make-argument *sod-keywords* (c-type (* (void :const))))
239                  method-args)
240            (push next-method-arg method-args)
241            (setf method-args
242                  (fix-up-keyword-method-args method method-args)))
243           (t
244            (push next-method-arg method-args)))
245     (c-type (fun (lisp (c-type-subtype type))
246                  ("me" (* (class (sod-method-class method))))
247                  . method-args))))
248
249 ;;;--------------------------------------------------------------------------
250 ;;; Effective method classes.
251
252 (defmethod method-keyword-argument-lists
253     ((method effective-method) direct-methods)
254   (with-slots (message) method
255     (and (keyword-message-p message)
256          (mapcar (lambda (m)
257                    (let ((type (sod-method-type m)))
258                      (cons (c-function-keywords type)
259                            (format nil "method for ~A on ~A (at ~A)"
260                                    message
261                                    (sod-method-class m)
262                                    (file-location m)))))
263                  direct-methods))))
264
265 (defmethod shared-initialize :after
266     ((method effective-method) slot-names &key direct-methods)
267   (declare (ignore slot-names))
268
269   ;; Set the keyword argument list.
270   (with-slots (message keywords) method
271     (setf keywords
272           (merge-keyword-lists (method-keyword-argument-lists
273                                 method direct-methods)))))
274
275 (export '(basic-effective-method
276           effective-method-around-methods effective-method-before-methods
277           effective-method-after-methods))
278 (defclass basic-effective-method (effective-method)
279   ((around-methods :initarg :around-methods :initform nil
280                    :type list :reader effective-method-around-methods)
281    (before-methods :initarg :before-methods :initform nil
282                    :type list :reader effective-method-before-methods)
283    (after-methods :initarg :after-methods :initform nil
284                   :type list :reader effective-method-after-methods)
285    (basic-argument-names :type list
286                          :reader effective-method-basic-argument-names)
287    (functions :type list :reader effective-method-functions))
288   (:documentation
289    "Base class for built-in effective method classes.
290
291    This class maintains lists of the applicable `before', `after' and
292    `around' methods and provides behaviour for invoking these methods
293    correctly.
294
295    The argument names protocol is implemented on `basic-effective-method'
296    using a slot reader method."))
297
298 (define-on-demand-slot basic-effective-method basic-argument-names (method)
299   (let* ((message (effective-method-message method))
300          (raw-tail (sod-message-argument-tail message)))
301     (mapcar #'argument-name (reify-variable-argument-tail raw-tail))))
302
303 (defmethod effective-method-function-name ((method effective-method))
304   (let* ((class (effective-method-class method))
305          (message (effective-method-message method))
306          (message-class (sod-message-class message)))
307     (format nil "~A__emethod_~A__~A"
308             class
309             (sod-class-nickname message-class)
310             (sod-message-name message))))
311
312 (define-on-demand-slot basic-effective-method functions (method)
313   (compute-method-entry-functions method))
314
315 (export 'simple-effective-method)
316 (defclass simple-effective-method (basic-effective-method)
317   ((primary-methods :initarg :primary-methods :initform nil
318                     :type list :reader effective-method-primary-methods))
319   (:documentation
320    "Effective method counterpart to `simple-message'."))
321
322 (defmethod shared-initialize :after
323     ((method simple-effective-method) slot-names &key direct-methods)
324   (declare (ignore slot-names))
325   (categorize (method direct-methods :bind ((role (sod-method-role method))))
326       ((primary (null role))
327        (before (eq role :before))
328        (after (eq role :after))
329        (around (eq role :around)))
330     (with-slots (primary-methods before-methods after-methods around-methods)
331         method
332       (setf primary-methods primary
333             before-methods before
334             after-methods (reverse after)
335             around-methods around))))
336
337 ;;;--------------------------------------------------------------------------
338 ;;; Code generation.
339
340 (defmethod shared-initialize :after
341     ((codegen method-codegen) slot-names &key)
342   (declare (ignore slot-names))
343   (with-slots (message target) codegen
344     (setf target
345           (if (eq (c-type-subtype (sod-message-type message)) c-type-void)
346               :void
347               :return))))
348
349 ;;;--------------------------------------------------------------------------
350 ;;; Invoking direct methods.
351
352 (export 'basic-effective-method-body)
353 (defun basic-effective-method-body (codegen target method body)
354   "Build the common method-invocation structure.
355
356    Writes to CODEGEN some basic method-invocation instructions.  It invokes
357    the `around' methods, from most- to least-specific.  If they all delegate,
358    then the `before' methods are run, most-specific first; next, the
359    instructions generated by BODY (invoked with a target argument); then, the
360    `after' methods are run, least-specific first; and, finally, the value
361    delivered by the BODY is returned to the `around' methods.  The result
362    returned by the outermost `around' method -- or, if there are none,
363    delivered by the BODY -- is finally delivered to the TARGET."
364
365   (with-slots (message (class %class)
366                before-methods after-methods around-methods)
367       method
368     (let* ((message-type (sod-message-type message))
369            (return-type (c-type-subtype message-type))
370            (basic-tail (effective-method-basic-argument-names method)))
371       (flet ((method-kernel (target)
372                (dolist (before before-methods)
373                  (invoke-method codegen :void basic-tail before))
374                (if (null after-methods)
375                    (funcall body target)
376                    (convert-stmts codegen target return-type
377                                   (lambda (target)
378                                     (funcall body target)
379                                     (dolist (after (reverse after-methods))
380                                       (invoke-method codegen :void
381                                                      basic-tail after)))))))
382         (invoke-delegation-chain codegen target basic-tail
383                                  around-methods #'method-kernel)))))
384
385 ;;;--------------------------------------------------------------------------
386 ;;; Method entry points.
387
388 (defparameter *method-entry-inline-threshold* 200
389   "Threshold below which effective method bodies are inlined into entries.
390
391    After the effective method body has been computed, we calculate its
392    metric, multiply by the number of entries we need to generate, and compare
393    it with this threshold.  If the metric is below the threshold then we
394    fold the method body into the entry functions; otherwise we split the
395    effective method out into its own function.")
396
397 (defmethod method-entry-function-name
398     ((method effective-method) (chain-head sod-class) role)
399   (let* ((class (effective-method-class method))
400          (message (effective-method-message method))
401          (message-class (sod-message-class message)))
402     (if (or (not (slot-boundp method 'functions))
403             (slot-value method 'functions))
404         (format nil "~A__mentry~@[__~(~A~)~]_~A__~A__chain_~A"
405                 class role
406                 (sod-class-nickname message-class)
407                 (sod-message-name message)
408                 (sod-class-nickname chain-head))
409         *null-pointer*)))
410
411 (defmethod method-entry-slot-name ((entry method-entry))
412   (let* ((method (method-entry-effective-method entry))
413          (message (effective-method-message method))
414          (name (sod-message-name message))
415          (role (method-entry-role entry)))
416     (method-entry-slot-name-by-role entry role name)))
417
418 (defmethod method-entry-function-type ((entry method-entry))
419   (let* ((method (method-entry-effective-method entry))
420          (message (effective-method-message method))
421          (type (sod-message-type message))
422          (keywordsp (keyword-message-p message))
423          (raw-tail (append (sod-message-argument-tail message)
424                            (and keywordsp (list :ellipsis))))
425          (tail (ecase (method-entry-role entry)
426                  ((nil) raw-tail)
427                  (:valist (reify-variable-argument-tail raw-tail)))))
428     (c-type (fun (lisp (c-type-subtype type))
429                  ("me" (* (class (method-entry-chain-tail entry))))
430                  . tail))))
431
432 (defgeneric effective-method-keyword-parser-function-name (method)
433   (:documentation
434    "Return the name of the keyword-parsing function for an effective METHOD.
435
436    See `make-keyword-parser-function' for details of what this function
437    actually does."))
438
439 (defmethod effective-method-keyword-parser-function-name
440     ((method basic-effective-method))
441   (with-slots ((class %class) message) method
442     (format nil "~A__kwparse_~A__~A"
443             class
444             (sod-class-nickname (sod-message-class message))
445             (sod-message-name message))))
446
447 (defun make-keyword-parser-function (codegen method tag set keywords)
448   "Construct and return a keyword-argument parsing function.
449
450    The function is contributed to the CODEGEN, with the name constructed from
451    the effective METHOD.  It will populate an argument structure with the
452    given TAG.  In case of error, it will mention the name SET in its report.
453    The KEYWORDS are a list of `argument' objects naming the keywords to be
454    accepted.
455
456    The generated function has the signature
457
458         void NAME(struct TAG *kw, va_list *ap, struct kwval *v, size_t n)
459
460     It assumes that AP includes the first keyword name.  (This makes it
461     different from the keyword-parsing functions generated by the
462     `KWSET_PARSEFN' macro, but this interface is slightly more convenient and
463     we don't need to cope with functions which accept no required
464     arguments.)"
465
466   ;; Let's start, then.
467   (codegen-push codegen)
468
469   ;; Set up the local variables we'll need.
470   (macrolet ((var (name type)
471                `(ensure-var codegen ,name (c-type ,type))))
472     (var "k" const-string)
473     (var "aap" (* va-list))
474     (var "t" (* (struct "kwtab" :const)))
475     (var "vv" (* (struct "kwval" :const)))
476     (var "nn" size-t))
477
478   (flet ((call (target func &rest args)
479            ;; Call FUNC with ARGS; return result in TARGET.
480
481            (apply #'deliver-call codegen target func args))
482
483          (convert (target type)
484            ;; Fetch the object of TYPE pointed to by `v->val', and store it
485            ;; in TARGET.
486
487            (deliver-expr codegen target
488                          (format nil "*(~A)v->val"
489                                  (make-pointer-type (qualify-c-type
490                                                      type (list :const))))))
491
492          (namecheck (var name conseq alt)
493            ;; Return an instruction: if VAR matches the string NAME then do
494            ;; CONSEQ; otherwise do ALT.
495
496            (make-if-inst (make-call-inst "!strcmp"
497                                          var (prin1-to-string name))
498                          conseq alt)))
499
500     ;; Prepare the main parsing loops.  We're going to construct them both at
501     ;; the same time.  They're not quite similar enough for it to be
502     ;; worthwhile abstracting this further, but carving up the keywords is
503     ;; too tedious to write out more than once.
504     (let ((va-act (make-expr-inst (make-call-inst "kw_unknown" set "k")))
505           (tab-act (make-expr-inst (make-call-inst "kw_unknown"
506                                                    set "v->kw")))
507           (name (effective-method-keyword-parser-function-name method)))
508
509       ;; Work through the keywords.  We're going to be building up the
510       ;; conditional dispatch from the end, so reverse the (nicely sorted)
511       ;; list before processing it.
512       (dolist (key (reverse keywords))
513         (let* ((key-name (argument-name key))
514                (key-type (argument-type key)))
515
516           ;; Handle the varargs case.
517           (codegen-push codegen)
518           (deliver-expr codegen (format nil "kw->~A__suppliedp" key-name) 1)
519           (call (format nil "kw->~A" key-name) "va_arg" "*ap" key-type)
520           (setf va-act (namecheck "k" key-name
521                                   (codegen-pop-block codegen) va-act))
522
523           ;; Handle the table case.
524           (codegen-push codegen)
525           (deliver-expr codegen (format nil "kw->~A__suppliedp" key-name) 1)
526           (convert (format nil "kw->~A" key-name) key-type)
527           (setf tab-act (namecheck "v->kw" key-name
528                                    (codegen-pop-block codegen) tab-act))))
529
530       ;; Deal with the special `kw.' keywords read via varargs.
531       (codegen-push codegen)
532       (call "vv" "va_arg" "*ap" (c-type (* (struct "kwval" :const))))
533       (call "nn" "va_arg" "*ap" c-type-size-t)
534       (call :void name "kw" *null-pointer* "vv" "nn")
535       (setf va-act (namecheck "k" "kw.tab"
536                               (codegen-pop-block codegen) va-act))
537
538       (codegen-push codegen)
539       (call "aap" "va_arg" "*ap" (c-type (* va-list)))
540       (call :void name "kw" "aap" *null-pointer* 0)
541       (setf va-act (namecheck "k" "kw.va_list"
542                               (codegen-pop-block codegen) va-act))
543
544       ;; Finish up the varargs loop.
545       (emit-banner codegen "Parse keywords from the variable-length tail.")
546       (codegen-push codegen)
547       (call "k" "va_arg" "*ap" c-type-const-string)
548       (emit-inst codegen (make-if-inst "!k" (make-break-inst)))
549       (emit-inst codegen va-act)
550       (let ((loop (make-for-inst nil nil nil (codegen-pop-block codegen))))
551         (emit-inst codegen
552                    (make-if-inst "ap" (make-block-inst nil (list loop)))))
553
554       ;; Deal with the special `kw.' keywords read from a table.
555       (codegen-push codegen)
556       (deliver-expr codegen "t"
557                     (format nil "(~A)v->val"
558                             (c-type (* (struct "kwtab" :const)))))
559       (call :void name "kw" *null-pointer* "t->v" "t->n")
560       (setf tab-act (namecheck "v->kw" "kw.tab"
561                                (codegen-pop-block codegen) tab-act))
562
563       (emit-banner codegen "Parse keywords from the argument table.")
564       (codegen-push codegen)
565       (convert "aap" (c-type (* va-list)))
566       (call :void name "kw" "aap" *null-pointer* 0)
567       (setf tab-act (namecheck "v->kw" "kw.va_list"
568                                (codegen-pop-block codegen) tab-act))
569
570       ;; Finish off the table loop.
571       (codegen-push codegen)
572       (emit-inst codegen tab-act)
573       (emit-inst codegen (make-expr-inst "v++"))
574       (emit-inst codegen (make-expr-inst "n--"))
575       (emit-inst codegen (make-while-inst "n" (codegen-pop-block codegen)))
576
577       ;; Wrap the whole lot up with a nice bow.
578       (let ((message (effective-method-message method)))
579         (codegen-pop-function codegen name
580                               (c-type (fun void
581                                            ("kw" (* (struct tag)))
582                                            ("ap" (* va-list))
583                                            ("v" (* (struct "kwval" :const)))
584                                            ("n" size-t)))
585                               "Keyword parsing for `~A.~A' on class `~A'."
586                               (sod-class-nickname
587                                (sod-message-class message))
588                               (sod-message-name message)
589                               (effective-method-class method))))))
590
591 (defmethod make-method-entries ((method basic-effective-method)
592                                 (chain-head sod-class)
593                                 (chain-tail sod-class))
594   (let ((entries nil)
595         (message (effective-method-message method)))
596     (flet ((make (role)
597              (push (make-instance 'method-entry
598                                   :method method :role role
599                                   :chain-head chain-head
600                                   :chain-tail chain-tail)
601                    entries)))
602       (when (or (varargs-message-p message)
603                 (keyword-message-p message))
604         (make :valist))
605       (make nil)
606       entries)))
607
608 (defmethod compute-method-entry-functions ((method basic-effective-method))
609
610   ;; OK, there's quite a lot of this, so hold tight.
611   ;;
612   ;; The first thing we need to do is find all of the related objects.  This
613   ;; is a bit verbose but fairly straightforward.
614   ;;
615   ;; Next, we generate the effective method body -- using `compute-effective-
616   ;; method-body' of all things.  This gives us the declarations and body for
617   ;; an effective method function, but we don't have an actual function yet.
618   ;;
619   ;; Now we look at the chains which are actually going to need a method
620   ;; entry: only those chains whose tail (most specific) class is a
621   ;; superclass of the class which defined the message need an entry.  We
622   ;; build a list of these tail classes.
623   ;;
624   ;; Having done this, we decide whether it's better to generate a standalone
625   ;; effective-method function and call it from each of the method entries,
626   ;; or to inline the effective method body into each of the entries.
627   ;;
628   ;; Most of the complexity here comes from (a) dealing with the two
629   ;; different strategies for constructing method entry functions and (b)
630   ;; (unsurprisingly) the mess involved with dealing with varargs messages.
631
632   (let* ((message (effective-method-message method))
633          (class (effective-method-class method))
634          (message-class (sod-message-class message))
635          (return-type (c-type-subtype (sod-message-type message)))
636          (codegen (make-instance 'method-codegen
637                                  :message message
638                                  :class class
639                                  :method method))
640
641          ;; Method entry details.
642          (chain-tails (remove-if-not (lambda (super)
643                                        (sod-subclass-p super message-class))
644                                      (mapcar #'car
645                                              (sod-class-chains class))))
646          (n-entries (length chain-tails))
647          (raw-entry-args (append (sod-message-argument-tail message)
648                                  (and (keyword-message-p message)
649                                       (list :ellipsis))))
650          (entry-args (reify-variable-argument-tail raw-entry-args))
651          (parm-n (let ((tail (last (cons (make-argument "me" c-type-void)
652                                          raw-entry-args) 2)))
653                    (and tail (eq (cadr tail) :ellipsis)
654                         (argument-name (car tail)))))
655          (entry-target (codegen-target codegen))
656
657          ;; Effective method function details.
658          (emf-name (effective-method-function-name method))
659          (ilayout-type (c-type (* (struct (ilayout-struct-tag class)))))
660          (emf-type (c-type (fun (lisp return-type)
661                                 ("sod__obj" (lisp ilayout-type))
662                                 . entry-args))))
663
664     (flet ((setup-entry (tail)
665              (let ((head (sod-class-chain-head tail)))
666                (codegen-push codegen)
667                (ensure-var codegen "sod__obj" ilayout-type
668                            (make-convert-to-ilayout-inst class
669                                                          head "me"))))
670            (finish-entry (tail)
671              (let* ((head (sod-class-chain-head tail))
672                     (role (if parm-n :valist nil))
673                     (name (method-entry-function-name method head role))
674                     (type (c-type (fun (lisp return-type)
675                                        ("me" (* (class tail)))
676                                        . entry-args))))
677                (codegen-pop-function codegen name type
678                 "~@(~@[~A ~]entry~) function ~:_~
679                  for method `~A.~A' ~:_~
680                  via chain headed by `~A' ~:_~
681                  defined on `~A'."
682                 (if parm-n "Indirect argument-tail" nil)
683                 (sod-class-nickname message-class)
684                 (sod-message-name message)
685                 head class)
686
687                ;; If this is a varargs or keyword method then we've made the
688                ;; `:valist' role.  Also make the `nil' role.
689                (when parm-n
690                  (let ((call (apply #'make-call-inst name "me"
691                                     (mapcar #'argument-name entry-args)))
692                        (main (method-entry-function-name method head nil))
693                        (main-type (c-type (fun (lisp return-type)
694                                                ("me" (* (class tail)))
695                                                . raw-entry-args))))
696                    (codegen-push codegen)
697                    (ensure-var codegen *sod-ap* c-type-va-list)
698                    (convert-stmts codegen entry-target return-type
699                                   (lambda (target)
700                                     (deliver-call codegen :void "va_start"
701                                                   *sod-ap* parm-n)
702                                     (deliver-expr codegen target call)
703                                     (deliver-call codegen :void "va_end"
704                                                   *sod-ap*)))
705                    (codegen-pop-function codegen main main-type
706                     "Variable-length argument list ~:_~
707                      entry function ~:_~
708                      for method `~A.~A' ~:_~
709                      via chain headed by `~A' ~:_~
710                      defined on `~A'."
711                 (sod-class-nickname message-class)
712                 (sod-message-name message)
713                 head class))))))
714
715       ;; Generate the method body.  We'll work out what to do with it later.
716       (codegen-push codegen)
717       (let* ((result (if (eq return-type c-type-void) nil
718                          (temporary-var codegen return-type)))
719              (emf-target (or result :void)))
720         (compute-effective-method-body method codegen emf-target)
721         (multiple-value-bind (vars insts) (codegen-pop codegen)
722           (cond ((or (= n-entries 1)
723                      (<= (* n-entries (reduce #'+ insts :key #'inst-metric))
724                          *method-entry-inline-threshold*))
725
726                  ;; The effective method body is simple -- or there's only
727                  ;; one of them.  We'll inline the method body into the entry
728                  ;; functions.
729                  (dolist (tail chain-tails)
730                    (setup-entry tail)
731                    (dolist (var vars)
732                      (if (typep var 'var-inst)
733                          (ensure-var codegen (inst-name var)
734                                      (inst-type var) (inst-init var))
735                          (emit-decl codegen var)))
736                    (emit-insts codegen insts)
737                    (deliver-expr codegen entry-target result)
738                    (finish-entry tail)))
739
740                 (t
741
742                  ;; The effective method body is complicated and we'd need
743                  ;; more than one copy.  We'll generate an effective method
744                  ;; function and call it a lot.
745                  (codegen-build-function codegen emf-name emf-type vars
746                   (nconc insts (and result
747                                     (list (make-return-inst result))))
748                   "Effective method function ~:_for `~A.~A' ~:_~
749                    defined on `~A'."
750                   (sod-class-nickname message-class)
751                   (sod-message-name message)
752                   (effective-method-class method))
753
754                  (let ((call (apply #'make-call-inst emf-name "sod__obj"
755                                     (mapcar #'argument-name entry-args))))
756                    (dolist (tail chain-tails)
757                      (setup-entry tail)
758                      (deliver-expr codegen entry-target call)
759                      (finish-entry tail)))))))
760
761       (codegen-functions codegen))))
762
763 (defmethod compute-effective-method-body :around
764     ((method basic-effective-method) codegen target)
765   (let* ((message (effective-method-message method))
766          (keywordsp (keyword-message-p message))
767          (keywords (effective-method-keywords method))
768          (ap-addr (format nil "&~A" *sod-tmp-ap*))
769          (set (format nil "\"~A:~A.~A\""
770                       (sod-class-name (effective-method-class method))
771                       (sod-class-nickname (sod-message-class message))
772                       (sod-message-name message))))
773     (labels ((call (target func &rest args)
774                (apply #'deliver-call codegen target func args))
775              (parse-keywords (body)
776                (ensure-var codegen *sod-tmp-ap* c-type-va-list)
777                (call :void "va_copy" *sod-tmp-ap* *sod-ap*)
778                (funcall body)
779                (call :void "va_end" *sod-tmp-ap*)))
780       (cond ((not keywordsp)
781              (call-next-method))
782             ((null keywords)
783              (let ((*keyword-struct-disposition* :null))
784                (parse-keywords (lambda ()
785                                  (with-temporary-var
786                                      (codegen kw c-type-const-string)
787                                    (call kw "va_arg"
788                                          *sod-tmp-ap* c-type-const-string)
789                                    (call :void "kw_parseempty" set
790                                          kw ap-addr *null-pointer* 0))))
791                (call-next-method)))
792             (t
793              (let* ((name
794                      (effective-method-keyword-parser-function-name method))
795                     (tag (effective-method-keyword-struct-tag method))
796                     (kw-addr (format nil "&~A" *sod-keywords*))
797                     (*keyword-struct-disposition* :local))
798                (ensure-var codegen *sod-keywords* (c-type (struct tag)))
799                (make-keyword-parser-function codegen method tag set keywords)
800                (parse-keywords (lambda ()
801                                  (call :void name kw-addr ap-addr
802                                        *null-pointer* 0)))
803                (call-next-method)))))))
804
805 (defmethod effective-method-live-p ((method simple-effective-method))
806   (effective-method-primary-methods method))
807
808 (defmethod compute-method-entry-functions :around ((method effective-method))
809   (if (effective-method-live-p method)
810       (call-next-method)
811       nil))
812
813 (defmethod compute-effective-method-body
814     ((method simple-effective-method) codegen target)
815   (basic-effective-method-body codegen target method
816                                (lambda (target)
817                                  (simple-method-body method
818                                                      codegen
819                                                      target))))
820
821 ;;;--------------------------------------------------------------------------
822 ;;; Standard method combination.
823
824 (export 'standard-message)
825 (defclass standard-message (simple-message)
826   ()
827   (:documentation
828    "Message class for standard method combinations.
829
830    Standard method combination is a simple method combination where the
831    primary methods are invoked as a delegation chain, from most- to
832    least-specific."))
833
834 (export 'standard-effective-method)
835 (defclass standard-effective-method (simple-effective-method) ()
836   (:documentation "Effective method counterpart to `standard-message'."))
837
838 (defmethod primary-method-class ((message standard-message))
839   'delegating-direct-method)
840
841 (defmethod sod-message-effective-method-class ((message standard-message))
842   'standard-effective-method)
843
844 (defmethod simple-method-body
845     ((method standard-effective-method) codegen target)
846   (invoke-delegation-chain codegen
847                            target
848                            (effective-method-basic-argument-names method)
849                            (effective-method-primary-methods method)
850                            nil))
851
852 ;;;----- That's all, folks --------------------------------------------------