048837439a1e1ace9b2aacdb230358a71062835b
[sod] / src / method-proto.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Method combination protocol
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 ;;; Effective methods and entries.
30
31 (export '(effective-method
32 effective-method-message effective-method-class
33 effective-method-keywords))
34 (defclass effective-method ()
35 ((message :initarg :message :type sod-message
36 :reader effective-method-message)
37 (%class :initarg :class :type sod-class :reader effective-method-class)
38 (keywords :type list :reader effective-method-keywords))
39 (:documentation
40 "The behaviour invoked by sending a message to an instance of a class.
41
42 This class describes the behaviour when an instance of CLASS is sent
43 MESSAGE.
44
45 This is not a useful class by itself. Message classes are expected to
46 define their own effective-method classes.
47
48 An effective method class may accept a `:direct-methods' initarg, which
49 will be a list of applicable methods sorted in most-to-least specific
50 order."))
51
52 (export 'sod-message-applicable-methods)
53 (defgeneric sod-message-applicable-methods (message class)
54 (:documentation
55 "Return a list of applicable methods for a MESSAGE.
56
57 The list contains all methods applicable for MESSAGE when sent to an
58 instance of CLASS, most specific first."))
59
60 (export 'sod-message-keyword-argument-lists)
61 (defgeneric sod-message-keyword-argument-lists
62 (message class direct-methods state)
63 (:documentation
64 "Returns a list of keyword argument lists to be merged.
65
66 This should return a list suitable for passing to `merge-keyword-lists',
67 i.e., each element should be a pair consisting of a function describing
68 the source of the argument list (returning location and description), and
69 a list of `argument' objects.
70
71 The MESSAGE is the message being processed; CLASS is a receiver class
72 under consideration; DIRECT-METHODS is the complete list of applicable
73 direct methods (most specific first); and STATE is an `inheritance-path-
74 reporter-state' object which can be used by the returned reporting
75 functions."))
76
77 (export 'compute-effective-method-keyword-arguments)
78 (defun compute-effective-method-keyword-arguments
79 (message class direct-methods)
80 "Return a merged keyword argument list.
81
82 The returned list combines all of the applicable methods, provided as
83 DIRECT-METHODS, applicable to MESSAGE when received by an instance of
84 CLASS, possibly with other keywords as determined by `sod-keyword-
85 argument-lists'."
86 (let ((state (make-inheritance-path-reporter-state class)))
87 (merge-keyword-lists (lambda ()
88 (values class
89 (format nil
90 "methods for message `~A' ~
91 applicable to class `~A'"
92 message class)))
93 (sod-message-keyword-argument-lists message
94 class
95 direct-methods
96 state))))
97
98 (export 'sod-message-check-methods)
99 (defgeneric sod-message-check-methods (message class direct-methods)
100 (:documentation
101 "Check that the applicable methods for a MESSAGE are compatible.
102
103 Specifically, given the DIRECT-METHODS applicable for the message when
104 received by an instance of CLASS, signal errors if the methods don't
105 match the MESSAGE or each other."))
106
107 (export 'sod-message-effective-method-class)
108 (defgeneric sod-message-effective-method-class (message)
109 (:documentation
110 "Return the effective method class for the given MESSAGE.
111
112 This function is invoked by `compute-sod-effective-method'."))
113
114 (export 'primary-method-class)
115 (defgeneric primary-method-class (message)
116 (:documentation
117 "Return the name of the primary direct method class for MESSAGE.
118
119 This protocol is used by `simple-message' subclasses."))
120
121 (export 'compute-sod-effective-method)
122 (defgeneric compute-sod-effective-method (message class)
123 (:documentation
124 "Return the effective method when a CLASS instance receives MESSAGE.
125
126 The default method constructs an instance of the message's chosen
127 `sod-message-effective-method-class', passing the MESSAGE, the CLASS and
128 the list of applicable methods as initargs to `make-instance'."))
129
130 (export 'compute-effective-methods)
131 (defgeneric compute-effective-methods (class)
132 (:documentation
133 "Return a list of all of the effective methods needed for CLASS.
134
135 The list needn't be in any particular order."))
136
137 (export '(method-entry method-entry-effective-method
138 method-entry-chain-head method-entry-chain-tail
139 method-entry-role))
140 (defclass method-entry ()
141 ((%method :initarg :method :type effective-method
142 :reader method-entry-effective-method)
143 (chain-head :initarg :chain-head :type sod-class
144 :reader method-entry-chain-head)
145 (chain-tail :initarg :chain-tail :type sod-class
146 :reader method-entry-chain-tail)
147 (role :initarg :role :type (or keyword null) :reader method-entry-role))
148 (:documentation
149 "An entry point into an effective method.
150
151 Specifically, this is the entry point to the effective METHOD invoked via
152 the vtable for the chain headed by CHAIN-HEAD, and serving the given ROLE.
153 The CHAIN-TAIL is the most specific class on this chain; this is useful
154 because we can reuse the types of method entries from superclasses on
155 non-primary chains.
156
157 Each effective method may have several different method entries, because
158 an effective method can be called via vtables attached to different
159 chains, and such calls will pass instance pointers which point to
160 different `ichain' structures within the overall instance layout; it's the
161 job of the method entry to adjust the instance pointers correctly for the
162 rest of the effective method.
163
164 A vtable can contain more than one entry for the same message. Such
165 entries are distinguished by their roles. A message always has an entry
166 with the `nil role; in addition, a varargs message also has a `:valist'
167 role, which accepts a `va_list' argument in place of the variable argument
168 listNo other roles are currently defined, though they may be introduced by
169 extensions.
170
171 The boundaries between a method entry and the effective method
172 is (intentionally) somewhat fuzzy. In extreme cases, the effective method
173 may not exist at all as a distinct entity in the output because its
174 content is duplicated in all of the method entry functions. This is left
175 up to the effective method protocol."))
176
177 (export 'make-method-entries)
178 (defgeneric make-method-entries (effective-method chain-head chain-tail)
179 (:documentation
180 "Return a list of `method-entry' objects for an EFFECTIVE-METHOD called
181 via CHAIN-HEAD.
182
183 There is no default method for this function. (Maybe when the
184 effective-method/method-entry output protocol has settled down I'll know
185 what a sensible default action would be.)"))
186
187 ;;;--------------------------------------------------------------------------
188 ;;; Protocol for messages and direct-methods.
189
190 (export 'sod-message-argument-tail)
191 (defgeneric sod-message-argument-tail (message)
192 (:documentation
193 "Return the argument tail for the message, with invented argument names.
194
195 No `me' argument is prepended; any `:ellipsis' is left as it is."))
196
197 (export 'sod-method-description)
198 (defgeneric sod-method-description (method)
199 (:documentation
200 "Return an adjectival phrase describing METHOD.
201
202 The result will be placed into an error message reading something like
203 ``Conflicting definition of DESCRIPTION direct method `bogus'''. Two
204 direct methods which can coexist in the same class, defined on the same
205 message, should have differing descriptions."))
206
207 (export 'sod-method-function-type)
208 (defgeneric sod-method-function-type (method)
209 (:documentation
210 "Return the C function type for the direct method.
211
212 This is called during initialization of a direct method object, and the
213 result is cached.
214
215 A default method is provided (by `basic-direct-method') which simply
216 prepends an appropriate `me' argument to the user-provided argument list.
217 Fancy method classes may need to override this behaviour."))
218
219 (export 'sod-method-next-method-type)
220 (defgeneric sod-method-next-method-type (method)
221 (:documentation
222 "Return the C function type for the next-method trampoline.
223
224 This is called during initialization of a direct method object, and the
225 result is cached. It should return a function type, not a pointer type.
226
227 A default method is provided (by `delegating-direct-method') which should
228 do the right job. Very fancy subclasses might need to do something
229 different."))
230
231 (export 'sod-method-function-name)
232 (defgeneric sod-method-function-name (method)
233 (:documentation
234 "Return the C function name for the direct method."))
235
236 (export 'keyword-message-p)
237 (defun keyword-message-p (message)
238 "Answer whether the MESSAGE accepts a keyword arguments.
239
240 Dealing with keyword messages is rather fiddly, so this is useful to
241 know."
242 (typep (sod-message-type message) 'c-keyword-function-type))
243
244 (export 'varargs-message-p)
245 (defun varargs-message-p (message)
246 "Answer whether the MESSAGE accepts a variable-length argument list.
247
248 We need to jump through some extra hoops in order to cope with varargs
249 messages, so this is useful to know."
250 (member :ellipsis (sod-message-argument-tail message)))
251
252 ;;;--------------------------------------------------------------------------
253 ;;; Protocol for effective methods and method entries.
254
255 (export 'method-entry-function-type)
256 (defgeneric method-entry-function-type (entry)
257 (:documentation
258 "Return the C function type for a method entry."))
259
260 (export 'method-entry-slot-name)
261 (defgeneric method-entry-slot-name (entry)
262 (:documentation
263 "Return the `vtmsgs' slot name for a method entry.
264
265 The default method indirects through `method-entry-slot-name-by-role'."))
266
267 (export 'method-entry-slot-name-by-role)
268 (defgeneric method-entry-slot-name-by-role (entry role name)
269 (:documentation "Easier implementation for `method-entry-slot-name'.")
270 (:method ((entry method-entry) (role (eql nil)) name) name)
271 (:method ((entry method-entry) (role (eql :valist)) name)
272 (format nil "~A__v" name)))
273
274 (export 'effective-method-basic-argument-names)
275 (defgeneric effective-method-basic-argument-names (method)
276 (:documentation
277 "Return a list of argument names to be passed to direct methods.
278
279 The argument names are constructed from the message's arguments returned
280 by `sod-message-argument-tail', with any ellipsis replaced by an explicit
281 `va_list' argument. The basic arguments are the ones immediately derived
282 from the programmer's explicitly stated arguments; the `me' argument is
283 not included, and neither are more exotic arguments added as part of the
284 method delegation protocol."))
285
286 (export 'effective-method-live-p)
287 (defgeneric effective-method-live-p (method)
288 (:documentation
289 "Returns true if the effective METHOD is live.
290
291 An effective method is `live' if it should actually have proper method
292 entry functions associated with it and stored in the class vtable. The
293 other possibility is that the method is `dead', in which case the function
294 pointers in the vtable are left null."))
295
296 ;;;--------------------------------------------------------------------------
297 ;;; Code generation.
298
299 ;;; Enhanced code-generator class.
300
301 (export '(method-codegen codegen-message codegen-class
302 codegen-method codegen-target))
303 (defclass method-codegen (codegen)
304 ((message :initarg :message :type sod-message :reader codegen-message)
305 (%class :initarg :class :type sod-class :reader codegen-class)
306 (%method :initarg :method :type effective-method :reader codegen-method)
307 (target :initarg :target :reader codegen-target))
308 (:documentation
309 "Augments CODEGEN with additional state regarding an effective method.
310
311 We store the effective method, and also its target class and owning
312 message, so that these values are readily available to the code-generating
313 functions."))
314
315 ;;; Protocol.
316
317 (export 'compute-effective-method-body)
318 (defgeneric compute-effective-method-body (method codegen target)
319 (:documentation
320 "Generates the body of an effective method.
321
322 Writes the function body to the code generator. It can (obviously)
323 generate auxiliary functions if it needs to.
324
325 The arguments are as determined by agreement with the generic function
326 `compute-method-entry-functions'; usually this will be as specified by the
327 `sod-message-argument-tail', with any variable-argument tail reified to a
328 `va_list', and an additional argument `sod__obj' of type pointer-to-
329 ilayout. The code should deliver the result (if any) to the TARGET."))
330
331 (export 'simple-method-body)
332 (defgeneric simple-method-body (method codegen target)
333 (:documentation
334 "Generate the body of a simple effective method.
335
336 The function is invoked on an effective METHOD, with a CODEGEN to which it
337 should emit code delivering the method's value to TARGET."))
338
339 ;;; Additional instructions.
340
341 (definst convert-to-ilayout (stream :export t)
342 (%class chain-head %expr)
343 (format stream "SOD_ILAYOUT(~@<~A, ~_~A, ~_~A~:>)"
344 class (sod-class-nickname chain-head) expr))
345
346 ;;; Utilities.
347
348 (defvar-unbound *keyword-struct-disposition*
349 "The current state of the keyword structure.
350
351 This can be one of three values.
352
353 * `:local' -- the structure itself is in a local variable `sod__kw'.
354 This is used in the top-level effective method.
355
356 * `:pointer' -- the structure is pointed to by the local variable
357 `sod__kw'. This is used by delegation-chain trampolines.
358
359 * `:null' -- there is in fact no structure because none of the
360 applicable methods actually define any keywords.")
361
362 (defun keyword-access (name &optional suffix)
363 "Return an lvalue designating a named member of the keyword struct.
364
365 If a non-nil SUFFIX is provided, then the member is named NAMESUFFIX."
366 (flet ((mem (op)
367 (format nil "~A~A~A~@[~A~]" *sod-keywords* op name suffix)))
368 (ecase *keyword-struct-disposition*
369 (:local (mem "."))
370 (:pointer (mem "->")))))
371
372 (let ((kw-addr (format nil "&~A" *sod-keywords*)))
373 (defun keyword-struct-pointer ()
374 "Return a pointer to the keyword structure."
375 (ecase *keyword-struct-disposition*
376 (:local kw-addr)
377 (:pointer *sod-keywords*)
378 (:null *null-pointer*))))
379
380 (export 'invoke-method)
381 (defun invoke-method (codegen target arguments-tail direct-method)
382 "Emit code to invoke DIRECT-METHOD, passing it ARGUMENTS-TAIL.
383
384 The code is generated in the context of CODEGEN, which can be any instance
385 of the `codegen' class -- it needn't be an instance of `method-codegen'.
386 The DIRECT-METHOD is called with the given ARGUMENTS-TAIL (a list of
387 argument expressions), preceded by a `me' argument of type pointer-to-
388 CLASS where CLASS is the class on which the method was defined.
389
390 If the message accepts a variable-length argument list then a copy of the
391 prevailing argument pointer is provided in place of the `:ellipsis'."
392
393 (let* ((message (sod-method-message direct-method))
394 (class (sod-method-class direct-method))
395 (function (sod-method-function-name direct-method))
396 (type (sod-method-type direct-method))
397 (keywordsp (keyword-message-p message))
398 (keywords (and keywordsp (c-function-keywords type)))
399 (arguments (append (list (format nil "&sod__obj->~A.~A"
400 (sod-class-nickname
401 (sod-class-chain-head class))
402 (sod-class-nickname class)))
403 arguments-tail
404 (mapcar (lambda (arg)
405 (let ((name (argument-name arg))
406 (default (argument-default arg)))
407 (if default
408 (make-cond-inst
409 (keyword-access name
410 "__suppliedp")
411 (keyword-access name)
412 default)
413 (keyword-access name))))
414 keywords))))
415 (cond ((varargs-message-p message)
416 (convert-stmts codegen target (c-type-subtype type)
417 (lambda (var)
418 (ensure-var codegen *sod-tmp-ap* c-type-va-list)
419 (deliver-call codegen :void "va_copy"
420 *sod-tmp-ap* *sod-ap*)
421 (apply #'deliver-call codegen var
422 function arguments)
423 (deliver-call codegen :void "va_end"
424 *sod-tmp-ap*))))
425 (keywords
426 (let ((tag (direct-method-suppliedp-struct-tag direct-method)))
427 (with-temporary-var (codegen spvar (c-type (struct tag)))
428 (dolist (arg keywords)
429 (let ((name (argument-name arg)))
430 (deliver-expr codegen (format nil "~A.~A" spvar name)
431 (keyword-access name "__suppliedp"))))
432 (setf arguments (list* (car arguments) spvar
433 (cdr arguments)))
434 (apply #'deliver-call codegen target function arguments))))
435 (t
436 (apply #'deliver-call codegen target function arguments)))))
437
438 (export 'ensure-ilayout-var)
439 (defun ensure-ilayout-var (codegen super)
440 "Define a variable `sod__obj' pointing to the class's ilayout structure.
441
442 CODEGEN is a `method-codegen'. The class in question is CODEGEN's class,
443 i.e., the target class for the effective method. SUPER is one of the
444 class's superclasses; it is assumed that `me' is a pointer to a SUPER
445 (i.e., to SUPER's ichain within the ilayout)."
446
447 (let* ((class (codegen-class codegen))
448 (super-head (sod-class-chain-head super)))
449 (ensure-var codegen "sod__obj"
450 (c-type (* (struct (ilayout-struct-tag class))))
451 (make-convert-to-ilayout-inst class super-head "me"))))
452
453 (export 'make-trampoline)
454 (defun make-trampoline (codegen super body)
455 "Construct a trampoline function and return its name.
456
457 CODEGEN is a `method-codegen'. SUPER is a superclass of the CODEGEN
458 class. We construct a new trampoline function (with an unimaginative
459 name) suitable for being passed to a direct method defined on SUPER as its
460 `next_method'. In particular, it will have a `me' argument whose type is
461 pointer-to-SUPER.
462
463 The code of the function is generated by BODY, which will be invoked with
464 a single argument which is the TARGET to which it should deliver its
465 result.
466
467 The return value is the name of the generated function."
468
469 (let* ((message (codegen-message codegen))
470 (message-type (sod-message-type message))
471 (message-class (sod-message-class message))
472 (method (codegen-method codegen))
473 (return-type (c-type-subtype message-type))
474 (raw-args (sod-message-argument-tail message))
475 (arguments (cond ((varargs-message-p message)
476 (cons (make-argument *sod-ap* c-type-va-list)
477 (butlast raw-args)))
478 ((keyword-message-p message)
479 (cons (make-argument *sod-key-pointer*
480 (c-type (* (void :const))))
481 raw-args))
482 (t raw-args)))
483 (*keyword-struct-disposition* (if (effective-method-keywords method)
484 :pointer :null)))
485 (codegen-push codegen)
486 (ensure-ilayout-var codegen super)
487 (deliver-call codegen :void "SOD__IGNORE" "sod__obj")
488 (when (keyword-message-p message)
489 (if (eq *keyword-struct-disposition* :null)
490 (deliver-call codegen :void "SOD__IGNORE" *sod-key-pointer*)
491 (let ((tag (effective-method-keyword-struct-tag method)))
492 (ensure-var codegen *sod-keywords*
493 (c-type (* (struct tag :const)))
494 *sod-key-pointer*))))
495 (funcall body (codegen-target codegen))
496 (codegen-pop-function codegen (temporary-function)
497 (c-type (fun (lisp return-type)
498 ("me" (* (class super)))
499 . arguments))
500 "Delegation-chain trampoline ~:_~
501 for `~A.~A' ~:_on `~A'."
502 (sod-class-nickname message-class)
503 (sod-message-name message)
504 (effective-method-class method))))
505
506 ;;;--------------------------------------------------------------------------
507 ;;; Method entry protocol.
508
509 (export 'effective-method-function-name)
510 (defgeneric effective-method-function-name (method)
511 (:documentation
512 "Returns the function name of an effective method."))
513
514 (export 'method-entry-function-name)
515 (defgeneric method-entry-function-name (method chain-head role)
516 (:documentation
517 "Returns the function name of a method entry.
518
519 The method entry is given as an effective method/chain-head/role triple,
520 rather than as a method entry object because we want the function name
521 before we've made the entry object."))
522
523 (export 'compute-method-entry-functions)
524 (defgeneric compute-method-entry-functions (method)
525 (:documentation
526 "Construct method entry functions.
527
528 Builds the effective method function (if there is one) and the necessary
529 method entries. Returns a list of functions (i.e., `function-inst'
530 objects) which need to be defined in the generated source code."))
531
532 ;;;--------------------------------------------------------------------------
533 ;;; Invoking direct methods.
534
535 (export 'invoke-delegation-chain)
536 (defun invoke-delegation-chain (codegen target basic-tail chain kernel)
537 "Invoke a chain of delegating methods.
538
539 CODEGEN is a `method-codegen'. BASIC-TAIL is a list of argument
540 expressions to provide to the methods. The result of the delegation chain
541 will be delivered to TARGET.
542
543 The CHAIN is a list of method objects (it's intended to be used with
544 `delegating-direct-method' objects). The behaviour is as follows. The
545 first method in the chain is invoked with the necessary arguments (see
546 below) including a `next_method' pointer. If KERNEL is nil and there are
547 no more methods in the chain then the `next_method' pointer will be null;
548 otherwise it will point to a `trampoline' function, whose behaviour is to
549 call the remaining methods on the chain as a delegation chain. The method
550 may choose to call this function with its arguments. It will finally
551 return a value, which will be delivered to the TARGET.
552
553 If the chain is empty, then the code generated by KERNEL (given a TARGET
554 argument) will be invoked. It is an error if both CHAIN and KERNEL are
555 nil."
556
557 (let* ((message (codegen-message codegen))
558 (argument-tail (if (varargs-message-p message)
559 (cons *sod-tmp-ap* basic-tail)
560 basic-tail)))
561 (labels ((next-trampoline (method chain)
562 (if (or kernel chain)
563 (make-trampoline codegen (sod-method-class method)
564 (lambda (target)
565 (invoke chain target)))
566 *null-pointer*))
567 (invoke (chain target)
568 (if (null chain)
569 (funcall kernel target)
570 (let ((trampoline (next-trampoline (car chain)
571 (cdr chain)))
572 (tail (if (keyword-message-p message)
573 (cons (keyword-struct-pointer)
574 argument-tail)
575 argument-tail)))
576 (invoke-method codegen target
577 (cons trampoline tail)
578 (car chain))))))
579 (invoke chain target))))
580
581 ;;;----- That's all, folks --------------------------------------------------