[Go] Remove all generated calls to _swig_makegostring, as it will no

longer as of Go 1.5.  In Go 1.5 or later user calls to
_swig_makegostring will fail at link time.

Instead, use goout and godirectorin typemaps to allocate strings in Go
code.

Change the Go typemaps support to ignore empty strings, so that we can
define empty strings for regular types so that %apply will override
the definitions for string types.

Fix the gccgo code to wrap SwigCgoCallback around all godirectorin
typemaps.

Add a few newlines after typemap code so that the typemaps don't have
to include them.
This commit is contained in:
Ian Lance Taylor 2015-02-05 10:15:37 -08:00
commit 0a021a938e
7 changed files with 261 additions and 50 deletions

View file

@ -4,6 +4,8 @@
* Go configuration module.
* ------------------------------------------------------------ */
%include <gostring.swg>
/* Code insertion directives */
#define %go_import(...) %insert(go_imports) %{__VA_ARGS__%}
@ -75,6 +77,22 @@
double
%{ $result = $1; %}
%typemap(goout) bool,
char,
signed char,
unsigned char,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
""
%typemap(out) const bool &,
const char &,
const signed char &,
@ -91,8 +109,26 @@
const double &
%{ $result = ($*1_ltype)*$1; %}
%typemap(goout) const bool &,
const char &,
const signed char &,
const unsigned char &,
const short &,
const unsigned short &,
const int &,
const unsigned int &,
const long &,
const unsigned long &,
const long long &,
const unsigned long long &,
const float &,
const double &
""
%typemap(out) void ""
%typemap(goout) void ""
%typemap(directorin) bool,
char,
signed char,
@ -109,6 +145,22 @@
double
%{ $input = ($1_ltype)$1; %}
%typemap(godirectorin) bool,
char,
signed char,
unsigned char,
short,
unsigned short,
int,
unsigned int,
long,
unsigned long,
long long,
unsigned long long,
float,
double
""
%typemap(directorin) const bool &,
const char &,
const signed char &,
@ -125,6 +177,22 @@
const double &
%{ $input = ($*1_ltype)$1; %}
%typemap(godirectorin) const bool &,
const char &,
const signed char &,
const unsigned char &,
const short &,
const unsigned short &,
const int &,
const unsigned int &,
const long &,
const unsigned long &,
const long long &,
const unsigned long long &,
const float &,
const double &
""
%typemap(directorout) bool,
char,
signed char,
@ -173,15 +241,23 @@
%typemap(out) size_t
%{ $result = $1; %}
%typemap(goout) size_t ""
%typemap(out) const size_t &
%{ $result = ($*1_ltype)*$1; %}
%typemap(goout) const size_t & ""
%typemap(directorin) size_t
%{ $input = (size_t)$1; %}
%typemap(godirectorin) size_t ""
%typemap(directorin) const size_t &
%{ $input = ($*1_ltype)$1; %}
%typemap(godirectorin) const size_t & ""
%typemap(directorout) size_t
%{ $result = ($1_ltype)$input; %}
@ -234,6 +310,8 @@
%typemap(directorin) SWIGTYPE (CLASS::*)
%{ $input = *($&1_ltype)$1; %}
%typemap(godirectorin) SWIGTYPE (CLASS::*) ""
%typemap(directorout) SWIGTYPE (CLASS::*)
%{
$result = new $1_ltype($input);
@ -253,9 +331,13 @@
%typemap(out) SWIGTYPE *
%{ *($&1_ltype)&$result = ($1_ltype)$1; %}
%typemap(goout) SWIGTYPE * ""
%typemap(directorin) SWIGTYPE *
%{ *($&1_ltype)&$input = ($1_ltype)$1; %}
%typemap(godirectorin) SWIGTYPE * ""
%typemap(directorout) SWIGTYPE *
%{ $result = *($&1_ltype)&$input; %}
@ -275,6 +357,8 @@
%typemap(out) SWIGTYPE *const&
%{ *($1_ltype)&$result = *$1; %}
%typemap(goout) SWIGTYPE *const& ""
/* References. */
/* Converting a C++ reference to Go has to be handled in the C++
@ -288,9 +372,13 @@
%typemap(out) SWIGTYPE &
%{ *($&1_ltype)&$result = $1; %}
%typemap(goout) SWIGTYPE & ""
%typemap(directorin) SWIGTYPE &
%{ $input = ($1_ltype)&$1; %}
%typemap(godirectorin) SWIGTYPE & ""
%typemap(directorout) SWIGTYPE &
%{ *($&1_ltype)&$result = $input; %}
@ -303,9 +391,13 @@
%typemap(out) SWIGTYPE &&
%{ *($&1_ltype)&$result = $1; %}
%typemap(goout) SWIGTYPE && ""
%typemap(directorin) SWIGTYPE &&
%{ $input = ($1_ltype)&$1_name; %}
%typemap(godirectorin) SWIGTYPE && ""
%typemap(directorout) SWIGTYPE &&
%{ *($&1_ltype)&$result = $input; %}
@ -321,9 +413,13 @@
%typemap(out) SWIGTYPE []
%{ *($&1_ltype)&$result = $1; %}
%typemap(goout) SWIGTYPE [] ""
%typemap(directorin) SWIGTYPE []
%{ $input = *($1_ltype)&$1; %}
%typemap(godirectorin) SWIGTYPE [] ""
%typemap(directorout) SWIGTYPE []
%{ *($&1_ltype)&$result = $input; %}
@ -349,18 +445,32 @@
%typemap(in) char *&, signed char *&, unsigned char *&
%{ $1 = ($1_ltype)$input.p; %}
%typemap(out)
%typemap(out,fragment="AllocateString")
char *, char *&, char[ANY], char[],
signed char *, signed char *&, signed char[ANY], signed char[],
unsigned char *, unsigned char *&, unsigned char[ANY], unsigned char[]
%{ $result = _swig_makegostring((char*)$1, $1 ? strlen((char*)$1) : 0); %}
%{ $result = Swig_AllocateString((char*)$1, $1 ? strlen((char*)$1) : 0); %}
%typemap(directorin)
%typemap(goout,fragment="CopyString")
char *, char *&, char[ANY], char[],
signed char *, signed char *&, signed char[ANY], signed char[],
unsigned char *, unsigned char *&, unsigned char[ANY], unsigned char[]
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString")
char *, char *&, char[ANY], char[],
signed char *, signed char *&, signed char[ANY], signed char[],
unsigned char *, unsigned char *&, unsigned char[ANY], unsigned char[]
%{
$input = _swig_makegostring((char*)$1, $1 ? strlen((char*)$1) : 0);
$input = Swig_AllocateString((char*)$1, $1 ? strlen((char*)$1) : 0);
%}
%typemap(godirectorin,fragment="CopyString")
char *, char *&, char[ANY], char[],
signed char *, signed char *&, signed char[ANY], signed char[],
unsigned char *, unsigned char *&, unsigned char[ANY], unsigned char[]
%{
$result = swigCopyString($input)
%}
%typemap(directorout)
@ -379,11 +489,17 @@
$2 = ($2_ltype)$input.n;
%}
%typemap(out) (char *STRING, size_t LENGTH)
%{ $result = _swig_makegostring((char*)$1, (size_t)$2); %}
%typemap(out,fragment="AllocateString") (char *STRING, size_t LENGTH)
%{ $result = Swig_AllocateString((char*)$1, (size_t)$2); %}
%typemap(directorin) (char *STRING, size_t LENGTH)
%{ $input = _swig_makegostring((char*)$1, $2); %}
%typemap(goout,fragment="CopyString") (char *STRING, size_t LENGTH)
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString") (char *STRING, size_t LENGTH)
%{ $input = Swig_AllocateString((char*)$1, $2); %}
%typemap(godirectorin,fragment="CopyString") (char *STRING, size_t LENGTH)
%{ $result = swigCopyString($input) %}
%typemap(directorout) (char *STRING, size_t LENGTH)
%{
@ -404,9 +520,13 @@
%typemap(out) enum SWIGTYPE
%{ $result = (intgo)$1; %}
%typemap(goout) enum SWIGTYPE ""
%typemap(directorin) enum SWIGTYPE
%{ $input = (intgo)$1; %}
%typemap(godirectorin) enum SWIGTYPE ""
%typemap(directorout) enum SWIGTYPE
%{ $result = ($1_ltype)$input; %}
@ -416,6 +536,8 @@
$input = &e;
%}
%typemap(godirectorin) enum SWIGTYPE & ""
%typemap(directorout) enum SWIGTYPE &
%{
$*1_ltype f = ($*1_ltype)*$input;
@ -449,9 +571,13 @@
}
#endif
%typemap(goout) SWIGTYPE ""
%typemap(directorin) SWIGTYPE
%{ $input = ($&1_ltype)&$1; %}
%typemap(godirectorin) SWIGTYPE ""
%typemap(directorout) SWIGTYPE
%{ $result = *($&1_ltype)$input; %}

29
Lib/go/gostring.swg Normal file
View file

@ -0,0 +1,29 @@
/* ------------------------------------------------------------
* gostring.swg
*
* Support for returning strings from C to Go.
* ------------------------------------------------------------ */
// C/C++ code to convert a memory buffer into a Go string allocated in
// C/C++ memory.
%fragment("AllocateString", "runtime") %{
static _gostring_ Swig_AllocateString(const char *p, size_t l) {
_gostring_ ret;
ret.p = (char*)malloc(l);
memcpy(ret.p, p, l);
ret.n = l;
return ret;
}
%}
// Go code to convert a string allocated in C++ memory to one
// allocated in Go memory.
%fragment("CopyString", "go_runtime") %{
type swig_gostring struct { p uintptr; n int }
func swigCopyString(s string) string {
p := *(*swig_gostring)(unsafe.Pointer(&s))
r := string((*[0x7fffffff]byte)(unsafe.Pointer(p.p))[:p.n])
Swig_free(p.p)
return r
}
%}

View file

@ -27,11 +27,17 @@ class string;
%typemap(directorout) string
%{ $result.assign($input.p, $input.n); %}
%typemap(out) string
%{ $result = _swig_makegostring($1.data(), $1.length()); %}
%typemap(out,fragment="AllocateString") string
%{ $result = Swig_AllocateString($1.data(), $1.length()); %}
%typemap(directorin) string
%{ $input = _swig_makegostring($1.data(), $1.length()); %}
%typemap(goout,fragment="CopyString") string
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString") string
%{ $input = Swig_AllocateString($1.data(), $1.length()); %}
%typemap(godirectorin,fragment="CopyString") string
%{ $result = swigCopyString($input) %}
%typemap(in) const string &
%{
@ -46,10 +52,16 @@ class string;
$result = &$1_str;
%}
%typemap(out) const string &
%{ $result = _swig_makegostring((*$1).data(), (*$1).length()); %}
%typemap(out,fragment="AllocateString") const string &
%{ $result = Swig_AllocateString((*$1).data(), (*$1).length()); %}
%typemap(directorin) const string &
%{ $input = _swig_makegostring($1.data(), $1.length()); %}
%typemap(goout,fragment="CopyString") const string &
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString") const string &
%{ $input = Swig_AllocateString($1.data(), $1.length()); %}
%typemap(godirectorin,fragment="CopyString") const string &
%{ $result = swigCopyString($input) %}
}

View file

@ -67,6 +67,8 @@ char * typemaps instead:
%typemap(out) TYPE *INPUT, TYPE &INPUT ""
%typemap(goout) TYPE *INPUT, TYPE &INPUT ""
%typemap(freearg) TYPE *INPUT, TYPE &INPUT ""
%typemap(argout) TYPE *INPUT, TYPE &INPUT ""
@ -167,6 +169,8 @@ char * typemaps instead:
%typemap(out) TYPE *OUTPUT, TYPE &OUTPUT ""
%typemap(goout) TYPE *INPUT, TYPE &INPUT ""
%typemap(freearg) TYPE *OUTPUT, TYPE &OUTPUT ""
%typemap(argout) TYPE *OUTPUT, TYPE &OUTPUT
@ -268,6 +272,8 @@ char * typemaps instead:
%typemap(out) TYPE *INOUT, TYPE &INOUT ""
%typemap(goout) TYPE *INOUT, TYPE &INOUT ""
%typemap(freearg) TYPE *INOUT, TYPE &INOUT ""
%typemap(argout) TYPE *INOUT, TYPE &INOUT ""