swig -go: treat non-const references as pointers

Also clean up the handling of int* and int& to convert between the C
type int and the Go type int, which are often different sizes.

Fixes #2210
This commit is contained in:
Ian Lance Taylor 2022-03-05 21:57:36 -08:00
commit 27bdbc1f05
4 changed files with 115 additions and 19 deletions

View file

@ -453,9 +453,12 @@
%}
%typemap(freearg)
char *, char *&, char[ANY], char[]
char *, char[ANY], char[]
%{ free($1); %}
%typemap(freearg) char *&
%{ free(*$1); %}
%typemap(out,fragment="AllocateString")
char *, char *&, char[ANY], char[]
%{ $result = Swig_AllocateString((char*)$1, $1 ? strlen((char*)$1) : 0); %}
@ -520,6 +523,44 @@
$2 = ($2_ltype)$input.n;
%}
/* The int & type needs to convert to intgo. */
%typemap(gotype) int & "*int"
%typemap(in) int & (int e)
%{
e = (int)*$input;
$1 = &e;
%}
%typemap(out) int &
%{ $result = new intgo(*$1); %}
%typemap(argout) int &
%{ *$input = (intgo)e$argnum; %}
%typemap(goout) int & ""
%typemap(directorin) int & (intgo e)
%{
e = (intgo)$1;
$input = &e;
%}
%typemap(godirectorin) int & ""
%typemap(directorout) int &
%{
$*1_ltype f = ($*1_ltype)*$input;
$result = ($1_ltype)&f;
%}
%typemap(directorargout) int &
%{ $1 = (int)*$input; %}
%typemap(argout) const int & ""
%typemap(directorargout) const int & ""
/* Enums. We can't do the right thing for enums in typemap(gotype) so
we deliberately don't define them. The right thing would be to
capitalize the name. This is instead done in go.cxx. */
@ -552,10 +593,7 @@
%typemap(godirectorin) enum SWIGTYPE & ""
%typemap(directorout) enum SWIGTYPE &
%{
$*1_ltype f = ($*1_ltype)*$input;
$result = ($1_ltype)&f;
%}
%{ $result = $input; %}
/* Arbitrary type. This is a type passed by value in the C/C++ code.
We convert it to a pointer for the Go code. Note that all basic