Typemaps with function name?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Typemaps with function name?

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?

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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Typemaps with function name?

William S Fulton


On 1 July 2017 at 14:24, kaby76 <[hidden email]> wrote:
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
(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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...