Replace hardcoded type specifiers by typemap-based type specifier lookup.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6114 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
2633f4fc99
commit
0818500caf
2 changed files with 75 additions and 63 deletions
|
|
@ -3,6 +3,29 @@
|
||||||
|
|
||||||
#define SWIG_ALLEGRO_CL
|
#define SWIG_ALLEGRO_CL
|
||||||
|
|
||||||
|
/* Typespecs for basic types. */
|
||||||
|
|
||||||
|
%typemap(ffitype) char ":char";
|
||||||
|
%typemap(lisptype) char "character";
|
||||||
|
%typemap(ffitype) unsigned char ":unsigned-char";
|
||||||
|
%typemap(lisptype) unsigned char "integer";
|
||||||
|
%typemap(ffitype) signed char ":char";
|
||||||
|
%typemap(lisptype) signed char "integer";
|
||||||
|
%typemap(ffitype) short ":short";
|
||||||
|
%typemap(ffitype) signed short ":short";
|
||||||
|
%typemap(ffitype) unsigned short ":unsigned-short";
|
||||||
|
%typemap(ffitype) int ":int";
|
||||||
|
%typemap(ffitype) signed int ":int";
|
||||||
|
%typemap(ffitype) unsigned int ":unsigned-int";
|
||||||
|
%typemap(ffitype) long ":long";
|
||||||
|
%typemap(ffitype) signed long ":long";
|
||||||
|
%typemap(ffitype) unsigned long ":unsigned-long";
|
||||||
|
%typemap(ffitype) float ":float";
|
||||||
|
%typemap(ffitype) double ":double";
|
||||||
|
%typemap(ffitype) char * "(* :char)";
|
||||||
|
%typemap(ffitype) void * "(* :void)";
|
||||||
|
%typemap(ffitype) void ":void";
|
||||||
|
|
||||||
%wrapper %{
|
%wrapper %{
|
||||||
;; $Id$
|
;; $Id$
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -95,28 +95,6 @@ String *convert_literal(String *num_param, String *type) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct type_mapping {
|
|
||||||
char *c_type, *lisp_type;
|
|
||||||
} type_mapping;
|
|
||||||
|
|
||||||
/* Need a way for the user to specify where a char is really a character
|
|
||||||
or where it is an 8-bit number. Similarly, is a char * a string pointer
|
|
||||||
or a pointer to an array of 8-bit numbers */
|
|
||||||
type_mapping type_mappings[]={
|
|
||||||
{ "void" , ":void" },
|
|
||||||
{ "char", ":char"},
|
|
||||||
{ "signed char", ":char" },
|
|
||||||
{ "unsigned char", ":unsigned-char" },
|
|
||||||
{ "short", ":short" },
|
|
||||||
{ "unsigned short", ":unsigned-short" },
|
|
||||||
{ "int", ":int" },
|
|
||||||
{ "unsigned int", ":unsigned-int" },
|
|
||||||
{ "unsigned long", ":unsigned-int" },
|
|
||||||
{ "float", ":float" },
|
|
||||||
{ "double", ":double" },
|
|
||||||
{ NULL, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
int count;
|
int count;
|
||||||
String **entries;
|
String **entries;
|
||||||
|
|
@ -147,52 +125,60 @@ void add_defined_foreign_type(String *type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
String *convert_type(SwigType *ty) {
|
String *get_ffi_type(SwigType *ty, String_or_char *name) {
|
||||||
SwigType *tr=SwigType_typedef_resolve_all(ty);
|
Hash *typemap = Swig_typemap_search("ffitype", ty, name, 0);
|
||||||
char *type_reduced=Char(tr);
|
if (typemap) {
|
||||||
int i;
|
String *typespec = Getattr(typemap, "code");
|
||||||
|
return NewString(typespec);
|
||||||
//Printf(stdout,"convert_type %s\n", ty);
|
|
||||||
if (SwigType_isconst(tr)) {
|
|
||||||
SwigType_pop(tr);
|
|
||||||
type_reduced=Char(tr);
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
SwigType *tr=SwigType_typedef_resolve_all(ty);
|
||||||
|
char *type_reduced=Char(tr);
|
||||||
|
int i;
|
||||||
|
|
||||||
|
//Printf(stdout,"convert_type %s\n", ty);
|
||||||
|
if (SwigType_isconst(tr)) {
|
||||||
|
SwigType_pop(tr);
|
||||||
|
type_reduced=Char(tr);
|
||||||
|
}
|
||||||
|
|
||||||
if (SwigType_ispointer(type_reduced) || SwigType_isarray(ty) ||
|
if (SwigType_ispointer(type_reduced) || SwigType_isarray(ty) ||
|
||||||
!strncmp(type_reduced, "p.f", 3)) {
|
!strncmp(type_reduced, "p.f", 3)) {
|
||||||
#if 1
|
#if 1
|
||||||
return NewString("(* :void)");
|
return NewString("(* :void)");
|
||||||
#else
|
#else
|
||||||
return NewString(":foreign-address");
|
return NewString(":foreign-address");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i=0; type_mappings[i].c_type; i++) {
|
for(i=0; i<defined_foreign_types.count; i++) {
|
||||||
if (!strcmp(type_reduced, type_mappings[i].c_type)) {
|
if (!Strcmp(ty, defined_foreign_types.entries[i])) {
|
||||||
return NewString(type_mappings[i].lisp_type);
|
return NewStringf("#.(%s \"%s\" :type :type)",
|
||||||
}
|
identifier_converter,
|
||||||
}
|
ty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for(i=0; i<defined_foreign_types.count; i++) {
|
if (!Strncmp(type_reduced, "enum ", 5)) {
|
||||||
if (!Strcmp(ty, defined_foreign_types.entries[i])) {
|
return NewString(":int");
|
||||||
return NewStringf("#.(%s \"%s\" :type :type)",
|
}
|
||||||
identifier_converter,
|
|
||||||
ty);
|
Printf(stderr, "Unsupported data type: %s (was: %s)\n", type_reduced, ty);
|
||||||
}
|
SWIG_exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Strncmp(type_reduced, "enum ", 5)) {
|
|
||||||
return NewString(":int");
|
|
||||||
}
|
|
||||||
|
|
||||||
Printf(stderr, "Unsupported data type: %s (was: %s)\n", type_reduced, ty);
|
|
||||||
SWIG_exit(EXIT_FAILURE);
|
|
||||||
|
|
||||||
return NewString("this should never happen");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String *get_lisp_type(SwigType *ty, String_or_char *name)
|
||||||
|
{
|
||||||
|
Hash *typemap = Swig_typemap_search("lisptype", ty, name, 0);
|
||||||
|
if (typemap) {
|
||||||
|
String *typespec = Getattr(typemap, "code");
|
||||||
|
return NewString(typespec);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return NewString("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ALLEGROCL :: main(int argc, char *argv[]) {
|
void ALLEGROCL :: main(int argc, char *argv[]) {
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -299,9 +285,10 @@ int ALLEGROCL :: functionWrapper(Node *n) {
|
||||||
varargs=1;
|
varargs=1;
|
||||||
} else {
|
} else {
|
||||||
for (p=pl; p; p=nextSibling(p), argnum++) {
|
for (p=pl; p; p=nextSibling(p), argnum++) {
|
||||||
String *argname=Getattr(p, "sym:name");
|
String *argname=Getattr(p, "name");
|
||||||
SwigType *argtype=Getattr(p, "type");
|
SwigType *argtype=Getattr(p, "type");
|
||||||
String *lisptype=convert_type(argtype);
|
String *ffitype=get_ffi_type(argtype, argname);
|
||||||
|
String *lisptype=get_lisp_type(argtype, argname);
|
||||||
int tempargname=0;
|
int tempargname=0;
|
||||||
|
|
||||||
if (!argname) {
|
if (!argname) {
|
||||||
|
|
@ -312,9 +299,10 @@ int ALLEGROCL :: functionWrapper(Node *n) {
|
||||||
if (!first) {
|
if (!first) {
|
||||||
Printf(f_cl, "\n ");
|
Printf(f_cl, "\n ");
|
||||||
}
|
}
|
||||||
Printf(f_cl, "(%s %s)", argname, lisptype);
|
Printf(f_cl, "(%s %s %s)", argname, ffitype, lisptype);
|
||||||
first=0;
|
first=0;
|
||||||
|
|
||||||
|
Delete(ffitype);
|
||||||
Delete(lisptype);
|
Delete(lisptype);
|
||||||
if (tempargname)
|
if (tempargname)
|
||||||
Delete(argname);
|
Delete(argname);
|
||||||
|
|
@ -322,8 +310,9 @@ int ALLEGROCL :: functionWrapper(Node *n) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Printf(f_cl, ")\n"); /* finish arg list */
|
Printf(f_cl, ")\n"); /* finish arg list */
|
||||||
Printf(f_cl, " :returning (%s)\n :strings-convert t\n :call-direct %s\n :optimize-for-space t)\n",
|
Printf(f_cl, " :returning (%s %s)\n :strings-convert t\n :call-direct %s\n :optimize-for-space t)\n",
|
||||||
convert_type(Getattr(n, "type")),
|
get_ffi_type(Getattr(n, "type"), "result"),
|
||||||
|
get_lisp_type(Getattr(n, "type"), "result"),
|
||||||
varargs ? "nil" : "t");
|
varargs ? "nil" : "t");
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -381,7 +370,7 @@ int ALLEGROCL :: classDeclaration(Node *n) {
|
||||||
|
|
||||||
|
|
||||||
/* Printf(stdout, "Converting %s in %s\n", type, name); */
|
/* Printf(stdout, "Converting %s in %s\n", type, name); */
|
||||||
lisp_type=convert_type(type);
|
lisp_type=get_ffi_type(type, Getattr(c, "sym:name"));
|
||||||
|
|
||||||
Printf(f_cl,
|
Printf(f_cl,
|
||||||
" (#.(%s \"%s\" :type :slot) %s)\n",
|
" (#.(%s \"%s\" :type :slot) %s)\n",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue