3 ;;; Method combination protocol
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 ;;;--------------------------------------------------------------------------
29 ;;; Effective methods and entries.
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))
40 "The behaviour invoked by sending a message to an instance of a class.
42 This class describes the behaviour when an instance of CLASS is sent
45 This is not a useful class by itself. Message classes are expected to
46 define their own effective-method classes.
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
52 (export 'sod-message-effective-method-class)
53 (defgeneric sod-message-effective-method-class (message)
55 "Return the effective method class for the given MESSAGE.
57 This function is invoked by `compute-sod-effective-method'."))
59 (export 'primary-method-class)
60 (defgeneric primary-method-class (message)
62 "Return the name of the primary direct method class for MESSAGE.
64 This protocol is used by `simple-message' subclasses."))
66 (export 'method-keyword-argument-lists)
67 (defgeneric method-keyword-argument-lists (method direct-methods state)
69 "Returns a list of keyword argument lists to be merged.
71 This should return a list suitable for passing to `merge-keyword-lists',
72 i.e., each element should be a pair consisting of a function describing
73 the source of the argument list (returning location and description), and
74 a list of `argument' objects.
76 The METHOD is the effective method being processed; DIRECT-METHODS is the
77 complete list of applicable direct methods (most specific first); and
78 STATE is an `inheritance-path-reporter-state' object which can be used by
79 the returned reporting functions."))
81 (export 'compute-sod-effective-method)
82 (defgeneric compute-sod-effective-method (message class)
84 "Return the effective method when a CLASS instance receives MESSAGE.
86 The default method constructs an instance of the message's chosen
87 `sod-message-effective-method-class', passing the MESSAGE, the CLASS and
88 the list of applicable methods as initargs to `make-instance'."))
90 (export 'compute-effective-methods)
91 (defgeneric compute-effective-methods (class)
93 "Return a list of all of the effective methods needed for CLASS.
95 The list needn't be in any particular order."))
97 (export '(method-entry method-entry-effective-method
98 method-entry-chain-head method-entry-chain-tail))
99 (defclass method-entry ()
100 ((%method :initarg :method :type effective-method
101 :reader method-entry-effective-method)
102 (chain-head :initarg :chain-head :type sod-class
103 :reader method-entry-chain-head)
104 (chain-tail :initarg :chain-tail :type sod-class
105 :reader method-entry-chain-tail)
106 (role :initarg :role :type (or keyword null) :reader method-entry-role))
108 "An entry point into an effective method.
110 Specifically, this is the entry point to the effective METHOD invoked via
111 the vtable for the chain headed by CHAIN-HEAD, and serving the given ROLE.
112 The CHAIN-TAIL is the most specific class on this chain; this is useful
113 because we can reuse the types of method entries from superclasses on
116 Each effective method may have several different method entries, because
117 an effective method can be called via vtables attached to different
118 chains, and such calls will pass instance pointers which point to
119 different `ichain' structures within the overall instance layout; it's the
120 job of the method entry to adjust the instance pointers correctly for the
121 rest of the effective method.
123 A vtable can contain more than one entry for the same message. Such
124 entries are distinguished by their roles. A message always has an entry
125 with the `nil role; in addition, a varargs message also has a `:valist'
126 role, which accepts a `va_list' argument in place of the variable argument
127 listNo other roles are currently defined, though they may be introduced by
130 The boundaries between a method entry and the effective method
131 is (intentionally) somewhat fuzzy. In extreme cases, the effective method
132 may not exist at all as a distinct entity in the output because its
133 content is duplicated in all of the method entry functions. This is left
134 up to the effective method protocol."))
136 (export 'make-method-entries)
137 (defgeneric make-method-entries (effective-method chain-head chain-tail)
139 "Return a list of `method-entry' objects for an EFFECTIVE-METHOD called
142 There is no default method for this function. (Maybe when the
143 effective-method/method-entry output protocol has settled down I'll know
144 what a sensible default action would be.)"))
146 ;;;--------------------------------------------------------------------------
147 ;;; Protocol for messages and direct-methods.
149 (export 'sod-message-argument-tail)
150 (defgeneric sod-message-argument-tail (message)
152 "Return the argument tail for the message, with invented argument names.
154 No `me' argument is prepended; any `:ellipsis' is left as it is."))
156 (export 'sod-method-description)
157 (defgeneric sod-method-description (method)
159 "Return an adjectival phrase describing METHOD.
161 The result will be placed into an error message reading something like
162 ``Conflicting definition of DESCRIPTION direct method `bogus'''. Two
163 direct methods which can coexist in the same class, defined on the same
164 message, should have differing descriptions."))
166 (export 'sod-method-function-type)
167 (defgeneric sod-method-function-type (method)
169 "Return the C function type for the direct method.
171 This is called during initialization of a direct method object, and the
174 A default method is provided (by `basic-direct-method') which simply
175 prepends an appropriate `me' argument to the user-provided argument list.
176 Fancy method classes may need to override this behaviour."))
178 (export 'sod-method-next-method-type)
179 (defgeneric sod-method-next-method-type (method)
181 "Return the C function type for the next-method trampoline.
183 This is called during initialization of a direct method object, and the
184 result is cached. It should return a function type, not a pointer type.
186 A default method is provided (by `delegating-direct-method') which should
187 do the right job. Very fancy subclasses might need to do something
190 (export 'sod-method-function-name)
191 (defgeneric sod-method-function-name (method)
193 "Return the C function name for the direct method."))
195 (export 'keyword-message-p)
196 (defun keyword-message-p (message)
197 "Answer whether the MESSAGE accepts a keyword arguments.
199 Dealing with keyword messages is rather fiddly, so this is useful to
201 (typep (sod-message-type message) 'c-keyword-function-type))
203 (export 'varargs-message-p)
204 (defun varargs-message-p (message)
205 "Answer whether the MESSAGE accepts a variable-length argument list.
207 We need to jump through some extra hoops in order to cope with varargs
208 messages, so this is useful to know."
209 (member :ellipsis (sod-message-argument-tail message)))
211 ;;;--------------------------------------------------------------------------
212 ;;; Protocol for effective methods and method entries.
214 (export 'method-entry-function-type)
215 (defgeneric method-entry-function-type (entry)
217 "Return the C function type for a method entry."))
219 (export 'method-entry-slot-name)
220 (defgeneric method-entry-slot-name (entry)
222 "Return the `vtmsgs' slot name for a method entry.
224 The default method indirects through `method-entry-slot-name-by-role'."))
226 (defgeneric method-entry-slot-name-by-role (entry role name)
227 (:documentation "Easier implementation for `method-entry-slot-name'.")
228 (:method ((entry method-entry) (role (eql nil)) name) name)
229 (:method ((entry method-entry) (role (eql :valist)) name)
230 (format nil "~A__v" name)))
232 (export 'effective-method-basic-argument-names)
233 (defgeneric effective-method-basic-argument-names (method)
235 "Return a list of argument names to be passed to direct methods.
237 The argument names are constructed from the message's arguments returned
238 by `sod-message-argument-tail', with any ellipsis replaced by an explicit
239 `va_list' argument. The basic arguments are the ones immediately derived
240 from the programmer's explicitly stated arguments; the `me' argument is
241 not included, and neither are more exotic arguments added as part of the
242 method delegation protocol."))
244 (export 'effective-method-live-p)
245 (defgeneric effective-method-live-p (method)
247 "Returns true if the effective METHOD is live.
249 An effective method is `live' if it should actually have proper method entry
250 functions associated with it and stored in the class vtable. The other
251 possibility is that the method is `dead', in which case the function
252 pointers in the vtable are left null."))
254 ;;;--------------------------------------------------------------------------
257 ;;; Enhanced code-generator class.
259 (export '(method-codegen codegen-message codegen-class
260 codegen-method codegen-target))
261 (defclass method-codegen (codegen)
262 ((message :initarg :message :type sod-message :reader codegen-message)
263 (%class :initarg :class :type sod-class :reader codegen-class)
264 (%method :initarg :method :type effective-method :reader codegen-method)
265 (target :initarg :target :reader codegen-target))
267 "Augments CODEGEN with additional state regarding an effective method.
269 We store the effective method, and also its target class and owning
270 message, so that these values are readily available to the code-generating
275 (export 'compute-effective-method-body)
276 (defgeneric compute-effective-method-body (method codegen target)
278 "Generates the body of an effective method.
280 Writes the function body to the code generator. It can (obviously)
281 generate auxiliary functions if it needs to.
283 The arguments are as determined by agreement with the generic function
284 `compute-method-entry-functions'; usually this will be as specified by the
285 `sod-message-argument-tail', with any variable-argument tail reified to a
286 `va_list', and an additional argument `sod__obj' of type pointer-to-
287 ilayout. The code should deliver the result (if any) to the TARGET."))
289 (export 'simple-method-body)
290 (defgeneric simple-method-body (method codegen target)
292 "Generate the body of a simple effective method.
294 The function is invoked on an effective METHOD, with a CODEGEN to which it
295 should emit code delivering the method's value to TARGET."))
297 ;;; Additional instructions.
299 ;; HACK: use gensyms for the `class' and `expr' slots to avoid leaking the
300 ;; slot names, because `expr' is exported by our package, and `class' is
301 ;; actually from the `common-lisp' package.
302 (definst convert-to-ilayout (stream :export t)
303 (#1=#:class chain-head #2=#:expr)
304 (format stream "SOD_ILAYOUT(~@<~A, ~_~A, ~_~A~:>)"
305 #1# (sod-class-nickname chain-head) #2#))
309 (defvar-unbound *keyword-struct-disposition*
310 "The current state of the keyword structure.
312 This can be one of three values.
314 * `:local' -- the structure itself is in a local variable `sod__kw'.
315 This is used in the top-level effective method.
317 * `:pointer' -- the structure is pointed to by the local variable
318 `sod__kw'. This is used by delegation-chain trampolines.
320 * `:null' -- there is in fact no structure because none of the
321 applicable methods actually define any keywords.")
323 (defun keyword-access (name &optional suffix)
324 "Return an lvalue designating a named member of the keyword struct.
326 If a non-nil SUFFIX is provided, then the member is named NAMESUFFIX."
328 (format nil "~A~A~A~@[~A~]" *sod-keywords* op name suffix)))
329 (ecase *keyword-struct-disposition*
331 (:pointer (mem "->")))))
333 (let ((kw-addr (format nil "&~A" *sod-keywords*)))
334 (defun keyword-struct-pointer ()
335 "Return a pointer to the keyword structure."
336 (ecase *keyword-struct-disposition*
338 (:pointer *sod-keywords*)
339 (:null *null-pointer*))))
341 (export 'invoke-method)
342 (defun invoke-method (codegen target arguments-tail direct-method)
343 "Emit code to invoke DIRECT-METHOD, passing it ARGUMENTS-TAIL.
345 The code is generated in the context of CODEGEN, which can be any instance
346 of the `codegen' class -- it needn't be an instance of `method-codegen'.
347 The DIRECT-METHOD is called with the given ARGUMENTS-TAIL (a list of
348 argument expressions), preceded by a `me' argument of type pointer-to-
349 CLASS where CLASS is the class on which the method was defined.
351 If the message accepts a variable-length argument list then a copy of the
352 prevailing argument pointer is provided in place of the `:ellipsis'."
354 (let* ((message (sod-method-message direct-method))
355 (class (sod-method-class direct-method))
356 (function (sod-method-function-name direct-method))
357 (type (sod-method-type direct-method))
358 (keywordsp (keyword-message-p message))
359 (keywords (and keywordsp (c-function-keywords type)))
360 (arguments (append (list (format nil "&sod__obj->~A.~A"
362 (sod-class-chain-head class))
363 (sod-class-nickname class)))
365 (mapcar (lambda (arg)
366 (let ((name (argument-name arg))
367 (default (argument-default arg)))
372 (keyword-access name)
374 (keyword-access name))))
376 (cond ((varargs-message-p message)
377 (convert-stmts codegen target (c-type-subtype type)
379 (ensure-var codegen *sod-tmp-ap* c-type-va-list)
380 (deliver-call codegen :void "va_copy"
381 *sod-tmp-ap* *sod-ap*)
382 (apply #'deliver-call codegen var
384 (deliver-call codegen :void "va_end"
387 (let ((tag (direct-method-suppliedp-struct-tag direct-method)))
388 (with-temporary-var (codegen spvar (c-type (struct tag)))
389 (dolist (arg keywords)
390 (let ((name (argument-name arg)))
391 (deliver-expr codegen (format nil "~A.~A" spvar name)
392 (keyword-access name "__suppliedp"))))
393 (setf arguments (list* (car arguments) spvar
395 (apply #'deliver-call codegen target function arguments))))
397 (apply #'deliver-call codegen target function arguments)))))
399 (export 'ensure-ilayout-var)
400 (defun ensure-ilayout-var (codegen super)
401 "Define a variable `sod__obj' pointing to the class's ilayout structure.
403 CODEGEN is a `method-codegen'. The class in question is CODEGEN's class,
404 i.e., the target class for the effective method. SUPER is one of the
405 class's superclasses; it is assumed that `me' is a pointer to a SUPER
406 (i.e., to SUPER's ichain within the ilayout)."
408 (let* ((class (codegen-class codegen))
409 (super-head (sod-class-chain-head super)))
410 (ensure-var codegen "sod__obj"
411 (c-type (* (struct (ilayout-struct-tag class))))
412 (make-convert-to-ilayout-inst class super-head "me"))))
414 (export 'make-trampoline)
415 (defun make-trampoline (codegen super body)
416 "Construct a trampoline function and return its name.
418 CODEGEN is a `method-codegen'. SUPER is a superclass of the CODEGEN
419 class. We construct a new trampoline function (with an unimaginative
420 name) suitable for being passed to a direct method defined on SUPER as its
421 `next_method'. In particular, it will have a `me' argument whose type is
424 The code of the function is generated by BODY, which will be invoked with
425 a single argument which is the TARGET to which it should deliver its
428 The return value is the name of the generated function."
430 (let* ((message (codegen-message codegen))
431 (message-type (sod-message-type message))
432 (message-class (sod-message-class message))
433 (method (codegen-method codegen))
434 (return-type (c-type-subtype message-type))
435 (raw-args (sod-message-argument-tail message))
436 (arguments (cond ((varargs-message-p message)
437 (cons (make-argument *sod-ap* c-type-va-list)
439 ((keyword-message-p message)
440 (cons (make-argument *sod-key-pointer*
441 (c-type (* (void :const))))
443 (*keyword-struct-disposition* (if (effective-method-keywords method)
445 (codegen-push codegen)
446 (ensure-ilayout-var codegen super)
447 (when (keyword-message-p message)
448 (if (eq *keyword-struct-disposition* :null)
449 (deliver-call codegen :void "SOD__IGNORE" *sod-key-pointer*)
450 (let ((tag (effective-method-keyword-struct-tag method)))
451 (ensure-var codegen *sod-keywords*
452 (c-type (* (struct tag :const)))
453 *sod-key-pointer*))))
454 (funcall body (codegen-target codegen))
455 (codegen-pop-function codegen (temporary-function)
456 (c-type (fun (lisp return-type)
457 ("me" (* (class super)))
459 "Delegation-chain trampoline ~:_~
460 for `~A.~A' ~:_on `~A'."
461 (sod-class-nickname message-class)
462 (sod-message-name message)
463 (effective-method-class method))))
465 ;;;--------------------------------------------------------------------------
466 ;;; Method entry protocol.
468 (export 'effective-method-function-name)
469 (defgeneric effective-method-function-name (method)
471 "Returns the function name of an effective method."))
473 (export 'method-entry-function-name)
474 (defgeneric method-entry-function-name (method chain-head role)
476 "Returns the function name of a method entry.
478 The method entry is given as an effective method/chain-head/role triple,
479 rather than as a method entry object because we want the function name
480 before we've made the entry object."))
482 (export 'compute-method-entry-functions)
483 (defgeneric compute-method-entry-functions (method)
485 "Construct method entry functions.
487 Builds the effective method function (if there is one) and the necessary
488 method entries. Returns a list of functions (i.e., `function-inst'
489 objects) which need to be defined in the generated source code."))
491 ;;;--------------------------------------------------------------------------
492 ;;; Invoking direct methods.
494 (export 'invoke-delegation-chain)
495 (defun invoke-delegation-chain (codegen target basic-tail chain kernel)
496 "Invoke a chain of delegating methods.
498 CODEGEN is a `method-codegen'. BASIC-TAIL is a list of argument
499 expressions to provide to the methods. The result of the delegation chain
500 will be delivered to TARGET.
502 The CHAIN is a list of method objects (it's intended to be used with
503 `delegating-direct-method' objects). The behaviour is as follows. The
504 first method in the chain is invoked with the necessary arguments (see
505 below) including a `next_method' pointer. If KERNEL is nil and there are
506 no more methods in the chain then the `next_method' pointer will be null;
507 otherwise it will point to a `trampoline' function, whose behaviour is to
508 call the remaining methods on the chain as a delegation chain. The method
509 may choose to call this function with its arguments. It will finally
510 return a value, which will be delivered to the TARGET.
512 If the chain is empty, then the code generated by KERNEL (given a TARGET
513 argument) will be invoked. It is an error if both CHAIN and KERNEL are
516 (let* ((message (codegen-message codegen))
517 (argument-tail (if (varargs-message-p message)
518 (cons *sod-tmp-ap* basic-tail)
520 (labels ((next-trampoline (method chain)
521 (if (or kernel chain)
522 (make-trampoline codegen (sod-method-class method)
524 (invoke chain target)))
526 (invoke (chain target)
528 (funcall kernel target)
529 (let ((trampoline (next-trampoline (car chain)
531 (tail (if (keyword-message-p message)
532 (cons (keyword-struct-pointer)
535 (invoke-method codegen target
536 (cons trampoline tail)
538 (invoke chain target))))
540 ;;;----- That's all, folks --------------------------------------------------