Added support for recursive types. Now handles extern keyword, and there is a new option which allows to generate wrapper for either all functions and variables or only externed ones. Now, automatically adds function names to "defpackage" so that they are exported.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7334 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Surendra Singhi 2005-07-18 06:17:49 +00:00
commit f027a4169a

View file

@ -9,8 +9,6 @@
* See the file LICENSE for information on usage and redistribution. * See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
char cvsroot_clisp_cxx[] = "$Header$"; char cvsroot_clisp_cxx[] = "$Header$";
#include "swigmod.h" #include "swigmod.h"
@ -22,13 +20,15 @@ public:
virtual void main(int argc, char *argv[]); virtual void main(int argc, char *argv[]);
virtual int top(Node *n); virtual int top(Node *n);
virtual int functionWrapper(Node *n); virtual int functionWrapper(Node *n);
virtual int variableWrapper(Node *n);
virtual int constantWrapper(Node *n); virtual int constantWrapper(Node *n);
virtual int classDeclaration(Node *n); virtual int classDeclaration(Node *n);
List *entries; List *entries;
private: private:
String* get_ffi_type(SwigType *ty, const String_or_char *name); String* get_ffi_type(SwigType *ty);
String* convert_literal(String *num_param, String *type); String* convert_literal(String *num_param, String *type);
String* strip_parens(String *string); String* strip_parens(String *string);
int generate_all_flag;
}; };
void CLISP :: main(int argc, char *argv[]) { void CLISP :: main(int argc, char *argv[]) {
@ -36,13 +36,20 @@ void CLISP :: main(int argc, char *argv[]) {
SWIG_library_directory("clisp"); SWIG_library_directory("clisp");
SWIG_config_file("clisp.swg"); SWIG_config_file("clisp.swg");
generate_all_flag = 0;
for(i=1; i<argc; i++) { for(i=1; i<argc; i++) {
if (!strcmp(argv[i], "-help")) { if (!strcmp(argv[i], "-help")) {
fprintf(stdout, "clisp Options (available with -clisp)\n"); Printf(stdout, "clisp Options (available with -clisp)\n");
fprintf(stdout, Printf(stdout,
" currently none"); " -generate-all\n"
"\t If this option is given then clisp definitions for all the functions\n"
"and global variables will be created otherwise only definitions for \n"
"externed functions and variables is created.");
}
else if ( (Strcmp(argv[i],"-generate-all") == 0)) {
generate_all_flag = 1;
Swig_mark_arg(i);
} }
} }
} }
@ -76,7 +83,7 @@ int CLISP :: top(Node *n) {
Swig_register_filebyname("wrapper", f_null); Swig_register_filebyname("wrapper", f_null);
String *header=NewStringf(";; This is an automatically generated file. \n;;Make changes as you feel are necessary (but remember if you try to regenerate this file, your changes will be lost). \n\n(defpackage :%s\n (:use :common-lisp :ffi)", module); String *header=NewStringf(";; This is an automatically generated file. \n;;Make changes as you feel are necessary (but remember if you try to regenerate this file, your changes will be lost). \n\n(defpackage \"%s\"\n (:use :common-lisp :ffi)", module);
Language::top(n); Language::top(n);
@ -113,26 +120,32 @@ int CLISP :: top(Node *n) {
int CLISP :: functionWrapper(Node *n) { int CLISP :: functionWrapper(Node *n) {
String *storage=Getattr(n,"storage");
if(!generate_all_flag && (!storage || (Strcmp(storage,"extern") && Strcmp(storage,"externc"))))
return SWIG_OK;
String *func_name=Getattr(n, "sym:name"); String *func_name=Getattr(n, "sym:name");
ParmList *pl=Getattr(n, "parms"); ParmList *pl=Getattr(n, "parms");
Parm *p;
int argnum=0, first=1; int argnum=0, first=1;
//Language::functionWrapper(n); Printf(f_cl, "\n(ffi:def-call-out %s\n\t(:name \"%s\")\n", func_name,func_name);
Printf(f_cl, "\n(ffi:def-cal-out %s-%s (:name \"%s\")\n", module, func_name,func_name); Append(entries,func_name);
Append(entries,NewStringf("%s-%s",module,func_name));
/* Special cases */ /* Special cases */
if (ParmList_len(pl) != 0) { if (ParmList_len(pl) != 0) {
Printf(f_cl, "\t(:arguments "); Printf(f_cl, "\t(:arguments ");
} }
for (p=pl; p; p=nextSibling(p), argnum++) { for (Parm *p=pl; p; p=nextSibling(p), argnum++) {
String *argname=Getattr(p, "name"); String *argname=Getattr(p, "name");
SwigType *argtype=Getattr(p, "type"); SwigType *argtype=Getattr(p, "type");
String *ffitype=get_ffi_type(argtype, argname);
String *ffitype=get_ffi_type(argtype);
int tempargname=0; int tempargname=0;
if (!argname) { if (!argname) {
@ -151,9 +164,10 @@ int CLISP :: functionWrapper(Node *n) {
if (tempargname) if (tempargname)
Delete(argname); Delete(argname);
} }
if (ParmList_len(pl) != 0) {
Printf(f_cl, ")\n"); /* finish arg list */ Printf(f_cl, ")\n"); /* finish arg list */
Printf(f_cl, "\t(:return-type %s)\n", get_ffi_type(Getattr(n, "type"), "result")); }
Printf(f_cl, "\t(:return-type %s)\n", get_ffi_type(Getattr(n, "type")));
Printf(f_cl, "\t(:library +library-name+))\n"); Printf(f_cl, "\t(:library +library-name+))\n");
return SWIG_OK; return SWIG_OK;
@ -165,12 +179,12 @@ int CLISP :: constantWrapper(Node *n) {
String *converted_value=convert_literal(Getattr(n, "value"), type); String *converted_value=convert_literal(Getattr(n, "value"), type);
String *name=Getattr(n, "sym:name"); String *name=Getattr(n, "sym:name");
#if 1 #if 0
Printf(stdout, "constant %s is of type %s. value: %s\n", Printf(stdout, "constant %s is of type %s. value: %s\n",
name, type, converted_value); name, type, converted_value);
#endif #endif
Printf(f_cl, "(defconstant \"%s\" %s)\n", Printf(f_cl, "\n(defconstant %s %s)\n",
name, converted_value); name, converted_value);
Delete(converted_value); Delete(converted_value);
@ -178,11 +192,24 @@ int CLISP :: constantWrapper(Node *n) {
return SWIG_OK; return SWIG_OK;
} }
int CLISP :: variableWrapper(Node *n) {
String *storage=Getattr(n,"storage");
if(!generate_all_flag && (!storage || (Strcmp(storage,"extern") && Strcmp(storage,"externc"))))
return SWIG_OK;
String *type=Getattr(n, "type");
String *var_name=Getattr(n, "sym:name");
String *lisp_type=get_ffi_type(type);
Printf(f_cl,"\n(def-c-var %s (:type %s)\n",var_name,lisp_type);
Printf(f_cl, "\t(:library +library-name+))\n");
return SWIG_OK;
}
// Includes structs // Includes structs
int CLISP :: classDeclaration(Node *n) { int CLISP :: classDeclaration(Node *n) {
String *name=Getattr(n, "sym:name"); String *name=Getattr(n, "sym:name");
String *kind = Getattr(n,"kind"); String *kind = Getattr(n,"kind");
Node *c;
if (Strcmp(kind, "struct")) { if (Strcmp(kind, "struct")) {
Printf(stderr, "Don't know how to deal with %s kind of class yet.\n", Printf(stderr, "Don't know how to deal with %s kind of class yet.\n",
@ -191,12 +218,11 @@ int CLISP :: classDeclaration(Node *n) {
SWIG_exit(EXIT_FAILURE); SWIG_exit(EXIT_FAILURE);
} }
Printf(f_cl,"\n(ffi:def-c-struct %s-%s",module,name); Printf(f_cl,"\n(ffi:def-c-struct %s",name);
Append(entries,NewStringf("make-%s-%s",module,name)); Append(entries,NewStringf("make-%s",name));
for (c=firstChild(n); c; c=nextSibling(c)) { for (Node *c=firstChild(n); c; c=nextSibling(c)) {
SwigType *type=Getattr(c, "type");
String *lisp_type; String *lisp_type;
if (Strcmp(nodeType(c), "cdecl")) { if (Strcmp(nodeType(c), "cdecl")) {
@ -209,9 +235,11 @@ int CLISP :: classDeclaration(Node *n) {
SWIG_exit(EXIT_FAILURE); SWIG_exit(EXIT_FAILURE);
} }
/* Printf(stdout, "Converting %s in %s\n", type, name); */ /* Printf(stdout, "Converting %s in %s\n", type, name); */
lisp_type=get_ffi_type(type, Getattr(c, "sym:name")); String *temp=Copy(Getattr(c,"decl"));
Append(temp,Getattr(c,"type"));
lisp_type=get_ffi_type(temp);
Delete(temp);
String *slot_name = Getattr(c, "sym:name"); String *slot_name = Getattr(c, "sym:name");
Printf(f_cl, Printf(f_cl,
@ -219,7 +247,7 @@ int CLISP :: classDeclaration(Node *n) {
slot_name, slot_name,
lisp_type); lisp_type);
Append(entries,NewStringf("%s-%s-%s",module,name,slot_name)); Append(entries,NewStringf("%s-%s",name,slot_name));
Delete(lisp_type); Delete(lisp_type);
} }
@ -233,8 +261,6 @@ int CLISP :: classDeclaration(Node *n) {
return SWIG_OK; return SWIG_OK;
} }
/* utilities */ /* utilities */
/* returns new string w/ parens stripped */ /* returns new string w/ parens stripped */
String* CLISP::strip_parens(String *string) { String* CLISP::strip_parens(String *string) {
@ -299,48 +325,75 @@ String* CLISP::convert_literal(String *num_param, String *type) {
return res; return res;
} }
String* CLISP::get_ffi_type(SwigType *ty, const String_or_char *name) { String* CLISP::get_ffi_type(SwigType *ty) {
Hash *typemap = Swig_typemap_search("in", ty, name, 0); Hash *typemap =Swig_typemap_search("in", ty,"", 0);
if (typemap) { if (typemap) {
String *typespec = Getattr(typemap, "code"); String *typespec = Getattr(typemap, "code");
return NewString(typespec); return NewString(typespec);
} }
else if(SwigType_ispointer(ty)) {
SwigType *cp = Copy(ty);
SwigType_del_pointer(cp);
String *inner_type=get_ffi_type(cp);
String *str = NewStringf("(ffi:c-pointer %s)",inner_type);
Delete(cp);
Delete(inner_type);
return str;
}
else if(SwigType_isarray(ty)) {
SwigType *cp = Copy(ty);
String *array_dim=SwigType_array_getdim(ty,0);
if(!Strcmp(array_dim,"")) { //dimension less array convert to pointer
Delete(array_dim);
SwigType_del_array(cp);
SwigType_add_pointer(cp);
String *str =get_ffi_type(cp);
Delete(cp);
return str;
}
else { else {
SwigType *tr=SwigType_typedef_resolve_all(ty); SwigType_pop_arrays(cp);
char *type_reduced=Char(tr); String *inner_type = get_ffi_type(cp);
// int i; Delete(cp);
//Printf(stdout,"convert_type %s\n", ty); int ndim=SwigType_array_ndim(ty);
if (SwigType_isconst(tr)) { String *dimension;
SwigType_pop(tr); if(ndim == 1) {
type_reduced=Char(tr); dimension=array_dim;
} }
else {
if (SwigType_ispointer(type_reduced) || SwigType_isarray(ty) || dimension = array_dim;
!strncmp(type_reduced, "p.f", 3)) { for(int i=1;i<ndim;i++) {
#if 1 array_dim=SwigType_array_getdim(ty,i);
return NewString("(* :void)"); Append(dimension," ");
#else Append(dimension,array_dim);
return NewString(":foreign-address"); Delete(array_dim);
#endif
} }
String *temp=dimension;
// for(i=0; i<defined_foreign_types.count; i++) { dimension=NewStringf("(%s)",dimension);
// if (!Strcmp(ty, defined_foreign_types.entries[i])) { Delete(temp);
// return NewStringf("#.(%s \"%s\" :type :type)",
// identifier_converter,
// ty);
// }
// }
if (!Strncmp(type_reduced, "enum ", 5)) {
return NewString(":int");
} }
String *str=NewStringf("(ffi:c-array %s %s)",inner_type,dimension);
Printf(stderr, "Unsupported data type: %s (was: %s)\n", type_reduced, ty); Delete(inner_type);
SWIG_exit(EXIT_FAILURE); Delete(dimension);
return str;
} }
return 0; }
String *str=SwigType_str(ty,0);
if(str) {
char *st = Strstr(str,"struct");
if(st) {
st+=7;
return NewString(st);
}
char *cl = Strstr(str,"class");
if(cl) {
cl+=6;
return NewString(cl);
}
}
return str;
} }
extern "C" Language *swig_clisp(void) { extern "C" Language *swig_clisp(void) {