1
char cvsroot_uffi_cxx[] = "$Header: /cvsroot/swig/SWIG/Source/Modules/uffi.cxx,v 1.2 2005/09/08 22:31:21 wsfulton Exp $";
2
// TODO: remove remnants of lisptype
5
class UFFI : public Language {
8
virtual void main(int argc, char *argv[]);
9
virtual int top(Node *n);
10
virtual int functionWrapper(Node *n);
11
virtual int constantWrapper(Node *n);
12
virtual int classHandler(Node *n);
13
virtual int membervariableHandler(Node *n);
18
static File *f_null=0;
23
} defined_foreign_types;
25
static const char *identifier_converter="identifier-convert-null";
27
static int any_varargs(ParmList *pl) {
30
for(p=pl; p; p=nextSibling(p)) {
31
if (SwigType_isvarargs(Getattr(p, "type")))
40
/* returns new string w/ parens stripped */
41
static String *strip_parens(String *string) {
42
char *s=Char(string), *p;
46
if (len==0 || s[0] != '(' || s[len-1] != ')') {
47
return NewString(string);
50
p=(char *)malloc(len-2+1);
52
Printf(stderr, "Malloc failed\n");
53
SWIG_exit(EXIT_FAILURE);
56
strncpy(p, s+1, len-1);
57
p[len-2]=0; /* null terminate */
66
static String *convert_literal(String *num_param, String *type) {
67
String *num=strip_parens(num_param), *res;
70
/* Make sure doubles use 'd' instead of 'e' */
71
if (!Strcmp(type, "double")) {
72
String *updated=Copy(num);
73
if (Replace(updated, "e", "d", DOH_REPLACE_ANY) > 1) {
74
Printf(stderr, "Weird!! number %s looks invalid.\n", num);
75
SWIG_exit(EXIT_FAILURE);
81
if (SwigType_type(type) == T_CHAR) {
82
/* Use CL syntax for character literals */
83
return NewStringf("#\\%s", num_param);
85
else if (SwigType_type(type) == T_STRING) {
86
/* Use CL syntax for string literals */
87
return NewStringf("\"%s\"", num_param);
90
if (Len(num) < 2 || s[0] != '0') {
96
res=NewStringf("#%c%s",
97
s[1] == 'x' ? 'x' : 'o',
104
static void add_defined_foreign_type(String *type) {
105
if (!defined_foreign_types.count) {
107
defined_foreign_types.count=1;
108
defined_foreign_types.entries=(String **)malloc(sizeof(String *));
111
defined_foreign_types.count++;
112
defined_foreign_types.entries=(String **)
113
realloc(defined_foreign_types.entries,
114
defined_foreign_types.count*sizeof(String *));
117
if (!defined_foreign_types.entries) {
118
Printf(stderr, "Out of memory\n");
119
SWIG_exit(EXIT_FAILURE);
122
/* Fill in the new data */
123
defined_foreign_types.entries[defined_foreign_types.count-1]=
129
static String *get_ffi_type(SwigType *ty, const String_or_char *name) {
130
Hash *typemap = Swig_typemap_search("ffitype", ty, name, 0);
132
String *typespec = Getattr(typemap, "code");
133
return NewString(typespec);
136
SwigType *tr=SwigType_typedef_resolve_all(ty);
137
char *type_reduced=Char(tr);
140
//Printf(stdout,"convert_type %s\n", ty);
141
if (SwigType_isconst(tr)) {
143
type_reduced=Char(tr);
146
if (SwigType_ispointer(type_reduced) || SwigType_isarray(ty) ||
147
!strncmp(type_reduced, "p.f", 3)) {
148
return NewString(":pointer-void");
151
for(i=0; i<defined_foreign_types.count; i++) {
152
if (!Strcmp(ty, defined_foreign_types.entries[i])) {
153
return NewStringf("#.(%s \"%s\" :type :type)",
154
identifier_converter,
159
if (!Strncmp(type_reduced, "enum ", 5)) {
160
return NewString(":int");
163
Printf(stderr, "Unsupported data type: %s (was: %s)\n", type_reduced, ty);
164
SWIG_exit(EXIT_FAILURE);
169
static String *get_lisp_type(SwigType *ty, const String_or_char *name)
171
Hash *typemap = Swig_typemap_search("lisptype", ty, name, 0);
173
String *typespec = Getattr(typemap, "code");
174
return NewString(typespec);
177
return NewString("");
181
void UFFI :: main(int argc, char *argv[]) {
184
SWIG_library_directory("uffi");
185
SWIG_config_file("uffi.swg");
188
for(i=1; i<argc; i++) {
189
if (!strcmp(argv[i], "-identifier-converter")) {
190
char *conv=argv[i+1];
199
/* check for built-ins */
200
if (!strcmp(conv, "lispify")) {
201
identifier_converter="identifier-convert-lispify";
202
} else if (!strcmp(conv, "null")) {
203
identifier_converter="identifier-convert-null";
205
/* Must be user defined */
206
char *idconv = new char[strlen(conv)+1];
207
strcpy(idconv, conv);
208
identifier_converter=idconv;
212
if (!strcmp(argv[i], "-help")) {
213
fprintf(stdout, "UFFI Options (available with -uffi)\n");
215
" -identifier-converter <type or funcname>\n"
216
"\tSpecifies the type of conversion to do on C identifiers to convert\n"
217
"\tthem to symbols. There are two built-in converters: 'null' and\n"
218
"\t 'lispify'. The default is 'null'. If you supply a name other\n"
219
"\tthan one of the built-ins, then a function by that name will be\n"
220
"\tcalled to convert identifiers to symbols.\n");
229
int UFFI :: top(Node *n) {
230
String *module=Getattr(n, "name");
231
String *output_filename=NewString("");
232
String *devnull=NewString("/dev/null");
234
f_null=NewFile(devnull, "w+");
236
FileErrorDisplay(devnull);
237
SWIG_exit(EXIT_FAILURE);
242
Printf(output_filename, "%s%s.cl", SWIG_output_directory(), module);
245
f_cl=NewFile(output_filename, "w");
247
FileErrorDisplay(output_filename);
248
SWIG_exit(EXIT_FAILURE);
251
Swig_register_filebyname("header",f_null);
252
Swig_register_filebyname("runtime",f_null);
253
Swig_register_filebyname("wrapper", f_cl);
255
Printf(f_cl, ";; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; package: %s -*-\n;; This is an automatically generated file. Make changes in\n;; the definition file, not here.\n\n(defpackage :%s\n (:use :common-lisp :uffi))\n\n(in-package :%s)\n", module, module, module);
256
Printf(f_cl, "(eval-when (compile load eval)\n (defparameter *swig-identifier-converter* '%s))\n", identifier_converter);
261
Delete(f_cl); // Delete the handle, not the file
268
int UFFI :: functionWrapper(Node *n) {
269
String *funcname=Getattr(n, "sym:name");
270
ParmList *pl=Getattr(n, "parms");
272
int argnum=0, first=1, varargs=0;
274
//Language::functionWrapper(n);
276
Printf(f_cl, "(swig-defun \"%s\"\n", funcname);
281
if (ParmList_len(pl) == 0) {
282
Printf(f_cl, ":void");
283
} else if (any_varargs(pl)) {
284
Printf(f_cl, "#| varargs |#");
287
for (p=pl; p; p=nextSibling(p), argnum++) {
288
String *argname=Getattr(p, "name");
289
SwigType *argtype=Getattr(p, "type");
290
String *ffitype=get_ffi_type(argtype, argname);
291
String *lisptype=get_lisp_type(argtype, argname);
295
argname=NewStringf("arg%d", argnum);
302
Printf(f_cl, "(%s %s %s)", argname, ffitype, lisptype);
312
Printf(f_cl, ")\n"); /* finish arg list */
313
Printf(f_cl, " :returning %s\n"
314
//" :strings-convert t\n"
315
//" :call-direct %s\n"
316
//" :optimize-for-space t"
318
get_ffi_type(Getattr(n, "type"), "result")
319
//,varargs ? "nil" : "t"
326
int UFFI :: constantWrapper(Node *n) {
327
String *type=Getattr(n, "type");
328
String *converted_value=convert_literal(Getattr(n, "value"), type);
329
String *name=Getattr(n, "sym:name");
332
Printf(stdout, "constant %s is of type %s. value: %s\n",
333
name, type, converted_value);
336
Printf(f_cl, "(swig-defconstant \"%s\" %s)\n",
337
name, converted_value);
339
Delete(converted_value);
345
int UFFI :: classHandler(Node *n) {
347
String *name=Getattr(n, "sym:name");
348
String *kind = Getattr(n,"kind");
351
if (Strcmp(kind, "struct")) {
352
Printf(stderr, "Don't know how to deal with %s kind of class yet.\n",
354
Printf(stderr, " (name: %s)\n", name);
355
SWIG_exit(EXIT_FAILURE);
359
"(swig-def-struct \"%s\"\n \n",
362
for (c=firstChild(n); c; c=nextSibling(c)) {
363
SwigType *type=Getattr(c, "type");
364
SwigType *decl=Getattr(c, "decl");
366
SwigType_push(type, decl);
369
if (Strcmp(nodeType(c), "cdecl")) {
370
Printf(stderr, "Structure %s has a slot that we can't deal with.\n",
372
Printf(stderr, "nodeType: %s, name: %s, type: %s\n",
376
SWIG_exit(EXIT_FAILURE);
380
/* Printf(stdout, "Converting %s in %s\n", type, name); */
381
lisp_type=get_ffi_type(type, Getattr(c, "sym:name"));
384
" (#.(%s \"%s\" :type :slot) %s)\n",
385
identifier_converter,
386
Getattr(c, "sym:name"),
392
// Language::classHandler(n);
394
Printf(f_cl, " )\n");
396
/* Add this structure to the known lisp types */
397
//Printf(stdout, "Adding %s foreign type\n", name);
398
add_defined_foreign_type(name);
403
int UFFI :: membervariableHandler(Node *n)
405
Language::membervariableHandler(n);
410
extern "C" Language *swig_uffi(void) {