blob: f9092134a63e439a9668fad21c566700f6e8775e [file] [log] [blame]
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* go.cxx
*
* Go language module for SWIG.
* ----------------------------------------------------------------------------- */
#include "swigmod.h"
#include "cparse.h"
#include <ctype.h>
/* ----------------------------------------------------------------------
* siphash()
*
* 64-bit SipHash-2-4 to generate unique id for each module
* ---------------------------------------------------------------------- */
// An unsigned 64-bit integer that works on a 32-bit host.
typedef struct {
// Assume unsigned long is at least 32 bits.
unsigned long hi;
unsigned long lo;
} swig_uint64;
// Rotate v left by bits, which must be <= 32.
static inline void _rotl(swig_uint64 *v, int bits) {
assert(bits <= 32);
unsigned long tmp = v->hi;
if (bits == 32) {
v->hi = v->lo;
v->lo = tmp;
} else {
v->hi = (tmp << bits) | ((0xfffffffful & v->lo) >> (32 - bits));
v->lo = (v->lo << bits) | ((0xfffffffful & tmp) >> (32 - bits));
}
}
// dst ^= src
static inline void _xor(swig_uint64 *dst, swig_uint64 *src) {
dst->lo ^= src->lo;
dst->hi ^= src->hi;
}
// dst += src
static inline void _add(swig_uint64 *dst, swig_uint64 *src) {
dst->lo += src->lo;
dst->hi += src->hi + ((dst->lo & 0xfffffffful) < (src->lo&0xfffffffful) ? 1 : 0);
}
#define SIPROUND \
do { \
_add(&v0, &v1); _rotl(&v1, 13); _xor(&v1, &v0); _rotl(&v0, 32); \
_add(&v2, &v3); _rotl(&v3, 16); _xor(&v3, &v2); \
_add(&v0, &v3); _rotl(&v3, 21); _xor(&v3, &v0); \
_add(&v2, &v1); _rotl(&v1, 17); _xor(&v1, &v2); _rotl(&v2, 32); \
} while(0)
// Set out to the hash of inc/inlen.
static void siphash(swig_uint64 *out, const char *inc, unsigned long inlen) {
/* "somepseudorandomlygeneratedbytes" */
swig_uint64 v0 = {0x736f6d65UL, 0x70736575UL};
swig_uint64 v1 = {0x646f7261UL, 0x6e646f6dUL};
swig_uint64 v2 = {0x6c796765UL, 0x6e657261UL};
swig_uint64 v3 = {0x74656462UL, 0x79746573UL};
swig_uint64 b;
/* hard-coded k. */
swig_uint64 k0 = {0x07060504UL, 0x03020100UL};
swig_uint64 k1 = {0x0F0E0D0CUL, 0x0B0A0908UL};
int i;
const int cROUNDS = 2, dROUNDS = 4;
const unsigned char *in = (const unsigned char *)inc;
const unsigned char *end = in + inlen - (inlen % 8);
int left = inlen & 7;
_xor(&v3, &k1); _xor(&v2, &k0); _xor(&v1, &k1); _xor(&v0, &k0);
for (; in != end; in += 8) {
b.hi = 0; b.lo = 0;
for (i = 0; i < 4; i++) {
b.lo |= ((unsigned long)in[i]) << (8*i);
}
for (i = 0; i < 4; i++) {
b.hi |= ((unsigned long)in[i+4]) << (8*i);
}
_xor(&v3, &b);
for (i = 0; i < cROUNDS; i++) {
SIPROUND;
}
_xor(&v0, &b);
}
b.hi = (inlen & 0xff)<<24; b.lo = 0;
for (; left; left--) {
if (left > 4) {
b.hi |= ((unsigned long)in[left-1]) << (8*left-8-32);
} else {
b.lo |= ((unsigned long)in[left-1]) << (8*left-8);
}
}
_xor(&v3, &b);
for(i=0; i<cROUNDS; i++) {
SIPROUND;
}
_xor(&v0, &b); v2.lo ^= 0xff;
for(i=0; i<dROUNDS; i++) {
SIPROUND;
}
out->lo = 0; out->hi = 0;
_xor(out, &v0); _xor(out, &v1); _xor(out, &v2); _xor(out, &v3);
}
#undef SIPROUND
class GO:public Language {
static const char *const usage;
// Go package name.
String *package;
// SWIG module name.
String *module;
// Flag for generating cgo input files.
bool cgo_flag;
// Flag for generating gccgo output.
bool gccgo_flag;
// Prefix to use with gccgo.
String *go_prefix;
// -fgo-prefix option.
String *prefix_option;
// -fgo-pkgpath option.
String *pkgpath_option;
// Prefix for translating %import directive to import statements.
String *import_prefix;
// Whether to use a shared library.
bool use_shlib;
// Name of shared library to import.
String *soname;
// Size in bits of the Go type "int". 0 if not specified.
int intgo_type_size;
/* Output files */
File *f_c_begin;
File *f_go_begin;
File *f_gc_begin;
/* Output fragments */
File *f_c_runtime;
File *f_c_header;
File *f_c_wrappers;
File *f_c_init;
File *f_c_directors;
File *f_c_directors_h;
File *f_go_imports;
File *f_go_runtime;
File *f_go_header;
File *f_go_wrappers;
File *f_go_directors;
File *f_gc_runtime;
File *f_gc_header;
File *f_gc_wrappers;
File *f_cgo_comment;
File *f_cgo_comment_typedefs;
// True if we imported a module.
bool saw_import;
// If not NULL, name of import package being processed.
String *imported_package;
// Build interface methods while handling a class. This is only
// non-NULL when we are handling methods.
String *interfaces;
// The class node while handling a class. This is only non-NULL
// when we are handling methods.
Node *class_node;
// The class name while handling a class. This is only non-NULL
// when we are handling methods. This is the name of the class as
// SWIG sees it.
String *class_name;
// The receiver name while handling a class. This is only non-NULL
// when we are handling methods. This is the name of the class
// as run through goCPointerType.
String *class_receiver;
// A hash table of method names that we have seen when processing a
// class. This lets us detect base class methods that we don't want
// to use.
Hash *class_methods;
// True when we are generating the wrapper functions for a variable.
bool making_variable_wrappers;
// True when working with a static member function.
bool is_static_member_function;
// A hash table of enum types that we have seen but which may not have
// been defined. The index is a SwigType.
Hash *undefined_enum_types;
// A hash table of types that we have seen but which may not have
// been defined. The index is a SwigType.
Hash *undefined_types;
// A hash table of classes which were defined. The index is a Go
// type name.
Hash *defined_types;
// A hash table of all the go_imports already imported. The index is a full
// import name e.g. '"runtime"' or '_ "runtime/cgo"' or 'sc "syscall"'.
Hash *go_imports;
// A unique ID used to make public symbols unique.
String *unique_id;
public:
GO():package(NULL),
module(NULL),
cgo_flag(true),
gccgo_flag(false),
go_prefix(NULL),
prefix_option(NULL),
pkgpath_option(NULL),
import_prefix(NULL),
use_shlib(false),
soname(NULL),
intgo_type_size(0),
f_c_begin(NULL),
f_go_begin(NULL),
f_gc_begin(NULL),
f_c_runtime(NULL),
f_c_header(NULL),
f_c_wrappers(NULL),
f_c_init(NULL),
f_c_directors(NULL),
f_c_directors_h(NULL),
f_go_imports(NULL),
f_go_runtime(NULL),
f_go_header(NULL),
f_go_wrappers(NULL),
f_go_directors(NULL),
f_gc_runtime(NULL),
f_gc_header(NULL),
f_gc_wrappers(NULL),
f_cgo_comment(NULL),
f_cgo_comment_typedefs(NULL),
saw_import(false),
imported_package(NULL),
interfaces(NULL),
class_node(NULL),
class_name(NULL),
class_receiver(NULL),
class_methods(NULL),
making_variable_wrappers(false),
is_static_member_function(false),
undefined_enum_types(NULL),
undefined_types(NULL),
defined_types(NULL),
go_imports(NULL),
unique_id(NULL) {
director_multiple_inheritance = 1;
director_language = 1;
director_prot_ctor_code = NewString("_swig_gopanic(\"accessing abstract class or protected constructor\");");
}
private:
/* ------------------------------------------------------------
* main()
* ------------------------------------------------------------ */
virtual void main(int argc, char *argv[]) {
SWIG_library_directory("go");
bool display_help = false;
// Process command line options.
for (int i = 1; i < argc; i++) {
if (argv[i]) {
if (strcmp(argv[i], "-package") == 0) {
if (argv[i + 1]) {
package = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-cgo") == 0) {
Swig_mark_arg(i);
cgo_flag = true;
} else if (strcmp(argv[i], "-no-cgo") == 0) {
Swig_mark_arg(i);
cgo_flag = false;
} else if (strcmp(argv[i], "-gccgo") == 0) {
Swig_mark_arg(i);
gccgo_flag = true;
} else if (strcmp(argv[i], "-go-prefix") == 0) {
if (argv[i + 1]) {
prefix_option = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-go-pkgpath") == 0) {
if (argv[i + 1]) {
pkgpath_option = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-import-prefix") == 0) {
if (argv[i + 1]) {
import_prefix = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-use-shlib") == 0) {
Swig_mark_arg(i);
use_shlib = true;
} else if (strcmp(argv[i], "-soname") == 0) {
if (argv[i + 1]) {
soname = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-longsize") == 0) {
// Ignore for backward compatibility.
if (argv[i + 1]) {
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
++i;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-intgosize") == 0) {
if (argv[i + 1]) {
intgo_type_size = atoi(argv[i + 1]);
if (intgo_type_size != 32 && intgo_type_size != 64) {
Printf(stderr, "-intgosize not 32 or 64\n");
Swig_arg_error();
}
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
++i;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-help") == 0) {
display_help = true;
Printf(stdout, "%s\n", usage);
}
}
}
if (gccgo_flag && !pkgpath_option && !prefix_option) {
prefix_option = NewString("go");
}
// Add preprocessor symbol to parser.
Preprocessor_define("SWIGGO 1", 0);
if (cgo_flag) {
Preprocessor_define("SWIGGO_CGO 1", 0);
}
if (gccgo_flag) {
Preprocessor_define("SWIGGO_GCCGO 1", 0);
}
// This test may be removed in the future, when we can assume that
// everybody has upgraded to Go 1.1. The code below is prepared
// for this test to simply be taken out.
if (intgo_type_size == 0 && !display_help) {
Printf(stderr, "SWIG -go: -intgosize option required but not specified\n");
SWIG_exit(EXIT_FAILURE);
}
if (intgo_type_size == 32) {
Preprocessor_define("SWIGGO_INTGO_SIZE 32", 0);
} else if (intgo_type_size == 64) {
Preprocessor_define("SWIGGO_INTGO_SIZE 64", 0);
} else {
Preprocessor_define("SWIGGO_INTGO_SIZE 0", 0);
}
// Add typemap definitions.
SWIG_typemap_lang("go");
SWIG_config_file("go.swg");
allow_overloading();
}
/* ---------------------------------------------------------------------
* top()
*
* For 6g/8g, we are going to create the following files:
*
* 1) A .c or .cxx file compiled with gcc. This file will contain
* function wrappers. Each wrapper will take a pointer to a
* struct holding the arguments, unpack them, and call the real
* function.
*
* 2) A .go file which defines the Go form of all types, and which
* defines Go function wrappers. Each wrapper will call the C
* function wrapper in the second file.
*
* 3) A .c file compiled with 6c/8c. This file will define
* Go-callable C function wrappers. Each wrapper will use
* cgocall to call the function wrappers in the first file.
*
* When generating code for gccgo, we don't need the third file, and
* the function wrappers in the first file have a different form.
*
* --------------------------------------------------------------------- */
virtual int top(Node *n) {
Node *optionsnode = Getattr(Getattr(n, "module"), "options");
if (optionsnode) {
if (Getattr(optionsnode, "directors")) {
allow_directors();
}
if (Getattr(optionsnode, "dirprot")) {
allow_dirprot();
}
allow_allprotected(GetFlag(optionsnode, "allprotected"));
}
module = Getattr(n, "name");
if (!package) {
package = Copy(module);
}
if (!soname && use_shlib) {
soname = Copy(package);
Append(soname, ".so");
}
if (gccgo_flag) {
String *pref;
if (pkgpath_option) {
pref = pkgpath_option;
} else {
pref = prefix_option;
}
go_prefix = NewString("");
for (char *p = Char(pref); *p != '\0'; p++) {
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || (*p >= '0' && *p <= '9') || *p == '.' || *p == '$') {
Putc(*p, go_prefix);
} else {
Putc('_', go_prefix);
}
}
if (!pkgpath_option) {
Append(go_prefix, ".");
Append(go_prefix, getModuleName(package));
}
}
// Get filenames.
String *swig_filename = Getattr(n, "infile");
String *c_filename = Getattr(n, "outfile");
String *c_filename_h = Getattr(n, "outfile_h");
String *go_filename = NewString("");
Printf(go_filename, "%s%s.go", SWIG_output_directory(), module);
String *gc_filename = NULL;
if (!gccgo_flag) {
gc_filename = NewString("");
Printf(gc_filename, "%s%s_gc.c", SWIG_output_directory(), module);
}
// Generate a unique ID based on a hash of the SWIG input.
swig_uint64 hash = {0, 0};
FILE *swig_input = Swig_open(swig_filename);
if (swig_input == NULL) {
FileErrorDisplay(swig_filename);
SWIG_exit(EXIT_FAILURE);
}
String *swig_input_content = Swig_read_file(swig_input);
siphash(&hash, Char(swig_input_content), Len(swig_input_content));
Delete(swig_input_content);
fclose(swig_input);
unique_id = NewString("");
Printf(unique_id, "_%s_%08x%08x", getModuleName(package), hash.hi, hash.lo);
// Open files.
f_c_begin = NewFile(c_filename, "w", SWIG_output_files());
if (!f_c_begin) {
FileErrorDisplay(c_filename);
SWIG_exit(EXIT_FAILURE);
}
if (directorsEnabled()) {
if (!c_filename_h) {
Printf(stderr, "Unable to determine outfile_h\n");
SWIG_exit(EXIT_FAILURE);
}
f_c_directors_h = NewFile(c_filename_h, "w", SWIG_output_files());
if (!f_c_directors_h) {
FileErrorDisplay(c_filename_h);
SWIG_exit(EXIT_FAILURE);
}
}
f_go_begin = NewFile(go_filename, "w", SWIG_output_files());
if (!f_go_begin) {
FileErrorDisplay(go_filename);
SWIG_exit(EXIT_FAILURE);
}
if (!gccgo_flag && !cgo_flag) {
f_gc_begin = NewFile(gc_filename, "w", SWIG_output_files());
if (!f_gc_begin) {
FileErrorDisplay(gc_filename);
SWIG_exit(EXIT_FAILURE);
}
}
f_c_runtime = NewString("");
f_c_header = NewString("");
f_c_wrappers = NewString("");
f_c_init = NewString("");
f_c_directors = NewString("");
f_go_imports = NewString("");
f_go_runtime = NewString("");
f_go_header = NewString("");
f_go_wrappers = NewString("");
f_go_directors = NewString("");
if (!gccgo_flag && !cgo_flag) {
f_gc_runtime = NewString("");
f_gc_header = NewString("");
f_gc_wrappers = NewString("");
}
if (cgo_flag) {
f_cgo_comment = NewString("");
f_cgo_comment_typedefs = NewString("");
}
Swig_register_filebyname("begin", f_c_begin);
Swig_register_filebyname("runtime", f_c_runtime);
Swig_register_filebyname("header", f_c_header);
Swig_register_filebyname("wrapper", f_c_wrappers);
Swig_register_filebyname("init", f_c_init);
Swig_register_filebyname("director", f_c_directors);
Swig_register_filebyname("director_h", f_c_directors_h);
Swig_register_filebyname("go_begin", f_go_begin);
Swig_register_filebyname("go_imports", f_go_imports);
Swig_register_filebyname("go_runtime", f_go_runtime);
Swig_register_filebyname("go_header", f_go_header);
Swig_register_filebyname("go_wrapper", f_go_wrappers);
Swig_register_filebyname("go_director", f_go_directors);
if (!gccgo_flag && !cgo_flag) {
Swig_register_filebyname("gc_begin", f_gc_begin);
Swig_register_filebyname("gc_runtime", f_gc_runtime);
Swig_register_filebyname("gc_header", f_gc_header);
Swig_register_filebyname("gc_wrapper", f_gc_wrappers);
}
if (cgo_flag) {
Swig_register_filebyname("cgo_comment", f_cgo_comment);
Swig_register_filebyname("cgo_comment_typedefs", f_cgo_comment_typedefs);
}
Swig_banner(f_c_begin);
if (CPlusPlus) {
Printf(f_c_begin, "\n// source: %s\n\n", swig_filename);
} else {
Printf(f_c_begin, "\n/* source: %s */\n\n", swig_filename);
}
Printf(f_c_runtime, "#define SWIGMODULE %s\n", module);
if (gccgo_flag) {
Printf(f_c_runtime, "#define SWIGGO_PREFIX %s\n", go_prefix);
}
if (directorsEnabled()) {
Printf(f_c_runtime, "#define SWIG_DIRECTORS\n");
Swig_banner(f_c_directors_h);
Printf(f_c_directors_h, "\n// source: %s\n\n", swig_filename);
Printf(f_c_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module);
Printf(f_c_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module);
Printf(f_c_directors_h, "class Swig_memory;\n\n");
Printf(f_c_directors, "\n// C++ director class methods.\n");
String *filename = Swig_file_filename(c_filename_h);
Printf(f_c_directors, "#include \"%s\"\n\n", filename);
Delete(filename);
}
Swig_banner(f_go_begin);
Printf(f_go_begin, "\n// source: %s\n", swig_filename);
if (!gccgo_flag && !cgo_flag && soname) {
Swig_banner(f_gc_begin);
Printf(f_gc_begin, "\n/* source: %s */\n\n", swig_filename);
Printf(f_gc_begin, "\n/* This file should be compiled with 6c/8c. */\n");
Printf(f_gc_begin, "#pragma dynimport _ _ \"%s\"\n", soname);
}
if (cgo_flag) {
Printv(f_cgo_comment_typedefs, "/*\n", NULL);
// The cgo program defines the intgo type after our function
// definitions, but we want those definitions to be able to use
// intgo also.
Printv(f_cgo_comment_typedefs, "#define intgo swig_intgo\n", NULL);
Printv(f_cgo_comment_typedefs, "typedef void *swig_voidp;\n", NULL);
}
// Output module initialization code.
Printf(f_go_begin, "\npackage %s\n\n", getModuleName(package));
if (gccgo_flag && !cgo_flag) {
Printf(f_go_runtime, "func SwigCgocall()\n");
Printf(f_go_runtime, "func SwigCgocallDone()\n");
Printf(f_go_runtime, "func SwigCgocallBack()\n");
Printf(f_go_runtime, "func SwigCgocallBackDone()\n\n");
}
// All the C++ wrappers should be extern "C".
Printv(f_c_wrappers, "#ifdef __cplusplus\n", "extern \"C\" {\n", "#endif\n\n", NULL);
// Set up the hash table for types not defined by SWIG.
undefined_enum_types = NewHash();
undefined_types = NewHash();
defined_types = NewHash();
go_imports = NewHash();
// Emit code.
Language::top(n);
if (directorsEnabled()) {
// Insert director runtime into the f_runtime file (make it occur before %header section)
Swig_insert_file("director_common.swg", f_c_runtime);
Swig_insert_file("director.swg", f_c_runtime);
}
Delete(go_imports);
// Write out definitions for the types not defined by SWIG.
if (Len(undefined_enum_types) > 0)
Printv(f_go_wrappers, "\n", NULL);
for (Iterator p = First(undefined_enum_types); p.key; p = Next(p)) {
String *name = p.item;
Printv(f_go_wrappers, "type ", name, " int\n", NULL);
}
Printv(f_go_wrappers, "\n", NULL);
for (Iterator p = First(undefined_types); p.key; p = Next(p)) {
String *ty = goType(NULL, p.key);
if (!Getattr(defined_types, ty)) {
String *cp = goCPointerType(p.key, false);
if (!Getattr(defined_types, cp)) {
Printv(f_go_wrappers, "type ", cp, " uintptr\n", NULL);
Printv(f_go_wrappers, "type ", ty, " interface {\n", NULL);
Printv(f_go_wrappers, "\tSwigcptr() uintptr;\n", NULL);
Printv(f_go_wrappers, "}\n", NULL);
Printv(f_go_wrappers, "func (p ", cp, ") Swigcptr() uintptr {\n", NULL);
Printv(f_go_wrappers, "\treturn uintptr(p)\n", NULL);
Printv(f_go_wrappers, "}\n\n", NULL);
}
Delete(cp);
}
Delete(ty);
}
Delete(undefined_enum_types);
Delete(undefined_types);
Delete(defined_types);
/* Write and cleanup */
Dump(f_c_header, f_c_runtime);
if (directorsEnabled()) {
Printf(f_c_directors_h, "#endif\n");
Delete(f_c_directors_h);
f_c_directors_h = NULL;
Dump(f_c_directors, f_c_runtime);
Delete(f_c_directors);
f_c_directors = NULL;
}
// End the extern "C".
Printv(f_c_wrappers, "#ifdef __cplusplus\n", "}\n", "#endif\n\n", NULL);
if (cgo_flag) {
// End the cgo comment.
Printv(f_cgo_comment, "#undef intgo\n", NULL);
Printv(f_cgo_comment, "*/\n", NULL);
Printv(f_cgo_comment, "import \"C\"\n", NULL);
Printv(f_cgo_comment, "\n", NULL);
}
Dump(f_c_runtime, f_c_begin);
Dump(f_c_wrappers, f_c_begin);
Dump(f_c_init, f_c_begin);
if (cgo_flag) {
Dump(f_cgo_comment_typedefs, f_go_begin);
Dump(f_cgo_comment, f_go_begin);
}
Dump(f_go_imports, f_go_begin);
Dump(f_go_header, f_go_begin);
Dump(f_go_runtime, f_go_begin);
Dump(f_go_wrappers, f_go_begin);
if (directorsEnabled()) {
Dump(f_go_directors, f_go_begin);
}
if (!gccgo_flag && !cgo_flag) {
Dump(f_gc_header, f_gc_begin);
Dump(f_gc_runtime, f_gc_begin);
Dump(f_gc_wrappers, f_gc_begin);
}
Delete(f_c_runtime);
Delete(f_c_header);
Delete(f_c_wrappers);
Delete(f_c_init);
Delete(f_go_imports);
Delete(f_go_runtime);
Delete(f_go_header);
Delete(f_go_wrappers);
Delete(f_go_directors);
if (!gccgo_flag && !cgo_flag) {
Delete(f_gc_runtime);
Delete(f_gc_header);
Delete(f_gc_wrappers);
}
if (cgo_flag) {
Delete(f_cgo_comment);
Delete(f_cgo_comment_typedefs);
}
Delete(f_c_begin);
Delete(f_go_begin);
if (!gccgo_flag && !cgo_flag) {
Delete(f_gc_begin);
}
return SWIG_OK;
}
/* ------------------------------------------------------------
* importDirective()
*
* Handle a SWIG import statement by generating a Go import
* statement.
* ------------------------------------------------------------ */
virtual int importDirective(Node *n) {
String *hold_import = imported_package;
String *modname = Getattr(n, "module");
if (modname) {
if (!Getattr(go_imports, modname)) {
Setattr(go_imports, modname, modname);
Printv(f_go_imports, "import \"", NULL);
if (import_prefix) {
Printv(f_go_imports, import_prefix, "/", NULL);
}
Printv(f_go_imports, modname, "\"\n", NULL);
}
imported_package = modname;
saw_import = true;
}
int r = Language::importDirective(n);
imported_package = hold_import;
return r;
}
/* ----------------------------------------------------------------------
* Language::insertDirective()
*
* If the section is go_imports, store them for later.
* ---------------------------------------------------------------------- */
virtual int insertDirective(Node *n) {
char *section = Char(Getattr(n, "section"));
if ((ImportMode && !Getattr(n, "generated")) ||
!section || (strcmp(section, "go_imports") != 0)) {
return Language::insertDirective(n);
}
char *code = Char(Getattr(n, "code"));
char *pch = strtok(code, ",");
while (pch != NULL) {
// Do not import same thing more than once.
if (!Getattr(go_imports, pch)) {
Setattr(go_imports, pch, pch);
Printv(f_go_imports, "import ", pch, "\n", NULL);
}
pch = strtok(NULL, ",");
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* functionWrapper()
*
* Implement a function.
* ---------------------------------------------------------------------- */
virtual int functionWrapper(Node *n) {
if (GetFlag(n, "feature:ignore")) {
return SWIG_OK;
}
// We don't need explicit calls.
if (GetFlag(n, "explicitcall")) {
return SWIG_OK;
}
// Don't emit constructors for abstract director classes. They
// will never succeed anyhow.
if (Swig_methodclass(n) && Swig_directorclass(n)
&& Strcmp(Char(Getattr(n, "wrap:action")), director_prot_ctor_code) == 0) {
return SWIG_OK;
}
String *name = Getattr(n, "sym:name");
String *nodetype = Getattr(n, "nodeType");
bool is_static = is_static_member_function || isStatic(n);
bool is_friend = isFriend(n);
bool is_ctor_dtor = false;
SwigType *result = Getattr(n, "type");
// For some reason SWIG changs the "type" value during the call to
// functionWrapper. We need to remember the type for possible
// overload processing.
Setattr(n, "go:type", Copy(result));
String *go_name;
String *r1 = NULL;
if (making_variable_wrappers) {
// Change the name of the variable setter and getter functions
// to be more Go like.
bool is_set = Strcmp(Char(name) + Len(name) - 4, "_set") == 0;
assert(is_set || Strcmp(Char(name) + Len(name) - 4, "_get") == 0);
// Start with Set or Get.
go_name = NewString(is_set ? "Set" : "Get");
// If this is a static variable, put in the class name,
// capitalized.
if (is_static && class_name) {
String *ccn = exportedName(class_name);
Append(go_name, ccn);
Delete(ccn);
}
// Add the rest of the name, capitalized, dropping the _set or
// _get.
String *c1 = removeClassname(name);
String *c2 = exportedName(c1);
char *p = Char(c2);
int len = Len(p);
for (int i = 0; i < len - 4; ++i) {
Putc(p[i], go_name);
}
Delete(c2);
Delete(c1);
if (!checkIgnoredParameters(n, go_name)) {
Delete(go_name);
return SWIG_NOWRAP;
}
} else if (Cmp(nodetype, "constructor") == 0) {
is_ctor_dtor = true;
// Change the name of a constructor to be more Go like. Change
// new_ to New, and capitalize the class name.
assert(Strncmp(name, "new_", 4) == 0);
String *c1 = NewString(Char(name) + 4);
String *c2 = exportedName(c1);
go_name = NewString("New");
Append(go_name, c2);
Delete(c2);
Delete(c1);
if (Swig_methodclass(n) && Swig_directorclass(n)) {
// The core SWIG code skips the first parameter when
// generating the $nondirector_new string. Recreate the
// action in this case. But don't it if we are using the
// special code for an abstract class.
String *call = Swig_cppconstructor_call(getClassType(),
Getattr(n, "parms"));
SwigType *type = Copy(getClassType());
SwigType_add_pointer(type);
String *cres = Swig_cresult(type, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
}
} else if (Cmp(nodetype, "destructor") == 0) {
// No need to emit protected destructors.
if (!is_public(n)) {
return SWIG_OK;
}
is_ctor_dtor = true;
// Change the name of a destructor to be more Go like. Change
// delete_ to Delete and capitalize the class name.
assert(Strncmp(name, "delete_", 7) == 0);
String *c1 = NewString(Char(name) + 7);
String *c2 = exportedName(c1);
go_name = NewString("Delete");
Append(go_name, c2);
Delete(c2);
Delete(c1);
result = NewString("void");
r1 = result;
} else {
if (!checkFunctionVisibility(n, NULL)) {
return SWIG_OK;
}
go_name = buildGoName(name, is_static, is_friend);
if (!checkIgnoredParameters(n, go_name)) {
Delete(go_name);
return SWIG_NOWRAP;
}
}
String *overname = NULL;
if (Getattr(n, "sym:overloaded")) {
overname = Getattr(n, "sym:overname");
} else {
String *scope;
if (!class_name || is_static || is_ctor_dtor) {
scope = NULL;
} else {
scope = NewString("swiggoscope.");
Append(scope, class_name);
}
if (!checkNameConflict(go_name, n, scope)) {
Delete(go_name);
return SWIG_NOWRAP;
}
}
String *wname = Swig_name_wrapper(name);
if (overname) {
Append(wname, overname);
}
Append(wname, unique_id);
Setattr(n, "wrap:name", wname);
ParmList *parms = Getattr(n, "parms");
Setattr(n, "wrap:parms", parms);
int r = makeWrappers(n, name, go_name, overname, wname, NULL, parms, result, is_static);
if (r != SWIG_OK) {
return r;
}
if (Getattr(n, "sym:overloaded") && !Getattr(n, "sym:nextSibling")) {
String *scope ;
if (!class_name || is_static || is_ctor_dtor) {
scope = NULL;
} else {
scope = NewString("swiggoscope.");
Append(scope, class_name);
}
if (!checkNameConflict(go_name, n, scope)) {
Delete(go_name);
return SWIG_NOWRAP;
}
String *receiver = class_receiver;
if (is_static || is_ctor_dtor) {
receiver = NULL;
}
r = makeDispatchFunction(n, go_name, receiver, is_static, NULL, false);
if (r != SWIG_OK) {
return r;
}
}
Delete(wname);
Delete(go_name);
Delete(r1);
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* staticmemberfunctionHandler()
*
* For some reason the language code removes the "storage" attribute
* for a static function before calling functionWrapper, which means
* that we have no way of knowing whether a function is static or
* not. That makes no sense in the Go context. Here we note that a
* function is static.
* ---------------------------------------------------------------------- */
int staticmemberfunctionHandler(Node *n) {
assert(!is_static_member_function);
is_static_member_function = true;
int r = Language::staticmemberfunctionHandler(n);
is_static_member_function = false;
return r;
}
/* ----------------------------------------------------------------------
* makeWrappers()
*
* Write out the various function wrappers.
* n: The function we are emitting.
* name: The function name.
* go_name: The name of the function in Go.
* overname: The overload string for overloaded function.
* wname: The SWIG wrapped name--the name of the C function.
* base: A list of the names of base classes, in the case where this
* is a virtual method not defined in the current class.
* parms: The parameters.
* result: The result type.
* is_static: Whether this is a static method or member.
* ---------------------------------------------------------------------- */
int makeWrappers(Node *n, String *name, String *go_name, String *overname, String *wname, List *base, ParmList *parms, SwigType *result, bool is_static) {
assert(result);
int ret = SWIG_OK;
if (cgo_flag) {
int r = makeCgoWrappers(n, go_name, overname, wname, base, parms, result, is_static);
if (r != SWIG_OK) {
ret = r;
}
} else {
int r = goFunctionWrapper(n, name, go_name, overname, wname, base, parms, result, is_static);
if (r != SWIG_OK) {
ret = r;
}
if (!gccgo_flag) {
r = gcFunctionWrapper(wname);
if (r != SWIG_OK) {
ret = r;
}
r = gccFunctionWrapper(n, base, wname, parms, result);
if (r != SWIG_OK) {
ret = r;
}
} else {
r = gccgoFunctionWrapper(n, base, wname, parms, result);
if (r != SWIG_OK) {
ret = r;
}
}
}
if (class_methods) {
Setattr(class_methods, Getattr(n, "name"), NewString(""));
}
return ret;
}
/* ----------------------------------------------------------------------
* struct cgoWrapperInfo
*
* Information needed by the CGO wrapper functions.
* ---------------------------------------------------------------------- */
struct cgoWrapperInfo {
// The function we are generating code for.
Node *n;
// The name of the Go function.
String *go_name;
// The overload string for an overloaded function.
String *overname;
// The name of the C wrapper function.
String *wname;
// The base classes.
List *base;
// The parameters.
ParmList *parms;
// The result type.
SwigType *result;
// Whether this is a static function, not a class method.
bool is_static;
// The Go receiver type.
String *receiver;
// Whether this is a class constructor.
bool is_constructor;
// Whether this is a class destructor.
bool is_destructor;
};
/* ----------------------------------------------------------------------
* makeCgoWrappers()
*
* Write out the wrappers for a function when producing cgo input
* files.
* ---------------------------------------------------------------------- */
int makeCgoWrappers(Node *n, String *go_name, String *overname, String *wname, List *base, ParmList *parms, SwigType *result, bool is_static) {
Swig_save("makeCgoWrappers", n, "emit:cgotype", "emit:cgotypestruct", NULL);
cgoWrapperInfo info;
info.n = n;
info.go_name = go_name;
info.overname = overname;
info.wname = wname;
info.base = base;
info.parms = parms;
info.result = result;
info.is_static = is_static;
info.receiver = class_receiver;
if (is_static) {
info.receiver = NULL;
}
String *nodetype = Getattr(n, "nodeType");
info.is_constructor = Cmp(nodetype, "constructor") == 0;
info.is_destructor = Cmp(nodetype, "destructor") == 0;
if (info.is_constructor || info.is_destructor) {
assert(class_receiver);
assert(!base);
info.receiver = NULL;
}
int ret = SWIG_OK;
int r = cgoGoWrapper(&info);
if (r != SWIG_OK) {
ret = r;
}
r = cgoCommentWrapper(&info);
if (r != SWIG_OK) {
ret = r;
}
r = cgoGccWrapper(&info);
if (r != SWIG_OK) {
ret = r;
}
Swig_restore(n);
return ret;
}
/* ----------------------------------------------------------------------
* cgoGoWrapper()
*
* Write out Go code to call a cgo function. This code will go into
* the generated Go output file.
* ---------------------------------------------------------------------- */
int cgoGoWrapper(const cgoWrapperInfo *info) {
Wrapper *dummy = initGoTypemaps(info->parms);
bool add_to_interface = interfaces && !info->is_constructor && !info->is_destructor && !info->is_static && !info->overname && checkFunctionVisibility(info->n, NULL);
Printv(f_go_wrappers, "func ", NULL);
Parm *p = info->parms;
int pi = 0;
// Add the receiver first if this is a method.
if (info->receiver) {
Printv(f_go_wrappers, "(", NULL);
if (info->base && info->receiver) {
Printv(f_go_wrappers, "_swig_base", NULL);
} else {
Printv(f_go_wrappers, Getattr(p, "lname"), NULL);
p = nextParm(p);
++pi;
}
Printv(f_go_wrappers, " ", info->receiver, ") ", NULL);
}
Printv(f_go_wrappers, info->go_name, NULL);
if (info->overname) {
Printv(f_go_wrappers, info->overname, NULL);
}
Printv(f_go_wrappers, "(", NULL);
// If we are doing methods, add this method to the interface.
if (add_to_interface) {
Printv(interfaces, "\t", info->go_name, "(", NULL);
}
// Write out the parameters to both the function definition and
// the interface.
String *parm_print = NewString("");
int parm_count = emit_num_arguments(info->parms);
int required_count = emit_num_required(info->parms);
int args = 0;
for (; pi < parm_count; ++pi) {
p = getParm(p);
if (pi == 0 && info->is_destructor) {
String *cl = exportedName(class_name);
Printv(parm_print, Getattr(p, "lname"), " ", cl, NULL);
Delete(cl);
++args;
} else {
if (args > 0) {
Printv(parm_print, ", ", NULL);
}
++args;
if (pi >= required_count) {
Printv(parm_print, "_swig_args ...interface{}", NULL);
break;
}
Printv(parm_print, Getattr(p, "lname"), " ", NULL);
String *tm = goType(p, Getattr(p, "type"));
Printv(parm_print, tm, NULL);
Delete(tm);
}
p = nextParm(p);
}
Printv(parm_print, ")", NULL);
// Write out the result type.
if (info->is_constructor) {
String *cl = exportedName(class_name);
Printv(parm_print, " (_swig_ret ", cl, ")", NULL);
Delete(cl);
} else {
if (SwigType_type(info->result) != T_VOID) {
String *tm = goType(info->n, info->result);
Printv(parm_print, " (_swig_ret ", tm, ")", NULL);
Delete(tm);
}
}
Printv(f_go_wrappers, parm_print, NULL);
if (add_to_interface) {
Printv(interfaces, parm_print, "\n", NULL);
}
// Write out the function body.
Printv(f_go_wrappers, " {\n", NULL);
if (parm_count > required_count) {
Parm *p = info->parms;
int i;
for (i = 0; i < required_count; ++i) {
p = getParm(p);
p = nextParm(p);
}
for (; i < parm_count; ++i) {
p = getParm(p);
String *tm = goType(p, Getattr(p, "type"));
Printv(f_go_wrappers, "\tvar ", Getattr(p, "lname"), " ", tm, "\n", NULL);
Printf(f_go_wrappers, "\tif len(_swig_args) > %d {\n", i - required_count);
Printf(f_go_wrappers, "\t\t%s = _swig_args[%d].(%s)\n", Getattr(p, "lname"), i - required_count, tm);
Printv(f_go_wrappers, "\t}\n", NULL);
Delete(tm);
p = nextParm(p);
}
}
String *call = NewString("\t");
String *ret_type = NULL;
bool memcpy_ret = false;
String *wt = NULL;
if (SwigType_type(info->result) != T_VOID) {
if (info->is_constructor) {
ret_type = exportedName(class_name);
} else {
ret_type = goImType(info->n, info->result);
}
Printv(f_go_wrappers, "\tvar swig_r ", ret_type, "\n", NULL);
bool c_struct_type;
Delete(cgoTypeForGoValue(info->n, info->result, &c_struct_type));
if (c_struct_type) {
memcpy_ret = true;
}
if (memcpy_ret) {
Printv(call, "swig_r_p := ", NULL);
} else {
Printv(call, "swig_r = (", ret_type, ")(", NULL);
}
if (info->is_constructor || goTypeIsInterface(info->n, info->result)) {
if (info->is_constructor) {
wt = Copy(class_receiver);
} else {
wt = goWrapperType(info->n, info->result, true);
}
Printv(call, wt, "(", NULL);
}
}
Printv(call, "C.", info->wname, "(", NULL);
args = 0;
if (parm_count > required_count) {
Printv(call, "C.swig_intgo(len(_swig_args))", NULL);
++args;
}
if (info->base && info->receiver) {
if (args > 0) {
Printv(call, ", ", NULL);
}
++args;
Printv(call, "C.uintptr_t(_swig_base)", NULL);
}
p = info->parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
if (args > 0) {
Printv(call, ", ", NULL);
}
++args;
SwigType *pt = Getattr(p, "type");
String *ln = Getattr(p, "lname");
String *ivar = NewStringf("_swig_i_%d", i);
String *goin = goGetattr(p, "tmap:goin");
if (goin == NULL) {
Printv(f_go_wrappers, "\t", ivar, " := ", ln, NULL);
if ((i == 0 && info->is_destructor) || ((i > 0 || !info->receiver || info->base || info->is_constructor) && goTypeIsInterface(p, pt))) {
Printv(f_go_wrappers, ".Swigcptr()", NULL);
}
Printv(f_go_wrappers, "\n", NULL);
Setattr(p, "emit:goinput", ln);
} else {
String *itm = goImType(p, pt);
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, "\n", NULL);
Delete(goin);
Setattr(p, "emit:goinput", ivar);
}
bool c_struct_type;
String *ct = cgoTypeForGoValue(p, pt, &c_struct_type);
if (c_struct_type) {
Printv(call, "*(*C.", ct, ")(unsafe.Pointer(&", ivar, "))", NULL);
} else {
Printv(call, "C.", ct, "(", ivar, ")", NULL);
}
Delete(ct);
p = nextParm(p);
}
Printv(f_go_wrappers, call, ")", NULL);
Delete(call);
if (wt) {
// Close the type conversion to the wrapper type.
Printv(f_go_wrappers, ")", NULL);
}
if (SwigType_type(info->result) != T_VOID && !memcpy_ret) {
// Close the type conversion of the return value.
Printv(f_go_wrappers, ")", NULL);
}
Printv(f_go_wrappers, "\n", NULL);
if (memcpy_ret) {
Printv(f_go_wrappers, "\tswig_r = *(*", ret_type, ")(unsafe.Pointer(&swig_r_p))\n", NULL);
}
if (ret_type) {
Delete(ret_type);
}
goargout(info->parms);
if (SwigType_type(info->result) != T_VOID) {
Swig_save("cgoGoWrapper", info->n, "type", "tmap:goout", NULL);
Setattr(info->n, "type", info->result);
String *goout = goTypemapLookup("goout", info->n, "swig_r");
if (goout == NULL) {
Printv(f_go_wrappers, "\treturn swig_r\n", NULL);
} else {
String *tm = goType(info->n, info->result);
Printv(f_go_wrappers, "\tvar swig_r_1 ", tm, "\n", NULL);
goout = Copy(goout);
Replaceall(goout, "$input", "swig_r");
Replaceall(goout, "$result", "swig_r_1");
Printv(f_go_wrappers, goout, "\n", NULL);
Printv(f_go_wrappers, "\treturn swig_r_1\n", NULL);
}
Swig_restore(info->n);
}
Printv(f_go_wrappers, "}\n\n", NULL);
DelWrapper(dummy);
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* cgoCommentWrapper()
*
* Write out a cgo function to call a C/C++ function. This code
* will go into the cgo comment in the generated Go output file.
* ---------------------------------------------------------------------- */
int cgoCommentWrapper(const cgoWrapperInfo *info) {
String *ret_type;
if (SwigType_type(info->result) == T_VOID) {
ret_type = NewString("void");
} else {
bool c_struct_type;
ret_type = cgoTypeForGoValue(info->n, info->result, &c_struct_type);
}
Printv(f_cgo_comment, "extern ", ret_type, " ", info->wname, "(", NULL);
Delete(ret_type);
int parm_count = emit_num_arguments(info->parms);
int required_count = emit_num_required(info->parms);
int args = 0;
if (parm_count > required_count) {
Printv(f_cgo_comment, "intgo _swig_args", NULL);
++args;
}
if (info->base && info->receiver) {
if (args > 0) {
Printv(f_cgo_comment, ", ", NULL);
}
++args;
Printv(f_cgo_comment, "uintptr_t _swig_base", NULL);
}
Parm *p = info->parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
if (args > 0) {
Printv(f_cgo_comment, ", ", NULL);
}
++args;
SwigType *pt = Getattr(p, "type");
String *ln = Getattr(p, "lname");
bool c_struct_type;
String *ct = cgoTypeForGoValue(p, pt, &c_struct_type);
Printv(f_cgo_comment, ct, " ", ln, NULL);
Delete(ct);
p = nextParm(p);
}
if (args == 0) {
Printv(f_cgo_comment, "void", NULL);
}
Printv(f_cgo_comment, ");\n", NULL);
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* cgoGccWrapper()
*
* Write out code to the C/C++ wrapper file. This code will be
* called by the code generated by cgoCommentWrapper.
* ---------------------------------------------------------------------- */
int cgoGccWrapper(const cgoWrapperInfo *info) {
Wrapper *f = NewWrapper();
Swig_save("cgoGccWrapper", info->n, "parms", NULL);
ParmList *parms = info->parms;
Parm *base_parm = NULL;
if (info->base && !isStatic(info->n)) {
SwigType *base_type = Copy(getClassType());
SwigType_add_pointer(base_type);
base_parm = NewParm(base_type, NewString("arg1"), info->n);
set_nextSibling(base_parm, parms);
parms = base_parm;
}
emit_parameter_variables(parms, f);
emit_attach_parmmaps(parms, f);
int parm_count = emit_num_arguments(parms);
int required_count = emit_num_required(parms);
emit_return_variable(info->n, info->result, f);
// Start the function definition.
String *fnname = NewString("");
Printv(fnname, info->wname, "(", NULL);
int args = 0;
if (parm_count > required_count) {
Printv(fnname, "intgo _swig_optargc", NULL);
++args;
}
Parm *p = parms;
for (int i = 0; i < parm_count; ++i) {
if (args > 0) {
Printv(fnname, ", ", NULL);
}
++args;
p = getParm(p);
SwigType *pt = Copy(Getattr(p, "type"));
if (SwigType_isarray(pt)) {
SwigType_del_array(pt);
SwigType_add_pointer(pt);
}
String *pn = NewStringf("_swig_go_%d", i);
String *ct = gcCTypeForGoValue(p, pt, pn);
Printv(fnname, ct, NULL);
Delete(ct);
Delete(pn);
Delete(pt);
p = nextParm(p);
}
Printv(fnname, ")", NULL);
if (SwigType_type(info->result) == T_VOID) {
Printv(f->def, "void ", fnname, NULL);
} else {
String *ct = gcCTypeForGoValue(info->n, info->result, fnname);
Printv(f->def, ct, NULL);
Delete(ct);
String *ln = NewString("_swig_go_result");
ct = gcCTypeForGoValue(info->n, info->result, ln);
Wrapper_add_local(f, "_swig_go_result", ct);
Delete(ct);
Delete(ln);
}
Delete(fnname);
Printv(f->def, " {\n", NULL);
// Apply the in typemaps.
p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
String *tm = Getattr(p, "tmap:in");
if (!tm) {
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "unable to use type %s as a function argument\n", SwigType_str(Getattr(p, "type"), 0));
} else {
tm = Copy(tm);
String *pn = NewStringf("_swig_go_%d", i);
Replaceall(tm, "$input", pn);
if (i < required_count) {
Printv(f->code, "\t", tm, "\n", NULL);
} else {
Printf(f->code, "\tif (_swig_optargc > %d) {\n", i - required_count);
Printv(f->code, "\t\t", tm, "\n", NULL);
Printv(f->code, "\t}\n", NULL);
}
Delete(tm);
Setattr(p, "emit:input", pn);
}
p = nextParm(p);
}
Printv(f->code, "\n", NULL);
// Do the real work of the function.
checkConstraints(parms, f);
emitGoAction(info->n, info->base, parms, info->result, f);
argout(parms, f);
cleanupFunction(info->n, f, parms);
if (SwigType_type(info->result) != T_VOID) {
Printv(f->code, "\treturn _swig_go_result;\n", NULL);
}
Printv(f->code, "}\n", NULL);
Wrapper_print(f, f_c_wrappers);
Swig_restore(info->n);
DelWrapper(f);
if (base_parm) {
Delete(base_parm);
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* goFunctionWrapper()
*
* Write out a function wrapper in Go. When not implementing a
* method, the actual code is all in C; here we just declare the C
* function. When implementing a method, we have to call the C
* function, because it will have a different name. If base is not
* NULL, then we are being called to forward a virtual method to a
* base class.
* ---------------------------------------------------------------------- */
int goFunctionWrapper(Node *n, String *name, String *go_name, String *overname, String *wname, List *base, ParmList *parms, SwigType *result, bool is_static) {
Wrapper *dummy = initGoTypemaps(parms);
int parm_count = emit_num_arguments(parms);
int required_count = emit_num_required(parms);
String *receiver = class_receiver;
if (receiver && is_static) {
receiver = NULL;
}
String *nodetype = Getattr(n, "nodeType");
bool is_constructor = Cmp(nodetype, "constructor") == 0;
bool is_destructor = Cmp(nodetype, "destructor") == 0;
if (is_constructor || is_destructor) {
assert(class_receiver);
assert(!base);
receiver = NULL;
}
Swig_save("cgoGoWrapper", n, "type", "tmap:goout", NULL);
Setattr(n, "type", result);
String *goout = goTypemapLookup("goout", n, "swig_r");
Swig_restore(n);
bool add_to_interface = (interfaces && !is_constructor && !is_destructor && !is_static && !overname && checkFunctionVisibility(n, NULL));
bool needs_wrapper = (gccgo_flag || receiver || is_constructor || is_destructor || parm_count > required_count);
bool has_goout = false;
if (goout) {
has_goout = true;
}
// See whether any of the function parameters are represented by
// interface values. When calling the C++ code, we need to convert
// back to a uintptr.
Parm *p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
String *ty = Getattr(p, "type");
if (goGetattr(p, "tmap:goargout")) {
has_goout = true;
needs_wrapper = true;
} else if (goTypeIsInterface(p, ty) || goGetattr(p, "tmap:goin")) {
needs_wrapper = true;
}
if (paramNeedsEscape(p)) {
needs_wrapper = true;
}
p = nextParm(p);
}
if (goTypeIsInterface(n, result) || goout != NULL) {
needs_wrapper = true;
}
if (!gccgo_flag) {
Printv(f_go_wrappers, "var ", wname, " unsafe.Pointer\n\n", NULL);
}
// If this is a method, first declare the C function we will call.
// If we do not need a wrapper, then we will only be writing a
// declaration.
String *wrapper_name = NULL;
if (needs_wrapper) {
wrapper_name = buildGoWrapperName(name, overname);
if (gccgo_flag) {
Printv(f_go_wrappers, "//extern ", go_prefix, "_", wname, "\n", NULL);
}
bool arg = false;
Printv(f_go_wrappers, "func ", wrapper_name, "(", NULL);
if (parm_count > required_count) {
Printv(f_go_wrappers, argName(&arg), " int", NULL);
}
Parm *p = getParm(parms);
int i = 0;
if (is_destructor) {
if (parm_count > required_count) {
Printv(f_go_wrappers, ", ", NULL);
}
Printv(f_go_wrappers, argName(&arg), " uintptr", NULL);
++i;
p = nextParm(p);
} else if (receiver && (base || !is_constructor)) {
if (parm_count > required_count) {
Printv(f_go_wrappers, ", ", NULL);
}
Printv(f_go_wrappers, argName(&arg), " ", receiver, NULL);
if (!base) {
++i;
p = nextParm(p);
}
}
for (; i < parm_count; ++i) {
p = getParm(p);
if (i > 0 || (base && receiver) || parm_count > required_count) {
Printv(f_go_wrappers, ", ", NULL);
}
String *tm = goWrapperType(p, Getattr(p, "type"), false);
Printv(f_go_wrappers, argName(&arg), " ", tm, NULL);
Delete(tm);
p = nextParm(p);
}
Printv(f_go_wrappers, ")", NULL);
if (is_constructor) {
Printv(f_go_wrappers, " (", argName(&arg), " ", class_receiver, ")", NULL);
} else {
if (SwigType_type(result) != T_VOID) {
String *tm = goWrapperType(n, result, true);
Printv(f_go_wrappers, " (", argName(&arg), " ", tm, ")", NULL);
Delete(tm);
}
}
if (!gccgo_flag) {
Printv(f_go_wrappers, " {\n", NULL);
if (arg) {
Printv(f_go_wrappers, "\t_swig_p := uintptr(unsafe.Pointer(&base))\n", NULL);
} else {
Printv(f_go_wrappers, "\tvar _swig_p uintptr\n", NULL);
}
Printv(f_go_wrappers, "\t_cgo_runtime_cgocall(", wname, ", _swig_p)\n", NULL);
Printv(f_go_wrappers, "\treturn\n", NULL);
Printv(f_go_wrappers, "}", NULL);
}
Printv(f_go_wrappers, "\n\n", NULL);
}
// Start defining the Go function.
if (!needs_wrapper && gccgo_flag) {
Printv(f_go_wrappers, "//extern ", go_prefix, "_", wname, "\n", NULL);
}
Printv(f_go_wrappers, "func ", NULL);
p = parms;
int pi = 0;
// Add the receiver if this is a method.
String *first = NULL;
if (receiver) {
Printv(f_go_wrappers, "(", NULL);
if (base && receiver) {
Printv(f_go_wrappers, "_swig_base", NULL);
if (first == NULL) {
first = NewString("_swig_base");
}
} else {
Printv(f_go_wrappers, Getattr(p, "lname"), NULL);
if (first == NULL) {
first = Copy(Getattr(p, "lname"));
}
p = nextParm(p);
++pi;
}
Printv(f_go_wrappers, " ", receiver, ") ", NULL);
}
Printv(f_go_wrappers, go_name, NULL);
if (overname) {
Printv(f_go_wrappers, overname, NULL);
}
Printv(f_go_wrappers, "(", NULL);
// If we are doing methods, add this function to the interface.
if (add_to_interface) {
Printv(interfaces, "\t", go_name, "(", NULL);
}
// Write out the parameters to both the function definition and
// the interface.
String *parm_print = NewString("");
for (; pi < parm_count; ++pi) {
p = getParm(p);
if (pi == 0 && is_destructor) {
String *cl = exportedName(class_name);
Printv(parm_print, Getattr(p, "lname"), " ", cl, NULL);
if (first == NULL) {
first = Copy(Getattr(p, "lname"));
}
Delete(cl);
} else {
if (pi > (receiver && !base ? 1 : 0)) {
Printv(parm_print, ", ", NULL);
}
if (pi >= required_count) {
Printv(parm_print, "_swig_args ...interface{}", NULL);
if (first == NULL) {
first = NewString("_swig_args");
}
break;
}
Printv(parm_print, Getattr(p, "lname"), " ", NULL);
if (first == NULL) {
first = Copy(Getattr(p, "lname"));
}
String *tm = goType(p, Getattr(p, "type"));
Printv(parm_print, tm, NULL);
Delete(tm);
}
p = nextParm(p);
}
Printv(parm_print, ")", NULL);
// Write out the result type.
if (is_constructor) {
String *cl = exportedName(class_name);
Printv(parm_print, " (_swig_ret ", cl, ")", NULL);
if (first == NULL) {
first = NewString("_swig_ret");
}
Delete(cl);
} else {
if (SwigType_type(result) != T_VOID) {
String *tm = goType(n, result);
Printv(parm_print, " (_swig_ret ", tm, ")", NULL);
if (first == NULL) {
first = NewString("_swig_ret");
}
Delete(tm);
}
}
Printv(f_go_wrappers, parm_print, NULL);
if (add_to_interface) {
Printv(interfaces, parm_print, "\n", NULL);
}
// If this is a wrapper, we need to actually call the C function.
if (needs_wrapper) {
Printv(f_go_wrappers, " {\n", NULL);
if (parm_count > required_count) {
Parm *p = parms;
int i;
for (i = 0; i < required_count; ++i) {
p = getParm(p);
p = nextParm(p);
}
for (; i < parm_count; ++i) {
p = getParm(p);
String *tm = goType(p, Getattr(p, "type"));
Printv(f_go_wrappers, "\tvar ", Getattr(p, "lname"), " ", tm, "\n", NULL);
Printf(f_go_wrappers, "\tif len(_swig_args) > %d {\n", i - required_count);
Printf(f_go_wrappers, "\t\t%s = _swig_args[%d].(%s)\n", Getattr(p, "lname"), i - required_count, tm);
Printv(f_go_wrappers, "\t}\n", NULL);
Delete(tm);
p = nextParm(p);
}
}
String *call = NewString("");
bool need_return_var = SwigType_type(result) != T_VOID && ((gccgo_flag && is_constructor) || has_goout);
if (need_return_var) {
Printv(f_go_wrappers, "\tvar swig_r ", NULL);
if (is_constructor) {
String *cl = exportedName(class_name);
Printv(f_go_wrappers, cl, NULL);
Delete(cl);
} else {
Printv(f_go_wrappers, goImType(n, result), NULL);
}
Printv(f_go_wrappers, "\n", NULL);
}
if (gccgo_flag) {
if (has_goout || is_constructor) {
Printv(call, "\tfunc() {\n", NULL);
}
Printv(call, "\tdefer SwigCgocallDone()\n", NULL);
Printv(call, "\tSwigCgocall()\n", NULL);
}
Printv(call, "\t", NULL);
if (SwigType_type(result) != T_VOID) {
if (need_return_var) {
Printv(call, "swig_r = ", NULL);
} else {
Printv(call, "return ", NULL);
}
}
Printv(call, wrapper_name, "(", NULL);
if (parm_count > required_count) {
Printv(call, "len(_swig_args)", NULL);
}
if (base && receiver) {
if (parm_count > required_count) {
Printv(call, ", ", NULL);
}
Printv(call, "_swig_base", NULL);
}
Parm *p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
if (i > 0 || (base && receiver)
|| parm_count > required_count) {
Printv(call, ", ", NULL);
}
SwigType *pt = Getattr(p, "type");
String *ln = Getattr(p, "lname");
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))) {
Printv(call, ".Swigcptr()", NULL);
}
Setattr(p, "emit:goinput", ln);
} else {
String *ivar = NewString("");
Printf(ivar, "_swig_i_%d", i);
String *itm = goImType(p, pt);
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, "\n", NULL);
Delete(goin);
Printv(call, ivar, NULL);
Setattr(p, "emit:goinput", ivar);
}
// If the parameter has an argout or freearg typemap, make
// sure that it escapes.
if (paramNeedsEscape(p)) {
Printv(f_go_wrappers, "\tif Swig_escape_always_false {\n", NULL);
Printv(f_go_wrappers, "\t\tSwig_escape_val = ", Getattr(p, "emit:goinput"), "\n", NULL);
Printv(f_go_wrappers, "\t}\n", NULL);
}
p = nextParm(p);
}
Printv(call, ")\n", NULL);
if (gccgo_flag && (has_goout || is_constructor)) {
Printv(call, "\t}()\n", NULL);
}
Printv(f_go_wrappers, call, NULL);
Delete(call);
goargout(parms);
if (need_return_var) {
if (goout == NULL) {
Printv(f_go_wrappers, "\treturn swig_r\n", NULL);
} else {
String *tm = goType(n, result);
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, "\n", NULL);
Printv(f_go_wrappers, "\treturn swig_r_1\n", NULL);
}
}
Printv(f_go_wrappers, "}\n", NULL);
} else if (!gccgo_flag) {
// We don't need a wrapper. If we're using gccgo, the function
// declaration is all we need--it has a //extern comment to
// GCC-compiled wrapper. If we're not using gccgo, we need to
// call the GCC-compiled wrapper here.
Printv(f_go_wrappers, " {\n", NULL);
if (first == NULL) {
Printv(f_go_wrappers, "\tvar _swig_p uintptr\n", NULL);
} else {
Printv(f_go_wrappers, "\t_swig_p := uintptr(unsafe.Pointer(&", first, "))\n", NULL);
}
Printv(f_go_wrappers, "\t_cgo_runtime_cgocall(", wname, ", _swig_p)\n", NULL);
Printv(f_go_wrappers, "\treturn\n", NULL);
Printv(f_go_wrappers, "}", NULL);
}
Printv(f_go_wrappers, "\n", NULL);
Delete(wrapper_name);
DelWrapper(dummy);
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* initGoTypemaps()
*
* Initialize the typenames for a Go wrapper, returning a dummy
* Wrapper*. Also set consistent names for the parameters.
* ---------------------------------------------------------------------- */
Wrapper* initGoTypemaps(ParmList *parms) {
Wrapper *dummy = NewWrapper();
emit_attach_parmmaps(parms, dummy);
Parm *p = parms;
int parm_count = emit_num_arguments(parms);
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
Swig_cparm_name(p, i);
p = nextParm(p);
}
Swig_typemap_attach_parms("default", parms, dummy);
Swig_typemap_attach_parms("gotype", parms, dummy);
Swig_typemap_attach_parms("goin", parms, dummy);
Swig_typemap_attach_parms("goargout", parms, dummy);
Swig_typemap_attach_parms("imtype", parms, dummy);
return dummy;
}
/* ----------------------------------------------------------------------
* argName()
*
* A helper for goFunctionWrapper to output the first argument name
* as "base" and all others as "_".
* ---------------------------------------------------------------------- */
const char *argName(bool *arg) {
if (*arg) {
return "_";
}
*arg = true;
return "base";
}
/* ----------------------------------------------------------------------
* paramNeedsEscape()
*
* A helper for goFunctionWrapper that returns whether a parameter
* needs to explicitly escape. This is true if the parameter has a
* non-empty argout or freearg typemap, because in those cases the
* Go argument might be or contain a pointer. We need to ensure
* that that pointer does not point into the stack, which means that
* it needs to escape.
* ---------------------------------------------------------------------- */
bool paramNeedsEscape(Parm *p) {
String *argout = Getattr(p, "tmap:argout");
String *freearg = Getattr(p, "tmap:freearg");
if ((!argout || Len(argout) == 0) && (!freearg || Len(freearg) == 0)) {
return false;
}
// If a C++ type is represented as an interface type in Go, then
// we don't care whether it escapes, because we know that the
// pointer is a C++ pointer.
if (goTypeIsInterface(p, Getattr(p, "type"))) {
return false;
}
return true;
}
/* ----------------------------------------------------------------------
* gcFunctionWrapper()
*
* This is used for 6g/8g, not for gccgo. Write out the function
* redirector that will be compiled with 6c/8c. This used to write
* out a real function wrapper, but that has moved into Go code.
* ---------------------------------------------------------------------- */
int gcFunctionWrapper(String *wname) {
Wrapper *f = NewWrapper();
Printv(f->def, "#pragma dynimport ", wname, " ", wname, " \"\"\n", NULL);
Printv(f->def, "#pragma cgo_import_static ", wname, "\n", NULL);
Printv(f->def, "extern void ", wname, "(void*);\n", NULL);
// Declare this as a uintptr, since it is not a pointer into the
// Go heap.
// \xc2\xb7 is UTF-8 for U+00B7 which is Unicode 'Middle Dot'
Printv(f->def, "uintptr \xc2\xb7", wname, " = (uintptr)", wname, ";\n", NULL);
Wrapper_print(f, f_gc_wrappers);
DelWrapper(f);
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* gccFunctionWrapper()
*
* This is used for 6g/8g, not for gccgo. Write out the function
* wrapper which will be compiled with gcc. If the base parameter
* is not NULL, this is calls the base class method rather than
* executing the SWIG wrapper code.
* ---------------------------------------------------------------------- */
int gccFunctionWrapper(Node *n, List *base, String *wname, ParmList *parms, SwigType *result) {
Wrapper *f = NewWrapper();
Swig_save("gccFunctionWrapper", n, "parms", NULL);
Parm *base_parm = NULL;
if (base && !isStatic(n)) {
SwigType *base_type = Copy(getClassType());
SwigType_add_pointer(base_type);
base_parm = NewParm(base_type, NewString("arg1"), n);
set_nextSibling(base_parm, parms);
parms = base_parm;
}
emit_parameter_variables(parms, f);
emit_attach_parmmaps(parms, f);
int parm_count = emit_num_arguments(parms);
int required_count = emit_num_required(parms);
bool needs_swigargs = false;
emit_return_variable(n, result, f);
// Start the function definition.
Printv(f->def, "void\n", wname, "(void *swig_v)\n", "{\n", NULL);
// The single function parameter is a pointer to the real argument
// values. Define the structure that it points to.
String *swigargs = NewString("\tstruct swigargs {\n");
if (parm_count > required_count) {
needs_swigargs = true;
Printv(swigargs, "\t\tintgo _swig_optargc;\n", NULL);
}
Parm *p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
String *ln = Getattr(p, "lname");
SwigType *pt = Getattr(p, "type");
String *ct = gcCTypeForGoValue(p, pt, ln);
Printv(swigargs, "\t\t\t", ct, ";\n", NULL);
needs_swigargs = true;
Delete(ct);
String *gn = NewStringf("_swig_go_%d", i);
ct = gcCTypeForGoValue(p, pt, gn);
Setattr(p, "emit:input", gn);
Wrapper_add_local(f, gn, ct);
Delete(ct);
p = nextParm(p);
}
if (SwigType_type(result) != T_VOID) {
Printv(swigargs, "\t\tlong : 0;\n", NULL);
String *ln = NewString(Swig_cresult_name());
String *ct = gcCTypeForGoValue(n, result, ln);
Delete(ln);
Printv(swigargs, "\t\t", ct, ";\n", NULL);
needs_swigargs = true;
Delete(ct);
ln = NewString("_swig_go_result");
ct = gcCTypeForGoValue(n, result, ln);
Wrapper_add_local(f, "_swig_go_result", ct);
Delete(ct);
Delete(ln);
}
Printv(swigargs, "\t} SWIGSTRUCTPACKED *swig_a = (struct swigargs *) swig_v;\n", NULL);
// Copy the input arguments out of the structure into the Go local
// variables.
p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
String *ln = Getattr(p, "lname");
String *gn = Getattr(p, "emit:input");
Printv(f->code, "\t", gn, " = swig_a->", ln, ";\n", NULL);
p = nextParm(p);
}
// Apply the in typemaps.
p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
String *tm = Getattr(p, "tmap:in");
if (!tm) {
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument\n", SwigType_str(Getattr(p, "type"), 0));
} else {
tm = Copy(tm);
String *gn = Getattr(p, "emit:input");
Replaceall(tm, "$input", gn);
if (i < required_count) {
Printv(f->code, "\t", tm, "\n", NULL);
} else {
Printf(f->code, "\tif (swig_a->_swig_optargc > %d) {\n", i - required_count);
Printv(f->code, "\t\t", tm, "\n", NULL);
Printv(f->code, "\t}\n", NULL);
}
Delete(tm);
}
p = nextParm(p);
}
Printv(f->code, "\n", NULL);
// Do the real work of the function.
checkConstraints(parms, f);
emitGoAction(n, base, parms, result, f);
argout(parms, f);
cleanupFunction(n, f, parms);
if (needs_swigargs)
{
Printv(f->locals, swigargs, NULL);
}
Printv(f->code, "}\n", NULL);
Wrapper_print(f, f_c_wrappers);
Swig_restore(n);
Delete(swigargs);
DelWrapper(f);
Delete(base_parm);
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* gccgoFunctionWrapper()
*
* This is used for gccgo, not 6g/8g. Write out the function
* wrapper which will be compiled with gcc. If the base parameter
* is not NULL, this is calls the base class method rather than
* executing the SWIG wrapper code.
* ---------------------------------------------------------------------- */
int gccgoFunctionWrapper(Node *n, List *base, String *wname, ParmList *parms, SwigType *result) {
Wrapper *f = NewWrapper();
Swig_save("gccgoFunctionWrapper", n, "parms", NULL);
Parm *base_parm = NULL;
if (base && !isStatic(n)) {
SwigType *base_type = Copy(getClassType());
SwigType_add_pointer(base_type);
base_parm = NewParm(base_type, NewString("arg1"), n);
set_nextSibling(base_parm, parms);
parms = base_parm;
}
emit_parameter_variables(parms, f);
emit_attach_parmmaps(parms, f);
int parm_count = emit_num_arguments(parms);
int required_count = emit_num_required(parms);
emit_return_variable(n, result, f);
// Start the function definition.
String *fnname = NewString("");
Printv(fnname, "go_", wname, "(", NULL);
if (parm_count > required_count) {
Printv(fnname, "intgo _swig_optargc", NULL);
}
Parm *p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
SwigType *pt = Copy(Getattr(p, "type"));
if (SwigType_isarray(pt)) {
SwigType_del_array(pt);
SwigType_add_pointer(pt);
}
String *pn = NewString("g");
Append(pn, Getattr(p, "lname"));
String *ct = gccgoCTypeForGoValue(p, pt, pn);
if (i > 0 || parm_count > required_count) {
Printv(fnname, ", ", NULL);
}
Printv(fnname, ct, NULL);
Delete(ct);
Delete(pn);
Delete(pt);
p = nextParm(p);
}
Printv(fnname, ")", NULL);
String *fndef = NewString("");
if (SwigType_type(result) == T_VOID) {
Printv(fndef, "void ", fnname, NULL);
} else {
String *ct = gccgoCTypeForGoValue(n, result, fnname);
Printv(fndef, ct, NULL);
Delete(ct);
}
Printv(f->def, fndef, " __asm__(\"", go_prefix, "_", wname, "\");\n", NULL);
Printv(f->def, fndef, " {\n", NULL);
Delete(fnname);
Delete(fndef);
if (SwigType_type(result) != T_VOID) {
String *ln = NewString("_swig_go_result");
String *ct = gccgoCTypeForGoValue(n, result, ln);
Wrapper_add_local(f, "_swig_go_result", ct);
Delete(ct);
Delete(ln);
}
// Copy the parameters into the variables which hold their values,
// applying appropriate transformations.
p = parms;
for (int i = 0; i < parm_count; ++i) {
p = getParm(p);
String *tm = Getattr(p, "tmap:in");
if (!tm) {
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number,
"Unable to use type %s as a function argument\n", SwigType_str(Getattr(p, "type"), 0));
} else {
String *ln = Getattr(p, "lname");
String *pn = NewString("g");
Append(pn, ln);
tm = Copy(tm);
Replaceall(tm, "$input", pn);
Setattr(p, "emit:input", pn);
if (i < required_count) {
Printv(f->code, " ", tm, "\n", NULL);
} else {
Printf(f->code, " if (_swig_optargc > %d) {\n", i - required_count);
Printv(f->code, " ", tm, "\n", NULL);
Printv(f->code, " }\n", NULL);
}
Delete(tm);
}
p = nextParm(p);
}
Printv(f->code, "\n", NULL);
// Do the real work of the function.
checkConstraints(parms, f);
emitGoAction(n, base, parms, result, f);
argout(parms, f);
cleanupFunction(n, f, parms);
if (SwigType_type(result) != T_VOID) {
Printv(f->code, " return _swig_go_result;\n", NULL);
}
Printv(f->code, "}\n", NULL);
Wrapper_print(f, f_c_wrappers);
Swig_restore(n);
DelWrapper(f);
Delete(base_parm);
return SWIG_OK;
}
/* -----------------------------------------------------------------------
* checkConstraints()
*
* Check parameter constraints if any. This is used for the C/C++
* function. This assumes that each parameter has an "emit:input"
* property with the name to use to refer to that parameter.
* ----------------------------------------------------------------------- */
void checkConstraints(ParmList *parms, Wrapper *f) {
Parm *p = parms;
while (p) {
String *tm = Getattr(p, "tmap:check");
if (!tm) {
p = nextSibling(p);
} else {
tm = Copy(tm);
Replaceall(tm, "$input", Getattr(p, "emit:input"));
Printv(f->code, tm, "\n\n", NULL);
Delete(tm);
p = Getattr(p, "tmap:check:next");
}
}
}
/* -----------------------------------------------------------------------
* emitGoAction()
*
* Emit the action of the function. This is used for the C/C++ function.
* ----------------------------------------------------------------------- */
void emitGoAction(Node *n, List *base, ParmList *parms, SwigType *result, Wrapper *f) {
if (!gccgo_flag && !cgo_flag && SwigType_type(result) != T_VOID) {
Wrapper_add_local(f, "swig_stktop", "char *swig_stktop");
Printv(f->code, "\tswig_stktop = _swig_topofstack();\n", NULL);
}
String *actioncode;
if (!base || isStatic(n)) {
Swig_director_emit_dynamic_cast(n, f);
actioncode = emit_action(n);
} else {
// Call the base class method.
actioncode = NewString("");
String *current = NewString("");
if (!gccgo_flag && !cgo_flag) {
Printv(current, "swig_a->", NULL);
}
Printv(current, Getattr(parms, "lname"), NULL);
int vc = 0;
for (Iterator bi = First(base); bi.item; bi = Next(bi)) {
Printf(actioncode, " %s *swig_b%d = (%s *)%s;\n", bi.item, vc, bi.item, current);
Delete(current);
current = NewString("");
Printf(current, "swig_b%d", vc);
++vc;
}
String *code = Copy(Getattr(n, "wrap:action"));
Replace(code, Getattr(parms, "lname"), current, DOH_REPLACE_ANY | DOH_REPLACE_ID);
Delete(current);
Printv(actioncode, code, "\n", NULL);
}
Swig_save("emitGoAction", n, "type", "tmap:out", NULL);
Setattr(n, "type", result);
String *tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode);
if (!tm) {
Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s\n", SwigType_str(result, 0));
} else {
Replaceall(tm, "$result", "_swig_go_result");
if (GetFlag(n, "feature:new")) {
Replaceall(tm, "$owner", "1");
} else {
Replaceall(tm, "$owner", "0");
}
Printv(f->code, tm, "\n", NULL);
Delete(tm);
}
if (!gccgo_flag && !cgo_flag && SwigType_type(result) != T_VOID) {
// If the function called back into the Go code, the stack might
// have been copied. We need to adjust swig_a accordingly here.
// This is what cgo does.
Printv(f->code, "\tswig_a = (struct swigargs*)((char*)swig_a + (_swig_topofstack() - swig_stktop));\n", NULL);
Printv(f->code, "\tswig_a->", Swig_cresult_name(), " = ", "_swig_go_result;\n", NULL);
}