body-expand.lisp 5.31 KB
Newer Older
1 2
;; Expand the body of a defmfun
;; Liam Healy 2009-04-13 22:07:13EDT body-expand.lisp
3
;; Time-stamp: <2011-10-30 00:35:29EDT body-expand.lisp>
4
;;
5
;; Copyright 2009, 2010, 2011 Liam M. Healy
6 7 8 9 10 11 12 13 14 15 16 17 18 19
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 21 22 23 24

(in-package :gsl)

(defun creturn-st (c-return)
  "The symbol-type of the return from the C function."
25 26 27 28 29 30 31 32 33 34
  (let ((supplied-symbol-p		; return symbol supplied
	  (and (listp c-return)		; accommodate (:struct foo) type spec
	       (not (eq (symbol-package (first c-return)) (find-package :keyword))))))
    (grid:make-st
     (if supplied-symbol-p
      (grid:st-symbol c-return)
      (make-symbol "CRETURN"))
     (if (member c-return *special-c-return*)
	 :int
	 (if supplied-symbol-p (grid:st-type c-return) c-return)))))
35

36 37
(defun cl-convert-form (decl)
  "Generate a form that calls the appropriate converter from C/GSL to CL."
38
  (list `(cffi:mem-ref ,(grid:st-symbol decl) ',(grid:st-actual-type decl))))
39

40 41 42 43 44 45
(defun values-unless-singleton (forms)
  (unless (listp forms) (error "Values are not a list."))
  (if (rest forms)
    `(values ,@forms)
    (first forms)))

46 47 48 49 50
(defun body-expand (name arglist gsl-name c-arguments key-args)
  "Expand the body (computational part) of the defmfun."
  (with-defmfun-key-args key-args
    (let* ((creturn-st (creturn-st c-return))
	   (allocated-return ; Allocated and then returned from CL function
51
	     (mapcar
52
	      (lambda (s)
53 54 55 56 57 58 59
		(or (find s c-arguments :key #'grid:st-symbol)
		    ;; Catch programming errors, usually typos
		    (error "Could not find ~a among the arguments" s)))
	      (remove-if
	       (lambda (s)
		 (member s (arglist-plain-and-categories arglist nil)))
	       (variables-used-in-c-arguments c-arguments))))
60 61
	   (clret (or			; better as a symbol macro
		   (substitute
62
		    (grid:st-symbol creturn-st) :c-return
63
		    (mapcar (lambda (sym)
64
			      (let ((it (find sym allocated-return :key 'grid:st-symbol)))
65 66 67 68 69 70
				(if it
				    (first (cl-convert-form it))
				    sym)))
			    return))
		   (mappend
		    #'cl-convert-form
71
		    (callback-remove-arg allocated-return cbinfo 'grid:st-symbol))
72 73
		   outputs
		   (unless (eq c-return :void)
74
		     (list (grid:st-symbol creturn-st))))))
75 76 77 78 79 80 81 82 83 84
      (wrap-letlike
       allocated-return
       (mapcar (lambda (d) (wfo-declare d cbinfo))
	       allocated-return)
       'cffi:with-foreign-objects
       `(,@(append
	    (callback-symbol-set
	     callback-dynamic cbinfo (first callback-dynamic-variables))
	    before
	    (when callback-object (callback-set-dynamic callback-object arglist)))
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	 ,@(callback-set-slots
	    cbinfo callback-dynamic-variables callback-dynamic)
	 (let ((,(grid:st-symbol creturn-st)
		 (cffi:foreign-funcall
		  ,gsl-name
		  ,@(append
		     (mappend
		      (lambda (arg)
			(list (cond
				((member (grid:st-symbol arg)
					 allocated-return)
				 :pointer)
				(t (grid:st-type arg)))
			      (grid:st-symbol arg)))
		      (mapcar 'grid:st-pointer-generic-pointer
			      c-arguments))
		     (list (grid:st-type creturn-st))))))
	   ,@(case c-return
	       (:void `((declare (ignore ,(grid:st-symbol creturn-st)))))
	       (:error-code		; fill in arguments
		`((check-gsl-status ,(grid:st-symbol creturn-st)
				    ',(or (defgeneric-method-p name) name)))))
	   ,@(when (eq (grid:st-type creturn-st) :pointer)
	       `((check-null-pointer
		  ,(grid:st-symbol creturn-st)
		  ,@'('memory-allocation-failure "No memory allocated."))))
	   ,@after
	   ,(values-unless-singleton
	     (defmfun-return
		 c-return (grid:st-symbol creturn-st) clret
	       allocated-return
	       return return-supplied-p
	       enumeration outputs))))))))
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

(defun defmfun-return
    (c-return cret-name clret allocated return return-supplied-p enumeration outputs)
  "Generate the return computation expression for defmfun."
  (case c-return
    (:number-of-answers
     (mappend
      (lambda (vbl seq)
	`((when (> ,cret-name ,seq) ,vbl)))
      clret
      (loop for i below (length clret) collect i)))
    (:success-failure
     (if (equal clret outputs)
	 ;; success-failure more important than passed-in
	 `((success-failure ,cret-name))
	 (remove cret-name		; don't return c-return itself
		 `(,@clret (success-failure ,cret-name)))))
    (:success-continue
     (if (equal clret outputs)
	 ;; success-failure more important than passed-in
	 `((success-continue ,cret-name))
	 (remove cret-name		; don't return c-return itself
		 `(,@clret (success-continue ,cret-name)))))
    (:true-false
     `((not (zerop ,cret-name))))
    (:enumerate
     `((cffi:foreign-enum-keyword ',enumeration ,cret-name)))
    (t (unless
	   (or
	    (and (eq c-return :error-code)
		 (not outputs)
		 (not allocated)
		 (not return-supplied-p))
	    (and (null return) return-supplied-p))
	 clret))))