X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/sod/blobdiff_plain/d84f7ee7c5b3a6434e59428452ee506eb02e275e..d6bb2ccd1acd7d3ed72e26d7d9a2ec6f72d96e1a:/src/method-aggregate.lisp diff --git a/src/method-aggregate.lisp b/src/method-aggregate.lisp index caaa7ea..ec0a119 100644 --- a/src/method-aggregate.lisp +++ b/src/method-aggregate.lisp @@ -33,6 +33,7 @@ (export '(aggregating-message (defclass aggregating-message (simple-message) ((combination :initarg :combination :type keyword :reader sod-message-combination) + (plist :type list :accessor sod-message-plist) (kernel-function :type function :reader sod-message-kernel-function)) (:documentation "Message class for aggregating method combinations. @@ -95,6 +96,12 @@ (defgeneric check-aggregating-message-type (message combination type) (:method (message combination type) t)) +(defgeneric aggregating-message-method-return-type (message combination) + (:documentation + "Return the primary method return type for this MESSAGE and COMBINATION.") + (:method ((message aggregating-message) (combination t)) + (c-type-subtype (sod-message-type message)))) + (export 'aggregating-effective-method) (defclass aggregating-effective-method (simple-effective-method) () (:documentation "Effective method counterpart to `aggregating-message'.")) @@ -119,7 +126,7 @@ (defmethod simple-method-body (defmethod shared-initialize :before ((message aggregating-message) slot-names &key pset) (declare (ignore slot-names)) - (with-slots (combination kernel-function) message + (with-slots (combination plist kernel-function) message (let ((most-specific (get-property pset :most-specific :keyword :first)) (comb (get-property pset :combination :keyword))) @@ -158,6 +165,7 @@ (defmethod shared-initialize :before (prop (get-property pset name type magic))) (unless (eq prop magic) (setf keys (list* name prop keys))))) + (setf plist keys) ;; Set the kernel function for later. (setf kernel-function @@ -169,7 +177,20 @@ (defmethod shared-initialize :before (:first methods) (:last (setf methods (reverse methods)))) arg-names - keys))))))) + plist))))))) + +(defmethod check-method-type + ((method sod-method) (message aggregating-message) + (type c-function-type)) + (let ((wanted (aggregating-message-method-return-type + message (sod-message-combination message))) + (msgtype (sod-message-type message))) + (unless (c-type-equal-p (c-type-subtype type) wanted) + (error "Method return type ~A doesn't match message ~A" + (c-type-subtype msgtype) (c-type-subtype type))) + (unless (argument-lists-compatible-p (c-function-arguments msgtype) + (c-function-arguments type)) + (error "Method arguments ~A don't match message ~A" type msgtype)))) ;;;-------------------------------------------------------------------------- ;;; Utilities. @@ -358,7 +379,7 @@ (define-aggregating-method-combination :min ((acc val) :codegen codegen) :methods (lambda (invoke) (funcall invoke val) (emit-inst codegen (make-if-inst (format nil "~A > ~A" acc val) - (make-set-inst acc val) nil)))) + (make-set-inst acc val))))) (define-aggregating-method-combination :max ((acc val) :codegen codegen) :first-method (lambda (invoke) @@ -367,34 +388,28 @@ (define-aggregating-method-combination :max ((acc val) :codegen codegen) :methods (lambda (invoke) (funcall invoke val) (emit-inst codegen (make-if-inst (format nil "~A < ~A" acc val) - (make-set-inst acc val) nil)))) + (make-set-inst acc val))))) -(define-aggregating-method-combination :and ((ret val) :codegen codegen) - :return-type int +(define-aggregating-method-combination :and ((ret) :codegen codegen) :around (lambda (body) (codegen-push codegen) - (deliver-expr codegen ret 0) (funcall body) - (deliver-expr codegen ret 1) (emit-inst codegen (make-do-while-inst (codegen-pop-block codegen) 0))) :methods (lambda (invoke) - (funcall invoke val) - (emit-inst codegen (make-if-inst (format nil "!~A" val) - (make-break-inst) nil)))) + (funcall invoke ret) + (emit-inst codegen (make-if-inst (format nil "!~A" ret) + (make-break-inst))))) -(define-aggregating-method-combination :or ((ret val) :codegen codegen) - :return-type int +(define-aggregating-method-combination :or ((ret) :codegen codegen) :around (lambda (body) (codegen-push codegen) - (deliver-expr codegen ret 1) (funcall body) - (deliver-expr codegen ret 0) (emit-inst codegen (make-do-while-inst (codegen-pop-block codegen) 0))) :methods (lambda (invoke) - (funcall invoke val) - (emit-inst codegen (make-if-inst val (make-break-inst) nil)))) + (funcall invoke ret) + (emit-inst codegen (make-if-inst ret (make-break-inst))))) ;;;-------------------------------------------------------------------------- ;;; A customizable aggregating method combination. @@ -403,6 +418,7 @@ (defmethod aggregating-message-properties ((message aggregating-message) (combination (eql :custom))) '(:retvar :id :valvar :id + :methty :type :decls :fragment :before :fragment :first :fragment @@ -410,16 +426,22 @@ (defmethod aggregating-message-properties :after :fragment :count :id)) +(defmethod aggregating-message-method-return-type + ((message aggregating-message) (combination (eql :custom))) + (getf (sod-message-plist message) :methty + (c-type-subtype (sod-message-type message)))) + (defmethod compute-aggregating-message-kernel ((message aggregating-message) (combination (eql :custom)) codegen target methods arg-names - &key (retvar "sod_ret") (valvar "sod_val") + &key (retvar "sod_ret") (valvar "sod_val") (methty nil methtyp) decls before each (first each) after count) (let* ((type (c-type-subtype (sod-message-type message))) - (not-void-p (not (eq type c-type-void)))) - (when not-void-p - (ensure-var codegen retvar type) - (ensure-var codegen valvar type)) + (methty (if methtyp methty type))) + (unless (eq type c-type-void) + (ensure-var codegen retvar type)) + (unless (eq methty c-type-void) + (ensure-var codegen valvar methty)) (when count (ensure-var codegen count c-type-size-t (length methods))) (when decls @@ -427,7 +449,8 @@ (defmethod compute-aggregating-message-kernel (labels ((maybe-emit (fragment) (when fragment (emit-inst codegen fragment))) (invoke (method fragment) - (invoke-method codegen (if not-void-p valvar :void) + (invoke-method codegen + (if (eq methty c-type-void) :void valvar) arg-names method) (maybe-emit fragment))) (maybe-emit before)