r1737: Initial MCL version.
[uffi.git] / src / mcl / functions.cl
1 ;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          function.cl
6 ;;;; Purpose:       UFFI source to C function defintions
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: functions.cl,v 1.1 2002/04/04 05:01:45 desoi Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
22 (defun process-function-args (args)
23   (if (null args)
24       #+lispworks nil
25       #+allegro '(:void)
26       #+cmu nil
27       #+mcl nil
28       (let (processed)
29         (dolist (arg args)
30           (push (process-one-function-arg arg) processed))
31         (nreverse processed))))
32
33 (defun process-one-function-arg (arg)
34   (let ((name (car arg))
35         (type (convert-from-uffi-type (cadr arg) :routine)))
36     (if (and (listp type) (listp (car type)))
37         (append (list name) type)
38       (list name type))
39     ))
40
41 (defun allegro-convert-return-type (type)
42   (if (and (listp type) (not (listp (car type))))
43       (list type)
44     type))
45
46 ;; name is either a string representing foreign name, or a list
47 ;; of foreign-name as a string and lisp name as a symbol
48
49
50 (defmacro def-function (names args &key module returning)
51   (declare (ignore module))
52   
53   (let* ((result-type (convert-from-uffi-type returning :return))
54          (function-args (process-function-args args))
55          (foreign-name (if (atom names) names (car names)))
56          (lisp-name (if (atom names) (make-lisp-name names) (cadr names))))
57     `(eval-when (:compile-toplevel :load-toplevel :execute)
58        (ccl:define-entry-point (,lisp-name ,foreign-name)
59          ,function-args
60          ,result-type))))
61
62
63 (defun make-lisp-name (name)
64   (let ((converted (substitute #\- #\_ name)))
65      (intern 
66       #+case-sensitive converted
67       #-case-sensitive (string-upcase converted))))
68
69