defmfun-single.lisp 8.1 KB
Newer Older
1 2
;; Helpers that define a single GSL function interface
;; Liam Healy 2009-01-07 22:02:20EST defmfun-single.lisp
3
;; Time-stamp: <2009-01-15 22:27:56EST defmfun-single.lisp>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
;; $Id: $

(in-package :gsl)

(defun defgeneric-method-p (name)
  "When defining :method in a defgeneric, (nil foo) is used for
   the name, and foo will be returned from this function."
  (if (and (listp name) (null (first name)))
      (second name)))

(defun complex-scalars (cl-arguments c-arguments-types)
  (loop for sd in c-arguments-types
	for cl-type = (cffi-cl (st-type sd))
	append
	(when (and cl-type (subtypep cl-type 'complex)
		   (member (st-symbol sd)
			   (arglist-plain-and-categories cl-arguments)))
	  (list (list (st-symbol sd)
		      (gensym (string (st-symbol sd)))
		      (st-type sd))))))

(defun stupid-code-walk-find-variables (sexp)
  "This will work with the simplest s-expression forms only to find
   variables used."
  (labels ((scwfv (sexp)
	     (if (atom sexp)
		 (when (and (symbolp sexp)
			    (not (eql (symbol-package sexp)
				      (find-package :keyword))))
		   (list sexp))
		 (unless (member (first sexp) '(quote))
		   (mapcan #'scwfv (rest sexp))))))
    (remove nil (remove-duplicates (scwfv sexp)))))

(defun variables-used-in-c-arguments (c-arguments)
  "Find the arguments passed to the C function.  This is a poor
  quality code walker, but is sufficient for actual usage of defmfun."
  (remove-duplicates
   (mapcan (lambda (carg)
	     (stupid-code-walk-find-variables (st-symbol carg)))
	   c-arguments)
   :from-end t))

#+native
(defun native-pointer (array-symbols body)
  "Wrap the body with a form that obtains the native pointer
   and protects it during execution of the body."
  (if array-symbols
      ;; http://www.sbcl.org/manual/Calling-Lisp-From-C.html
      (native-pointer-protect array-symbols body)
      body))

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
(define-condition obsolete-gsl-version (error)
  ((name :initarg :name :reader name)
   (gsl-name :initarg :gsl-name :reader gsl-name)
   (gsl-version :initarg :gsl-version :reader gsl-version))
  (:report
   (lambda (condition stream)
     (apply 'format stream
	    "Function ~a (~a) is not available in the ~
               ~%currently loaded releases ~a of GSL; it was introduced in release ~d.~d."
	    (name condition)
	    (gsl-name condition)
	    *gsl-version*
	    (gsl-version condition))))
  (:documentation
   "An error indicating that the currently loaded version of the GSL libary
    does not have the function defined."))

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
(defun complete-definition
    (definition name arglist gsl-name c-arguments key-args
     &optional
     (body-maker 'body-no-optional-arg)
     (mapdown (eq body-maker 'body-optional-arg)))
  "A complete definition form, starting with defun, :method, or defmethod."
  (destructuring-bind
	(&key documentation inputs outputs before after
	      qualifier gsl-version &allow-other-keys)
      key-args
    (declare (ignorable inputs outputs)) ; workaround for compiler errors that don't see it's used
    (if (or (not gsl-version) (apply 'have-at-least-gsl-version gsl-version))
	`(,definition
	     ,@(when (and name (not (defgeneric-method-p name)))
		     (list name))
	     ,@(when qualifier (list qualifier))
	   ,arglist
	   ,(declaration-form
	     (cl-argument-types arglist c-arguments)
92
	     (set-difference	       ; find all the unused variables
93 94 95 96 97 98 99 100
	      (arglist-plain-and-categories arglist nil)
	      (union
	       (if mapdown
		   (apply 'union
			  (mapcar 'variables-used-in-c-arguments c-arguments))
		   (variables-used-in-c-arguments c-arguments))
	       ;; Forms in :before, :after are checked for used variables
	       (stupid-code-walk-find-variables
101 102 103 104 105 106
		(cons
		 'values
		 (append before after
			 (let ((auxstart (position '&aux arglist)))
			   ;; &aux bindings are checked
			   (when auxstart
107 108 109
			     (apply
			      'append
			      (mapcar 'rest (subseq arglist (1+ auxstart))))))))))))
110 111 112 113 114 115 116 117 118 119 120 121 122
	   ,@(when documentation (list documentation))
	   #-native
	   ,(funcall body-maker name arglist gsl-name c-arguments key-args)
	   #+native
	   ,(native-pointer
	     (union inputs outputs)
	     (funcall body-maker name arglist gsl-name c-arguments key-args)))
        `(,definition
	     ,@(when (and name (not (defgeneric-method-p name)))
		     (list name))
	     ,@(when qualifier (list qualifier))
	   (&rest args)
	   (declare (ignore args))
123 124
	   (error 'obsolete-gsl-version
		  :name ',name :gsl-name ,gsl-name :gsl-version ',gsl-version)))))
125

126 127 128 129 130 131 132
(defun wrap-letlike (when binding wrapping body)
  (if when
      `(,wrapping ,binding ,@body)
      (if (eql (length body) 1)
	  (first body)
	  `(progn ,@body))))

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
(defun body-no-optional-arg (name arglist gsl-name c-arguments key-args)
  "Expand the body (computational part) of the defmfun."
  (with-defmfun-key-args key-args
    (let* ((cret-type (if (member c-return *special-c-return*)
			  :int
			  (if (listp c-return) (st-type c-return) c-return)))
	   (cret-name
	    (if (listp c-return) (st-symbol c-return) (make-symbol "CRETURN")))
	   (complex-args (complex-scalars arglist c-arguments))
	   (allocated		     ; Foreign objects to be allocated
	    (remove-if
	     (lambda (s)
	       (member s (arglist-plain-and-categories arglist nil)))
	     (variables-used-in-c-arguments c-arguments)))
	   (allocated-decl
	    (mapcar
Liam Healy's avatar
Liam Healy committed
149 150 151 152
	     (lambda (s)
	       (or (find s c-arguments :key #'st-symbol)
		   ;; Catch programming errors, usually typos
		   (error "Could not find ~a among the arguments" s)))
153 154 155 156 157 158 159
	     allocated))
	   (clret (or			; better as a symbol macro
		   (substitute cret-name :c-return return)
		   (mapcan #'cl-convert-form allocated-decl)
		   outputs
		   (unless (eq c-return :void)
		     (list cret-name)))))
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
      (if (and complex-args (not *pass-complex-scalar-as-two-reals*))
	  '(error 'pass-complex-by-value) ; arglist should be declared ignore
	  (wrap-letlike
	   allocated-decl
	   (mapcar #'wfo-declare allocated-decl)
	   'cffi:with-foreign-objects
	   `(#-native
	     ,@(mapcar (lambda (v) `(copy-cl-to-c ,v)) inputs)
	     ,@before
	     (let ((,cret-name
		    (cffi:foreign-funcall
		     ,gsl-name
		     ,@(mapcan
			(lambda (arg)
			  (let ((cfind	; variable is complex
				 (find (st-symbol arg) complex-args :key 'first)))
			    (if cfind	; make two successive scalars
				(passing-complex-by-value cfind)
				;; otherwise use without conversion
				(list (if (member (st-symbol arg) allocated)
					  :pointer
					  (st-type arg))
				      (st-symbol arg)))))
			c-arguments)
		     ,cret-type)))
	       ,@(case c-return
		       (:void `((declare (ignore ,cret-name))))
		       (:error-code	; fill in arguments
			`((check-gsl-status ,cret-name
					    ',(or (defgeneric-method-p name) name)))))
	       #-native
	       ,@(when outputs
		       (mapcar
			(lambda (x) `(setf (cl-invalid ,x) t (c-invalid ,x) nil))
			outputs))
	       ,@(when (eq cret-type :pointer)
		       `((check-null-pointer
			  ,cret-name
			  ,@'('memory-allocation-failure "No memory allocated."))))
	       ,@after
	       (values
		,@(defmfun-return
		   c-return cret-name clret allocated return return-supplied-p
		   enumeration outputs)))))))))
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

(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
     (mapcan
      (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))))