[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

@ -5,3 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 3.0.6 (in progress)
===========================
2015-02-05: ianlancetaylor
[Go] Ignore Go specific type maps (goin, goout, etc.) if they are empty.
2015-02-05: ianlancetaylor
[Go] Generated Go code no longer calls _swig_goallocate or
_swig_makegostring, as they will no longer work as of Go 1.5.

View file

@ -795,7 +795,8 @@ gotype is best converted to C/C++ using Go code.
<td>
Go code to convert from gotype to imtype when calling a C/C++
function. SWIG will then internally convert imtype to a C/C++ type
and pass it down. If this is not defined no conversion is done.
and pass it down. If this is not defined, or is the empty string, no
conversion is done.
</td>
</tr>
@ -822,7 +823,8 @@ to the desired type.
<td>goout</td>
<td>
Go code to convert a value returned from a C/C++ function from imtype
to gotype. If this is not defined no conversion is done.
to gotype. If this is not defined, or is the empty string, no
conversion is done.
</td>
</tr>
@ -843,7 +845,7 @@ be done.
Go code to adjust an argument value when returning from a function.
This is called after the real C/C++ function has run. The value will
be in imtype. This is only useful for a pointer type of some sort.
If this is not defined nothing will be done.
If this is not defined, or is the empty string, nothing will be done.
</td>
</tr>
@ -861,7 +863,8 @@ to the desired type.
<td>godirectorin</td>
<td>
Go code to convert a value used to call a director method from imtype
to gotype. If this is not defined no conversion is done.
to gotype. If this is not defined, or is the empty string, no
conversion is done.
</td>
</tr>
@ -869,7 +872,8 @@ to gotype. If this is not defined no conversion is done.
<td>godirectorout</td>
<td>
Go code to convert a value returned from a director method from gotype
to imtype. If this is not defined no conversion is done.
to imtype. If this is not defined, or is the empty string, no
conversion is done.
</td>
</tr>

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 ""

View file

@ -1010,7 +1010,7 @@ private:
receiver = NULL;
}
String *goout = Swig_typemap_lookup("goout", n, "swig_r", NULL);
String *goout = goTypemapLookup("goout", n, "swig_r");
bool add_to_interface = (interfaces && !is_constructor && !is_destructor && !is_static && !overname && checkFunctionVisibility(n, NULL));
@ -1028,10 +1028,10 @@ private:
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
String *ty = Getattr(p, "type");
if (Getattr(p, "tmap:goargout")) {
if (goGetattr(p, "tmap:goargout")) {
has_goout = true;
needs_wrapper = true;
} else if (goTypeIsInterface(p, ty) || Getattr(p, "tmap:goin")) {
} else if (goTypeIsInterface(p, ty) || goGetattr(p, "tmap:goin")) {
needs_wrapper = true;
}
@ -1303,7 +1303,7 @@ private:
SwigType *pt = Getattr(p, "type");
String *ln = Getattr(p, "lname");
String *goin = Getattr(p, "tmap:goin");
String *goin = goGetattr(p, "tmap:goin");
if (goin == NULL) {
Printv(call, ln, NULL);
if ((i == 0 && is_destructor) || ((i > 0 || !receiver || base || is_constructor) && goTypeIsInterface(p, pt))) {
@ -1353,7 +1353,7 @@ private:
Printv(f_go_wrappers, "\tvar swig_r_1 ", tm, "\n", NULL);
Replaceall(goout, "$input", "swig_r");
Replaceall(goout, "$result", "swig_r_1");
Printv(f_go_wrappers, goout, NULL);
Printv(f_go_wrappers, goout, "\n", NULL);
Printv(f_go_wrappers, "\treturn swig_r_1\n", NULL);
}
}
@ -1860,7 +1860,7 @@ private:
Parm *p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
String *tm = Getattr(p, "tmap:goargout");
String *tm = goGetattr(p, "tmap:goargout");
if (!tm) {
p = nextSibling(p);
} else {
@ -3533,7 +3533,7 @@ private:
String *goout = NULL;
if (SwigType_type(result) != T_VOID) {
Printv(f_go_wrappers, "\tvar swig_r ", goImType(n, result), "\n", NULL);
goout = Swig_typemap_lookup("goout", n, "swig_r", NULL);
goout = goTypemapLookup("goout", n, "swig_r");
if (goout) {
has_goout = true;
}
@ -3542,7 +3542,7 @@ private:
p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
if (Getattr(p, "tmap:goargout")) {
if (goGetattr(p, "tmap:goargout")) {
has_goout = true;
}
p = nextParm(p);
@ -3574,7 +3574,7 @@ private:
// This is an ordinary call from Go to C++, so adjust using
// the goin typemap.
String *goin = Getattr(p, "tmap:goin");
String *goin = goGetattr(p, "tmap:goin");
if (goin == NULL) {
Printv(call, ln, NULL);
if (goTypeIsInterface(p, pt)) {
@ -3617,7 +3617,7 @@ private:
Printv(f_go_wrappers, "\tvar swig_r_1 ", tm, "\n", NULL);
Replaceall(goout, "$input", "swig_r");
Replaceall(goout, "$result", "swig_r_1");
Printv(f_go_wrappers, goout, NULL);
Printv(f_go_wrappers, goout, "\n", NULL);
Printv(f_go_wrappers, "\treturn swig_r_1\n", NULL);
}
}
@ -3756,7 +3756,7 @@ private:
String *goout = NULL;
if (SwigType_type(result) != T_VOID) {
Printv(f_go_wrappers, "\tvar swig_r ", goImType(n, result), "\n", NULL);
goout = Swig_typemap_lookup("goout", n, "swig_r", NULL);
goout = goTypemapLookup("goout", n, "swig_r");
}
String *call = NewString("");
@ -3786,7 +3786,7 @@ private:
Printv(ln, ".Swigcptr()", NULL);
}
String *goin = Getattr(p, "tmap:goin");
String *goin = goGetattr(p, "tmap:goin");
if (goin == NULL) {
Printv(call, ln, NULL);
Setattr(p, "emit:goinput", ln);
@ -3828,7 +3828,7 @@ private:
Printv(f_go_wrappers, "\tvar swig_r_1 ", tm, "\n", NULL);
Replaceall(goout, "$input", "swig_r");
Replaceall(goout, "$result", "swig_r_1");
Printv(f_go_wrappers, goout, NULL);
Printv(f_go_wrappers, goout, "\n", NULL);
Printv(f_go_wrappers, "\treturn swig_r_1\n", NULL);
}
}
@ -3872,19 +3872,10 @@ private:
Printv(f_go_wrappers, result_wrapper, NULL);
}
Printv(f_go_wrappers, "\n", NULL);
goout = Swig_typemap_lookup("godirectorout", n, "swig_r", NULL);
goout = goTypemapLookup("godirectorout", n, "swig_r");
}
String *call = NewString("");
if (gccgo_flag) {
if (goout != NULL) {
Printv(call, "\tfunc() {\n", NULL);
}
Printv(call, "\tSwigCgocallBack()\n", NULL);
Printv(call, "\tdefer SwigCgocallBackDone()\n", NULL);
}
Printv(call, "\t", NULL);
if (SwigType_type(result) != T_VOID) {
@ -3895,6 +3886,8 @@ private:
}
Printv(call, "p.", go_with_over_name, "(", NULL);
String *goincode = NewString("");
p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
@ -3923,18 +3916,18 @@ private:
Printv(ln, ")", NULL);
}
String *goin = Getattr(p, "tmap:godirectorin");
String *goin = goGetattr(p, "tmap:godirectorin");
if (goin == NULL) {
Printv(call, ln, NULL);
} else {
String *ivar = NewString("");
Printf(ivar, "_swig_i_%d", i);
String *itm = goType(p, pt);
Printv(f_go_wrappers, "\tvar ", ivar, " ", itm, NULL);
Printv(f_go_wrappers, "\tvar ", ivar, " ", itm, "\n", NULL);
goin = Copy(goin);
Replaceall(goin, "$input", ln);
Replaceall(goin, "$result", ivar);
Printv(f_go_wrappers, goin, NULL);
Printv(goincode, goin, "\n", NULL);
Delete(goin);
Printv(call, ivar, NULL);
Delete(ivar);
@ -3952,13 +3945,22 @@ private:
}
Printv(call, "\n", NULL);
if (gccgo_flag && goout != NULL) {
Printv(call, "\t}()\n", NULL);
if (gccgo_flag) {
if (goout != NULL) {
Printv(f_go_wrappers, "\tfunc() {\n", NULL);
}
Printv(f_go_wrappers, "\tSwigCgocallBack()\n", NULL);
Printv(f_go_wrappers, "\tdefer SwigCgocallBackDone()\n", NULL);
}
Printv(f_go_wrappers, goincode, NULL);
Printv(f_go_wrappers, call, NULL);
Delete(call);
if (gccgo_flag && goout != NULL) {
Printv(f_go_wrappers, "\t}()\n", NULL);
}
if (SwigType_type(result) != T_VOID) {
if (goout == NULL) {
Printv(f_go_wrappers, "\treturn swig_r\n", NULL);
@ -3967,7 +3969,7 @@ private:
Printv(f_go_wrappers, "\tvar swig_r_1 ", tm, "\n", NULL);
Replaceall(goout, "$input", "swig_r");
Replaceall(goout, "$result", "swig_r_1");
Printv(f_go_wrappers, goout, NULL);
Printv(f_go_wrappers, goout, "\n", NULL);
Printv(f_go_wrappers, "\treturn swig_r_1\n", NULL);
}
}
@ -5549,6 +5551,32 @@ private:
return storage && Strcmp(storage, "friend") == 0;
}
/* ----------------------------------------------------------------------
* goGetattr
*
* Fetch an attribute from a node but return NULL if it is the empty string.
* ---------------------------------------------------------------------- */
Node *goGetattr(Node *n, const char *name) {
Node *ret = Getattr(n, name);
if (ret != NULL && Len(ret) == 0) {
ret = NULL;
}
return ret;
}
/* ----------------------------------------------------------------------
* goTypemapLookup
*
* Look up a typemap but return NULL if it is the empty string.
* ---------------------------------------------------------------------- */
String *goTypemapLookup(const char *name, Node *node, const char *lname) {
String *ret = Swig_typemap_lookup(name, node, lname, NULL);
if (ret != NULL && Len(ret) == 0) {
ret = NULL;
}
return ret;
}
}; /* class GO */
/* -----------------------------------------------------------------------------