Discussion:
[Swig-user] Typemaps with function name?
kaby76
2017-07-01 13:24:35 UTC
Permalink
I am using SWIG to wrap the CUDA Driver API in C to C#. I was wondering, is
it possible to define a typemap specific for a function, taking into account
the function name? If so, how?

I can match based on type, or type with the parameter name. E.g.,

%include "typemaps.i"
%module Cuda
%typemap(cstype) char * "string"
%typemap(cstype) char * foo "foo"
typedef int CUresult;
CUresult cuFunc1(char *name, int bar);
CUresult cuFunc2(char * foo);
CUresult cuFunc3(int hi, char * foo);

The first typemap matches cuFunc1. The second typemap refines the pattern
match of "char*" with the addition of the parameter name for additional
context, so it matches cuFunc2 and cuFunc3. But, suppose I'd like to refine
the typemaps further to distinguish between cuFunc2 and cuFunc3. Is this
possible?

I tried adjacent parameter typemaps
(http://www.swig.org/Doc3.0/Typemaps.html#Typemaps_multi_argument_typemaps)
as a further refinement of the pattern, but the documentation is scant, and
they don’t seem to work for me. (It only shows an example with "in"-type
typemaps, not "cstype", "ctype", "imtype", etc.) For example, adding the
additional typemap,
%typemap(cstype) (int hi, char * foo) "sadfasdf"
Produces
public static int cuFunc3(sadfasdf hi, foo) { ////// NOTE MISSING TYPE ON
SECOND PARAMETER, FOO.
int ret = CudaPINVOKE.cuFunc3(hi, foo);
return ret;
}
I cannot get rewrite the second parameter and on.




--
View this message in context: http://swig.10945.n7.nabble.com/Typemaps-with-function-name-tp15195.html
Sent from the swig-user mailing list archive at Nabble.com.
William S Fulton
2017-07-01 21:25:29 UTC
Permalink
Post by kaby76
I am using SWIG to wrap the CUDA Driver API in C to C#. I was wondering, is
it possible to define a typemap specific for a function, taking into account
the function name? If so, how?
I can match based on type, or type with the parameter name. E.g.,
%include "typemaps.i"
%module Cuda
%typemap(cstype) char * "string"
%typemap(cstype) char * foo "foo"
typedef int CUresult;
CUresult cuFunc1(char *name, int bar);
CUresult cuFunc2(char * foo);
CUresult cuFunc3(int hi, char * foo);
The first typemap matches cuFunc1. The second typemap refines the pattern
match of "char*" with the addition of the parameter name for additional
context, so it matches cuFunc2 and cuFunc3. But, suppose I'd like to refine
the typemaps further to distinguish between cuFunc2 and cuFunc3. Is this
possible?
Not currently. It might be a nice enhancement to SWIG to qualify parameter
names in a non-standard C/C++ way to achieve this but it hasn't been
implemented. Such as:
%typemap(cstype) char *cuFunc1::name "string".

Currently you can use %clear in the appropriate place:

%typemap(cstype) char * foo "foo"
CUresult cuFunc2(char * foo);
%clear char *foo; // remove the above typemap for code from hereon
CUresult cuFunc3(int hi, char * foo);

or just provide a new typemap to override the initial one

%typemap(cstype) char * foo "foo"
CUresult cuFunc2(char * foo);
%typemap(cstype) char * foo "string" // new typemap to be used from hereon
CUresult cuFunc3(int hi, char * foo);


I tried adjacent parameter typemaps
Post by kaby76
(http://www.swig.org/Doc3.0/Typemaps.html#Typemaps_multi_argument_typemaps
)
as a further refinement of the pattern, but the documentation is scant, and
they don’t seem to work for me. (It only shows an example with "in"-type
typemaps, not "cstype", "ctype", "imtype", etc.) For example, adding the
additional typemap,
%typemap(cstype) (int hi, char * foo) "sadfasdf"
Produces
public static int cuFunc3(sadfasdf hi, foo) { ////// NOTE MISSING TYPE ON
SECOND PARAMETER, FOO.
int ret = CudaPINVOKE.cuFunc3(hi, foo);
return ret;
}
I cannot get rewrite the second parameter and on.
Using multi-argument typemaps is not the correct approach as they are
intended to combine 2 or more arguments in the C layer to 1 argument in the
target language (eg C#) layer.

William

Loading...